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