Skip to content

Commit 24d2bcb

Browse files
committed
Rewrite tests using the resource_github_organization_custom_properties
Setup the tests case using the config instead of using the API
1 parent 3d68770 commit 24d2bcb

1 file changed

Lines changed: 93 additions & 92 deletions

File tree

github/resource_github_organization_ruleset_test.go

Lines changed: 93 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package github
22

33
import (
4-
"context"
54
"fmt"
65
"regexp"
76
"testing"
87

9-
"github.com/google/go-github/v82/github"
108
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
119
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
1210
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
@@ -187,17 +185,25 @@ resource "github_organization_ruleset" "test" {
187185
t.Run("create_ruleset_with_repository_property", func(t *testing.T) {
188186
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
189187
rulesetName := fmt.Sprintf("%s-repo-prop-ruleset-%s", testResourcePrefix, randomID)
188+
propName := fmt.Sprintf("e2e_test_team_%s", randomID)
190189

191190
config := fmt.Sprintf(`
191+
resource "github_organization_custom_properties" "team" {
192+
property_name = "%[2]s"
193+
value_type = "single_select"
194+
required = false
195+
allowed_values = ["blue", "red", "backend", "platform"]
196+
}
197+
192198
resource "github_organization_ruleset" "test" {
193-
name = "%s"
199+
name = "%[1]s"
194200
target = "branch"
195201
enforcement = "active"
196202
197203
conditions {
198204
repository_property {
199205
include = [{
200-
name = "team"
206+
name = "%[2]s"
201207
source = "custom"
202208
property_values = ["blue"]
203209
}]
@@ -216,8 +222,10 @@ resource "github_organization_ruleset" "test" {
216222
deletion = true
217223
required_linear_history = true
218224
}
225+
226+
depends_on = [github_organization_custom_properties.team]
219227
}
220-
`, rulesetName)
228+
`, rulesetName, propName)
221229

222230
resource.Test(t, resource.TestCase{
223231
PreCheck: func() { skipUnlessHasPaidOrgs(t) },
@@ -230,7 +238,7 @@ resource "github_organization_ruleset" "test" {
230238
resource.TestCheckResourceAttr("github_organization_ruleset.test", "target", "branch"),
231239
resource.TestCheckResourceAttr("github_organization_ruleset.test", "enforcement", "active"),
232240
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.#", "1"),
233-
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.0.name", "team"),
241+
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.0.name", propName),
234242
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.0.source", "custom"),
235243
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.0.property_values.#", "1"),
236244
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.0.property_values.0", "blue"),
@@ -243,18 +251,26 @@ resource "github_organization_ruleset" "test" {
243251
t.Run("create_ruleset_with_repository_property_exclude", func(t *testing.T) {
244252
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
245253
rulesetName := fmt.Sprintf("%s-repo-prop-exclude-ruleset-%s", testResourcePrefix, randomID)
254+
propName := fmt.Sprintf("e2e_test_team_%s", randomID)
246255

247256
config := fmt.Sprintf(`
257+
resource "github_organization_custom_properties" "team" {
258+
property_name = "%[2]s"
259+
value_type = "single_select"
260+
required = false
261+
allowed_values = ["blue", "red", "backend", "platform"]
262+
}
263+
248264
resource "github_organization_ruleset" "test" {
249-
name = "%s"
265+
name = "%[1]s"
250266
target = "branch"
251267
enforcement = "active"
252268
253269
conditions {
254270
repository_property {
255271
include = []
256272
exclude = [{
257-
name = "team"
273+
name = "%[2]s"
258274
source = "custom"
259275
property_values = ["red"]
260276
}]
@@ -269,8 +285,10 @@ resource "github_organization_ruleset" "test" {
269285
rules {
270286
required_linear_history = true
271287
}
288+
289+
depends_on = [github_organization_custom_properties.team]
272290
}
273-
`, rulesetName)
291+
`, rulesetName, propName)
274292

275293
resource.Test(t, resource.TestCase{
276294
PreCheck: func() { skipUnlessHasPaidOrgs(t) },
@@ -282,7 +300,7 @@ resource "github_organization_ruleset" "test" {
282300
resource.TestCheckResourceAttr("github_organization_ruleset.test", "name", rulesetName),
283301
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.#", "0"),
284302
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.exclude.#", "1"),
285-
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.exclude.0.name", "team"),
303+
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.exclude.0.name", propName),
286304
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.exclude.0.source", "custom"),
287305
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.exclude.0.property_values.0", "red"),
288306
),
@@ -294,62 +312,39 @@ resource "github_organization_ruleset" "test" {
294312
t.Run("create_ruleset_with_multiple_repository_properties", func(t *testing.T) {
295313
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
296314
rulesetName := fmt.Sprintf("%s-repo-prop-multiple-%s", testResourcePrefix, randomID)
315+
propEnvironmentName := fmt.Sprintf("e2e_test_environment_%s", randomID)
316+
propTierName := fmt.Sprintf("e2e_test_tier_%s", randomID)
297317

298-
// Setup: Create custom properties for testing
299-
meta, err := getTestMeta()
300-
if err != nil {
301-
t.Fatalf("Error getting test meta: %v", err)
302-
}
303-
304-
ctx := t.Context()
305-
org := testAccConf.owner
306-
307-
// Create test properties
308-
properties := []struct {
309-
name string
310-
values []string
311-
}{
312-
{name: "e2e_test_environment", values: []string{"production", "staging"}},
313-
{name: "e2e_test_tier", values: []string{"premium", "enterprise"}},
314-
}
315-
316-
for _, prop := range properties {
317-
_, _, err := meta.v3client.Organizations.CreateOrUpdateCustomProperty(ctx, org, prop.name, &github.CustomProperty{
318-
ValueType: github.PropertyValueTypeSingleSelect,
319-
Required: github.Ptr(false),
320-
AllowedValues: prop.values,
321-
})
322-
if err != nil {
323-
t.Logf("Warning: Could not create custom property %s (may already exist): %v", prop.name, err)
324-
}
325-
}
318+
config := fmt.Sprintf(`
319+
resource "github_organization_custom_properties" "environment" {
320+
property_name = "%[2]s"
321+
value_type = "single_select"
322+
required = false
323+
allowed_values = ["production", "staging"]
324+
}
326325
327-
// Cleanup: Remove custom properties after test
328-
defer func() {
329-
for _, prop := range properties {
330-
_, err := meta.v3client.Organizations.RemoveCustomProperty(ctx, org, prop.name)
331-
if err != nil {
332-
t.Logf("Warning: Could not remove custom property %s: %v", prop.name, err)
333-
}
334-
}
335-
}()
326+
resource "github_organization_custom_properties" "tier" {
327+
property_name = "%[3]s"
328+
value_type = "single_select"
329+
required = false
330+
allowed_values = ["premium", "enterprise"]
331+
}
336332
337-
config := fmt.Sprintf(`
338333
resource "github_organization_ruleset" "test" {
339-
name = "%s"
334+
name = "%[1]s"
340335
target = "branch"
341336
enforcement = "active"
342337
343338
conditions {
344339
repository_property {
345340
include = [
346341
{
347-
name = "e2e_test_environment"
342+
name = "%[2]s"
348343
source = "custom"
349344
property_values = ["production"]
350345
},
351346
{
352-
name = "e2e_test_tier"
347+
name = "%[3]s"
353348
source = "custom"
354349
property_values = ["premium", "enterprise"]
355350
}
@@ -366,8 +361,13 @@ resource "github_organization_ruleset" "test" {
366361
rules {
367362
required_signatures = true
368363
}
364+
365+
depends_on = [
366+
github_organization_custom_properties.environment,
367+
github_organization_custom_properties.tier,
368+
]
369369
}
370-
`, rulesetName)
370+
`, rulesetName, propEnvironmentName, propTierName)
371371

372372
resource.Test(t, resource.TestCase{
373373
PreCheck: func() { skipUnlessHasPaidOrgs(t) },
@@ -378,11 +378,11 @@ resource "github_organization_ruleset" "test" {
378378
Check: resource.ComposeTestCheckFunc(
379379
resource.TestCheckResourceAttr("github_organization_ruleset.test", "name", rulesetName),
380380
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.#", "2"),
381-
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.0.name", "e2e_test_environment"),
381+
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.0.name", propEnvironmentName),
382382
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.0.source", "custom"),
383383
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.0.property_values.#", "1"),
384384
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.0.property_values.0", "production"),
385-
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.1.name", "e2e_test_tier"),
385+
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.1.name", propTierName),
386386
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.1.source", "custom"),
387387
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.1.property_values.#", "2"),
388388
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.1.property_values.0", "premium"),
@@ -396,17 +396,25 @@ resource "github_organization_ruleset" "test" {
396396
t.Run("update_repository_property", func(t *testing.T) {
397397
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
398398
rulesetName := fmt.Sprintf("%s-repo-prop-update-%s", testResourcePrefix, randomID)
399+
propName := fmt.Sprintf("e2e_test_team_%s", randomID)
399400

400401
config := fmt.Sprintf(`
402+
resource "github_organization_custom_properties" "team" {
403+
property_name = "%[2]s"
404+
value_type = "single_select"
405+
required = false
406+
allowed_values = ["blue", "red", "backend", "platform"]
407+
}
408+
401409
resource "github_organization_ruleset" "test" {
402-
name = "%s"
410+
name = "%[1]s"
403411
target = "branch"
404412
enforcement = "active"
405413
406414
conditions {
407415
repository_property {
408416
include = [{
409-
name = "team"
417+
name = "%[2]s"
410418
source = "custom"
411419
property_values = ["blue"]
412420
}]
@@ -422,19 +430,28 @@ resource "github_organization_ruleset" "test" {
422430
rules {
423431
creation = true
424432
}
433+
434+
depends_on = [github_organization_custom_properties.team]
425435
}
426-
`, rulesetName)
436+
`, rulesetName, propName)
427437

428438
configUpdated := fmt.Sprintf(`
439+
resource "github_organization_custom_properties" "team" {
440+
property_name = "%[2]s"
441+
value_type = "single_select"
442+
required = false
443+
allowed_values = ["blue", "red", "backend", "platform"]
444+
}
445+
429446
resource "github_organization_ruleset" "test" {
430-
name = "%s"
447+
name = "%[1]s"
431448
target = "branch"
432449
enforcement = "active"
433450
434451
conditions {
435452
repository_property {
436453
include = [{
437-
name = "team"
454+
name = "%[2]s"
438455
source = "custom"
439456
property_values = ["backend", "platform"]
440457
}]
@@ -451,8 +468,10 @@ resource "github_organization_ruleset" "test" {
451468
creation = true
452469
update = true
453470
}
471+
472+
depends_on = [github_organization_custom_properties.team]
454473
}
455-
`, rulesetName)
474+
`, rulesetName, propName)
456475

457476
resource.Test(t, resource.TestCase{
458477
PreCheck: func() { skipUnlessHasPaidOrgs(t) },
@@ -1057,45 +1076,25 @@ resource "github_organization_ruleset" "test" {
10571076
t.Run("validates_repository_property_works_as_single_targeting_option", func(t *testing.T) {
10581077
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
10591078
rulesetName := fmt.Sprintf("%s-repo-prop-only-%s", testResourcePrefix, randomID)
1060-
1061-
// Setup: Create custom property for testing
1062-
meta, err := getTestMeta()
1063-
if err != nil {
1064-
t.Fatalf("Error getting test meta: %v", err)
1065-
}
1066-
1067-
ctx := context.Background()
1068-
org := testAccConf.owner
1069-
propName := "e2e_test_environment"
1070-
propValues := []string{"production", "staging"}
1071-
1072-
_, _, err = meta.v3client.Organizations.CreateOrUpdateCustomProperty(ctx, org, propName, &github.CustomProperty{
1073-
ValueType: github.PropertyValueTypeSingleSelect,
1074-
Required: github.Ptr(false),
1075-
AllowedValues: propValues,
1076-
})
1077-
if err != nil {
1078-
t.Logf("Warning: Could not create custom property %s (may already exist): %v", propName, err)
1079-
}
1080-
1081-
// Cleanup: Remove custom property after test
1082-
defer func() {
1083-
_, err := meta.v3client.Organizations.RemoveCustomProperty(ctx, org, propName)
1084-
if err != nil {
1085-
t.Logf("Warning: Could not remove custom property %s: %v", propName, err)
1086-
}
1087-
}()
1079+
propName := fmt.Sprintf("e2e_test_environment_%s", randomID)
10881080

10891081
config := fmt.Sprintf(`
1082+
resource "github_organization_custom_properties" "environment" {
1083+
property_name = "%[2]s"
1084+
value_type = "single_select"
1085+
required = false
1086+
allowed_values = ["production", "staging"]
1087+
}
1088+
10901089
resource "github_organization_ruleset" "test" {
1091-
name = "%s"
1090+
name = "%[1]s"
10921091
target = "branch"
10931092
enforcement = "active"
10941093
10951094
conditions {
10961095
repository_property {
10971096
include = [{
1098-
name = "e2e_test_environment"
1097+
name = "%[2]s"
10991098
source = "custom"
11001099
property_values = ["production", "staging"]
11011100
}]
@@ -1112,8 +1111,10 @@ resource "github_organization_ruleset" "test" {
11121111
creation = true
11131112
update = true
11141113
}
1114+
1115+
depends_on = [github_organization_custom_properties.environment]
11151116
}
1116-
`, rulesetName)
1117+
`, rulesetName, propName)
11171118

11181119
resource.Test(t, resource.TestCase{
11191120
PreCheck: func() { skipUnlessHasPaidOrgs(t) },
@@ -1126,7 +1127,7 @@ resource "github_organization_ruleset" "test" {
11261127
resource.TestCheckResourceAttr("github_organization_ruleset.test", "target", "branch"),
11271128
resource.TestCheckResourceAttr("github_organization_ruleset.test", "enforcement", "active"),
11281129
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.#", "1"),
1129-
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.0.name", "e2e_test_environment"),
1130+
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.0.name", propName),
11301131
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.0.source", "custom"),
11311132
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.0.property_values.#", "2"),
11321133
resource.TestCheckResourceAttr("github_organization_ruleset.test", "conditions.0.repository_property.0.include.0.property_values.0", "production"),

0 commit comments

Comments
 (0)