-
Notifications
You must be signed in to change notification settings - Fork 5
/
cmdimport_test.go
95 lines (88 loc) · 3.13 KB
/
cmdimport_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import "testing"
func TestRunImportSuccess(t *testing.T) {
testCases := []struct {
name string
resDefs string
srcPlanPath string
wantUpPath string
wantDownPath string
}{
{
name: "import resources",
resDefs: "testdata/import/terravalet_imports_definitions.json",
srcPlanPath: "testdata/import/08_import_src-plan.json",
wantUpPath: "testdata/import/08_import_up.sh",
wantDownPath: "testdata/import/08_import_down.sh",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
args := []string{"terravalet", "import",
"--res-defs", tc.resDefs,
"--src-plan", tc.srcPlanPath,
}
runSuccess(t, args, tc.wantUpPath, tc.wantDownPath)
})
}
}
func TestRunImportFailure(t *testing.T) {
testCases := []struct {
name string
resDefs string
srcPlanPath string
wantErr string
}{
{
name: "non existing src-plan",
resDefs: "testdata/import/terravalet_imports_definitions.json",
srcPlanPath: "src-plan-path-dummy",
wantErr: "opening the terraform plan file: open src-plan-path-dummy: no such file or directory",
},
{
name: "src-plan is invalid json",
resDefs: "testdata/import/terravalet_imports_definitions.json",
srcPlanPath: "testdata/import/09_import_empty_src-plan.json",
wantErr: "parse src-plan: parsing the plan: unexpected end of JSON input",
},
{
name: "src-plan must create resource",
resDefs: "testdata/import/terravalet_imports_definitions.json",
srcPlanPath: "testdata/import/10_import_no-new-resources.json",
wantErr: "parse src-plan: src-plan doesn't contains resources to create",
},
{
name: "src-plan contains only undefined resources",
resDefs: "testdata/import/terravalet_imports_definitions.json",
srcPlanPath: "testdata/import/11_import_src-plan_undefined_resources.json",
wantErr: "parse src-plan: src-plan contains only undefined resources",
},
{
name: "src-plan contains a not existing resource parameter",
resDefs: "testdata/import/terravalet_imports_definitions.json",
srcPlanPath: "testdata/import/12_import_src-plan_invalid_resource_param.json",
wantErr: "parse src-plan: error in resources definition dummy_resource2: field 'long_name' doesn't exist in plan",
},
{
name: "terravalet missing resources definitions file",
resDefs: "testdata/import/missing.file",
srcPlanPath: "testdata/import/08_import_src-plan.json",
wantErr: "opening the definitions file: open testdata/import/missing.file: no such file or directory",
},
{
name: "terravalet invalid resources definitions file",
resDefs: "testdata/import/invalid_imports_definitions.json",
srcPlanPath: "testdata/import/08_import_src-plan.json",
wantErr: "parse src-plan: parsing resources definitions: invalid character '}' after object key",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
args := []string{"terravalet", "import",
"--res-defs", tc.resDefs,
"--src-plan", tc.srcPlanPath,
}
runFailure(t, args, tc.wantErr)
})
}
}