github.com/nir0s/nomad@v0.8.7-rc1/command/job_init_test.go (about)

     1  package command
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/mitchellh/cli"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestInitCommand_Implements(t *testing.T) {
    14  	t.Parallel()
    15  	var _ cli.Command = &JobInitCommand{}
    16  }
    17  
    18  func TestInitCommand_Run(t *testing.T) {
    19  	t.Parallel()
    20  	ui := new(cli.MockUi)
    21  	cmd := &JobInitCommand{Meta: Meta{Ui: ui}}
    22  
    23  	// Fails on misuse
    24  	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
    25  		t.Fatalf("expect exit code 1, got: %d", code)
    26  	}
    27  	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
    28  		t.Fatalf("expect help output, got: %s", out)
    29  	}
    30  	ui.ErrorWriter.Reset()
    31  
    32  	// Ensure we change the cwd back
    33  	origDir, err := os.Getwd()
    34  	if err != nil {
    35  		t.Fatalf("err: %s", err)
    36  	}
    37  	defer os.Chdir(origDir)
    38  
    39  	// Create a temp dir and change into it
    40  	dir, err := ioutil.TempDir("", "nomad")
    41  	if err != nil {
    42  		t.Fatalf("err: %s", err)
    43  	}
    44  	defer os.RemoveAll(dir)
    45  	if err := os.Chdir(dir); err != nil {
    46  		t.Fatalf("err: %s", err)
    47  	}
    48  
    49  	// Works if the file doesn't exist
    50  	if code := cmd.Run([]string{}); code != 0 {
    51  		t.Fatalf("expect exit code 0, got: %d", code)
    52  	}
    53  	content, err := ioutil.ReadFile(DefaultInitName)
    54  	if err != nil {
    55  		t.Fatalf("err: %s", err)
    56  	}
    57  	if string(content) != defaultJob {
    58  		t.Fatalf("unexpected file content\n\n%s", string(content))
    59  	}
    60  
    61  	// Works with -short flag
    62  	os.Remove(DefaultInitName)
    63  	if code := cmd.Run([]string{"-short"}); code != 0 {
    64  		require.Zero(t, code, "unexpected exit code: %d", code)
    65  	}
    66  	content, err = ioutil.ReadFile(DefaultInitName)
    67  	require.NoError(t, err)
    68  	require.Equal(t, string(content), shortJob)
    69  
    70  	// Fails if the file exists
    71  	if code := cmd.Run([]string{}); code != 1 {
    72  		t.Fatalf("expect exit code 1, got: %d", code)
    73  	}
    74  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "exists") {
    75  		t.Fatalf("expect file exists error, got: %s", out)
    76  	}
    77  }
    78  
    79  func TestInitCommand_defaultJob(t *testing.T) {
    80  	t.Parallel()
    81  	// Ensure the job file is always written with spaces instead of tabs. Since
    82  	// the default job file is embedded in the go file, it's easy for tabs to
    83  	// slip in.
    84  	if strings.Contains("\t", defaultJob) {
    85  		t.Error("default job contains tab character - please convert to spaces")
    86  	}
    87  }