1
1
package provider_test
2
2
3
3
import (
4
+ "regexp"
4
5
"testing"
5
6
6
7
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -9,6 +10,7 @@ import (
9
10
)
10
11
11
12
func TestWorkspacePreset (t * testing.T ) {
13
+ // Happy Path:
12
14
resource .Test (t , resource.TestCase {
13
15
ProviderFactories : coderFactory (),
14
16
IsUnitTest : true ,
@@ -32,4 +34,94 @@ func TestWorkspacePreset(t *testing.T) {
32
34
},
33
35
}},
34
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 {{
43
+ Config : `
44
+ data "coder_workspace_preset" "preset_1" {
45
+ parameters = {
46
+ "region" = "us-east1-a"
47
+ }
48
+ }` ,
49
+ // This is from terraform's validation based on our schema, not based on our validation in ReadContext:
50
+ 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 {{
59
+ Config : `
60
+ data "coder_workspace_preset" "preset_1" {
61
+ name = ""
62
+ parameters = {
63
+ "region" = "us-east1-a"
64
+ }
65
+ }` ,
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 {{
75
+ Config : `
76
+ data "coder_workspace_preset" "preset_1" {
77
+ name = [1, 2, 3]
78
+ parameters = {
79
+ "region" = "us-east1-a"
80
+ }
81
+ }` ,
82
+ 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 {{
91
+ Config : `
92
+ data "coder_workspace_preset" "preset_1" {
93
+ name = "preset_1"
94
+ }` ,
95
+ 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 {{
104
+ Config : `
105
+ data "coder_workspace_preset" "preset_1" {
106
+ name = "preset_1"
107
+ parameters = {}
108
+ }` ,
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 {{
118
+ Config : `
119
+ data "coder_workspace_preset" "preset_1" {
120
+ name = "preset_1"
121
+ parameters = "not a map"
122
+ }` ,
123
+ // This is from terraform's validation based on our schema, not based on our validation in ReadContext:
124
+ ExpectError : regexp .MustCompile ("Inappropriate value for attribute \" parameters\" : map of string required" ),
125
+ }},
126
+ })
35
127
}
0 commit comments