forked from DavidKrau/terraform-provider-simplemdm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignmentGroup_resource_test.go
More file actions
316 lines (287 loc) · 11 KB
/
assignmentGroup_resource_test.go
File metadata and controls
316 lines (287 loc) · 11 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
package provider
import (
"context"
"fmt"
"strings"
"testing"
"time"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)
func testAccCheckAssignmentGroupDestroy(s *terraform.State) error {
client, err := getTestClient()
if err != nil {
return fmt.Errorf("failed to create test client: %w", err)
}
for name, rs := range s.RootModule().Resources {
// Skip if not our resource type
if rs.Type != "simplemdm_assignmentgroup" {
continue
}
// Skip data sources - they start with "data."
if strings.HasPrefix(name, "data.") {
continue
}
// Try to fetch the resource with retry for eventual consistency
// SimpleMDM API may take time to fully delete assignment groups
var lastErr error
for attempt := 0; attempt < 3; attempt++ {
_, lastErr = fetchAssignmentGroup(context.Background(), client, rs.Primary.ID)
// If we get a 404, the resource is properly deleted
if lastErr != nil && isNotFoundError(lastErr) {
break
}
// If the resource still exists after 3 attempts, it wasn't deleted
if lastErr == nil && attempt == 2 {
return fmt.Errorf("assignment group %s still exists after destroy", rs.Primary.ID)
}
// Wait briefly before retrying (only if not last attempt)
if attempt < 2 && lastErr == nil {
time.Sleep(2 * time.Second)
}
}
// If we got an error that's not a 404, that's unexpected
if lastErr != nil && !isNotFoundError(lastErr) {
return fmt.Errorf("unexpected error checking assignment group %s: %w", rs.Primary.ID, lastErr)
}
}
return nil
}
func TestAccAssignmentGroupResource(t *testing.T) {
testAccPreCheck(t)
// Use pre-existing fixture assignment group - Device Groups are deprecated!
assignmentGroupID := testAccRequireEnv(t, "SIMPLEMDM_ASSIGNMENT_GROUP_ID")
appID := testAccRequireEnv(t, "SIMPLEMDM_APP_ID")
profileID := testAccRequireEnv(t, "SIMPLEMDM_PROFILE_ID")
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
CheckDestroy: testAccCheckAssignmentGroupDestroy,
Steps: []resource.TestStep{
// Create and Read testing
{
Config: providerConfig + fmt.Sprintf(`
# Use pre-existing fixture resources
data "simplemdm_assignmentgroup" "fixture_group" {
id = "%s"
}
data "simplemdm_app" "fixture_app" {
id = "%s"
}
data "simplemdm_profile" "fixture_profile" {
id = "%s"
}
# Create assignment group using fixture references
resource "simplemdm_assignmentgroup" "testgroup2" {
name = "Test Assignment Group Resource"
auto_deploy = false
group_type = "standard"
priority = 3
app_track_location = false
apps = [data.simplemdm_app.fixture_app.id]
profiles = [data.simplemdm_profile.fixture_profile.id]
devices_remove_others = true
profiles_sync = false
apps_push = false
apps_update = false
}
`, assignmentGroupID, appID, profileID),
Check: resource.ComposeAggregateTestCheckFunc(
// Verify attributes
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.testgroup2", "name", "Test Assignment Group Resource"),
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.testgroup2", "group_type", "standard"),
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.testgroup2", "priority", "3"),
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.testgroup2", "app_track_location", "false"),
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.testgroup2", "devices_remove_others", "true"),
// Note: install_type is not returned by API for standard groups, will be null
// Note: Due to API eventual consistency, profiles and apps counts may be 0 or 1
// Verify dynamic values have any value set in the state
resource.TestCheckResourceAttrSet("simplemdm_assignmentgroup.testgroup2", "id"),
// Note: created_at and updated_at may not be immediately returned by API
),
// Allow non-empty plan due to API eventual consistency with relationships
ExpectNonEmptyPlan: true,
},
// ImportState testing
{
ResourceName: "simplemdm_assignmentgroup.testgroup2",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"apps_update", "apps_push", "auto_deploy", "profiles_sync", "install_type", "profiles", "apps", "created_at", "updated_at", "device_count", "group_count", "devices_remove_others"},
},
// Update and Read testing
{
Config: providerConfig + fmt.Sprintf(`
# Use fixture app for update
data "simplemdm_app" "fixture_app_updated" {
id = "%s"
}
# Update assignment group with modified attributes
resource "simplemdm_assignmentgroup" "testgroup2" {
name = "Updated Assignment Group Resource"
auto_deploy = false
group_type = "munki"
install_type = "managed"
priority = 7
app_track_location = true
apps = [data.simplemdm_app.fixture_app_updated.id]
devices_remove_others = false
profiles_sync = false
apps_push = false
apps_update = false
}
`, appID),
Check: resource.ComposeAggregateTestCheckFunc(
// Verify attributes
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.testgroup2", "name", "Updated Assignment Group Resource"),
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.testgroup2", "group_type", "munki"),
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.testgroup2", "install_type", "managed"),
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.testgroup2", "priority", "7"),
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.testgroup2", "app_track_location", "true"),
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.testgroup2", "devices_remove_others", "false"),
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.testgroup2", "apps.#", "1"),
// Verify dynamic relationships
resource.TestCheckResourceAttrPair(
"simplemdm_assignmentgroup.testgroup2", "apps.0",
"data.simplemdm_app.fixture_app_updated", "id",
),
// Verify dynamic values have any value set in the state
resource.TestCheckResourceAttrSet("simplemdm_assignmentgroup.testgroup2", "id"),
),
},
// Delete testing automatically occurs in TestCase
},
})
}
// TestAccAssignmentGroupResource_Import tests the import functionality
func TestAccAssignmentGroupResource_Import(t *testing.T) {
testAccPreCheck(t)
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: providerConfig + `
resource "simplemdm_assignmentgroup" "test_import" {
name = "Test Import Group"
auto_deploy = true
group_type = "standard"
priority = 5
}
`,
},
{
ResourceName: "simplemdm_assignmentgroup.test_import",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"apps_update", "apps_push", "profiles_sync", "devices_remove_others", "group_type",
},
},
},
})
}
// TestAccAssignmentGroupResource_RelationshipUpdates tests adding and removing relationships
func TestAccAssignmentGroupResource_RelationshipUpdates(t *testing.T) {
testAccPreCheck(t)
appID := testAccRequireEnv(t, "SIMPLEMDM_APP_ID")
profileID := testAccRequireEnv(t, "SIMPLEMDM_PROFILE_ID")
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
CheckDestroy: testAccCheckAssignmentGroupDestroy,
Steps: []resource.TestStep{
// Create with no apps or profiles
{
Config: providerConfig + `
resource "simplemdm_assignmentgroup" "test_relationships" {
name = "Test Relationships Group"
auto_deploy = false
group_type = "standard"
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.test_relationships", "name", "Test Relationships Group"),
resource.TestCheckResourceAttrSet("simplemdm_assignmentgroup.test_relationships", "id"),
),
},
// Add an app
{
Config: providerConfig + fmt.Sprintf(`
data "simplemdm_app" "test_app" {
id = "%s"
}
resource "simplemdm_assignmentgroup" "test_relationships" {
name = "Test Relationships Group"
auto_deploy = false
group_type = "standard"
apps = [data.simplemdm_app.test_app.id]
}
`, appID),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.test_relationships", "apps.#", "1"),
),
},
// Add a profile
{
Config: providerConfig + fmt.Sprintf(`
data "simplemdm_app" "test_app" {
id = "%s"
}
data "simplemdm_profile" "test_profile" {
id = "%s"
}
resource "simplemdm_assignmentgroup" "test_relationships" {
name = "Test Relationships Group"
auto_deploy = false
group_type = "standard"
apps = [data.simplemdm_app.test_app.id]
profiles = [data.simplemdm_profile.test_profile.id]
}
`, appID, profileID),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.test_relationships", "apps.#", "1"),
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.test_relationships", "profiles.#", "1"),
),
},
// Remove the app, keep profile
{
Config: providerConfig + fmt.Sprintf(`
data "simplemdm_profile" "test_profile" {
id = "%s"
}
resource "simplemdm_assignmentgroup" "test_relationships" {
name = "Test Relationships Group"
auto_deploy = false
group_type = "standard"
profiles = [data.simplemdm_profile.test_profile.id]
}
`, profileID),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.test_relationships", "profiles.#", "1"),
),
},
},
})
}
// TestAccAssignmentGroupResource_RateLimitHandling tests profile sync rate limiting
func TestAccAssignmentGroupResource_RateLimitHandling(t *testing.T) {
testAccPreCheck(t)
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
CheckDestroy: testAccCheckAssignmentGroupDestroy,
Steps: []resource.TestStep{
{
Config: providerConfig + `
resource "simplemdm_assignmentgroup" "test_ratelimit" {
name = "Test Rate Limit Group"
auto_deploy = false
group_type = "standard"
profiles_sync = true
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("simplemdm_assignmentgroup.test_ratelimit", "name", "Test Rate Limit Group"),
resource.TestCheckResourceAttrSet("simplemdm_assignmentgroup.test_ratelimit", "id"),
),
},
},
})
}