github.com/mongey/nomad@v0.5.2/command/agent/command_test.go (about) 1 package agent 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "strings" 8 "testing" 9 10 "github.com/hashicorp/nomad/testutil" 11 "github.com/mitchellh/cli" 12 ) 13 14 func TestCommand_Implements(t *testing.T) { 15 var _ cli.Command = &Command{} 16 } 17 18 func TestCommand_Args(t *testing.T) { 19 tmpDir, err := ioutil.TempDir("", "nomad") 20 if err != nil { 21 t.Fatalf("err: %s", err) 22 } 23 defer os.RemoveAll(tmpDir) 24 25 type tcase struct { 26 args []string 27 errOut string 28 } 29 tcases := []tcase{ 30 { 31 []string{}, 32 "Must specify either server, client or dev mode for the agent.", 33 }, 34 { 35 []string{"-client", "-data-dir=" + tmpDir, "-bootstrap-expect=1"}, 36 "Bootstrap requires server mode to be enabled", 37 }, 38 { 39 []string{"-data-dir=" + tmpDir, "-server", "-bootstrap-expect=1"}, 40 "WARNING: Bootstrap mode enabled!", 41 }, 42 { 43 []string{"-server"}, 44 "Must specify data directory", 45 }, 46 { 47 []string{"-client", "-alloc-dir="}, 48 "Must specify both the state and alloc dir if data-dir is omitted.", 49 }, 50 } 51 for _, tc := range tcases { 52 // Make a new command. We pre-emptively close the shutdownCh 53 // so that the command exits immediately instead of blocking. 54 ui := new(cli.MockUi) 55 shutdownCh := make(chan struct{}) 56 close(shutdownCh) 57 cmd := &Command{ 58 Ui: ui, 59 ShutdownCh: shutdownCh, 60 } 61 62 // To prevent test failures on hosts whose hostname resolves to 63 // a loopback address, we must append a bind address 64 tc.args = append(tc.args, "-bind=169.254.0.1") 65 if code := cmd.Run(tc.args); code != 1 { 66 t.Fatalf("args: %v\nexit: %d\n", tc.args, code) 67 } 68 69 if expect := tc.errOut; expect != "" { 70 out := ui.ErrorWriter.String() 71 if !strings.Contains(out, expect) { 72 t.Fatalf("expect to find %q\n\n%s", expect, out) 73 } 74 } 75 } 76 } 77 78 func TestRetryJoin(t *testing.T) { 79 dir, agent := makeAgent(t, nil) 80 defer os.RemoveAll(dir) 81 defer agent.Shutdown() 82 83 doneCh := make(chan struct{}) 84 shutdownCh := make(chan struct{}) 85 86 defer func() { 87 close(shutdownCh) 88 <-doneCh 89 }() 90 91 cmd := &Command{ 92 ShutdownCh: shutdownCh, 93 Ui: &cli.BasicUi{ 94 Reader: os.Stdin, 95 Writer: os.Stdout, 96 ErrorWriter: os.Stderr, 97 }, 98 } 99 100 serfAddr := fmt.Sprintf( 101 "%s:%d", 102 agent.config.BindAddr, 103 agent.config.Ports.Serf) 104 105 args := []string{ 106 "-dev", 107 "-node", fmt.Sprintf(`"Node %d"`, getPort()), 108 "-retry-join", serfAddr, 109 "-retry-interval", "1s", 110 } 111 112 go func() { 113 if code := cmd.Run(args); code != 0 { 114 t.Logf("bad: %d", code) 115 } 116 close(doneCh) 117 }() 118 119 testutil.WaitForResult(func() (bool, error) { 120 mem := agent.server.Members() 121 if len(mem) != 2 { 122 return false, fmt.Errorf("bad :%#v", mem) 123 } 124 return true, nil 125 }, func(err error) { 126 t.Fatalf(err.Error()) 127 }) 128 }