github.com/jmitchell/nomad@v0.1.3-0.20151007230021-7ab84c2862d8/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/mitchellh/cli"
    10  )
    11  
    12  func TestCommand_Implements(t *testing.T) {
    13  	var _ cli.Command = &Command{}
    14  }
    15  
    16  func TestCommand_Args(t *testing.T) {
    17  	tmpDir, err := ioutil.TempDir("", "nomad")
    18  	if err != nil {
    19  		t.Fatalf("err: %s", err)
    20  	}
    21  	defer os.RemoveAll(tmpDir)
    22  
    23  	type tcase struct {
    24  		args   []string
    25  		errOut string
    26  	}
    27  	tcases := []tcase{
    28  		{
    29  			[]string{},
    30  			"Must specify either server, client or dev mode for the agent.",
    31  		},
    32  		{
    33  			[]string{"-client", "-data-dir=" + tmpDir, "-bootstrap-expect=1"},
    34  			"Bootstrap requires server mode to be enabled",
    35  		},
    36  		{
    37  			[]string{"-data-dir=" + tmpDir, "-server", "-bootstrap-expect=1"},
    38  			"WARNING: Bootstrap mode enabled!",
    39  		},
    40  		{
    41  			[]string{"-server"},
    42  			"Must specify data directory",
    43  		},
    44  		{
    45  			[]string{"-client", "-alloc-dir="},
    46  			"Must specify both the state and alloc dir if data-dir is omitted.",
    47  		},
    48  	}
    49  	for _, tc := range tcases {
    50  		// Make a new command. We pre-emptively close the shutdownCh
    51  		// so that the command exits immediately instead of blocking.
    52  		ui := new(cli.MockUi)
    53  		shutdownCh := make(chan struct{})
    54  		close(shutdownCh)
    55  		cmd := &Command{
    56  			Ui:         ui,
    57  			ShutdownCh: shutdownCh,
    58  		}
    59  
    60  		if code := cmd.Run(tc.args); code != 1 {
    61  			t.Fatalf("args: %v\nexit: %d\n", tc.args, code)
    62  		}
    63  
    64  		if expect := tc.errOut; expect != "" {
    65  			out := ui.ErrorWriter.String()
    66  			if !strings.Contains(out, expect) {
    67  				t.Fatalf("expect to find %q\n\n%s", expect, out)
    68  			}
    69  		}
    70  	}
    71  }