github.com/ryanslade/nomad@v0.2.4-0.20160128061903-fc95782f2089/command/status_test.go (about) 1 package command 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/mitchellh/cli" 8 ) 9 10 func TestStatusCommand_Implements(t *testing.T) { 11 var _ cli.Command = &StatusCommand{} 12 } 13 14 func TestStatusCommand_Run(t *testing.T) { 15 srv, client, url := testServer(t, nil) 16 defer srv.Stop() 17 18 ui := new(cli.MockUi) 19 cmd := &StatusCommand{Meta: Meta{Ui: ui}} 20 21 // Should return blank for no jobs 22 if code := cmd.Run([]string{"-address=" + url}); code != 0 { 23 t.Fatalf("expected exit 0, got: %d", code) 24 } 25 26 // Check for this awkward nil string, since a nil bytes.Buffer 27 // returns this purposely, and mitchellh/cli has a nil pointer 28 // if nothing was ever output. 29 exp := "No running jobs" 30 if out := strings.TrimSpace(ui.OutputWriter.String()); out != exp { 31 t.Fatalf("expected %q; got: %q", exp, out) 32 } 33 34 // Register two jobs 35 job1 := testJob("job1_sfx") 36 evalId, _, err := client.Jobs().Register(job1, nil) 37 if err != nil { 38 t.Fatalf("err: %s", err) 39 } 40 job2 := testJob("job2_sfx") 41 if _, _, err := client.Jobs().Register(job2, nil); err != nil { 42 t.Fatalf("err: %s", err) 43 } 44 45 // Query again and check the result 46 if code := cmd.Run([]string{"-address=" + url}); code != 0 { 47 t.Fatalf("expected exit 0, got: %d", code) 48 } 49 out := ui.OutputWriter.String() 50 if !strings.Contains(out, "job1_sfx") || !strings.Contains(out, "job2_sfx") { 51 t.Fatalf("expected job1_sfx and job2_sfx, got: %s", out) 52 } 53 ui.OutputWriter.Reset() 54 55 // Query a single job 56 if code := cmd.Run([]string{"-address=" + url, "job2_sfx"}); code != 0 { 57 t.Fatalf("expected exit 0, got: %d", code) 58 } 59 out = ui.OutputWriter.String() 60 if strings.Contains(out, "job1_sfx") || !strings.Contains(out, "job2_sfx") { 61 t.Fatalf("expected only job2_sfx, got: %s", out) 62 } 63 if !strings.Contains(out, "Evaluations") { 64 t.Fatalf("should dump evaluations") 65 } 66 if !strings.Contains(out, "Allocations") { 67 t.Fatalf("should dump allocations") 68 } 69 ui.OutputWriter.Reset() 70 71 // Query jobs with prefix match 72 if code := cmd.Run([]string{"-address=" + url, "job"}); code != 0 { 73 t.Fatalf("expected exit 0, got: %d", code) 74 } 75 out = ui.OutputWriter.String() 76 if !strings.Contains(out, "job1_sfx") || !strings.Contains(out, "job2_sfx") { 77 t.Fatalf("expected job1_sfx and job2_sfx, got: %s", out) 78 } 79 ui.OutputWriter.Reset() 80 81 // Query a single job with prefix match 82 if code := cmd.Run([]string{"-address=" + url, "job1"}); code != 0 { 83 t.Fatalf("expected exit 0, got: %d", code) 84 } 85 out = ui.OutputWriter.String() 86 if !strings.Contains(out, "job1_sfx") || strings.Contains(out, "job2_sfx") { 87 t.Fatalf("expected only job1_sfx, got: %s", out) 88 } 89 ui.OutputWriter.Reset() 90 91 // Query in short view mode 92 if code := cmd.Run([]string{"-address=" + url, "-short", "job2"}); code != 0 { 93 t.Fatalf("expected exit 0, got: %d", code) 94 } 95 out = ui.OutputWriter.String() 96 if !strings.Contains(out, "job2") { 97 t.Fatalf("expected job2, got: %s", out) 98 } 99 if strings.Contains(out, "Evaluations") { 100 t.Fatalf("should not dump evaluations") 101 } 102 if strings.Contains(out, "Allocations") { 103 t.Fatalf("should not dump allocations") 104 } 105 if strings.Contains(out, evalId) { 106 t.Fatalf("should not contain full identifiers, got %s", out) 107 } 108 ui.OutputWriter.Reset() 109 110 // Request full identifiers 111 if code := cmd.Run([]string{"-address=" + url, "-verbose", "job1"}); code != 0 { 112 t.Fatalf("expected exit 0, got: %d", code) 113 } 114 out = ui.OutputWriter.String() 115 if !strings.Contains(out, evalId) { 116 t.Fatalf("should contain full identifiers, got %s", out) 117 } 118 } 119 120 func TestStatusCommand_Fails(t *testing.T) { 121 ui := new(cli.MockUi) 122 cmd := &StatusCommand{Meta: Meta{Ui: ui}} 123 124 // Fails on misuse 125 if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 { 126 t.Fatalf("expected exit code 1, got: %d", code) 127 } 128 if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { 129 t.Fatalf("expected help output, got: %s", out) 130 } 131 ui.ErrorWriter.Reset() 132 133 // Fails on connection failure 134 if code := cmd.Run([]string{"-address=nope"}); code != 1 { 135 t.Fatalf("expected exit code 1, got: %d", code) 136 } 137 if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying jobs") { 138 t.Fatalf("expected failed query error, got: %s", out) 139 } 140 }