Skip to content

Commit 938707a

Browse files
committed
add test and fix for more branch and tags combo
1 parent 0b5c1f3 commit 938707a

File tree

2 files changed

+155
-7
lines changed

2 files changed

+155
-7
lines changed

internal/provider/resource_tfe_registry_module.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ func resourceTFERegistryModule() *schema.Resource {
132132
"tests_enabled": {
133133
Type: schema.TypeBool,
134134
Optional: true,
135-
Computed: true,
136135
},
137136
},
138137
},
@@ -176,8 +175,13 @@ func resourceTFERegistryModuleCreateWithVCS(v interface{}, meta interface{}, d *
176175
branch, branchOk := vcsRepo["branch"].(string)
177176
initialVersion, initialVersionOk := d.GetOk("initial_version")
178177

179-
if tagsOk && tags && branchOk && branch != "" {
180-
return nil, fmt.Errorf("tags must be set to false when a branch is provided")
178+
err = validateVcsRepo(tagsOk, tags, branchOk, branch)
179+
if err != nil {
180+
return nil, err
181+
}
182+
183+
if tagsOk {
184+
options.VCSRepo.Tags = tfe.Bool(tags)
181185
}
182186

183187
if branchOk && branch != "" {
@@ -315,8 +319,9 @@ func resourceTFERegistryModuleUpdate(d *schema.ResourceData, meta interface{}) e
315319
tags, tagsOk := vcsRepo["tags"].(bool)
316320
branch, branchOk := vcsRepo["branch"].(string)
317321

318-
if tagsOk && tags && branchOk && branch != "" {
319-
return fmt.Errorf("tags must be set to false when a branch is provided")
322+
err = validateVcsRepo(tagsOk, tags, branchOk, branch)
323+
if err != nil {
324+
return err
320325
}
321326

322327
if tagsOk {
@@ -416,10 +421,10 @@ func resourceTFERegistryModuleRead(d *schema.ResourceData, meta interface{}) err
416421
}
417422

418423
testConfig = append(testConfig, testConfigValues)
419-
420-
d.Set("test_config", testConfig)
421424
}
422425

426+
d.Set("test_config", testConfig)
427+
423428
return nil
424429
}
425430

@@ -484,3 +489,14 @@ func resourceTFERegistryModuleImporter(ctx context.Context, d *schema.ResourceDa
484489
d.Id(),
485490
)
486491
}
492+
493+
func validateVcsRepo(tagsOk bool, tags bool, branchOk bool, branch string) error {
494+
// tags must be set to true or branch provided but not both
495+
if tagsOk && tags && branchOk && branch != "" {
496+
return fmt.Errorf("tags must be set to false when a branch is provided")
497+
} else if tagsOk && !tags && branchOk && branch == "" {
498+
return fmt.Errorf("tags must be set to true when no branch is provided")
499+
}
500+
501+
return nil
502+
}

internal/provider/resource_tfe_registry_module_test.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,30 @@ func TestAccTFERegistryModule_publicRegistryModule(t *testing.T) {
300300
})
301301
}
302302

303+
func TestAccTFERegistryModule_vcsRepoWithTagField(t *testing.T) {
304+
skipUnlessBeta(t)
305+
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
306+
307+
resource.Test(t, resource.TestCase{
308+
PreCheck: func() {
309+
testAccPreCheck(t)
310+
testAccPreCheckTFERegistryModule(t)
311+
},
312+
Providers: testAccProviders,
313+
Steps: []resource.TestStep{
314+
{
315+
Config: testAccTFERegistryModule_vcsRepoWithFalseTagField(rInt),
316+
Check: resource.ComposeTestCheckFunc(
317+
resource.TestCheckResourceAttr("tfe_registry_module.foobar", "publishing_mechanism", "branch"),
318+
resource.TestCheckResourceAttr("tfe_registry_module.foobar", "test_config.0.tests_enabled", strconv.FormatBool(false)),
319+
resource.TestCheckResourceAttr("tfe_registry_module.foobar", "vcs_repo.0.tags", strconv.FormatBool(false)),
320+
resource.TestCheckResourceAttr("tfe_registry_module.foobar", "vcs_repo.0.branch", "main"),
321+
),
322+
},
323+
},
324+
})
325+
}
326+
303327
func TestAccTFERegistryModule_noCodeModule(t *testing.T) {
304328
skipIfEnterprise(t)
305329

@@ -633,6 +657,25 @@ func TestAccTFERegistryModule_branchAndTagsEnabledOnCreate(t *testing.T) {
633657
})
634658
}
635659

660+
func TestAccTFERegistryModule_branchAndTagsDisabledOnCreate(t *testing.T) {
661+
skipUnlessBeta(t)
662+
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
663+
664+
resource.Test(t, resource.TestCase{
665+
PreCheck: func() {
666+
testAccPreCheck(t)
667+
testAccPreCheckTFERegistryModule(t)
668+
},
669+
Providers: testAccProviders,
670+
Steps: []resource.TestStep{
671+
{
672+
Config: testAccTFERegistryModule_vcsWithBranchAndTagsDisabled(rInt),
673+
ExpectError: regexp.MustCompile(`tags must be set to true when no branch is provided`),
674+
},
675+
},
676+
})
677+
}
678+
636679
func TestAccTFERegistryModule_branchAndTagsEnabledOnUpdate(t *testing.T) {
637680
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
638681

@@ -660,6 +703,34 @@ func TestAccTFERegistryModule_branchAndTagsEnabledOnUpdate(t *testing.T) {
660703
})
661704
}
662705

706+
func TestAccTFERegistryModule_branchAndTagsDisabledOnUpdate(t *testing.T) {
707+
skipUnlessBeta(t)
708+
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
709+
710+
resource.Test(t, resource.TestCase{
711+
PreCheck: func() {
712+
testAccPreCheck(t)
713+
testAccPreCheckTFERegistryModule(t)
714+
},
715+
Providers: testAccProviders,
716+
Steps: []resource.TestStep{
717+
{
718+
Config: testAccTFERegistryModule_vcsTags(rInt),
719+
Check: resource.ComposeTestCheckFunc(
720+
resource.TestCheckResourceAttr("tfe_registry_module.foobar", "publishing_mechanism", "git_tag"),
721+
resource.TestCheckNoResourceAttr("tfe_registry_module.foobar", "test_config.0.tests_enabled"),
722+
resource.TestCheckResourceAttr("tfe_registry_module.foobar", "vcs_repo.0.tags", strconv.FormatBool(true)),
723+
resource.TestCheckResourceAttr("tfe_registry_module.foobar", "vcs_repo.0.branch", ""),
724+
),
725+
},
726+
{
727+
Config: testAccTFERegistryModule_vcsWithBranchAndTagsDisabled(rInt),
728+
ExpectError: regexp.MustCompile(`tags must be set to true when no branch is provided`),
729+
},
730+
},
731+
})
732+
}
733+
663734
func TestAccTFERegistryModuleImport_nonVCSPrivateRM(t *testing.T) {
664735
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
665736

@@ -1175,6 +1246,37 @@ resource "tfe_registry_module" "foobar" {
11751246
envGithubRegistryModuleIdentifer)
11761247
}
11771248

1249+
func testAccTFERegistryModule_vcsWithBranchAndTagsDisabled(rInt int) string {
1250+
return fmt.Sprintf(`
1251+
resource "tfe_organization" "foobar" {
1252+
name = "tst-terraform-%d"
1253+
1254+
}
1255+
1256+
resource "tfe_oauth_client" "foobar" {
1257+
organization = tfe_organization.foobar.name
1258+
api_url = "https://api.github.com"
1259+
http_url = "https://github.com"
1260+
oauth_token = "%s"
1261+
service_provider = "github"
1262+
}
1263+
1264+
resource "tfe_registry_module" "foobar" {
1265+
organization = tfe_organization.foobar.name
1266+
vcs_repo {
1267+
display_identifier = "%s"
1268+
identifier = "%s"
1269+
oauth_token_id = tfe_oauth_client.foobar.oauth_token_id
1270+
branch = ""
1271+
tags = false
1272+
}
1273+
}`,
1274+
rInt,
1275+
envGithubToken,
1276+
envGithubRegistryModuleIdentifer,
1277+
envGithubRegistryModuleIdentifer)
1278+
}
1279+
11781280
func testAccTFERegistryModule_vcsTags(rInt int) string {
11791281
return fmt.Sprintf(`
11801282
resource "tfe_organization" "foobar" {
@@ -1205,6 +1307,36 @@ resource "tfe_registry_module" "foobar" {
12051307
envGithubRegistryModuleIdentifer,
12061308
envGithubRegistryModuleIdentifer)
12071309
}
1310+
func testAccTFERegistryModule_vcsRepoWithFalseTagField(rInt int) string {
1311+
return fmt.Sprintf(`
1312+
resource "tfe_organization" "foobar" {
1313+
name = "tst-terraform-%d"
1314+
1315+
}
1316+
1317+
resource "tfe_oauth_client" "foobar" {
1318+
organization = tfe_organization.foobar.name
1319+
api_url = "https://api.github.com"
1320+
http_url = "https://github.com"
1321+
oauth_token = "%s"
1322+
service_provider = "github"
1323+
}
1324+
1325+
resource "tfe_registry_module" "foobar" {
1326+
organization = tfe_organization.foobar.name
1327+
vcs_repo {
1328+
display_identifier = "%s"
1329+
identifier = "%s"
1330+
oauth_token_id = tfe_oauth_client.foobar.oauth_token_id
1331+
branch = "main"
1332+
tags = false
1333+
}
1334+
}`,
1335+
rInt,
1336+
envGithubToken,
1337+
envGithubRegistryModuleIdentifer,
1338+
envGithubRegistryModuleIdentifer)
1339+
}
12081340

12091341
func testAccTFERegistryModule_GitHubApp(rInt int) string {
12101342
return fmt.Sprintf(`

0 commit comments

Comments
 (0)