Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/github: Randomize acceptance tests #14460

Merged
merged 4 commits into from
May 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions builtin/providers/github/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
54 changes: 38 additions & 16 deletions builtin/providers/github/resource_github_branch_protection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,29 @@ 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"
)

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"),
Expand All @@ -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"),
Expand All @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand All @@ -217,4 +238,5 @@ resource "github_branch_protection" "master" {
contexts = ["github/bar"]
}
}
`, testRepo)
`, repoName, repoName)
}
62 changes: 49 additions & 13 deletions builtin/providers/github/resource_github_issue_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,53 @@ 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"
)

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"),
Expand All @@ -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",
Expand Down Expand Up @@ -126,31 +148,45 @@ 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" {
repository = "${github_repository.test.name}"
name = "enhancement" // Important! This is a pre-created label
color = "FF00FF"
}
`)
`, repoName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

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"),
Expand All @@ -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",
Expand Down Expand Up @@ -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)
}
Loading