github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/job_scaling_events_test.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/nomad/helper" 9 "github.com/hashicorp/nomad/testutil" 10 "github.com/mitchellh/cli" 11 ) 12 13 func TestJobScalingEventsCommand_Run(t *testing.T) { 14 t.Parallel() 15 srv, client, url := testServer(t, true, nil) 16 defer srv.Shutdown() 17 testutil.WaitForResult(func() (bool, error) { 18 nodes, _, err := client.Nodes().List(nil) 19 if err != nil { 20 return false, err 21 } 22 if len(nodes) == 0 { 23 return false, fmt.Errorf("missing node") 24 } 25 if _, ok := nodes[0].Drivers["mock_driver"]; !ok { 26 return false, fmt.Errorf("mock_driver not ready") 27 } 28 return true, nil 29 }, func(err error) { 30 t.Fatalf("err: %s", err) 31 }) 32 33 ui := cli.NewMockUi() 34 cmd := &JobScalingEventsCommand{Meta: Meta{Ui: ui}} 35 36 // Register a test job and ensure it is running before moving on. 37 resp, _, err := client.Jobs().Register(testJob("scale_events_test_job"), nil) 38 if err != nil { 39 t.Fatalf("err: %s", err) 40 } 41 if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 { 42 t.Fatalf("expected waitForSuccess exit code 0, got: %d", code) 43 } 44 45 // List events without passing the jobID which should result in an error. 46 if code := cmd.Run([]string{"-address=" + url}); 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 argument: <job_id>") { 50 t.Fatalf("Expected argument error: %v", out) 51 } 52 53 // List events for the job, which should present zero. 54 if code := cmd.Run([]string{"-address=" + url, "scale_events_test_job"}); code != 0 { 55 t.Fatalf("expected cmd run exit code 0, got: %d", code) 56 } 57 if out := ui.OutputWriter.String(); !strings.Contains(out, "No events found") { 58 t.Fatalf("Expected no events output but got: %v", out) 59 } 60 61 // Perform a scaling action to generate an event. 62 _, _, err = client.Jobs().Scale( 63 "scale_events_test_job", 64 "group1", helper.IntToPtr(2), 65 "searchable custom test message", false, nil, nil) 66 if err != nil { 67 t.Fatalf("err: %s", err) 68 } 69 70 // List the scaling events which should include an entry. 71 if code := cmd.Run([]string{"-address=" + url, "scale_events_test_job"}); code != 0 { 72 t.Fatalf("expected cmd run exit code 0, got: %d", code) 73 } 74 if out := ui.OutputWriter.String(); !strings.Contains(out, "Task Group Count PrevCount Date") { 75 t.Fatalf("Expected table headers but got: %v", out) 76 } 77 78 // List the scaling events with verbose flag to search for our message as 79 // well as the verbose table headers. 80 if code := cmd.Run([]string{"-address=" + url, "-verbose", "scale_events_test_job"}); code != 0 { 81 t.Fatalf("expected cmd run exit code 0, got: %d", code) 82 } 83 out := ui.OutputWriter.String() 84 if !strings.Contains(out, "searchable custom test message") { 85 t.Fatalf("Expected to find scaling message but got: %v", out) 86 } 87 if !strings.Contains(out, "Eval ID") { 88 t.Fatalf("Expected to verbose table headers: %v", out) 89 } 90 }