mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 01:41:48 -06:00
Merge pull request #14460 from hashicorp/b-github-test-repo
provider/github: Randomize acceptance tests
This commit is contained in:
commit
65af35fb25
@ -8,8 +8,6 @@ import (
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
|
||||
const testRepo string = "test-repo"
|
||||
|
||||
var testUser string = os.Getenv("GITHUB_TEST_USER")
|
||||
var testCollaborator string = os.Getenv("GITHUB_TEST_COLLABORATOR")
|
||||
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-github/github"
|
||||
"github.com/hashicorp/terraform/helper/acctest"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
@ -14,18 +15,21 @@ import (
|
||||
func TestAccGithubBranchProtection_basic(t *testing.T) {
|
||||
var protection github.Protection
|
||||
|
||||
rString := acctest.RandString(5)
|
||||
repoName := fmt.Sprintf("tf-acc-test-branch-prot-%s", rString)
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccGithubBranchProtectionDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccGithubBranchProtectionConfig,
|
||||
Config: testAccGithubBranchProtectionConfig(repoName),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckGithubProtectedBranchExists("github_branch_protection.master", &protection),
|
||||
testAccCheckGithubProtectedBranchExists("github_branch_protection.master", repoName+":master", &protection),
|
||||
testAccCheckGithubBranchProtectionRequiredStatusChecks(&protection, true, true, []string{"github/foo"}),
|
||||
testAccCheckGithubBranchProtectionRestrictions(&protection, []string{testUser}, []string{}),
|
||||
resource.TestCheckResourceAttr("github_branch_protection.master", "repository", testRepo),
|
||||
resource.TestCheckResourceAttr("github_branch_protection.master", "repository", repoName),
|
||||
resource.TestCheckResourceAttr("github_branch_protection.master", "branch", "master"),
|
||||
resource.TestCheckResourceAttr("github_branch_protection.master", "required_status_checks.0.include_admins", "true"),
|
||||
resource.TestCheckResourceAttr("github_branch_protection.master", "required_status_checks.0.strict", "true"),
|
||||
@ -38,12 +42,12 @@ func TestAccGithubBranchProtection_basic(t *testing.T) {
|
||||
),
|
||||
},
|
||||
{
|
||||
Config: testAccGithubBranchProtectionUpdateConfig,
|
||||
Config: testAccGithubBranchProtectionUpdateConfig(repoName),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckGithubProtectedBranchExists("github_branch_protection.master", &protection),
|
||||
testAccCheckGithubProtectedBranchExists("github_branch_protection.master", repoName+":master", &protection),
|
||||
testAccCheckGithubBranchProtectionRequiredStatusChecks(&protection, false, false, []string{"github/bar"}),
|
||||
testAccCheckGithubBranchProtectionNoRestrictionsExist(&protection),
|
||||
resource.TestCheckResourceAttr("github_branch_protection.master", "repository", testRepo),
|
||||
resource.TestCheckResourceAttr("github_branch_protection.master", "repository", repoName),
|
||||
resource.TestCheckResourceAttr("github_branch_protection.master", "branch", "master"),
|
||||
resource.TestCheckResourceAttr("github_branch_protection.master", "required_status_checks.0.include_admins", "false"),
|
||||
resource.TestCheckResourceAttr("github_branch_protection.master", "required_status_checks.0.strict", "false"),
|
||||
@ -58,13 +62,15 @@ func TestAccGithubBranchProtection_basic(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAccGithubBranchProtection_importBasic(t *testing.T) {
|
||||
rString := acctest.RandString(5)
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccGithubBranchProtectionDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccGithubBranchProtectionConfig,
|
||||
Config: testAccGithubBranchProtectionConfig(rString),
|
||||
},
|
||||
{
|
||||
ResourceName: "github_branch_protection.master",
|
||||
@ -75,15 +81,15 @@ func TestAccGithubBranchProtection_importBasic(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func testAccCheckGithubProtectedBranchExists(n string, protection *github.Protection) resource.TestCheckFunc {
|
||||
func testAccCheckGithubProtectedBranchExists(n, id string, protection *github.Protection) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
rs, ok := s.RootModule().Resources[n]
|
||||
if !ok {
|
||||
return fmt.Errorf("Not Found: %s", n)
|
||||
}
|
||||
|
||||
if rs.Primary.ID != "test-repo:master" {
|
||||
return fmt.Errorf("Expected ID to be %v, got %v", "test-repo:master", rs.Primary.ID)
|
||||
if rs.Primary.ID != id {
|
||||
return fmt.Errorf("Expected ID to be %v, got %v", id, rs.Primary.ID)
|
||||
}
|
||||
|
||||
conn := testAccProvider.Meta().(*Organization).client
|
||||
@ -185,9 +191,16 @@ func testAccGithubBranchProtectionDestroy(s *terraform.State) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var testAccGithubBranchProtectionConfig string = fmt.Sprintf(`
|
||||
func testAccGithubBranchProtectionConfig(repoName string) string {
|
||||
return fmt.Sprintf(`
|
||||
resource "github_repository" "test" {
|
||||
name = "%s"
|
||||
description = "Terraform Acceptance Test %s"
|
||||
auto_init = true
|
||||
}
|
||||
|
||||
resource "github_branch_protection" "master" {
|
||||
repository = "%s"
|
||||
repository = "${github_repository.test.name}"
|
||||
branch = "master"
|
||||
|
||||
required_status_checks = {
|
||||
@ -204,11 +217,19 @@ resource "github_branch_protection" "master" {
|
||||
users = ["%s"]
|
||||
}
|
||||
}
|
||||
`, testRepo, testUser)
|
||||
`, repoName, repoName, testUser)
|
||||
}
|
||||
|
||||
func testAccGithubBranchProtectionUpdateConfig(repoName string) string {
|
||||
return fmt.Sprintf(`
|
||||
resource "github_repository" "test" {
|
||||
name = "%s"
|
||||
description = "Terraform Acceptance Test %s"
|
||||
auto_init = true
|
||||
}
|
||||
|
||||
var testAccGithubBranchProtectionUpdateConfig string = fmt.Sprintf(`
|
||||
resource "github_branch_protection" "master" {
|
||||
repository = "%s"
|
||||
repository = "${github_repository.test.name}"
|
||||
branch = "master"
|
||||
|
||||
required_status_checks = {
|
||||
@ -217,4 +238,5 @@ resource "github_branch_protection" "master" {
|
||||
contexts = ["github/bar"]
|
||||
}
|
||||
}
|
||||
`, testRepo)
|
||||
`, repoName, repoName)
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-github/github"
|
||||
"github.com/hashicorp/terraform/helper/acctest"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
@ -13,27 +14,45 @@ import (
|
||||
func TestAccGithubIssueLabel_basic(t *testing.T) {
|
||||
var label github.Label
|
||||
|
||||
rString := acctest.RandString(5)
|
||||
repoName := fmt.Sprintf("tf-acc-test-branch-issue-label-%s", rString)
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccGithubIssueLabelDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccGithubIssueLabelConfig,
|
||||
Config: testAccGithubIssueLabelConfig(repoName),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckGithubIssueLabelExists("github_issue_label.test", &label),
|
||||
testAccCheckGithubIssueLabelAttributes(&label, "foo", "000000"),
|
||||
),
|
||||
},
|
||||
{
|
||||
Config: testAccGithubIssueLabelUpdateConfig,
|
||||
Config: testAccGithubIssueLabelUpdateConfig(repoName),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckGithubIssueLabelExists("github_issue_label.test", &label),
|
||||
testAccCheckGithubIssueLabelAttributes(&label, "bar", "FFFFFF"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccGithubIssueLabel_existingLabel(t *testing.T) {
|
||||
var label github.Label
|
||||
|
||||
rString := acctest.RandString(5)
|
||||
repoName := fmt.Sprintf("tf-acc-test-branch-issue-label-%s", rString)
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccGithubIssueLabelDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccGitHubIssueLabelExistsConfig,
|
||||
Config: testAccGitHubIssueLabelExistsConfig(repoName),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckGithubIssueLabelExists("github_issue_label.test", &label),
|
||||
testAccCheckGithubIssueLabelAttributes(&label, "enhancement", "FF00FF"),
|
||||
@ -44,13 +63,16 @@ func TestAccGithubIssueLabel_basic(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAccGithubIssueLabel_importBasic(t *testing.T) {
|
||||
rString := acctest.RandString(5)
|
||||
repoName := fmt.Sprintf("tf-acc-test-branch-issue-label-%s", rString)
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccGithubIssueLabelDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccGithubIssueLabelConfig,
|
||||
Config: testAccGithubIssueLabelConfig(repoName),
|
||||
},
|
||||
{
|
||||
ResourceName: "github_issue_label.test",
|
||||
@ -126,26 +148,39 @@ func testAccGithubIssueLabelDestroy(s *terraform.State) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var testAccGithubIssueLabelConfig string = fmt.Sprintf(`
|
||||
func testAccGithubIssueLabelConfig(repoName string) string {
|
||||
return fmt.Sprintf(`
|
||||
resource "github_repository" "test" {
|
||||
name = "%s"
|
||||
}
|
||||
|
||||
resource "github_issue_label" "test" {
|
||||
repository = "%s"
|
||||
repository = "${github_repository.test.name}"
|
||||
name = "foo"
|
||||
color = "000000"
|
||||
}
|
||||
`, testRepo)
|
||||
`, repoName)
|
||||
}
|
||||
|
||||
func testAccGithubIssueLabelUpdateConfig(repoName string) string {
|
||||
return fmt.Sprintf(`
|
||||
resource "github_repository" "test" {
|
||||
name = "%s"
|
||||
}
|
||||
|
||||
var testAccGithubIssueLabelUpdateConfig string = fmt.Sprintf(`
|
||||
resource "github_issue_label" "test" {
|
||||
repository = "%s"
|
||||
repository = "${github_repository.test.name}"
|
||||
name = "bar"
|
||||
color = "FFFFFF"
|
||||
}
|
||||
`, testRepo)
|
||||
`, repoName)
|
||||
}
|
||||
|
||||
var testAccGitHubIssueLabelExistsConfig string = fmt.Sprintf(`
|
||||
func testAccGitHubIssueLabelExistsConfig(repoName string) string {
|
||||
return fmt.Sprintf(`
|
||||
// Create a repository which has the default labels
|
||||
resource "github_repository" "test" {
|
||||
name = "tf-acc-repo-label-abc1234"
|
||||
name = "%s"
|
||||
}
|
||||
|
||||
resource "github_issue_label" "test" {
|
||||
@ -153,4 +188,5 @@ resource "github_issue_label" "test" {
|
||||
name = "enhancement" // Important! This is a pre-created label
|
||||
color = "FF00FF"
|
||||
}
|
||||
`)
|
||||
`, repoName)
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/acctest"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
)
|
||||
@ -12,13 +13,15 @@ import (
|
||||
const expectedPermission string = "admin"
|
||||
|
||||
func TestAccGithubRepositoryCollaborator_basic(t *testing.T) {
|
||||
repoName := fmt.Sprintf("tf-acc-test-collab-%s", acctest.RandString(5))
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckGithubRepositoryCollaboratorDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccGithubRepositoryCollaboratorConfig,
|
||||
Config: testAccGithubRepositoryCollaboratorConfig(repoName),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckGithubRepositoryCollaboratorExists("github_repository_collaborator.test_repo_collaborator"),
|
||||
testAccCheckGithubRepositoryCollaboratorPermission("github_repository_collaborator.test_repo_collaborator"),
|
||||
@ -29,13 +32,15 @@ func TestAccGithubRepositoryCollaborator_basic(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAccGithubRepositoryCollaborator_importBasic(t *testing.T) {
|
||||
repoName := fmt.Sprintf("tf-acc-test-collab-%s", acctest.RandString(5))
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckGithubRepositoryCollaboratorDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccGithubRepositoryCollaboratorConfig,
|
||||
Config: testAccGithubRepositoryCollaboratorConfig(repoName),
|
||||
},
|
||||
{
|
||||
ResourceName: "github_repository_collaborator.test_repo_collaborator",
|
||||
@ -148,10 +153,16 @@ func testAccCheckGithubRepositoryCollaboratorPermission(n string) resource.TestC
|
||||
}
|
||||
}
|
||||
|
||||
var testAccGithubRepositoryCollaboratorConfig string = fmt.Sprintf(`
|
||||
func testAccGithubRepositoryCollaboratorConfig(repoName string) string {
|
||||
return fmt.Sprintf(`
|
||||
resource "github_repository" "test" {
|
||||
name = "%s"
|
||||
}
|
||||
|
||||
resource "github_repository_collaborator" "test_repo_collaborator" {
|
||||
repository = "%s"
|
||||
repository = "${github_repository.test.name}"
|
||||
username = "%s"
|
||||
permission = "%s"
|
||||
}
|
||||
`, testRepo, testCollaborator, expectedPermission)
|
||||
`, repoName, testCollaborator, expectedPermission)
|
||||
}
|
||||
|
@ -13,7 +13,9 @@ import (
|
||||
|
||||
func TestAccGithubTeamRepository_basic(t *testing.T) {
|
||||
var repository github.Repository
|
||||
|
||||
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
|
||||
repoName := fmt.Sprintf("tf-acc-test-team-%s", acctest.RandString(5))
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
@ -21,14 +23,14 @@ func TestAccGithubTeamRepository_basic(t *testing.T) {
|
||||
CheckDestroy: testAccCheckGithubTeamRepositoryDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccGithubTeamRepositoryConfig(randString, testRepo),
|
||||
Config: testAccGithubTeamRepositoryConfig(randString, repoName),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckGithubTeamRepositoryExists("github_team_repository.test_team_test_repo", &repository),
|
||||
testAccCheckGithubTeamRepositoryRoleState("pull", &repository),
|
||||
),
|
||||
},
|
||||
{
|
||||
Config: testAccGithubTeamRepositoryUpdateConfig(randString, testRepo),
|
||||
Config: testAccGithubTeamRepositoryUpdateConfig(randString, repoName),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckGithubTeamRepositoryExists("github_team_repository.test_team_test_repo", &repository),
|
||||
testAccCheckGithubTeamRepositoryRoleState("push", &repository),
|
||||
@ -40,6 +42,7 @@ func TestAccGithubTeamRepository_basic(t *testing.T) {
|
||||
|
||||
func TestAccGithubTeamRepository_importBasic(t *testing.T) {
|
||||
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
|
||||
repoName := fmt.Sprintf("tf-acc-test-team-%s", acctest.RandString(5))
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
@ -47,7 +50,7 @@ func TestAccGithubTeamRepository_importBasic(t *testing.T) {
|
||||
CheckDestroy: testAccCheckGithubTeamRepositoryDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccGithubTeamRepositoryConfig(randString, testRepo),
|
||||
Config: testAccGithubTeamRepositoryConfig(randString, repoName),
|
||||
},
|
||||
{
|
||||
ResourceName: "github_team_repository.test_team_test_repo",
|
||||
@ -159,9 +162,13 @@ resource "github_team" "test_team" {
|
||||
description = "Terraform acc test group"
|
||||
}
|
||||
|
||||
resource "github_repository" "test" {
|
||||
name = "%s"
|
||||
}
|
||||
|
||||
resource "github_team_repository" "test_team_test_repo" {
|
||||
team_id = "${github_team.test_team.id}"
|
||||
repository = "%s"
|
||||
repository = "${github_repository.test.name}"
|
||||
permission = "pull"
|
||||
}
|
||||
`, randString, repoName)
|
||||
@ -174,9 +181,13 @@ resource "github_team" "test_team" {
|
||||
description = "Terraform acc test group"
|
||||
}
|
||||
|
||||
resource "github_repository" "test" {
|
||||
name = "%s"
|
||||
}
|
||||
|
||||
resource "github_team_repository" "test_team_test_repo" {
|
||||
team_id = "${github_team.test_team.id}"
|
||||
repository = "%s"
|
||||
repository = "${github_repository.test.name}"
|
||||
permission = "push"
|
||||
}
|
||||
`, randString, repoName)
|
||||
|
Loading…
Reference in New Issue
Block a user