github.com/maier/nomad@v0.4.1-0.20161110003312-a9e3d0b8549d/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  		if code := cmd.Run(tc.args); code != 1 {
    63  			t.Fatalf("args: %v\nexit: %d\n", tc.args, code)
    64  		}
    65  
    66  		if expect := tc.errOut; expect != "" {
    67  			out := ui.ErrorWriter.String()
    68  			if !strings.Contains(out, expect) {
    69  				t.Fatalf("expect to find %q\n\n%s", expect, out)
    70  			}
    71  		}
    72  	}
    73  }
    74  
    75  func TestRetryJoin(t *testing.T) {
    76  	dir, agent := makeAgent(t, nil)
    77  	defer os.RemoveAll(dir)
    78  	defer agent.Shutdown()
    79  
    80  	tmpDir, err := ioutil.TempDir("", "nomad")
    81  	if err != nil {
    82  		t.Fatalf("err: %s", err)
    83  	}
    84  	defer os.RemoveAll(tmpDir)
    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:         new(cli.MockUi),
    97  	}
    98  
    99  	serfAddr := fmt.Sprintf(
   100  		"%s:%d",
   101  		agent.config.BindAddr,
   102  		agent.config.Ports.Serf)
   103  
   104  	args := []string{
   105  		"-server",
   106  		"-data-dir", tmpDir,
   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  }