github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/command/status_test.go (about) 1 package command 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/hashicorp/nomad/api" 8 "github.com/mitchellh/cli" 9 ) 10 11 func TestStatusCommand_Implements(t *testing.T) { 12 t.Parallel() 13 var _ cli.Command = &StatusCommand{} 14 } 15 16 func TestStatusCommand_Run(t *testing.T) { 17 t.Parallel() 18 srv, client, url := testServer(t, true, nil) 19 defer srv.Shutdown() 20 21 ui := new(cli.MockUi) 22 cmd := &StatusCommand{Meta: Meta{Ui: ui}} 23 24 // Should return blank for no jobs 25 if code := cmd.Run([]string{"-address=" + url}); code != 0 { 26 t.Fatalf("expected exit 0, got: %d", code) 27 } 28 29 // Check for this awkward nil string, since a nil bytes.Buffer 30 // returns this purposely, and mitchellh/cli has a nil pointer 31 // if nothing was ever output. 32 exp := "No running jobs" 33 if out := strings.TrimSpace(ui.OutputWriter.String()); out != exp { 34 t.Fatalf("expected %q; got: %q", exp, out) 35 } 36 37 // Register two jobs 38 job1 := testJob("job1_sfx") 39 resp, _, err := client.Jobs().Register(job1, nil) 40 if err != nil { 41 t.Fatalf("err: %s", err) 42 } 43 if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 { 44 t.Fatalf("status code non zero saw %d", code) 45 } 46 47 job2 := testJob("job2_sfx") 48 resp2, _, err := client.Jobs().Register(job2, nil) 49 if err != nil { 50 t.Fatalf("err: %s", err) 51 } 52 if code := waitForSuccess(ui, client, fullId, t, resp2.EvalID); code != 0 { 53 t.Fatalf("status code non zero saw %d", code) 54 } 55 56 // Query again and check the result 57 if code := cmd.Run([]string{"-address=" + url}); code != 0 { 58 t.Fatalf("expected exit 0, got: %d", code) 59 } 60 out := ui.OutputWriter.String() 61 if !strings.Contains(out, "job1_sfx") || !strings.Contains(out, "job2_sfx") { 62 t.Fatalf("expected job1_sfx and job2_sfx, got: %s", out) 63 } 64 ui.OutputWriter.Reset() 65 66 // Query a single job 67 if code := cmd.Run([]string{"-address=" + url, "job2_sfx"}); code != 0 { 68 t.Fatalf("expected exit 0, got: %d", code) 69 } 70 out = ui.OutputWriter.String() 71 if strings.Contains(out, "job1_sfx") || !strings.Contains(out, "job2_sfx") { 72 t.Fatalf("expected only job2_sfx, got: %s", out) 73 } 74 if !strings.Contains(out, "Allocations") { 75 t.Fatalf("should dump allocations") 76 } 77 if !strings.Contains(out, "Summary") { 78 t.Fatalf("should dump summary") 79 } 80 ui.OutputWriter.Reset() 81 82 // Query a single job showing evals 83 if code := cmd.Run([]string{"-address=" + url, "-evals", "job2_sfx"}); code != 0 { 84 t.Fatalf("expected exit 0, got: %d", code) 85 } 86 out = ui.OutputWriter.String() 87 if strings.Contains(out, "job1_sfx") || !strings.Contains(out, "job2_sfx") { 88 t.Fatalf("expected only job2_sfx, got: %s", out) 89 } 90 if !strings.Contains(out, "Evaluations") { 91 t.Fatalf("should dump evaluations") 92 } 93 if !strings.Contains(out, "Allocations") { 94 t.Fatalf("should dump allocations") 95 } 96 ui.OutputWriter.Reset() 97 98 // Query a single job in verbose mode 99 if code := cmd.Run([]string{"-address=" + url, "-verbose", "job2_sfx"}); code != 0 { 100 t.Fatalf("expected exit 0, got: %d", code) 101 } 102 out = ui.OutputWriter.String() 103 if strings.Contains(out, "job1_sfx") || !strings.Contains(out, "job2_sfx") { 104 t.Fatalf("expected only job2_sfx, got: %s", out) 105 } 106 if !strings.Contains(out, "Evaluations") { 107 t.Fatalf("should dump evaluations") 108 } 109 if !strings.Contains(out, "Allocations") { 110 t.Fatalf("should dump allocations") 111 } 112 if !strings.Contains(out, "Created At") { 113 t.Fatal("should have created header") 114 } 115 ui.ErrorWriter.Reset() 116 ui.OutputWriter.Reset() 117 118 // Query jobs with prefix match 119 if code := cmd.Run([]string{"-address=" + url, "job"}); code != 1 { 120 t.Fatalf("expected exit 1, got: %d", code) 121 } 122 out = ui.ErrorWriter.String() 123 if !strings.Contains(out, "job1_sfx") || !strings.Contains(out, "job2_sfx") { 124 t.Fatalf("expected job1_sfx and job2_sfx, got: %s", out) 125 } 126 ui.ErrorWriter.Reset() 127 ui.OutputWriter.Reset() 128 129 // Query a single job with prefix match 130 if code := cmd.Run([]string{"-address=" + url, "job1"}); code != 0 { 131 t.Fatalf("expected exit 0, got: %d", code) 132 } 133 out = ui.OutputWriter.String() 134 if !strings.Contains(out, "job1_sfx") || strings.Contains(out, "job2_sfx") { 135 t.Fatalf("expected only job1_sfx, got: %s", out) 136 } 137 ui.OutputWriter.Reset() 138 139 // Query in short view mode 140 if code := cmd.Run([]string{"-address=" + url, "-short", "job2"}); code != 0 { 141 t.Fatalf("expected exit 0, got: %d", code) 142 } 143 out = ui.OutputWriter.String() 144 if !strings.Contains(out, "job2") { 145 t.Fatalf("expected job2, got: %s", out) 146 } 147 if strings.Contains(out, "Evaluations") { 148 t.Fatalf("should not dump evaluations") 149 } 150 if strings.Contains(out, "Allocations") { 151 t.Fatalf("should not dump allocations") 152 } 153 if strings.Contains(out, resp.EvalID) { 154 t.Fatalf("should not contain full identifiers, got %s", out) 155 } 156 ui.OutputWriter.Reset() 157 158 // Request full identifiers 159 if code := cmd.Run([]string{"-address=" + url, "-verbose", "job1"}); code != 0 { 160 t.Fatalf("expected exit 0, got: %d", code) 161 } 162 out = ui.OutputWriter.String() 163 if !strings.Contains(out, resp.EvalID) { 164 t.Fatalf("should contain full identifiers, got %s", out) 165 } 166 } 167 168 func TestStatusCommand_Fails(t *testing.T) { 169 t.Parallel() 170 ui := new(cli.MockUi) 171 cmd := &StatusCommand{Meta: Meta{Ui: ui}} 172 173 // Fails on misuse 174 if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 { 175 t.Fatalf("expected exit code 1, got: %d", code) 176 } 177 if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { 178 t.Fatalf("expected help output, got: %s", out) 179 } 180 ui.ErrorWriter.Reset() 181 182 // Fails on connection failure 183 if code := cmd.Run([]string{"-address=nope"}); code != 1 { 184 t.Fatalf("expected exit code 1, got: %d", code) 185 } 186 if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying jobs") { 187 t.Fatalf("expected failed query error, got: %s", out) 188 } 189 } 190 191 func waitForSuccess(ui cli.Ui, client *api.Client, length int, t *testing.T, evalId string) int { 192 mon := newMonitor(ui, client, length) 193 monErr := mon.monitor(evalId, false) 194 return monErr 195 }