github.com/quite/nomad@v0.8.6/command/agent/command_test.go (about)

     1  package agent
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/nomad/version"
    10  	"github.com/mitchellh/cli"
    11  )
    12  
    13  func TestCommand_Implements(t *testing.T) {
    14  	t.Parallel()
    15  	var _ cli.Command = &Command{}
    16  }
    17  
    18  func TestCommand_Args(t *testing.T) {
    19  	t.Parallel()
    20  	tmpDir, err := ioutil.TempDir("", "nomad")
    21  	if err != nil {
    22  		t.Fatalf("err: %s", err)
    23  	}
    24  	defer os.RemoveAll(tmpDir)
    25  
    26  	type tcase struct {
    27  		args   []string
    28  		errOut string
    29  	}
    30  	tcases := []tcase{
    31  		{
    32  			[]string{},
    33  			"Must specify either server, client or dev mode for the agent.",
    34  		},
    35  		{
    36  			[]string{"-client", "-data-dir=" + tmpDir, "-bootstrap-expect=1"},
    37  			"Bootstrap requires server mode to be enabled",
    38  		},
    39  		{
    40  			[]string{"-data-dir=" + tmpDir, "-server", "-bootstrap-expect=1"},
    41  			"WARNING: Bootstrap mode enabled!",
    42  		},
    43  		{
    44  			[]string{"-server"},
    45  			"Must specify data directory",
    46  		},
    47  		{
    48  			[]string{"-client", "-alloc-dir="},
    49  			"Must specify both the state and alloc dir if data-dir is omitted.",
    50  		},
    51  	}
    52  	for _, tc := range tcases {
    53  		// Make a new command. We preemptively close the shutdownCh
    54  		// so that the command exits immediately instead of blocking.
    55  		ui := new(cli.MockUi)
    56  		shutdownCh := make(chan struct{})
    57  		close(shutdownCh)
    58  		cmd := &Command{
    59  			Version:    version.GetVersion(),
    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  }