2020-10-05 13:18:30 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2022-08-17 16:26:34 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pointer"
|
2020-10-05 13:18:30 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestScalingPolicyInfoCommand_Run(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-10-05 13:18:30 +00:00
|
|
|
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.
|
2021-02-04 15:08:13 +00:00
|
|
|
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.
|
2021-02-03 21:29:44 +00:00
|
|
|
if code := cmd.Run([]string{"-address=" + url, "first", "second"}); code != 1 {
|
2020-10-05 13:18:30 +00:00
|
|
|
t.Fatalf("expected cmd run exit code 1, got: %d", code)
|
|
|
|
}
|
2021-02-03 21:29:44 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "This command takes one of the following argument conditions") {
|
2020-10-05 13:18:30 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-02-03 21:29:44 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, `No scaling policies with prefix or id "scaling_policy_inf" found`) {
|
|
|
|
t.Fatalf("expected 'no policies found' within output: %v", out)
|
2020-10-05 13:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a test job.
|
|
|
|
job := testJob("scaling_policy_info")
|
|
|
|
|
|
|
|
// Generate an example scaling policy.
|
|
|
|
job.TaskGroups[0].Scaling = &api.ScalingPolicy{
|
2022-08-17 16:26:34 +00:00
|
|
|
Enabled: pointer.Of(true),
|
|
|
|
Min: pointer.Of(int64(1)),
|
|
|
|
Max: pointer.Of(int64(1)),
|
2020-10-05 13:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
2021-02-04 15:08:13 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2020-10-05 13:18:30 +00:00
|
|
|
}
|