-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathformtype_test.go
More file actions
434 lines (395 loc) · 12.1 KB
/
Copy pathformtype_test.go
File metadata and controls
434 lines (395 loc) · 12.1 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package provider_test
import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
"sync"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/coder/terraform-provider-coder/v2/provider"
)
// formTypeTestCase is the config for a single test case.
type formTypeTestCase struct {
name string
config formTypeCheck
assert paramAssert
expectError *regexp.Regexp
}
// paramAssert is asserted on the provider's parsed terraform state.
type paramAssert struct {
FormType provider.ParameterFormType
Type provider.OptionType
Styling json.RawMessage
}
// formTypeCheck is a struct that helps build the terraform config
type formTypeCheck struct {
formType provider.ParameterFormType
optionType provider.OptionType
options bool
// optional to inform the assert
customOptions []string
defValue string
styling json.RawMessage
}
func (c formTypeCheck) String() string {
return fmt.Sprintf("%s_%s_%t", c.formType, c.optionType, c.options)
}
func TestValidateFormType(t *testing.T) {
t.Parallel()
// formTypesChecked keeps track of all checks run. It will be used to
// ensure all combinations of form_type and option_type are tested.
// All untested options are assumed to throw an error.
var formTypesChecked sync.Map
expectType := func(expected provider.ParameterFormType, opts formTypeCheck) formTypeTestCase {
ftname := opts.formType
if ftname == "" {
ftname = "default"
}
if opts.styling == nil {
// Try passing arbitrary data in, as anything should be accepted
opts.styling, _ = json.Marshal(map[string]any{
"foo": "bar",
"disabled": true,
"nested": map[string]any{
"foo": "bar",
},
})
}
return formTypeTestCase{
name: fmt.Sprintf("%s_%s_%t",
ftname,
opts.optionType,
opts.options,
),
config: opts,
assert: paramAssert{
FormType: expected,
Type: opts.optionType,
Styling: opts.styling,
},
expectError: nil,
}
}
// expectSameFormType just assumes the FormType in the check is the expected
// FormType. Using `expectType` these fields can differ
expectSameFormType := func(opts formTypeCheck) formTypeTestCase {
return expectType(opts.formType, opts)
}
cases := []formTypeTestCase{
{
// When nothing is specified
name: "defaults",
config: formTypeCheck{},
assert: paramAssert{
FormType: provider.ParameterFormTypeInput,
Type: provider.OptionTypeString,
Styling: []byte("{}"),
},
},
// All default behaviors. Essentially legacy behavior.
// String
expectType(provider.ParameterFormTypeRadio, formTypeCheck{
options: true,
optionType: provider.OptionTypeString,
}),
expectType(provider.ParameterFormTypeInput, formTypeCheck{
options: false,
optionType: provider.OptionTypeString,
}),
// Number
expectType(provider.ParameterFormTypeRadio, formTypeCheck{
options: true,
optionType: provider.OptionTypeNumber,
}),
expectType(provider.ParameterFormTypeInput, formTypeCheck{
options: false,
optionType: provider.OptionTypeNumber,
}),
// Boolean
expectType(provider.ParameterFormTypeRadio, formTypeCheck{
options: true,
optionType: provider.OptionTypeBoolean,
}),
expectType(provider.ParameterFormTypeCheckbox, formTypeCheck{
options: false,
optionType: provider.OptionTypeBoolean,
}),
// List(string)
expectType(provider.ParameterFormTypeRadio, formTypeCheck{
options: true,
optionType: provider.OptionTypeListString,
}),
expectType(provider.ParameterFormTypeTagSelect, formTypeCheck{
options: false,
optionType: provider.OptionTypeListString,
}),
// ---- New Behavior
// String
expectSameFormType(formTypeCheck{
options: true,
optionType: provider.OptionTypeString,
formType: provider.ParameterFormTypeDropdown,
}),
expectSameFormType(formTypeCheck{
options: true,
optionType: provider.OptionTypeString,
formType: provider.ParameterFormTypeRadio,
}),
expectSameFormType(formTypeCheck{
options: false,
optionType: provider.OptionTypeString,
formType: provider.ParameterFormTypeInput,
}),
expectSameFormType(formTypeCheck{
options: false,
optionType: provider.OptionTypeString,
formType: provider.ParameterFormTypeTextArea,
}),
// Number
expectSameFormType(formTypeCheck{
options: true,
optionType: provider.OptionTypeNumber,
formType: provider.ParameterFormTypeDropdown,
}),
expectSameFormType(formTypeCheck{
options: true,
optionType: provider.OptionTypeNumber,
formType: provider.ParameterFormTypeRadio,
}),
expectSameFormType(formTypeCheck{
options: false,
optionType: provider.OptionTypeNumber,
formType: provider.ParameterFormTypeInput,
}),
expectSameFormType(formTypeCheck{
options: false,
optionType: provider.OptionTypeNumber,
formType: provider.ParameterFormTypeSlider,
}),
// Boolean
expectSameFormType(formTypeCheck{
options: true,
optionType: provider.OptionTypeBoolean,
formType: provider.ParameterFormTypeRadio,
}),
expectSameFormType(formTypeCheck{
options: true,
optionType: provider.OptionTypeBoolean,
formType: provider.ParameterFormTypeDropdown,
}),
expectSameFormType(formTypeCheck{
options: false,
optionType: provider.OptionTypeBoolean,
formType: provider.ParameterFormTypeSwitch,
}),
expectSameFormType(formTypeCheck{
options: false,
optionType: provider.OptionTypeBoolean,
formType: provider.ParameterFormTypeCheckbox,
}),
// List(string)
expectSameFormType(formTypeCheck{
options: true,
optionType: provider.OptionTypeListString,
formType: provider.ParameterFormTypeRadio,
}),
expectSameFormType(formTypeCheck{
options: true,
optionType: provider.OptionTypeListString,
formType: provider.ParameterFormTypeMultiSelect,
customOptions: []string{"red", "blue", "green"},
defValue: `["red", "blue"]`,
}),
expectSameFormType(formTypeCheck{
options: false,
optionType: provider.OptionTypeListString,
formType: provider.ParameterFormTypeTagSelect,
}),
// Some manual test cases
{
name: "list_string_bad_default",
config: formTypeCheck{
formType: provider.ParameterFormTypeMultiSelect,
optionType: provider.OptionTypeListString,
customOptions: []string{"red", "blue", "green"},
defValue: `["red", "yellow"]`,
styling: nil,
},
expectError: regexp.MustCompile("is not a valid option"),
},
}
passed := t.Run("TabledTests", func(t *testing.T) {
// TabledCases runs through all the manual test cases
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
t.Parallel()
if _, ok := formTypesChecked.Load(c.config.String()); ok {
t.Log("Duplicated form type check, delete this extra test case")
t.Fatalf("form type %q already checked", c.config.String())
}
formTypesChecked.Store(c.config.String(), struct{}{})
formTypeTest(t, c)
})
}
})
if !passed {
// Do not run additional tests and pollute the output
t.Log("Tests failed, will not run the assumed error cases")
return
}
// AssumeErrorCases assumes any uncovered test will return an error. Not covered
// cases in the truth table are assumed to be invalid. So if the tests above
// cover all valid cases, this asserts all the invalid cases.
//
// This test consequentially ensures all valid cases are covered manually above.
t.Run("AssumeErrorCases", func(t *testing.T) {
// requiredChecks loops through all possible form_type and option_type
// combinations.
requiredChecks := make([]formTypeCheck, 0)
for _, ft := range append(provider.ParameterFormTypes(), "") {
for _, ot := range provider.OptionTypes() {
requiredChecks = append(requiredChecks, formTypeCheck{
formType: ft,
optionType: ot,
options: false,
})
requiredChecks = append(requiredChecks, formTypeCheck{
formType: ft,
optionType: ot,
options: true,
})
}
}
for _, check := range requiredChecks {
if _, alreadyChecked := formTypesChecked.Load(check.String()); alreadyChecked {
continue
}
ftName := check.formType
if ftName == "" {
ftName = "default"
}
fc := formTypeTestCase{
name: fmt.Sprintf("%s_%s_%t",
ftName,
check.optionType,
check.options,
),
config: check,
assert: paramAssert{},
expectError: regexp.MustCompile("is not supported"),
}
t.Run(fc.name, func(t *testing.T) {
t.Parallel()
// This is just helpful log output to give the boilerplate
// to write the manual test.
tcText := fmt.Sprintf(`
expectSameFormType(%s, ezconfigOpts{
Options: %t,
OptionType: %q,
FormType: %q,
}),
//`, "<expected_form_type>", check.options, check.optionType, check.formType)
logDebugInfo := formTypeTest(t, fc)
if !logDebugInfo {
t.Logf("To construct this test case:\n%s", tcText)
}
})
}
})
}
// createTF converts a formTypeCheck into a terraform config string.
func createTF(paramName string, cfg formTypeCheck) (defaultValue string, tf string) {
options := cfg.customOptions
if cfg.options && len(cfg.customOptions) == 0 {
switch cfg.optionType {
case provider.OptionTypeString:
options = []string{"foo"}
defaultValue = "foo"
case provider.OptionTypeBoolean:
options = []string{"true", "false"}
defaultValue = "true"
case provider.OptionTypeNumber:
options = []string{"1"}
defaultValue = "1"
case provider.OptionTypeListString:
options = []string{`["red", "blue"]`}
defaultValue = `["red", "blue"]`
default:
panic(fmt.Sprintf("unknown option type %q when generating options", cfg.optionType))
}
}
if cfg.defValue == "" {
cfg.defValue = defaultValue
}
var body strings.Builder
if cfg.defValue != "" {
body.WriteString(fmt.Sprintf("default = %q\n", cfg.defValue))
}
if cfg.formType != "" {
body.WriteString(fmt.Sprintf("form_type = %q\n", cfg.formType))
}
if cfg.optionType != "" {
body.WriteString(fmt.Sprintf("type = %q\n", cfg.optionType))
}
if cfg.styling != nil {
body.WriteString(fmt.Sprintf("styling = %s\n", strconv.Quote(string(cfg.styling))))
}
for i, opt := range options {
body.WriteString("option {\n")
body.WriteString(fmt.Sprintf("name = \"val_%d\"\n", i))
body.WriteString(fmt.Sprintf("value = %q\n", opt))
body.WriteString("}\n")
}
return cfg.defValue, fmt.Sprintf(`
provider "coder" {
}
data "coder_parameter" "%s" {
name = "%s"
%s
}
`, paramName, paramName, body.String())
}
func formTypeTest(t *testing.T, c formTypeTestCase) bool {
t.Helper()
const paramName = "test_param"
// logDebugInfo is just a guess used for logging. It's not important. It cannot
// determine for sure if the test passed because the terraform test runner is a
// black box. It does not indicate if the test passed or failed. Since this is
// just used for logging, this is good enough.
logDebugInfo := true
def, tf := createTF(paramName, c.config)
checkFn := func(state *terraform.State) error {
require.Len(t, state.Modules, 1)
require.Len(t, state.Modules[0].Resources, 1)
key := strings.Join([]string{"data", "coder_parameter", paramName}, ".")
param := state.Modules[0].Resources[key]
logDebugInfo = logDebugInfo && assert.Equal(t, def, param.Primary.Attributes["default"], "default value")
logDebugInfo = logDebugInfo && assert.Equal(t, string(c.assert.FormType), param.Primary.Attributes["form_type"], "form_type")
logDebugInfo = logDebugInfo && assert.Equal(t, string(c.assert.Type), param.Primary.Attributes["type"], "type")
logDebugInfo = logDebugInfo && assert.JSONEq(t, string(c.assert.Styling), param.Primary.Attributes["styling"], "styling")
return nil
}
if c.expectError != nil {
checkFn = nil
}
resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProviderFactories: coderFactory(),
Steps: []resource.TestStep{
{
Config: tf,
Check: checkFn,
ExpectError: c.expectError,
},
},
})
if !logDebugInfo {
t.Logf("Terraform config:\n%s", tf)
}
return logDebugInfo
}