From b6edff80d8712b97a8806335b4aba7009bfcf50e Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Sat, 13 May 2017 08:32:00 +0200 Subject: [PATCH 1/4] provider/github: Randomize branch protection acc tests --- .../resource_github_branch_protection_test.go | 54 +++++++++++++------ 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/builtin/providers/github/resource_github_branch_protection_test.go b/builtin/providers/github/resource_github_branch_protection_test.go index 4a0e4c63c..89f4a7bff 100644 --- a/builtin/providers/github/resource_github_branch_protection_test.go +++ b/builtin/providers/github/resource_github_branch_protection_test.go @@ -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) +} From af47810e308dbc96ef02423e6817215279b13abc Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Sat, 13 May 2017 10:49:42 +0200 Subject: [PATCH 2/4] provider/github: Randomize issue label acc tests --- .../resource_github_issue_label_test.go | 62 +++++++++++++++---- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/builtin/providers/github/resource_github_issue_label_test.go b/builtin/providers/github/resource_github_issue_label_test.go index 66461302d..a73a209e1 100644 --- a/builtin/providers/github/resource_github_issue_label_test.go +++ b/builtin/providers/github/resource_github_issue_label_test.go @@ -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) +} From 23bb27e5f56af7bf05e9967dd32767e62ecc253a Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Sat, 13 May 2017 11:08:04 +0200 Subject: [PATCH 3/4] provider/github: Randomize team repository acc tests --- .../resource_github_team_repository_test.go | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/builtin/providers/github/resource_github_team_repository_test.go b/builtin/providers/github/resource_github_team_repository_test.go index 9f1007a3d..a3721c819 100644 --- a/builtin/providers/github/resource_github_team_repository_test.go +++ b/builtin/providers/github/resource_github_team_repository_test.go @@ -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) From 4041db698ccff8d6ea280338de7bcd6b13299398 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Sat, 13 May 2017 11:08:15 +0200 Subject: [PATCH 4/4] provider/github: Randomize repository collaborator acc tests --- builtin/providers/github/provider_test.go | 2 -- ...rce_github_repository_collaborator_test.go | 21 ++++++++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/builtin/providers/github/provider_test.go b/builtin/providers/github/provider_test.go index b227d5271..79924badf 100644 --- a/builtin/providers/github/provider_test.go +++ b/builtin/providers/github/provider_test.go @@ -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") diff --git a/builtin/providers/github/resource_github_repository_collaborator_test.go b/builtin/providers/github/resource_github_repository_collaborator_test.go index 065cadeab..c842bfe44 100644 --- a/builtin/providers/github/resource_github_repository_collaborator_test.go +++ b/builtin/providers/github/resource_github_repository_collaborator_test.go @@ -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) +}