forked from brimstone/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhcl2_upgrade_test.go
72 lines (65 loc) · 1.99 KB
/
hcl2_upgrade_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package command
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
)
func Test_hcl2_upgrade(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("Getwd: %v", err)
}
_ = cwd
tc := []struct {
folder string
flags []string
exitCode int
}{
{folder: "unknown_builder", flags: []string{}, exitCode: 1},
{folder: "complete", flags: []string{"-with-annotations"}},
{folder: "without-annotations", flags: []string{}},
{folder: "minimal", flags: []string{"-with-annotations"}},
{folder: "source-name", flags: []string{"-with-annotations"}},
{folder: "error-cleanup-provisioner", flags: []string{"-with-annotations"}},
{folder: "aws-access-config", flags: []string{}},
{folder: "variables-only", flags: []string{}},
{folder: "variables-with-variables", flags: []string{}},
{folder: "complete-variables-with-template-engine", flags: []string{}},
{folder: "escaping", flags: []string{}},
}
for _, tc := range tc {
t.Run(tc.folder, func(t *testing.T) {
inputPath := filepath.Join(testFixture("hcl2_upgrade", tc.folder, "input.json"))
outputPath := inputPath + ".pkr.hcl"
expectedPath := filepath.Join(testFixture("hcl2_upgrade", tc.folder, "expected.pkr.hcl"))
args := []string{"hcl2_upgrade"}
if len(tc.flags) > 0 {
args = append(args, tc.flags...)
}
args = append(args, inputPath)
p := helperCommand(t, args...)
err := p.Run()
if err != nil {
t.Logf("run returned an error: %s", err)
}
expected := string(mustBytes(ioutil.ReadFile(expectedPath)))
actual := string(mustBytes(ioutil.ReadFile(outputPath)))
if diff := cmp.Diff(expected, actual); diff != "" {
t.Fatalf("unexpected output: %s", diff)
}
actualExitCode := p.ProcessState.ExitCode()
if tc.exitCode != actualExitCode {
t.Fatalf("unexpected exit code: %d found; expected %d ", actualExitCode, tc.exitCode)
}
os.Remove(outputPath)
})
}
}
func mustBytes(b []byte, e error) []byte {
if e != nil {
panic(e)
}
return b
}