forked from sl1pm4t/k2tf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhcl_writer_test.go
158 lines (145 loc) · 2.56 KB
/
hcl_writer_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/sl1pm4t/k2tf/pkg/testutils"
"github.com/hashicorp/hcl2/hclwrite"
"github.com/stretchr/testify/assert"
)
var update bool
func init() {
v := os.Getenv("UPDATE_GOLDEN")
if strings.ToLower(v) == "true" {
update = true
}
}
func testLoadFile(t *testing.T, fileparts ...string) string {
filename := filepath.Join(fileparts...)
content, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatalf("failed to load test file, %s: %v", filename, err)
}
return string(content)
}
func TestWriteObject(t *testing.T) {
tests := []struct {
name string
resourceType string
wantedWarnCount int
}{
{
"basicDeployment",
"kubernetes_deployment",
0,
},
{
"configMap",
"kubernetes_config_map",
0,
},
{
"cronJob",
"kubernetes_cron_job",
0,
},
{
"daemonset",
"kubernetes_daemonset",
0,
},
{
"deployment",
"kubernetes_deployment",
0,
},
{
"deployment2Containers",
"kubernetes_deployment",
0,
},
{
"endpoints",
"kubernetes_endpoints",
0,
},
{
"ingress",
"kubernetes_ingress",
0,
},
{
"job",
"kubernetes_job",
0,
},
{
"namespace",
"kubernetes_namespace",
0,
},
{
"namespace_w_spec",
"kubernetes_namespace",
1,
},
{
"networkPolicy",
"kubernetes_network_policy",
0,
},
{
"podDisruptionBudget",
"kubernetes_pod_disruption_budget",
0,
},
{
"podNodeExporter",
"kubernetes_pod",
0,
},
{
"role",
"kubernetes_role",
0,
},
{
"roleBinding",
"kubernetes_role_binding",
0,
},
{
"service",
"kubernetes_service",
0,
},
{
"statefulSet",
"kubernetes_stateful_set",
0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Generate HCL from test data
obj := testutils.TestParseYAML(t, testLoadFile(t, "test-fixtures", tt.name+".yaml"))
hclFile := hclwrite.NewEmptyFile()
warnCount, err := WriteObject(obj, hclFile.Body())
if err != nil {
t.Fatal(err)
}
// Read our golden file (or optionally write if env var is set)
goldenFile := filepath.Join("test-fixtures", tt.name+".tf.golden")
if update {
ioutil.WriteFile(goldenFile, hclFile.Bytes(), 0644)
}
expected := testLoadFile(t, goldenFile)
// Validate configs are equal
assert.Equal(t, expected, string(hclFile.Bytes()), "should be equal")
// Validate warning count
assert.Equal(t, tt.wantedWarnCount, warnCount, "conversion warning count should match")
})
}
}