github.com/huiliang/nomad@v0.2.1-0.20151124023127-7a8b664699ff/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 if out := ui.OutputWriter.String(); out != "<nil>" { 30 t.Fatalf("expected empty output, got: %s", out) 31 } 32 33 // Register two jobs 34 job1 := testJob("job1") 35 if _, _, err := client.Jobs().Register(job1, nil); err != nil { 36 t.Fatalf("err: %s", err) 37 } 38 job2 := testJob("job2") 39 if _, _, err := client.Jobs().Register(job2, nil); err != nil { 40 t.Fatalf("err: %s", err) 41 } 42 43 // Query again and check the result 44 if code := cmd.Run([]string{"-address=" + url}); code != 0 { 45 t.Fatalf("expected exit 0, got: %d", code) 46 } 47 out := ui.OutputWriter.String() 48 if !strings.Contains(out, "job1") || !strings.Contains(out, "job2") { 49 t.Fatalf("expected job1 and job2, got: %s", out) 50 } 51 ui.OutputWriter.Reset() 52 53 // Query a single job 54 if code := cmd.Run([]string{"-address=" + url, "job2"}); code != 0 { 55 t.Fatalf("expected exit 0, got: %d", code) 56 } 57 out = ui.OutputWriter.String() 58 if strings.Contains(out, "job1") || !strings.Contains(out, "job2") { 59 t.Fatalf("expected only job2, got: %s", out) 60 } 61 if !strings.Contains(out, "Evaluations") { 62 t.Fatalf("should dump evaluations") 63 } 64 if !strings.Contains(out, "Allocations") { 65 t.Fatalf("should dump allocations") 66 } 67 ui.OutputWriter.Reset() 68 69 // Query in short view mode 70 if code := cmd.Run([]string{"-address=" + url, "-short", "job2"}); code != 0 { 71 t.Fatalf("expected exit 0, got: %d", code) 72 } 73 out = ui.OutputWriter.String() 74 if !strings.Contains(out, "job2") { 75 t.Fatalf("expected job2, got: %s", out) 76 } 77 if strings.Contains(out, "Evaluations") { 78 t.Fatalf("should not dump evaluations") 79 } 80 if strings.Contains(out, "Allocations") { 81 t.Fatalf("should not dump allocations") 82 } 83 } 84 85 func TestStatusCommand_Fails(t *testing.T) { 86 ui := new(cli.MockUi) 87 cmd := &StatusCommand{Meta: Meta{Ui: ui}} 88 89 // Fails on misuse 90 if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 { 91 t.Fatalf("expected exit code 1, got: %d", code) 92 } 93 if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { 94 t.Fatalf("expected help output, got: %s", out) 95 } 96 ui.ErrorWriter.Reset() 97 98 // Fails on connection failure 99 if code := cmd.Run([]string{"-address=nope"}); code != 1 { 100 t.Fatalf("expected exit code 1, got: %d", code) 101 } 102 if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying jobs") { 103 t.Fatalf("expected failed query error, got: %s", out) 104 } 105 }