forked from sl1pm4t/k2tf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhcl_writer_test.go
217 lines (194 loc) · 4.16 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"github.com/sl1pm4t/k2tf/pkg/testutils"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/hashicorp/hcl2/hclwrite"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/stretchr/testify/assert"
"github.com/terraform-providers/terraform-provider-kubernetes/kubernetes"
)
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,
},
{
"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
expectedConfig := parseResourceHCL(t, []byte(expected))
actualConfig := parseResourceHCL(t, hclFile.Bytes())
assert.Equal(t,
expectedConfig,
actualConfig,
"resource config should be equal",
)
// Validate the generated config is TF schema compliant
assert.True(
t,
validateTerraformConfig(t, tt.resourceType, actualConfig),
"HCL should pass provider validation",
)
// Validate warning count
assert.Equal(t, tt.wantedWarnCount, warnCount, "conversion warning count should match")
})
}
}
func validateTerraformConfig(t *testing.T, resourceType string, cfg *config.RawConfig) bool {
// extract our resources rawConfig
rsrcConfig := terraform.NewResourceConfig(cfg)
// validate against the Kubernetes provider
prov := kubernetes.Provider().(*schema.Provider)
_, errs := prov.ValidateResource(resourceType, rsrcConfig)
if len(errs) > 0 {
// log validation errors
for i, v := range errs {
t.Fatalf("Validation Error: %d> %v\n", i, v)
}
return false
}
return true
}
func parseResourceHCL(t *testing.T, hcl []byte) *config.RawConfig {
// write HCL to temp location where Terraform can load it
tmpDir, err := ioutil.TempDir("", "k2tf")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
// Write the file
err = ioutil.WriteFile(filepath.Join(tmpDir, "hcl.tf"), hcl, os.ModePerm)
if err != nil {
t.Fatalf("test setup error: %v", err)
}
// use terraform to load config from tmp dir
cfg, err := config.LoadDir(tmpDir)
if err != nil {
t.Fatal(err)
}
if len(cfg.Resources) == 0 {
t.Fatal("HCL config load did not return a resource config")
}
// extract our resources rawConfig
return cfg.Resources[0].RawConfig
}