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