Skip to content

Commit 5a83a3d

Browse files
authored
[MAINT] Add enterprise EMU testing config (integrations#3179)
* Add test config for EMU enterprise and helper functions to skip tests Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Introduce `testAcc` setting for repo visibility This enables GHEC EMU to run tests without having to manually skip everything public Signed-off-by: Timo Sand <timo.sand@f-secure.com> --------- Signed-off-by: Timo Sand <timo.sand@f-secure.com>
1 parent b5198aa commit 5a83a3d

File tree

5 files changed

+142
-173
lines changed

5 files changed

+142
-173
lines changed

CONTRIBUTING.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ This section describes a typical sequence performed when developing locally. Ful
3333
Once you have the repository cloned, there's a couple of additional steps you'll need to take. Since most of the testing is acceptance or integration testing, we need to manipulate real GitHub resources in order to run it. Useful setup steps are listed below:
3434

3535
- If you haven't already, [create a GitHub organization you can use for testing](#github-organization).
36-
- Optional: you may find it beneficial to create a test user as well in order to avoid potential rate-limiting issues on your main account.
37-
- Your organization _must_ have a repository called `terraform-template-module`. The [terraformtesting/terraform-template-module](https://github.com/terraformtesting/terraform-template-module) repo is a good, re-usable example.
38-
- You _must_ make sure that the "Template Repository" item in Settings is checked for this repo.
36+
- Optional: you may find it beneficial to create a test user as well in order to avoid potential rate-limiting issues on your main account.
37+
- Your organization _must_ have a repository called `terraform-template-module`. The [terraformtesting/terraform-template-module](https://github.com/terraformtesting/terraform-template-module) repo is a good, re-usable example.
38+
- You _must_ make sure that the "Template Repository" item in Settings is checked for this repo.
3939
- If you haven't already, generate a Personal Access Token (PAT) for authenticating your test runs.
4040
- Export the necessary configuration for authenticating your provider with GitHub
4141

@@ -52,7 +52,7 @@ Once you have the repository cloned, there's a couple of additional steps you'll
5252
### Local Development Iteration
5353

5454
1. Write a test describing what you will fix. See [`github_label`](./github/resource_github_issue_label_test.go) for an example format.
55-
1. Run your test and observe it fail. Enabling debug output allows for observing the underlying requests and responses made as well as viewing state (search `STATE:`) generated during the acceptance test run.
55+
2. Run your test and observe it fail. Enabling debug output allows for observing the underlying requests and responses made as well as viewing state (search `STATE:`) generated during the acceptance test run.
5656

5757
```sh
5858
TF_LOG=DEBUG TF_ACC=1 go test -v ./... -run ^TestAccGithubIssueLabel
@@ -182,6 +182,9 @@ export GH_TEST_ENTERPRISE_EMU_GROUP_ID=
182182

183183
# Configure test options
184184
export GH_TEST_ADVANCED_SECURITY=
185+
186+
# Configure if the enterprise is an EMU enterprise
187+
export GH_TEST_ENTERPRISE_IS_EMU=
185188
```
186189

187190
There are also a small amount of unit tests in the provider. Due to the nature of the provider, such tests are currently only recommended for exercising functionality completely internal to the provider. These may be executed by running `make test`.

github/acc_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ type testAccConfig struct {
4343
token string
4444

4545
// Enterprise configuration
46-
enterpriseSlug string
46+
enterpriseSlug string
47+
enterpriseIsEMU bool
4748

4849
// Global test configuration
4950
testPublicRepository string
@@ -76,6 +77,9 @@ type testAccConfig struct {
7677

7778
// Test options
7879
testAdvancedSecurity bool
80+
81+
// Test repository configuration
82+
testRepositoryVisibility string
7983
}
8084

8185
var testAccConf *testAccConfig
@@ -130,6 +134,8 @@ func TestMain(m *testing.M) {
130134
testExternalUserToken: os.Getenv("GH_TEST_EXTERNAL_USER_TOKEN"),
131135
testExternalUser2: os.Getenv("GH_TEST_EXTERNAL_USER2"),
132136
testAdvancedSecurity: os.Getenv("GH_TEST_ADVANCED_SECURITY") == "true",
137+
testRepositoryVisibility: "public",
138+
enterpriseIsEMU: authMode == enterprise && os.Getenv("GH_TEST_ENTERPRISE_IS_EMU") == "true",
133139
}
134140

135141
if config.authMode != anonymous {
@@ -165,6 +171,10 @@ func TestMain(m *testing.M) {
165171
if err == nil {
166172
config.testEnterpriseEMUGroupId = i
167173
}
174+
175+
if config.enterpriseIsEMU {
176+
config.testRepositoryVisibility = "private"
177+
}
168178
}
169179

170180
i, err := strconv.Atoi(os.Getenv("GH_TEST_ORG_APP_INSTALLATION_ID"))
@@ -318,6 +328,18 @@ func skipUnlessHasAppInstallations(t *testing.T) {
318328
}
319329
}
320330

331+
func skipUnlessEMUEnterprise(t *testing.T) {
332+
if !testAccConf.enterpriseIsEMU {
333+
t.Skip("Skipping as test mode is not EMU enterprise")
334+
}
335+
}
336+
337+
func skipIfEMUEnterprise(t *testing.T) {
338+
if testAccConf.enterpriseIsEMU {
339+
t.Skip("Skipping as this test is not supported for EMU enterprise")
340+
}
341+
}
342+
321343
func skipUnlessMode(t *testing.T, testModes ...testMode) {
322344
if !slices.Contains(testModes, testAccConf.authMode) {
323345
t.Skip("Skipping as not supported test mode")

github/resource_github_emu_group_mapping_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestAccGithubEMUGroupMapping(t *testing.T) {
2121
teamName := fmt.Sprintf("%steam-emu-%s", testResourcePrefix, randomID)
2222

2323
resource.Test(t, resource.TestCase{
24-
PreCheck: func() { skipUnlessMode(t, enterprise) },
24+
PreCheck: func() { skipUnlessEMUEnterprise(t) },
2525
ProviderFactories: providerFactories,
2626
CheckDestroy: testAccCheckGithubEMUGroupMappingDestroy,
2727
Steps: []resource.TestStep{
@@ -43,7 +43,7 @@ func TestAccGithubEMUGroupMapping(t *testing.T) {
4343
rn := "github_emu_group_mapping.test"
4444

4545
resource.Test(t, resource.TestCase{
46-
PreCheck: func() { skipUnlessMode(t, enterprise) },
46+
PreCheck: func() { skipUnlessEMUEnterprise(t) },
4747
ProviderFactories: providerFactories,
4848
CheckDestroy: testAccCheckGithubEMUGroupMappingDestroy,
4949
Steps: []resource.TestStep{
@@ -84,7 +84,7 @@ func TestAccGithubEMUGroupMapping(t *testing.T) {
8484
`, teamName, teamName2, groupID)
8585

8686
resource.Test(t, resource.TestCase{
87-
PreCheck: func() { skipUnlessMode(t, enterprise) },
87+
PreCheck: func() { skipUnlessEMUEnterprise(t) },
8888
ProviderFactories: providerFactories,
8989
CheckDestroy: testAccCheckGithubEMUGroupMappingDestroy,
9090
Steps: []resource.TestStep{
@@ -107,7 +107,7 @@ func TestAccGithubEMUGroupMapping(t *testing.T) {
107107
teamName2 := fmt.Sprintf("%s-upd", teamName1)
108108

109109
resource.Test(t, resource.TestCase{
110-
PreCheck: func() { skipUnlessMode(t, enterprise) },
110+
PreCheck: func() { skipUnlessEMUEnterprise(t) },
111111
ProviderFactories: providerFactories,
112112
CheckDestroy: testAccCheckGithubEMUGroupMappingDestroy,
113113
Steps: []resource.TestStep{
@@ -149,7 +149,7 @@ resource "github_emu_group_mapping" "test" {
149149
`
150150

151151
resource.Test(t, resource.TestCase{
152-
PreCheck: func() { skipUnlessEnterprise(t) },
152+
PreCheck: func() { skipUnlessEMUEnterprise(t) },
153153
ProviderFactories: providerFactories,
154154
CheckDestroy: testAccCheckGithubEMUGroupMappingDestroy,
155155
Steps: []resource.TestStep{

github/resource_github_repository_ruleset_test.go

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ import (
1212
)
1313

1414
func TestAccGithubRepositoryRuleset(t *testing.T) {
15-
baseRepoVisibility := "public"
16-
17-
if testAccConf.authMode == enterprise {
18-
// This enables repos to be created even in GHEC EMU
19-
baseRepoVisibility = "private"
20-
}
21-
2215
t.Run("create_branch_ruleset", func(t *testing.T) {
2316
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
2417
repoName := fmt.Sprintf("%srepo-ruleset-%s", testResourcePrefix, randomID)
@@ -120,7 +113,7 @@ resource "github_repository_ruleset" "test" {
120113
non_fast_forward = true
121114
}
122115
}
123-
`, repoName, baseRepoVisibility)
116+
`, repoName, testAccConf.testRepositoryVisibility)
124117

125118
resource.Test(t, resource.TestCase{
126119
PreCheck: func() { skipUnauthenticated(t) },
@@ -189,7 +182,7 @@ resource "github_repository_ruleset" "test" {
189182
}
190183
}
191184
}
192-
`, repoName, baseRepoVisibility)
185+
`, repoName, testAccConf.testRepositoryVisibility)
193186

194187
resource.Test(t, resource.TestCase{
195188
PreCheck: func() { skipUnauthenticated(t) },
@@ -308,13 +301,13 @@ resource "github_repository_ruleset" "test" {
308301
ProviderFactories: providerFactories,
309302
Steps: []resource.TestStep{
310303
{
311-
Config: fmt.Sprintf(config, repoName, randomID, baseRepoVisibility, name),
304+
Config: fmt.Sprintf(config, repoName, randomID, testAccConf.testRepositoryVisibility, name),
312305
Check: resource.ComposeTestCheckFunc(
313306
resource.TestCheckResourceAttr("github_repository_ruleset.test", "name", name),
314307
),
315308
},
316309
{
317-
Config: fmt.Sprintf(config, repoName, randomID, baseRepoVisibility, nameUpdated),
310+
Config: fmt.Sprintf(config, repoName, randomID, testAccConf.testRepositoryVisibility, nameUpdated),
318311
Check: resource.ComposeTestCheckFunc(
319312
resource.TestCheckResourceAttr("github_repository_ruleset.test", "name", nameUpdated),
320313
),
@@ -367,9 +360,9 @@ resource "github_repository_ruleset" "test" {
367360
}
368361
}
369362
`
370-
config := fmt.Sprintf(baseConfig, repoName, baseRepoVisibility, bypassActorsConfig)
363+
config := fmt.Sprintf(baseConfig, repoName, testAccConf.testRepositoryVisibility, bypassActorsConfig)
371364

372-
configUpdated := fmt.Sprintf(baseConfig, repoName, baseRepoVisibility, "")
365+
configUpdated := fmt.Sprintf(baseConfig, repoName, testAccConf.testRepositoryVisibility, "")
373366
resource.Test(t, resource.TestCase{
374367
PreCheck: func() { skipUnauthenticated(t) },
375368
ProviderFactories: providerFactories,
@@ -435,13 +428,13 @@ resource "github_repository_ruleset" "test" {
435428
ProviderFactories: providerFactories,
436429
Steps: []resource.TestStep{
437430
{
438-
Config: fmt.Sprintf(config, repoName, randomID, baseRepoVisibility, bypassMode),
431+
Config: fmt.Sprintf(config, repoName, randomID, testAccConf.testRepositoryVisibility, bypassMode),
439432
Check: resource.ComposeTestCheckFunc(
440433
resource.TestCheckResourceAttr("github_repository_ruleset.test", "bypass_actors.0.bypass_mode", bypassMode),
441434
),
442435
},
443436
{
444-
Config: fmt.Sprintf(config, repoName, randomID, baseRepoVisibility, bypassModeUpdated),
437+
Config: fmt.Sprintf(config, repoName, randomID, testAccConf.testRepositoryVisibility, bypassModeUpdated),
445438
Check: resource.ComposeTestCheckFunc(
446439
resource.TestCheckResourceAttr("github_repository_ruleset.test", "bypass_actors.0.bypass_mode", bypassModeUpdated),
447440
),
@@ -486,7 +479,7 @@ resource "github_repository_ruleset" "test" {
486479
creation = true
487480
}
488481
}
489-
`, repoName, randomID, baseRepoVisibility)
482+
`, repoName, randomID, testAccConf.testRepositoryVisibility)
490483

491484
resource.Test(t, resource.TestCase{
492485
PreCheck: func() { skipUnauthenticated(t) },
@@ -508,13 +501,6 @@ resource "github_repository_ruleset" "test" {
508501
}
509502

510503
func TestAccGithubRepositoryRulesetArchived(t *testing.T) {
511-
baseRepoVisibility := "public"
512-
513-
if testAccConf.authMode == enterprise {
514-
// This enables repos to be created even in GHEC EMU
515-
baseRepoVisibility = "private"
516-
}
517-
518504
t.Run("skips update and delete on archived repository", func(t *testing.T) {
519505
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
520506
repoName := fmt.Sprintf("%srepo-ruleset-arch-%s", testResourcePrefix, randomID)
@@ -543,9 +529,9 @@ func TestAccGithubRepositoryRulesetArchived(t *testing.T) {
543529
PreCheck: func() { skipUnauthenticated(t) },
544530
ProviderFactories: providerFactories,
545531
Steps: []resource.TestStep{
546-
{Config: fmt.Sprintf(config, repoName, archivedBefore, baseRepoVisibility, enforcementBefore)},
547-
{Config: fmt.Sprintf(config, repoName, archivedAfter, baseRepoVisibility, enforcementBefore)},
548-
{Config: fmt.Sprintf(config, repoName, archivedAfter, baseRepoVisibility, enforcementAfter)},
532+
{Config: fmt.Sprintf(config, repoName, archivedBefore, testAccConf.testRepositoryVisibility, enforcementBefore)},
533+
{Config: fmt.Sprintf(config, repoName, archivedAfter, testAccConf.testRepositoryVisibility, enforcementBefore)},
534+
{Config: fmt.Sprintf(config, repoName, archivedAfter, testAccConf.testRepositoryVisibility, enforcementAfter)},
549535
},
550536
})
551537
})
@@ -577,10 +563,10 @@ resource "github_repository_ruleset" "test" {
577563
ProviderFactories: providerFactories,
578564
Steps: []resource.TestStep{
579565
{
580-
Config: fmt.Sprintf(repoConfig, repoName, false, baseRepoVisibility, ""),
566+
Config: fmt.Sprintf(repoConfig, repoName, false, testAccConf.testRepositoryVisibility, ""),
581567
},
582568
{
583-
Config: fmt.Sprintf(repoConfig, repoName, true, baseRepoVisibility, rulesetConfig),
569+
Config: fmt.Sprintf(repoConfig, repoName, true, testAccConf.testRepositoryVisibility, rulesetConfig),
584570
ExpectError: regexp.MustCompile("cannot create ruleset on archived repository"),
585571
},
586572
},
@@ -764,12 +750,6 @@ func TestAccGithubRepositoryRuleset_requiredReviewers(t *testing.T) {
764750
repoName := fmt.Sprintf("%srepo-ruleset-req-rev-%s", testResourcePrefix, randomID)
765751
teamName := fmt.Sprintf("%steam-req-rev-%s", testResourcePrefix, randomID)
766752
rulesetName := fmt.Sprintf("%s-ruleset-req-rev-%s", testResourcePrefix, randomID)
767-
baseRepoVisibility := "public"
768-
769-
if testAccConf.authMode == enterprise {
770-
// This enables repos to be created even in GHEC EMU
771-
baseRepoVisibility = "private"
772-
}
773753

774754
config := fmt.Sprintf(`
775755
resource "github_repository" "test" {
@@ -822,7 +802,7 @@ resource "github_repository_ruleset" "test" {
822802
823803
depends_on = [github_team_repository.test]
824804
}
825-
`, repoName, baseRepoVisibility, teamName, rulesetName)
805+
`, repoName, testAccConf.testRepositoryVisibility, teamName, rulesetName)
826806

827807
// Updated config: change minimum_approvals from 1 to 2
828808
configUpdated := fmt.Sprintf(`
@@ -876,7 +856,7 @@ resource "github_repository_ruleset" "test" {
876856
877857
depends_on = [github_team_repository.test]
878858
}
879-
`, repoName, baseRepoVisibility, teamName, rulesetName)
859+
`, repoName, testAccConf.testRepositoryVisibility, teamName, rulesetName)
880860

881861
resource.Test(t, resource.TestCase{
882862
PreCheck: func() { skipUnlessHasOrgs(t) },

0 commit comments

Comments
 (0)