@@ -10,11 +10,16 @@ import (
10
10
)
11
11
12
12
func TestWorkspacePreset (t * testing.T ) {
13
- // Happy Path:
14
- resource .Test (t , resource.TestCase {
15
- ProviderFactories : coderFactory (),
16
- IsUnitTest : true ,
17
- Steps : []resource.TestStep {{
13
+ t .Parallel ()
14
+ type testcase struct {
15
+ Name string
16
+ Config string
17
+ ExpectError * regexp.Regexp
18
+ Check func (state * terraform.State ) error
19
+ }
20
+ testcases := []testcase {
21
+ {
22
+ Name : "Happy Path" ,
18
23
Config : `
19
24
data "coder_workspace_preset" "preset_1" {
20
25
name = "preset_1"
@@ -32,96 +37,92 @@ func TestWorkspacePreset(t *testing.T) {
32
37
require .Equal (t , attrs ["parameters.region" ], "us-east1-a" )
33
38
return nil
34
39
},
35
- }},
36
- })
37
-
38
- // Given the Name field is not provided
39
- resource .Test (t , resource.TestCase {
40
- ProviderFactories : coderFactory (),
41
- IsUnitTest : true ,
42
- Steps : []resource.TestStep {{
40
+ },
41
+ {
42
+ Name : "Name field is not provided" ,
43
43
Config : `
44
44
data "coder_workspace_preset" "preset_1" {
45
45
parameters = {
46
46
"region" = "us-east1-a"
47
47
}
48
48
}` ,
49
- // This is from terraform's validation based on our schema, not based on our validation in ReadContext:
49
+ // This validation is done by Terraform, but it could still break if we misconfigure the schema.
50
+ // So we test it here to make sure we don't regress.
50
51
ExpectError : regexp .MustCompile ("The argument \" name\" is required, but no definition was found" ),
51
- }},
52
- })
53
-
54
- // Given the Name field is empty
55
- resource .Test (t , resource.TestCase {
56
- ProviderFactories : coderFactory (),
57
- IsUnitTest : true ,
58
- Steps : []resource.TestStep {{
52
+ },
53
+ {
54
+ Name : "Name field is empty" ,
59
55
Config : `
60
56
data "coder_workspace_preset" "preset_1" {
61
57
name = ""
62
58
parameters = {
63
59
"region" = "us-east1-a"
64
60
}
65
61
}` ,
66
- ExpectError : regexp .MustCompile ("workspace preset name must be set" ),
67
- }},
68
- })
69
-
70
- // Given the Name field is not a string
71
- resource .Test (t , resource.TestCase {
72
- ProviderFactories : coderFactory (),
73
- IsUnitTest : true ,
74
- Steps : []resource.TestStep {{
62
+ // This validation is done by Terraform, but it could still break if we misconfigure the schema.
63
+ // So we test it here to make sure we don't regress.
64
+ ExpectError : regexp .MustCompile ("expected \" name\" to not be an empty string" ),
65
+ },
66
+ {
67
+ Name : "Name field is not a string" ,
75
68
Config : `
76
69
data "coder_workspace_preset" "preset_1" {
77
70
name = [1, 2, 3]
78
71
parameters = {
79
72
"region" = "us-east1-a"
80
73
}
81
74
}` ,
75
+ // This validation is done by Terraform, but it could still break if we misconfigure the schema.
76
+ // So we test it here to make sure we don't regress.
82
77
ExpectError : regexp .MustCompile ("Incorrect attribute value type" ),
83
- }},
84
- })
85
-
86
- // Given the Parameters field is not provided
87
- resource .Test (t , resource.TestCase {
88
- ProviderFactories : coderFactory (),
89
- IsUnitTest : true ,
90
- Steps : []resource.TestStep {{
78
+ },
79
+ {
80
+ Name : "Parameters field is not provided" ,
91
81
Config : `
92
82
data "coder_workspace_preset" "preset_1" {
93
83
name = "preset_1"
94
84
}` ,
85
+ // This validation is done by Terraform, but it could still break if we misconfigure the schema.
86
+ // So we test it here to make sure we don't regress.
95
87
ExpectError : regexp .MustCompile ("The argument \" parameters\" is required, but no definition was found" ),
96
- }},
97
- })
98
-
99
- // Given the Parameters field is empty
100
- resource .Test (t , resource.TestCase {
101
- ProviderFactories : coderFactory (),
102
- IsUnitTest : true ,
103
- Steps : []resource.TestStep {{
88
+ },
89
+ {
90
+ Name : "Parameters field is empty" ,
104
91
Config : `
105
92
data "coder_workspace_preset" "preset_1" {
106
93
name = "preset_1"
107
94
parameters = {}
108
95
}` ,
109
- ExpectError : regexp .MustCompile ("workspace preset must define a value for at least one parameter" ),
110
- }},
111
- })
112
-
113
- // Given the Parameters field is not a map
114
- resource .Test (t , resource.TestCase {
115
- ProviderFactories : coderFactory (),
116
- IsUnitTest : true ,
117
- Steps : []resource.TestStep {{
96
+ // This validation is *not* done by Terraform, because MinItems doesn't work with maps.
97
+ // We've implemented the validation in ReadContext, so we test it here to make sure we don't regress.
98
+ ExpectError : regexp .MustCompile ("expected \" parameters\" to not be an empty map" ),
99
+ },
100
+ {
101
+ Name : "Parameters field is not a map" ,
118
102
Config : `
119
103
data "coder_workspace_preset" "preset_1" {
120
104
name = "preset_1"
121
105
parameters = "not a map"
122
106
}` ,
123
- // This is from terraform's validation based on our schema, not based on our validation in ReadContext:
107
+ // This validation is done by Terraform, but it could still break if we misconfigure the schema.
108
+ // So we test it here to make sure we don't regress.
124
109
ExpectError : regexp .MustCompile ("Inappropriate value for attribute \" parameters\" : map of string required" ),
125
- }},
126
- })
110
+ },
111
+ }
112
+
113
+ for _ , testcase := range testcases {
114
+ t .Run (testcase .Name , func (t * testing.T ) {
115
+ t .Parallel ()
116
+
117
+ resource .Test (t , resource.TestCase {
118
+ ProviderFactories : coderFactory (),
119
+ IsUnitTest : true ,
120
+ Steps : []resource.TestStep {{
121
+ Config : testcase .Config ,
122
+ ExpectError : testcase .ExpectError ,
123
+ Check : testcase .Check ,
124
+ }},
125
+ })
126
+ })
127
+ }
127
128
}
0 commit comments