2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-09-09 21:55:20 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/ci"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestVarInitCommand_Implements(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
var _ cli.Command = &VarInitCommand{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVarInitCommand_Run(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
dir := t.TempDir()
|
|
|
|
origDir, err := os.Getwd()
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = os.Chdir(dir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() { os.Chdir(origDir) })
|
|
|
|
|
|
|
|
t.Run("hcl", func(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
dir := dir
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &VarInitCommand{Meta: Meta{Ui: ui}}
|
|
|
|
|
|
|
|
// Fails on misuse
|
|
|
|
ec := cmd.Run([]string{"some", "bad", "args"})
|
|
|
|
require.Equal(t, 1, ec)
|
|
|
|
require.Contains(t, ui.ErrorWriter.String(), commandErrorText(cmd))
|
|
|
|
require.Empty(t, ui.OutputWriter.String())
|
|
|
|
reset(ui)
|
|
|
|
|
|
|
|
// Works if the file doesn't exist
|
|
|
|
ec = cmd.Run([]string{"-out", "hcl"})
|
|
|
|
require.Empty(t, ui.ErrorWriter.String())
|
2022-09-12 20:26:18 +00:00
|
|
|
require.Equal(t, "Example variable specification written to spec.nv.hcl\n", ui.OutputWriter.String())
|
2022-09-09 21:55:20 +00:00
|
|
|
require.Zero(t, ec)
|
|
|
|
reset(ui)
|
2022-09-12 20:26:18 +00:00
|
|
|
t.Cleanup(func() { os.Remove(path.Join(dir, "spec.nv.hcl")) })
|
2022-09-09 21:55:20 +00:00
|
|
|
|
|
|
|
content, err := os.ReadFile(DefaultHclVarInitName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, defaultHclVarSpec, string(content))
|
|
|
|
|
|
|
|
// Fails if the file exists
|
|
|
|
ec = cmd.Run([]string{"-out", "hcl"})
|
|
|
|
require.Contains(t, ui.ErrorWriter.String(), "exists")
|
|
|
|
require.Empty(t, ui.OutputWriter.String())
|
|
|
|
require.Equal(t, 1, ec)
|
|
|
|
reset(ui)
|
|
|
|
|
|
|
|
// Works if file is passed
|
|
|
|
ec = cmd.Run([]string{"-out", "hcl", "myTest.hcl"})
|
|
|
|
require.Empty(t, ui.ErrorWriter.String())
|
|
|
|
require.Equal(t, "Example variable specification written to myTest.hcl\n", ui.OutputWriter.String())
|
|
|
|
require.Zero(t, ec)
|
|
|
|
reset(ui)
|
|
|
|
|
|
|
|
t.Cleanup(func() { os.Remove(path.Join(dir, "myTest.hcl")) })
|
|
|
|
content, err = os.ReadFile("myTest.hcl")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, defaultHclVarSpec, string(content))
|
|
|
|
})
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
dir := dir
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &VarInitCommand{Meta: Meta{Ui: ui}}
|
|
|
|
|
|
|
|
// Fails on misuse
|
|
|
|
code := cmd.Run([]string{"some", "bad", "args"})
|
|
|
|
require.Equal(t, 1, code)
|
|
|
|
require.Contains(t, ui.ErrorWriter.String(), "This command takes no arguments or one")
|
|
|
|
require.Empty(t, ui.OutputWriter.String())
|
|
|
|
reset(ui)
|
|
|
|
|
|
|
|
// Works if the file doesn't exist
|
|
|
|
code = cmd.Run([]string{"-out", "json"})
|
|
|
|
require.Contains(t, ui.ErrorWriter.String(), "REMINDER: While keys")
|
2022-09-12 20:26:18 +00:00
|
|
|
require.Contains(t, ui.OutputWriter.String(), "Example variable specification written to spec.nv.json\n")
|
2022-09-09 21:55:20 +00:00
|
|
|
require.Zero(t, code)
|
|
|
|
reset(ui)
|
|
|
|
|
2022-09-12 20:26:18 +00:00
|
|
|
t.Cleanup(func() { os.Remove(path.Join(dir, "spec.nv.json")) })
|
2022-09-09 21:55:20 +00:00
|
|
|
content, err := os.ReadFile(DefaultJsonVarInitName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, defaultJsonVarSpec, string(content))
|
|
|
|
|
|
|
|
// Fails if the file exists
|
|
|
|
code = cmd.Run([]string{"-out", "json"})
|
|
|
|
require.Contains(t, ui.ErrorWriter.String(), "exists")
|
|
|
|
require.Empty(t, ui.OutputWriter.String())
|
|
|
|
require.Equal(t, 1, code)
|
|
|
|
reset(ui)
|
|
|
|
|
|
|
|
// Works if file is passed
|
|
|
|
code = cmd.Run([]string{"-out", "json", "myTest.json"})
|
|
|
|
require.Contains(t, ui.ErrorWriter.String(), "REMINDER: While keys")
|
|
|
|
require.Contains(t, ui.OutputWriter.String(), "Example variable specification written to myTest.json\n")
|
|
|
|
require.Zero(t, code)
|
|
|
|
reset(ui)
|
|
|
|
|
|
|
|
t.Cleanup(func() { os.Remove(path.Join(dir, "myTest.json")) })
|
|
|
|
content, err = os.ReadFile("myTest.json")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, defaultJsonVarSpec, string(content))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func reset(ui *cli.MockUi) {
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
}
|