github.com/smintz/nomad@v0.8.3/command/quota_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 TestQuotaInitCommand_Implements(t *testing.T) { 13 t.Parallel() 14 var _ cli.Command = &QuotaInitCommand{} 15 } 16 17 func TestQuotaInitCommand_Run_HCL(t *testing.T) { 18 t.Parallel() 19 ui := new(cli.MockUi) 20 cmd := &QuotaInitCommand{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, commandErrorText(cmd)) { 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(DefaultHclQuotaInitName) 53 if err != nil { 54 t.Fatalf("err: %s", err) 55 } 56 if string(content) != defaultHclQuotaSpec { 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 TestQuotaInitCommand_Run_JSON(t *testing.T) { 70 t.Parallel() 71 ui := new(cli.MockUi) 72 cmd := &QuotaInitCommand{Meta: Meta{Ui: ui}} 73 74 // Fails on misuse 75 if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 { 76 t.Fatalf("expect exit code 1, got: %d", code) 77 } 78 if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) { 79 t.Fatalf("expect help output, got: %s", out) 80 } 81 ui.ErrorWriter.Reset() 82 83 // Ensure we change the cwd back 84 origDir, err := os.Getwd() 85 if err != nil { 86 t.Fatalf("err: %s", err) 87 } 88 defer os.Chdir(origDir) 89 90 // Create a temp dir and change into it 91 dir, err := ioutil.TempDir("", "nomad") 92 if err != nil { 93 t.Fatalf("err: %s", err) 94 } 95 defer os.RemoveAll(dir) 96 if err := os.Chdir(dir); err != nil { 97 t.Fatalf("err: %s", err) 98 } 99 100 // Works if the file doesn't exist 101 if code := cmd.Run([]string{"-json"}); code != 0 { 102 t.Fatalf("expect exit code 0, got: %d", code) 103 } 104 content, err := ioutil.ReadFile(DefaultJsonQuotaInitName) 105 if err != nil { 106 t.Fatalf("err: %s", err) 107 } 108 if string(content) != defaultJsonQuotaSpec { 109 t.Fatalf("unexpected file content\n\n%s", string(content)) 110 } 111 112 // Fails if the file exists 113 if code := cmd.Run([]string{"-json"}); code != 1 { 114 t.Fatalf("expect exit code 1, got: %d", code) 115 } 116 if out := ui.ErrorWriter.String(); !strings.Contains(out, "exists") { 117 t.Fatalf("expect file exists error, got: %s", out) 118 } 119 }