-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathdata_helm_template_test.go
More file actions
379 lines (346 loc) · 11.6 KB
/
Copy pathdata_helm_template_test.go
File metadata and controls
379 lines (346 loc) · 11.6 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright IBM Corp. 2017, 2026
// SPDX-License-Identifier: MPL-2.0
package helm
import (
"fmt"
"regexp"
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)
func TestAccDataTemplate_basic(t *testing.T) {
name := randName("basic")
namespace := randName(testNamespacePrefix)
datasourceAddress := fmt.Sprintf("data.helm_template.%s", testResourceName)
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{{
Config: testAccDataHelmTemplateConfigBasic(testResourceName, namespace, name, "1.2.3"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceAddress, "manifests.%", "6"),
resource.TestCheckResourceAttrSet(datasourceAddress, "manifests.templates/deployment.yaml"),
resource.TestCheckResourceAttrSet(datasourceAddress, "manifests.templates/service.yaml"),
resource.TestCheckResourceAttrSet(datasourceAddress, "manifests.templates/serviceaccount.yaml"),
resource.TestCheckResourceAttrSet(datasourceAddress, "manifests.templates/configmaps.yaml"),
resource.TestCheckResourceAttrSet(datasourceAddress, "manifests.templates/secrets.yaml"),
resource.TestCheckResourceAttrSet(datasourceAddress, "manifests.templates/tests/test-connection.yaml"),
resource.TestCheckResourceAttrSet(datasourceAddress, "manifest"),
resource.TestCheckResourceAttrSet(datasourceAddress, "notes"),
),
}},
})
}
func TestAccDataTemplate_crds(t *testing.T) {
name := randName("basic")
namespace := randName(testNamespacePrefix)
datasourceAddress := fmt.Sprintf("data.helm_template.%s", testResourceName)
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{{
Config: testAccDataHelmTemplateCRDs(testResourceName, namespace, name, "1.2.3"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceAddress, "manifests.%", "8"),
resource.TestCheckResourceAttrSet(datasourceAddress, "manifests.templates/deployment.yaml"),
resource.TestCheckResourceAttrSet(datasourceAddress, "manifests.templates/service.yaml"),
resource.TestCheckResourceAttrSet(datasourceAddress, "manifests.templates/serviceaccount.yaml"),
resource.TestCheckResourceAttrSet(datasourceAddress, "manifests.templates/configmaps.yaml"),
resource.TestCheckResourceAttrSet(datasourceAddress, "manifests.templates/tests/test-connection.yaml"),
resource.TestCheckResourceAttrSet(datasourceAddress, "manifest"),
resource.TestCheckResourceAttrSet(datasourceAddress, "notes"),
resource.TestCheckResourceAttr(datasourceAddress, "crds.0", `---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
# name must match the spec fields below, and be in the form: <plural>.<group>
name: apples.stable.example.com
spec:
# group name to use for REST API: /apis/<group>/<version>
group: stable.example.com
# list of versions supported by this CustomResourceDefinition
versions:
- name: v1
# Each version can be enabled/disabled by Served flag.
served: true
# One and only one version must be marked as the storage version.
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
cronSpec:
type: string
image:
type: string
replicas:
type: integer
# either Namespaced or Cluster
scope: Namespaced
names:
# plural name to be used in the URL: /apis/<group>/<version>/<plural>
plural: apples
# singular name to be used as an alias on the CLI and for display
singular: apple
# kind is normally the CamelCased singular type. Your resource manifests use this.
kind: Apple
# shortNames allow shorter string to match your resource on the CLI
shortNames:
- ap
`),
resource.TestCheckResourceAttr(datasourceAddress, "crds.1", `---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
# name must match the spec fields below, and be in the form: <plural>.<group>
name: oranges.stable.example.com
spec:
# group name to use for REST API: /apis/<group>/<version>
group: stable.example.com
# list of versions supported by this CustomResourceDefinition
versions:
- name: v1
# Each version can be enabled/disabled by Served flag.
served: true
# One and only one version must be marked as the storage version.
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
cronSpec:
type: string
image:
type: string
replicas:
type: integer
# either Namespaced or Cluster
scope: Namespaced
names:
# plural name to be used in the URL: /apis/<group>/<version>/<plural>
plural: oranges
# singular name to be used as an alias on the CLI and for display
singular: orange
# kind is normally the CamelCased singular type. Your resource manifests use this.
kind: Orange
# shortNames allow shorter string to match your resource on the CLI
shortNames:
- or
`),
),
}},
})
}
func TestAccDataTemplate_templates(t *testing.T) {
name := randName("basic")
namespace := randName(testNamespacePrefix)
datasourceAddress := fmt.Sprintf("data.helm_template.%s", testResourceName)
expectedTemplate := fmt.Sprintf(`---
# Source: test-chart/templates/configmaps.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: %[1]s-test-chart-one
labels:
helm.sh/chart: test-chart-1.2.3
app.kubernetes.io/name: test-chart
app.kubernetes.io/instance: %[1]s
app.kubernetes.io/version: "1.19.5"
app.kubernetes.io/managed-by: Helm
data:
test: one
---
# Source: test-chart/templates/configmaps.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: %[1]s-test-chart-two
labels:
helm.sh/chart: test-chart-1.2.3
app.kubernetes.io/name: test-chart
app.kubernetes.io/instance: %[1]s
app.kubernetes.io/version: "1.19.5"
app.kubernetes.io/managed-by: Helm
data:
test: two
`, name)
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{{
Config: testAccDataHelmTemplateConfigTemplates(testResourceName, namespace, name, "1.2.3"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceAddress, "manifests.%", "1"),
resource.TestCheckResourceAttrSet(datasourceAddress, "manifest"),
resource.TestCheckResourceAttrSet(datasourceAddress, "notes"),
resource.TestCheckResourceAttr(datasourceAddress, "manifests.templates/configmaps.yaml", expectedTemplate),
),
}},
})
}
func TestAccDataTemplate_kubeVersion(t *testing.T) {
name := randName("kube-version")
namespace := randName(testNamespacePrefix)
datasourceAddress := fmt.Sprintf("data.helm_template.%s", testResourceName)
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{{
Config: testAccDataHelmTemplateKubeVersionNoVersionSet(testResourceName, namespace, name, "1.2.3"),
ExpectError: regexp.MustCompile("chart requires kubeVersion.*"),
}},
})
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{{
Config: testAccDataHelmTemplateKubeVersion(testResourceName, namespace, name, "1.2.3", "1.18.0"),
ExpectError: regexp.MustCompile("chart requires kubeVersion.*"),
}},
})
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{{
Config: testAccDataHelmTemplateKubeVersion(testResourceName, namespace, name, "1.2.3", "abcdef"),
ExpectError: regexp.MustCompile(`couldn't parse string "abcdef" into kube-version`),
}},
})
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{{
Config: testAccDataHelmTemplateKubeVersion(testResourceName, namespace, name, "1.2.3", "1.22.0"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceAddress, "manifests.%", "1"),
resource.TestCheckResourceAttrSet(datasourceAddress, "manifests.templates/deployment.yaml"),
resource.TestCheckResourceAttrSet(datasourceAddress, "manifest"),
),
}},
})
}
func TestAccDataTemplate_configSetNull(t *testing.T) {
name := randName("basic")
namespace := randName(testNamespacePrefix)
datasourceAddress := fmt.Sprintf("data.helm_template.%s", testResourceName)
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{{
Config: testAccDataHelmTemplateConfigNullSet(testResourceName, namespace, name, "1.2.3"),
Check: resource.ComposeAggregateTestCheckFunc(
checkResourceAttrNotSet(datasourceAddress, "set.0.value"),
checkResourceAttrNotSet(datasourceAddress, "set.1.value"),
),
}},
})
}
func testAccDataHelmTemplateConfigBasic(resource, ns, name, version string) string {
return fmt.Sprintf(`
data "helm_template" "%s" {
show_only = [""]
name = %q
namespace = %q
description = "Test"
repository = %q
chart = "test-chart"
version = %q
set = [
{
name = "foo"
value = "bar"
},
{
name = "fizz"
value = 1337
}
]
}
`, resource, name, ns, testRepositoryURL, version)
}
func testAccDataHelmTemplateConfigTemplates(resource, ns, name, version string) string {
return fmt.Sprintf(`
data "helm_template" "%s" {
name = %q
namespace = %q
description = "Test"
repository = %q
chart = "test-chart"
version = %q
set = [
{
name = "foo"
value = "bar"
},
{
name = "fizz"
value = 1337
}
]
show_only = [
"templates/configmaps.yaml",
""
]
}
`, resource, name, ns, testRepositoryURL, version)
}
func testAccDataHelmTemplateConfigNullSet(resource, ns, name, version string) string {
return fmt.Sprintf(`
data "helm_template" "%s" {
name = %q
namespace = %q
description = "Test"
repository = %q
chart = "test-chart"
version = %q
set = [
{
name = "foo"
},
{
name = "fizz"
value = null
}
]
show_only = [
"templates/configmaps.yaml",
""
]
}
`, resource, name, ns, testRepositoryURL, version)
}
func testAccDataHelmTemplateKubeVersion(resource, ns, name, version, kubeVersion string) string {
return fmt.Sprintf(`
data "helm_template" "%s" {
name = %q
namespace = %q
description = "Test"
repository = %q
chart = "kube-version"
version = %q
kube_version = %q
}
`, resource, name, ns, testRepositoryURL, version, kubeVersion)
}
func testAccDataHelmTemplateKubeVersionNoVersionSet(resource, ns, name, version string) string {
return fmt.Sprintf(`
data "helm_template" "%s" {
name = %q
namespace = %q
description = "Test"
repository = %q
chart = "kube-version"
version = %q
}
`, resource, name, ns, testRepositoryURL, version)
}
func testAccDataHelmTemplateCRDs(resource, ns, name, version string) string {
return fmt.Sprintf(`
data "helm_template" "%s" {
name = %q
namespace = %q
description = "Test"
repository = %q
chart = "crds-chart"
include_crds = true
version = %q
}
`, resource, name, ns, testRepositoryURL, version)
}