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