github.com/hernad/nomad@v1.6.112/command/node_pool_init_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "os" 8 "path" 9 "testing" 10 11 "github.com/mitchellh/cli" 12 "github.com/shoenig/test/must" 13 14 "github.com/hernad/nomad/ci" 15 "github.com/hernad/nomad/command/asset" 16 ) 17 18 func TestNodePoolInitCommand_Implements(t *testing.T) { 19 ci.Parallel(t) 20 var _ cli.Command = &NodePoolInitCommand{} 21 } 22 23 func TestNodePoolInitCommand_Run(t *testing.T) { 24 ci.Parallel(t) 25 dir := t.TempDir() 26 origDir, err := os.Getwd() 27 must.NoError(t, err) 28 err = os.Chdir(dir) 29 must.NoError(t, err) 30 t.Cleanup(func() { os.Chdir(origDir) }) 31 32 t.Run("hcl", func(t *testing.T) { 33 ci.Parallel(t) 34 dir := dir 35 ui := cli.NewMockUi() 36 cmd := &NodePoolInitCommand{Meta: Meta{Ui: ui}} 37 38 // Fails on misuse 39 ec := cmd.Run([]string{"some", "bad", "args"}) 40 must.Eq(t, 1, ec) 41 must.StrContains(t, ui.ErrorWriter.String(), commandErrorText(cmd)) 42 must.Eq(t, "", ui.OutputWriter.String()) 43 reset(ui) 44 45 // Works if the file doesn't exist 46 ec = cmd.Run([]string{"-out", "hcl"}) 47 must.Eq(t, "", ui.ErrorWriter.String()) 48 must.Eq(t, "Example node pool specification written to pool.nomad.hcl\n", ui.OutputWriter.String()) 49 must.Zero(t, ec) 50 reset(ui) 51 t.Cleanup(func() { os.Remove(path.Join(dir, "pool.nomad.hcl")) }) 52 53 content, err := os.ReadFile(DefaultHclNodePoolInitName) 54 must.NoError(t, err) 55 must.Eq(t, asset.NodePoolSpec, content) 56 57 // Fails if the file exists 58 ec = cmd.Run([]string{"-out", "hcl"}) 59 must.StrContains(t, ui.ErrorWriter.String(), "exists") 60 must.Eq(t, "", ui.OutputWriter.String()) 61 must.Eq(t, 1, ec) 62 reset(ui) 63 64 // Works if file is passed 65 ec = cmd.Run([]string{"-out", "hcl", "myTest.hcl"}) 66 must.Eq(t, "", ui.ErrorWriter.String()) 67 must.Eq(t, "Example node pool specification written to myTest.hcl\n", ui.OutputWriter.String()) 68 must.Zero(t, ec) 69 reset(ui) 70 71 t.Cleanup(func() { os.Remove(path.Join(dir, "myTest.hcl")) }) 72 content, err = os.ReadFile("myTest.hcl") 73 must.NoError(t, err) 74 must.Eq(t, asset.NodePoolSpec, content) 75 }) 76 77 t.Run("json", func(t *testing.T) { 78 ci.Parallel(t) 79 dir := dir 80 ui := cli.NewMockUi() 81 cmd := &NodePoolInitCommand{Meta: Meta{Ui: ui}} 82 83 // Fails on misuse 84 code := cmd.Run([]string{"some", "bad", "args"}) 85 must.Eq(t, 1, code) 86 must.StrContains(t, ui.ErrorWriter.String(), "This command takes no arguments or one") 87 must.Eq(t, "", ui.OutputWriter.String()) 88 reset(ui) 89 90 // Works if the file doesn't exist 91 code = cmd.Run([]string{"-out", "json"}) 92 must.StrContains(t, ui.OutputWriter.String(), "Example node pool specification written to pool.nomad.json\n") 93 must.Zero(t, code) 94 reset(ui) 95 96 t.Cleanup(func() { os.Remove(path.Join(dir, "pool.nomad.json")) }) 97 content, err := os.ReadFile(DefaultJsonNodePoolInitName) 98 must.NoError(t, err) 99 must.Eq(t, asset.NodePoolSpecJSON, content) 100 101 // Fails if the file exists 102 code = cmd.Run([]string{"-out", "json"}) 103 must.StrContains(t, ui.ErrorWriter.String(), "exists") 104 must.Eq(t, "", ui.OutputWriter.String()) 105 must.Eq(t, 1, code) 106 reset(ui) 107 108 // Works if file is passed 109 code = cmd.Run([]string{"-out", "json", "myTest.json"}) 110 must.StrContains(t, ui.OutputWriter.String(), "Example node pool specification written to myTest.json\n") 111 must.Zero(t, code) 112 reset(ui) 113 114 t.Cleanup(func() { os.Remove(path.Join(dir, "myTest.json")) }) 115 content, err = os.ReadFile("myTest.json") 116 must.NoError(t, err) 117 must.Eq(t, asset.NodePoolSpecJSON, content) 118 }) 119 }