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