@@ -19,6 +19,11 @@ import (
19
19
"github.com/hashicorp/terraform-plugin-testing/terraform"
20
20
)
21
21
22
+ var (
23
+ reGroupURLPattern = regexp .MustCompile (`^/api(/controller)?/v2/groups/\d+/$` )
24
+ reInvalidVars = regexp .MustCompile ("Input type `str` is not a dictionary" )
25
+ )
26
+
22
27
func TestGroupResourceSchema (t * testing.T ) {
23
28
t .Parallel ()
24
29
@@ -35,14 +40,13 @@ func TestGroupResourceSchema(t *testing.T) {
35
40
36
41
// Validate the schema
37
42
diagnostics := schemaResponse .Schema .ValidateImplementation (ctx )
38
-
39
43
if diagnostics .HasError () {
40
44
t .Fatalf ("Schema validation diagnostics: %+v" , diagnostics )
41
45
}
42
46
}
43
47
44
48
func TestGroupResourceCreateRequestBody (t * testing.T ) {
45
- var testTable = []struct {
49
+ testCases : = []struct {
46
50
name string
47
51
input GroupResourceModel
48
52
expected []byte
@@ -97,14 +101,14 @@ func TestGroupResourceCreateRequestBody(t *testing.T) {
97
101
},
98
102
}
99
103
100
- for _ , test := range testTable {
101
- t .Run (test .name , func (t * testing.T ) {
102
- actual , diags := test .input .CreateRequestBody ()
104
+ for _ , testCase := range testCases {
105
+ t .Run (testCase .name , func (t * testing.T ) {
106
+ actual , diags := testCase .input .CreateRequestBody ()
103
107
if diags .HasError () {
104
108
t .Fatal (diags .Errors ())
105
109
}
106
- if ! bytes .Equal (test .expected , actual ) {
107
- t .Errorf ("Expected (%s) not equal to actual (%s)" , test .expected , actual )
110
+ if ! bytes .Equal (testCase .expected , actual ) {
111
+ t .Errorf ("Expected (%s) not equal to actual (%s)" , testCase .expected , actual )
108
112
}
109
113
})
110
114
}
@@ -114,7 +118,15 @@ func TestGroupResourceParseHttpResponse(t *testing.T) {
114
118
jsonError := diag.Diagnostics {}
115
119
jsonError .AddError ("Error parsing JSON response from AAP" , "invalid character 'N' looking for beginning of value" )
116
120
117
- var testTable = []struct {
121
+ const groupJSON = `{
122
+ "inventory": 1,
123
+ "description": "A basic test group",
124
+ "name": "group1",
125
+ "url": "/api/v2/groups/1/",
126
+ "variables": "{\"foo\":\"bar\",\"nested\":{\"foobar\":\"baz\"}}"
127
+ }`
128
+
129
+ testCases := []struct {
118
130
name string
119
131
input []byte
120
132
expected GroupResourceModel
@@ -139,9 +151,8 @@ func TestGroupResourceParseHttpResponse(t *testing.T) {
139
151
errors : diag.Diagnostics {},
140
152
},
141
153
{
142
- name : "test with all values" ,
143
- input : []byte (`{"inventory":1,"description":"A basic test group","name":"group1","url":"/api/v2/groups/1/",` +
144
- `"variables":"{\"foo\":\"bar\",\"nested\":{\"foobar\":\"baz\"}}"}` ),
154
+ name : "test with all values" ,
155
+ input : []byte (groupJSON ),
145
156
expected : GroupResourceModel {
146
157
InventoryId : types .Int64Value (1 ),
147
158
Id : types .Int64Value (0 ),
@@ -154,15 +165,15 @@ func TestGroupResourceParseHttpResponse(t *testing.T) {
154
165
},
155
166
}
156
167
157
- for _ , test := range testTable {
158
- t .Run (test .name , func (t * testing.T ) {
168
+ for _ , testCase := range testCases {
169
+ t .Run (testCase .name , func (t * testing.T ) {
159
170
resource := GroupResourceModel {}
160
- diags := resource .ParseHttpResponse (test .input )
161
- if ! test .errors .Equal (diags ) {
162
- t .Errorf ("Expected error diagnostics (%s), actual was (%s)" , test .errors , diags )
171
+ diags := resource .ParseHttpResponse (testCase .input )
172
+ if ! testCase .errors .Equal (diags ) {
173
+ t .Errorf ("Expected error diagnostics (%s), actual was (%s)" , testCase .errors , diags )
163
174
}
164
- if ! reflect .DeepEqual (test .expected , resource ) {
165
- t .Errorf ("Expected (%s) not equal to actual (%s)" , test .expected , resource )
175
+ if ! reflect .DeepEqual (testCase .expected , resource ) {
176
+ t .Errorf ("Expected (%s) not equal to actual (%s)" , testCase .expected , resource )
166
177
}
167
178
})
168
179
}
@@ -172,30 +183,29 @@ func TestGroupResourceParseHttpResponse(t *testing.T) {
172
183
173
184
func TestAccGroupResource (t * testing.T ) {
174
185
var groupApiModel GroupAPIModel
175
- var description = "A test group"
176
- var variables = "{\" foo\" : \" bar\" }"
177
186
inventoryName := acctest .RandStringFromCharSet (10 , acctest .CharSetAlphaNum )
178
187
groupName := acctest .RandStringFromCharSet (10 , acctest .CharSetAlphaNum )
179
188
updatedName := "updated" + groupName
189
+ description := "A test group"
190
+ variables := "{\" foo\" : \" bar\" }"
180
191
181
192
resource .Test (t , resource.TestCase {
182
193
PreCheck : func () { testAccPreCheck (t ) },
183
194
ProtoV6ProviderFactories : testAccProtoV6ProviderFactories ,
184
195
Steps : []resource.TestStep {
185
- // Invalid variables testing
196
+ // Invalid variables testing.
186
197
{
187
198
Config : testAccGroupResourceBadVariables (inventoryName , updatedName ),
188
- ExpectError : regexp . MustCompile ( "Input type `str` is not a dictionary" ) ,
199
+ ExpectError : reInvalidVars ,
189
200
},
190
- // Create and Read testing
191
201
{
192
202
Config : testAccGroupResourceMinimal (inventoryName , groupName ),
193
203
Check : resource .ComposeAggregateTestCheckFunc (
194
204
testAccCheckGroupResourceExists ("aap_group.test" , & groupApiModel ),
195
205
testAccCheckGroupResourceValues (& groupApiModel , groupName , "" , "" ),
196
206
resource .TestCheckResourceAttr ("aap_group.test" , "name" , groupName ),
197
207
resource .TestCheckResourceAttrPair ("aap_group.test" , "inventory_id" , "aap_inventory.test" , "id" ),
198
- resource .TestMatchResourceAttr ("aap_group.test" , "url" , regexp . MustCompile ( "^/api(/controller)?/v2/groups/[0-9]*/$" ) ),
208
+ resource .TestMatchResourceAttr ("aap_group.test" , "url" , reGroupURLPattern ),
199
209
),
200
210
},
201
211
{
@@ -207,7 +217,7 @@ func TestAccGroupResource(t *testing.T) {
207
217
resource .TestCheckResourceAttrPair ("aap_group.test" , "inventory_id" , "aap_inventory.test" , "id" ),
208
218
resource .TestCheckResourceAttr ("aap_group.test" , "description" , description ),
209
219
resource .TestCheckResourceAttr ("aap_group.test" , "variables" , variables ),
210
- resource .TestMatchResourceAttr ("aap_group.test" , "url" , regexp . MustCompile ( "^/api(/controller)?/v2/groups/[0-9]*/$" ) ),
220
+ resource .TestMatchResourceAttr ("aap_group.test" , "url" , reGroupURLPattern ),
211
221
),
212
222
},
213
223
},
0 commit comments