github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/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 t.Parallel() 16 var _ cli.Command = &Command{} 17 } 18 19 func TestCommand_Args(t *testing.T) { 20 t.Parallel() 21 tmpDir, err := ioutil.TempDir("", "nomad") 22 if err != nil { 23 t.Fatalf("err: %s", err) 24 } 25 defer os.RemoveAll(tmpDir) 26 27 type tcase struct { 28 args []string 29 errOut string 30 } 31 tcases := []tcase{ 32 { 33 []string{}, 34 "Must specify either server, client or dev mode for the agent.", 35 }, 36 { 37 []string{"-client", "-data-dir=" + tmpDir, "-bootstrap-expect=1"}, 38 "Bootstrap requires server mode to be enabled", 39 }, 40 { 41 []string{"-data-dir=" + tmpDir, "-server", "-bootstrap-expect=1"}, 42 "WARNING: Bootstrap mode enabled!", 43 }, 44 { 45 []string{"-server"}, 46 "Must specify data directory", 47 }, 48 { 49 []string{"-client", "-alloc-dir="}, 50 "Must specify both the state and alloc dir if data-dir is omitted.", 51 }, 52 } 53 for _, tc := range tcases { 54 // Make a new command. We pre-emptively close the shutdownCh 55 // so that the command exits immediately instead of blocking. 56 ui := new(cli.MockUi) 57 shutdownCh := make(chan struct{}) 58 close(shutdownCh) 59 cmd := &Command{ 60 Ui: ui, 61 ShutdownCh: shutdownCh, 62 } 63 64 // To prevent test failures on hosts whose hostname resolves to 65 // a loopback address, we must append a bind address 66 tc.args = append(tc.args, "-bind=169.254.0.1") 67 if code := cmd.Run(tc.args); code != 1 { 68 t.Fatalf("args: %v\nexit: %d\n", tc.args, code) 69 } 70 71 if expect := tc.errOut; expect != "" { 72 out := ui.ErrorWriter.String() 73 if !strings.Contains(out, expect) { 74 t.Fatalf("expect to find %q\n\n%s", expect, out) 75 } 76 } 77 } 78 } 79 80 // TODO Why is this failing 81 func TestRetryJoin(t *testing.T) { 82 t.Parallel() 83 agent := NewTestAgent(t.Name(), nil) 84 defer agent.Shutdown() 85 86 doneCh := make(chan struct{}) 87 shutdownCh := make(chan struct{}) 88 89 defer func() { 90 close(shutdownCh) 91 <-doneCh 92 }() 93 94 cmd := &Command{ 95 ShutdownCh: shutdownCh, 96 Ui: &cli.BasicUi{ 97 Reader: os.Stdin, 98 Writer: os.Stdout, 99 ErrorWriter: os.Stderr, 100 }, 101 } 102 103 serfAddr := agent.Config.normalizedAddrs.Serf 104 105 args := []string{ 106 "-dev", 107 "-node", "foo", 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 }