github.com/jrxfive/nomad@v0.6.1-0.20170802162750-1fef470e89bf/command/client_config_test.go (about) 1 package command 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/hashicorp/nomad/command/agent" 8 "github.com/mitchellh/cli" 9 ) 10 11 func TestClientConfigCommand_Implements(t *testing.T) { 12 t.Parallel() 13 var _ cli.Command = &ClientConfigCommand{} 14 } 15 16 func TestClientConfigCommand_UpdateServers(t *testing.T) { 17 t.Parallel() 18 srv, _, url := testServer(t, true, func(c *agent.Config) { 19 c.Server.BootstrapExpect = 0 20 }) 21 defer srv.Shutdown() 22 23 ui := new(cli.MockUi) 24 cmd := &ClientConfigCommand{Meta: Meta{Ui: ui}} 25 26 // Fails if trying to update with no servers 27 code := cmd.Run([]string{"-update-servers"}) 28 if code != 1 { 29 t.Fatalf("expected exit 1, got: %d", code) 30 } 31 if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { 32 t.Fatalf("expected help output, got: %s", out) 33 } 34 ui.ErrorWriter.Reset() 35 36 // Set the servers list 37 code = cmd.Run([]string{"-address=" + url, "-update-servers", "127.0.0.42", "198.18.5.5"}) 38 if code != 0 { 39 t.Fatalf("expected exit 0, got: %d", code) 40 } 41 42 // Query the servers list 43 code = cmd.Run([]string{"-address=" + url, "-servers"}) 44 if code != 0 { 45 t.Fatalf("expect exit 0, got: %d", code) 46 } 47 out := ui.OutputWriter.String() 48 if !strings.Contains(out, "127.0.0.42") { 49 t.Fatalf("missing 127.0.0.42") 50 } 51 if !strings.Contains(out, "198.18.5.5") { 52 t.Fatalf("missing 198.18.5.5") 53 } 54 } 55 56 func TestClientConfigCommand_Fails(t *testing.T) { 57 t.Parallel() 58 ui := new(cli.MockUi) 59 cmd := &ClientConfigCommand{Meta: Meta{Ui: ui}} 60 61 // Fails on misuse 62 if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 { 63 t.Fatalf("expected exit code 1, got: %d", code) 64 } 65 if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { 66 t.Fatalf("expected help output, got: %s", out) 67 } 68 ui.ErrorWriter.Reset() 69 70 // Fails if no valid flags given 71 if code := cmd.Run([]string{}); code != 1 { 72 t.Fatalf("expected exit code 1, got: %d", code) 73 } 74 if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { 75 t.Fatalf("expected help output, got: %s", out) 76 } 77 ui.ErrorWriter.Reset() 78 79 // Fails on connection failure 80 if code := cmd.Run([]string{"-address=nope", "-servers"}); code != 1 { 81 t.Fatalf("expected exit code 1, got: %d", code) 82 } 83 if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying server list") { 84 t.Fatalf("expected failed query error, got: %s", out) 85 } 86 }