github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/scaling_policy_info_test.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/nomad/api" 9 "github.com/hashicorp/nomad/helper" 10 "github.com/hashicorp/nomad/testutil" 11 "github.com/mitchellh/cli" 12 ) 13 14 func TestScalingPolicyInfoCommand_Run(t *testing.T) { 15 t.Parallel() 16 srv, client, url := testServer(t, true, nil) 17 defer srv.Shutdown() 18 testutil.WaitForResult(func() (bool, error) { 19 nodes, _, err := client.Nodes().List(nil) 20 if err != nil { 21 return false, err 22 } 23 if len(nodes) == 0 { 24 return false, fmt.Errorf("missing node") 25 } 26 if _, ok := nodes[0].Drivers["mock_driver"]; !ok { 27 return false, fmt.Errorf("mock_driver not ready") 28 } 29 return true, nil 30 }, func(err error) { 31 t.Fatalf("err: %s", err) 32 }) 33 34 ui := cli.NewMockUi() 35 cmd := &ScalingPolicyInfoCommand{Meta: Meta{Ui: ui}} 36 37 // Calling without the policyID should result in an error. 38 if code := cmd.Run([]string{"-address=" + url}); code != 1 { 39 t.Fatalf("expected cmd run exit code 1, got: %d", code) 40 } 41 if out := ui.ErrorWriter.String(); !strings.Contains(out, "This command takes one of the following argument conditions") { 42 t.Fatalf("expected argument error within output: %v", out) 43 } 44 45 // Calling with more than one argument should result in an error. 46 if code := cmd.Run([]string{"-address=" + url, "first", "second"}); code != 1 { 47 t.Fatalf("expected cmd run exit code 1, got: %d", code) 48 } 49 if out := ui.ErrorWriter.String(); !strings.Contains(out, "This command takes one of the following argument conditions") { 50 t.Fatalf("expected argument error within output: %v", out) 51 } 52 53 // Perform an initial info, which should return zero results. 54 if code := cmd.Run([]string{"-address=" + url, "scaling_policy_info"}); code != 1 { 55 t.Fatalf("expected cmd run exit code 1, got: %d", code) 56 } 57 if out := ui.ErrorWriter.String(); !strings.Contains(out, `No scaling policies with prefix or id "scaling_policy_inf" found`) { 58 t.Fatalf("expected 'no policies found' within output: %v", out) 59 } 60 61 // Generate a test job. 62 job := testJob("scaling_policy_info") 63 64 // Generate an example scaling policy. 65 job.TaskGroups[0].Scaling = &api.ScalingPolicy{ 66 Enabled: helper.BoolToPtr(true), 67 Min: helper.Int64ToPtr(1), 68 Max: helper.Int64ToPtr(1), 69 } 70 71 // Register the job. 72 resp, _, err := client.Jobs().Register(job, nil) 73 if err != nil { 74 t.Fatalf("err: %s", err) 75 } 76 if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 { 77 t.Fatalf("expected waitForSuccess exit code 0, got: %d", code) 78 } 79 80 // Grab the generated policyID. 81 policies, _, err := client.Scaling().ListPolicies(nil) 82 if err != nil { 83 t.Fatalf("err: %s", err) 84 } 85 numPolicies := len(policies) 86 if numPolicies == 0 || numPolicies > 1 { 87 t.Fatalf("expected 1 policy return, got %v", numPolicies) 88 } 89 90 if code := cmd.Run([]string{"-address=" + url, policies[0].ID}); code != 0 { 91 t.Fatalf("expected cmd run exit code 0, got: %d", code) 92 } 93 if out := ui.OutputWriter.String(); !strings.Contains(out, "Policy:") { 94 t.Fatalf("expected policy ID within output: %v", out) 95 } 96 97 prefix := policies[0].ID[:2] 98 if code := cmd.Run([]string{"-address=" + url, prefix}); code != 0 { 99 t.Fatalf("expected cmd run exit code 0, got: %d", code) 100 } 101 if out := ui.OutputWriter.String(); !strings.Contains(out, "Policy:") { 102 t.Fatalf("expected policy ID within output: %v", out) 103 } 104 }