2020-12-14 10:29:58 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2021-02-01 21:33:04 -05:00
|
|
|
"bytes"
|
2021-01-11 21:53:27 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-12-14 10:29:58 -05:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-01-11 21:53:27 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2020-12-14 10:29:58 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFmt(t *testing.T) {
|
|
|
|
s := &strings.Builder{}
|
|
|
|
ui := &packersdk.BasicUi{
|
|
|
|
Writer: s,
|
|
|
|
}
|
|
|
|
c := &FormatCommand{
|
|
|
|
Meta: testMeta(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui = ui
|
|
|
|
|
|
|
|
args := []string{"-check=true", filepath.Join(testFixture("fmt"), "formatted.pkr.hcl")}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
expected := ""
|
|
|
|
assert.Equal(t, expected, strings.TrimSpace(s.String()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFmt_unformattedPKRVarsTemplate(t *testing.T) {
|
|
|
|
c := &FormatCommand{
|
|
|
|
Meta: testMeta(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-check=true", filepath.Join(testFixture("fmt"), "unformatted.pkrvars.hcl")}
|
|
|
|
if code := c.Run(args); code != 3 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFmt_unfomattedTemlateDirectory(t *testing.T) {
|
|
|
|
c := &FormatCommand{
|
|
|
|
Meta: testMeta(t),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"-check=true", filepath.Join(testFixture("fmt"), "")}
|
|
|
|
|
|
|
|
if code := c.Run(args); code != 3 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
}
|
2021-01-08 21:22:26 -05:00
|
|
|
|
|
|
|
func TestFmt_Recursive(t *testing.T) {
|
2021-02-01 21:33:04 -05:00
|
|
|
unformattedData := `ami_filter_name ="amzn2-ami-hvm-*-x86_64-gp2"
|
2021-01-28 19:56:51 -05:00
|
|
|
ami_filter_owners =[ "137112412989" ]
|
|
|
|
|
2021-02-01 21:33:04 -05:00
|
|
|
`
|
2021-01-28 19:56:51 -05:00
|
|
|
|
2021-02-01 21:33:04 -05:00
|
|
|
formattedData := `ami_filter_name = "amzn2-ami-hvm-*-x86_64-gp2"
|
2021-01-28 19:56:51 -05:00
|
|
|
ami_filter_owners = ["137112412989"]
|
|
|
|
|
2021-02-01 21:33:04 -05:00
|
|
|
`
|
2021-01-28 19:56:51 -05:00
|
|
|
|
2021-02-01 21:33:04 -05:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
formatArgs []string // arguments passed to format
|
|
|
|
alreadyPresentContent map[string]string
|
|
|
|
expectedContent map[string]string
|
|
|
|
}{
|
2021-01-28 19:56:51 -05:00
|
|
|
{
|
2021-02-01 21:33:04 -05:00
|
|
|
name: "nested formats recursively",
|
|
|
|
formatArgs: []string{"-recursive=true"},
|
|
|
|
alreadyPresentContent: map[string]string{
|
|
|
|
"foo/bar/baz": unformattedData,
|
|
|
|
"foo/bar/baz/woo": unformattedData,
|
|
|
|
"": unformattedData,
|
|
|
|
},
|
|
|
|
expectedContent: map[string]string{
|
|
|
|
"foo/bar/baz": formattedData,
|
|
|
|
"foo/bar/baz/woo": formattedData,
|
|
|
|
"": formattedData,
|
|
|
|
},
|
2021-01-28 19:56:51 -05:00
|
|
|
},
|
|
|
|
{
|
2021-02-01 21:33:04 -05:00
|
|
|
name: "nested no recursive format",
|
|
|
|
formatArgs: []string{},
|
|
|
|
alreadyPresentContent: map[string]string{
|
|
|
|
"foo/bar/baz": unformattedData,
|
|
|
|
"foo/bar/baz/woo": unformattedData,
|
|
|
|
"": unformattedData,
|
|
|
|
},
|
|
|
|
expectedContent: map[string]string{
|
|
|
|
"foo/bar/baz": unformattedData,
|
|
|
|
"foo/bar/baz/woo": unformattedData,
|
|
|
|
"": formattedData,
|
|
|
|
},
|
2021-01-28 19:56:51 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-01-08 21:22:26 -05:00
|
|
|
c := &FormatCommand{
|
|
|
|
Meta: testMeta(t),
|
|
|
|
}
|
|
|
|
|
2021-02-10 21:10:34 -05:00
|
|
|
testDir := "test-fixtures/fmt"
|
2021-02-01 21:33:04 -05:00
|
|
|
|
|
|
|
for _, tt := range tests {
|
2021-02-10 21:10:34 -05:00
|
|
|
tempFileNames := make(map[string]string)
|
|
|
|
|
|
|
|
tempDirectory, err := ioutil.TempDir(testDir, "test-dir-*")
|
2021-02-01 21:33:04 -05:00
|
|
|
if err != nil {
|
2021-02-10 21:10:34 -05:00
|
|
|
t.Fatalf("Failed to create temp dir for test case: %s, error: %v", tt.name, err)
|
2021-02-01 21:33:04 -05:00
|
|
|
}
|
2021-02-10 21:10:34 -05:00
|
|
|
defer os.RemoveAll(tempDirectory)
|
2021-02-01 21:33:04 -05:00
|
|
|
|
2021-02-10 21:10:34 -05:00
|
|
|
for subDir, content := range tt.alreadyPresentContent {
|
|
|
|
dir := filepath.Join(tempDirectory, subDir)
|
|
|
|
err = os.MkdirAll(dir, 0700)
|
2021-02-01 21:33:04 -05:00
|
|
|
if err != nil {
|
2021-02-10 21:10:34 -05:00
|
|
|
t.Fatalf("Failed to create directory for test case: %s, error: %v", tt.name, err)
|
2021-02-01 21:33:04 -05:00
|
|
|
}
|
|
|
|
|
2021-02-10 21:10:34 -05:00
|
|
|
tempFile, err := ioutil.TempFile(dir, "*.pkrvars.hcl")
|
2021-02-01 21:33:04 -05:00
|
|
|
if err != nil {
|
2021-02-10 21:10:34 -05:00
|
|
|
t.Fatalf("Failed to create temp file for test case: %s, error: %v", tt.name, err)
|
2021-02-01 21:33:04 -05:00
|
|
|
}
|
|
|
|
|
2021-02-10 21:10:34 -05:00
|
|
|
_, err = tempFile.Write([]byte(content))
|
2021-02-01 21:33:04 -05:00
|
|
|
if err != nil {
|
2021-02-10 21:10:34 -05:00
|
|
|
t.Fatalf("Failed to write temp file for test case: %s, error: %v", tt.name, err)
|
2021-02-01 21:33:04 -05:00
|
|
|
}
|
2021-02-10 21:10:34 -05:00
|
|
|
tempFileNames[subDir] = tempFile.Name()
|
|
|
|
tempFile.Close()
|
2021-02-01 21:33:04 -05:00
|
|
|
}
|
|
|
|
|
2021-02-10 21:10:34 -05:00
|
|
|
testArgs := append(tt.formatArgs, tempDirectory)
|
2021-02-01 21:33:04 -05:00
|
|
|
if code := c.Run(testArgs); code != 0 {
|
2021-02-10 21:10:34 -05:00
|
|
|
os.RemoveAll(tempDirectory)
|
2021-02-01 21:33:04 -05:00
|
|
|
ui := c.Meta.Ui.(*packersdk.BasicUi)
|
|
|
|
out := ui.Writer.(*bytes.Buffer)
|
|
|
|
err := ui.ErrorWriter.(*bytes.Buffer)
|
|
|
|
t.Fatalf(
|
|
|
|
"Bad exit code for test case: %s.\n\nStdout:\n\n%s\n\nStderr:\n\n%s",
|
|
|
|
tt.name,
|
|
|
|
out.String(),
|
|
|
|
err.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
for expectedPath, expectedContent := range tt.expectedContent {
|
2021-02-10 21:10:34 -05:00
|
|
|
b, err := ioutil.ReadFile(tempFileNames[expectedPath])
|
2021-02-01 21:33:04 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ReadFile failed for test case: %s, error : %v", tt.name, err)
|
|
|
|
}
|
|
|
|
got := string(b)
|
|
|
|
if diff := cmp.Diff(got, expectedContent); diff != "" {
|
|
|
|
t.Errorf(
|
|
|
|
"format dir, unexpected result for test case: %s, path: %s, Expected: %s, Got: %s",
|
|
|
|
tt.name,
|
|
|
|
expectedPath,
|
|
|
|
expectedContent,
|
|
|
|
got)
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 21:53:27 -05:00
|
|
|
}
|
|
|
|
|
2021-01-08 21:22:26 -05:00
|
|
|
}
|