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