Skip to content

Commit 61a5d02

Browse files
maint: Adds tests for PR-2502 (#2799)
* fix: Fixed branch protection v3 status checks churn Signed-off-by: Steve Hipwell <[email protected]> * Adds tests on branch protection to validate no churn --------- Signed-off-by: Steve Hipwell <[email protected]> Co-authored-by: Steve Hipwell <[email protected]>
1 parent d38b99c commit 61a5d02

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

github/resource_github_branch_protection_v3_test.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,3 +544,153 @@ func TestAccGithubBranchProtectionV3_branch_push_restrictions(t *testing.T) {
544544
})
545545

546546
}
547+
548+
func TestAccGithubBranchProtectionV3_computed_status_checks_no_churn(t *testing.T) {
549+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
550+
551+
t.Run("handles computed status checks without churn", func(t *testing.T) {
552+
config := fmt.Sprintf(`
553+
resource "github_repository" "test" {
554+
name = "tf-acc-test-%s"
555+
auto_init = true
556+
}
557+
558+
resource "github_branch_protection_v3" "test" {
559+
repository = github_repository.test.name
560+
branch = "main"
561+
562+
required_status_checks {
563+
strict = true
564+
checks = [
565+
"ci/test",
566+
"ci/build"
567+
]
568+
}
569+
}
570+
`, randomID)
571+
572+
check := resource.ComposeAggregateTestCheckFunc(
573+
resource.TestCheckResourceAttr(
574+
"github_branch_protection_v3.test", "required_status_checks.#", "1",
575+
),
576+
resource.TestCheckResourceAttr(
577+
"github_branch_protection_v3.test", "required_status_checks.0.strict", "true",
578+
),
579+
resource.TestCheckResourceAttr(
580+
"github_branch_protection_v3.test", "required_status_checks.0.checks.#", "2",
581+
),
582+
resource.TestCheckTypeSetElemAttr(
583+
"github_branch_protection_v3.test", "required_status_checks.0.checks.*", "ci/test",
584+
),
585+
resource.TestCheckTypeSetElemAttr(
586+
"github_branch_protection_v3.test", "required_status_checks.0.checks.*", "ci/build",
587+
),
588+
)
589+
590+
testCase := func(t *testing.T, mode string) {
591+
resource.Test(t, resource.TestCase{
592+
PreCheck: func() { skipUnlessMode(t, mode) },
593+
Providers: testAccProviders,
594+
Steps: []resource.TestStep{
595+
{
596+
Config: config,
597+
Check: check,
598+
},
599+
// Re-apply the same config to test for churn
600+
{
601+
Config: config,
602+
Check: check,
603+
PlanOnly: true,
604+
},
605+
},
606+
})
607+
}
608+
609+
t.Run("with an anonymous account", func(t *testing.T) {
610+
t.Skip("anonymous account not supported for this operation")
611+
})
612+
613+
t.Run("with an individual account", func(t *testing.T) {
614+
t.Skip("individual account not supported for this operation")
615+
})
616+
617+
t.Run("with an organization account", func(t *testing.T) {
618+
testCase(t, organization)
619+
})
620+
})
621+
}
622+
623+
func TestAccGithubBranchProtectionV3_computed_status_contexts_no_churn(t *testing.T) {
624+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
625+
626+
t.Run("handles computed status contexts without churn", func(t *testing.T) {
627+
config := fmt.Sprintf(`
628+
resource "github_repository" "test" {
629+
name = "tf-acc-test-%s"
630+
auto_init = true
631+
}
632+
633+
resource "github_branch_protection_v3" "test" {
634+
repository = github_repository.test.name
635+
branch = "main"
636+
637+
required_status_checks {
638+
strict = true
639+
contexts = [
640+
"ci/test",
641+
"ci/build"
642+
]
643+
}
644+
}
645+
`, randomID)
646+
647+
check := resource.ComposeAggregateTestCheckFunc(
648+
resource.TestCheckResourceAttr(
649+
"github_branch_protection_v3.test", "required_status_checks.#", "1",
650+
),
651+
resource.TestCheckResourceAttr(
652+
"github_branch_protection_v3.test", "required_status_checks.0.strict", "true",
653+
),
654+
resource.TestCheckResourceAttr(
655+
"github_branch_protection_v3.test", "required_status_checks.0.contexts.#", "2",
656+
),
657+
resource.TestCheckTypeSetElemAttr(
658+
"github_branch_protection_v3.test", "required_status_checks.0.contexts.*", "ci/test",
659+
),
660+
resource.TestCheckTypeSetElemAttr(
661+
"github_branch_protection_v3.test", "required_status_checks.0.contexts.*", "ci/build",
662+
),
663+
)
664+
665+
testCase := func(t *testing.T, mode string) {
666+
resource.Test(t, resource.TestCase{
667+
PreCheck: func() { skipUnlessMode(t, mode) },
668+
Providers: testAccProviders,
669+
Steps: []resource.TestStep{
670+
{
671+
Config: config,
672+
Check: check,
673+
},
674+
// Re-apply the same config to test for churn
675+
{
676+
Config: config,
677+
Check: check,
678+
PlanOnly: true,
679+
},
680+
},
681+
})
682+
}
683+
684+
t.Run("with an anonymous account", func(t *testing.T) {
685+
t.Skip("anonymous account not supported for this operation")
686+
})
687+
688+
t.Run("with an individual account", func(t *testing.T) {
689+
t.Skip("individual account not supported for this operation")
690+
})
691+
692+
t.Run("with an organization account", func(t *testing.T) {
693+
testCase(t, organization)
694+
})
695+
})
696+
}

0 commit comments

Comments
 (0)