mirror of
https://github.com/kemko/nomad.git
synced 2026-01-01 16:05:42 +03:00
The `-type` option for `volume status` is a UX papercut because for many clusters there will be only one sort of volume in use. Update the CLI so that the default behavior is to query CSI and/or DHV. This behavior is subtly different when the user provides an ID or not. If the user doesn't provide an ID, we query both CSI and DHV and show both tables. If the user provides an ID, we query DHV first and then CSI, and show only the appropriate volume. Because DHV IDs are UUIDs, we're sure we won't have collisions between the two. We only show errors if both queries return an error. Fixes: https://hashicorp.atlassian.net/browse/NET-12214
109 lines
3.4 KiB
Go
109 lines
3.4 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package command
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/cli"
|
|
"github.com/hashicorp/nomad/api"
|
|
"github.com/hashicorp/nomad/ci"
|
|
"github.com/hashicorp/nomad/helper/pointer"
|
|
"github.com/hashicorp/nomad/testutil"
|
|
)
|
|
|
|
func TestScalingPolicyInfoCommand_Run(t *testing.T) {
|
|
ci.Parallel(t)
|
|
srv, client, url := testServer(t, true, nil)
|
|
defer srv.Shutdown()
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
nodes, _, err := client.Nodes().List(nil)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if len(nodes) == 0 {
|
|
return false, fmt.Errorf("missing node")
|
|
}
|
|
if _, ok := nodes[0].Drivers["mock_driver"]; !ok {
|
|
return false, fmt.Errorf("mock_driver not ready")
|
|
}
|
|
return true, nil
|
|
}, func(err error) {
|
|
t.Fatalf("err: %s", err)
|
|
})
|
|
|
|
ui := cli.NewMockUi()
|
|
cmd := &ScalingPolicyInfoCommand{Meta: Meta{Ui: ui}}
|
|
|
|
// Calling without the policyID should result in an error.
|
|
if code := cmd.Run([]string{"-address=" + url}); code != 1 {
|
|
t.Fatalf("expected cmd run exit code 1, got: %d", code)
|
|
}
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "This command takes one of the following argument conditions") {
|
|
t.Fatalf("expected argument error within output: %v", out)
|
|
}
|
|
|
|
// Calling with more than one argument should result in an error.
|
|
if code := cmd.Run([]string{"-address=" + url, "first", "second"}); code != 1 {
|
|
t.Fatalf("expected cmd run exit code 1, got: %d", code)
|
|
}
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "This command takes one of the following argument conditions") {
|
|
t.Fatalf("expected argument error within output: %v", out)
|
|
}
|
|
|
|
// Perform an initial info, which should return zero results.
|
|
if code := cmd.Run([]string{"-address=" + url, "scaling_policy_info"}); code != 1 {
|
|
t.Fatalf("expected cmd run exit code 1, got: %d", code)
|
|
}
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, `no scaling policies with prefix or ID "scaling_policy_inf" found`) {
|
|
t.Fatalf("expected 'no scaling policies with prefix' within output: %v", out)
|
|
}
|
|
|
|
// Generate a test job.
|
|
job := testJob("scaling_policy_info")
|
|
|
|
// Generate an example scaling policy.
|
|
job.TaskGroups[0].Scaling = &api.ScalingPolicy{
|
|
Enabled: pointer.Of(true),
|
|
Min: pointer.Of(int64(1)),
|
|
Max: pointer.Of(int64(1)),
|
|
}
|
|
|
|
// Register the job.
|
|
resp, _, err := client.Jobs().Register(job, nil)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 {
|
|
t.Fatalf("expected waitForSuccess exit code 0, got: %d", code)
|
|
}
|
|
|
|
// Grab the generated policyID.
|
|
policies, _, err := client.Scaling().ListPolicies(nil)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
numPolicies := len(policies)
|
|
if numPolicies == 0 || numPolicies > 1 {
|
|
t.Fatalf("expected 1 policy return, got %v", numPolicies)
|
|
}
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, policies[0].ID}); code != 0 {
|
|
t.Fatalf("expected cmd run exit code 0, got: %d", code)
|
|
}
|
|
if out := ui.OutputWriter.String(); !strings.Contains(out, "Policy:") {
|
|
t.Fatalf("expected policy ID within output: %v", out)
|
|
}
|
|
|
|
prefix := policies[0].ID[:2]
|
|
if code := cmd.Run([]string{"-address=" + url, prefix}); code != 0 {
|
|
t.Fatalf("expected cmd run exit code 0, got: %d", code)
|
|
}
|
|
if out := ui.OutputWriter.String(); !strings.Contains(out, "Policy:") {
|
|
t.Fatalf("expected policy ID within output: %v", out)
|
|
}
|
|
}
|