2017-04-20 20:11:49 +02:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-github/github"
|
2017-05-13 08:32:00 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2017-04-20 20:11:49 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccGithubBranchProtection_basic(t *testing.T) {
|
|
|
|
var protection github.Protection
|
|
|
|
|
2017-05-13 08:32:00 +02:00
|
|
|
rString := acctest.RandString(5)
|
|
|
|
repoName := fmt.Sprintf("tf-acc-test-branch-prot-%s", rString)
|
|
|
|
|
2017-04-20 20:11:49 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccGithubBranchProtectionDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
2017-05-13 08:32:00 +02:00
|
|
|
Config: testAccGithubBranchProtectionConfig(repoName),
|
2017-04-20 20:11:49 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2017-05-13 08:32:00 +02:00
|
|
|
testAccCheckGithubProtectedBranchExists("github_branch_protection.master", repoName+":master", &protection),
|
2017-04-20 20:11:49 +02:00
|
|
|
testAccCheckGithubBranchProtectionRequiredStatusChecks(&protection, true, true, []string{"github/foo"}),
|
|
|
|
testAccCheckGithubBranchProtectionRestrictions(&protection, []string{testUser}, []string{}),
|
2017-05-13 08:32:00 +02:00
|
|
|
resource.TestCheckResourceAttr("github_branch_protection.master", "repository", repoName),
|
2017-04-20 20:11:49 +02:00
|
|
|
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"),
|
|
|
|
resource.TestCheckResourceAttr("github_branch_protection.master", "required_status_checks.0.contexts.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr("github_branch_protection.master", "required_status_checks.0.contexts.0", "github/foo"),
|
|
|
|
resource.TestCheckResourceAttr("github_branch_protection.master", "required_pull_request_reviews.0.include_admins", "true"),
|
|
|
|
resource.TestCheckResourceAttr("github_branch_protection.master", "restrictions.0.users.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr("github_branch_protection.master", "restrictions.0.users.0", testUser),
|
|
|
|
resource.TestCheckResourceAttr("github_branch_protection.master", "restrictions.0.teams.#", "0"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
2017-05-13 08:32:00 +02:00
|
|
|
Config: testAccGithubBranchProtectionUpdateConfig(repoName),
|
2017-04-20 20:11:49 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2017-05-13 08:32:00 +02:00
|
|
|
testAccCheckGithubProtectedBranchExists("github_branch_protection.master", repoName+":master", &protection),
|
2017-04-20 20:11:49 +02:00
|
|
|
testAccCheckGithubBranchProtectionRequiredStatusChecks(&protection, false, false, []string{"github/bar"}),
|
|
|
|
testAccCheckGithubBranchProtectionNoRestrictionsExist(&protection),
|
2017-05-13 08:32:00 +02:00
|
|
|
resource.TestCheckResourceAttr("github_branch_protection.master", "repository", repoName),
|
2017-04-20 20:11:49 +02:00
|
|
|
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"),
|
|
|
|
resource.TestCheckResourceAttr("github_branch_protection.master", "required_status_checks.0.contexts.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr("github_branch_protection.master", "required_status_checks.0.contexts.0", "github/bar"),
|
|
|
|
resource.TestCheckResourceAttr("github_branch_protection.master", "required_pull_request_reviews.#", "0"),
|
|
|
|
resource.TestCheckResourceAttr("github_branch_protection.master", "restrictions.#", "0"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccGithubBranchProtection_importBasic(t *testing.T) {
|
2017-05-13 08:32:00 +02:00
|
|
|
rString := acctest.RandString(5)
|
|
|
|
|
2017-04-20 20:11:49 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccGithubBranchProtectionDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
2017-05-13 08:32:00 +02:00
|
|
|
Config: testAccGithubBranchProtectionConfig(rString),
|
2017-04-20 20:11:49 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ResourceName: "github_branch_protection.master",
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-05-13 08:32:00 +02:00
|
|
|
func testAccCheckGithubProtectedBranchExists(n, id string, protection *github.Protection) resource.TestCheckFunc {
|
2017-04-20 20:11:49 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not Found: %s", n)
|
|
|
|
}
|
|
|
|
|
2017-05-13 08:32:00 +02:00
|
|
|
if rs.Primary.ID != id {
|
|
|
|
return fmt.Errorf("Expected ID to be %v, got %v", id, rs.Primary.ID)
|
2017-04-20 20:11:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*Organization).client
|
|
|
|
o := testAccProvider.Meta().(*Organization).name
|
|
|
|
r, b := parseTwoPartID(rs.Primary.ID)
|
|
|
|
|
|
|
|
githubProtection, _, err := conn.Repositories.GetBranchProtection(context.TODO(), o, r, b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*protection = *githubProtection
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckGithubBranchProtectionRequiredStatusChecks(protection *github.Protection, expectedIncludeAdmins bool, expectedStrict bool, expectedContexts []string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rsc := protection.RequiredStatusChecks
|
|
|
|
if rsc == nil {
|
|
|
|
return fmt.Errorf("Expected RequiredStatusChecks to be present, but was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if rsc.IncludeAdmins != expectedIncludeAdmins {
|
|
|
|
return fmt.Errorf("Expected RequiredStatusChecks.IncludeAdmins to be %v, got %v", expectedIncludeAdmins, rsc.IncludeAdmins)
|
|
|
|
}
|
|
|
|
if rsc.Strict != expectedStrict {
|
|
|
|
return fmt.Errorf("Expected RequiredStatusChecks.Strict to be %v, got %v", expectedStrict, rsc.Strict)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(rsc.Contexts, expectedContexts) {
|
|
|
|
return fmt.Errorf("Expected RequiredStatusChecks.Contexts to be %v, got %v", expectedContexts, rsc.Contexts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckGithubBranchProtectionRestrictions(protection *github.Protection, expectedUserLogins []string, expectedTeamNames []string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
restrictions := protection.Restrictions
|
|
|
|
if restrictions == nil {
|
|
|
|
return fmt.Errorf("Expected Restrictions to be present, but was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
userLogins := []string{}
|
|
|
|
for _, u := range restrictions.Users {
|
|
|
|
userLogins = append(userLogins, *u.Login)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(userLogins, expectedUserLogins) {
|
|
|
|
return fmt.Errorf("Expected Restrictions.Users to be %v, got %v", expectedUserLogins, userLogins)
|
|
|
|
}
|
|
|
|
|
|
|
|
teamLogins := []string{}
|
|
|
|
for _, t := range restrictions.Teams {
|
|
|
|
teamLogins = append(teamLogins, *t.Name)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(teamLogins, expectedTeamNames) {
|
|
|
|
return fmt.Errorf("Expected Restrictions.Teams to be %v, got %v", expectedTeamNames, teamLogins)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckGithubBranchProtectionNoRestrictionsExist(protection *github.Protection) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if protection.Restrictions != nil {
|
|
|
|
return fmt.Errorf("Expected Restrictions to be nil, but was %v", protection.Restrictions)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccGithubBranchProtectionDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*Organization).client
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "github_branch_protection" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
o := testAccProvider.Meta().(*Organization).name
|
|
|
|
r, b := parseTwoPartID(rs.Primary.ID)
|
|
|
|
protection, res, err := conn.Repositories.GetBranchProtection(context.TODO(), o, r, b)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if protection != nil {
|
|
|
|
return fmt.Errorf("Branch protection still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if res.StatusCode != 404 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-13 08:32:00 +02:00
|
|
|
func testAccGithubBranchProtectionConfig(repoName string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "github_repository" "test" {
|
|
|
|
name = "%s"
|
|
|
|
description = "Terraform Acceptance Test %s"
|
|
|
|
auto_init = true
|
|
|
|
}
|
|
|
|
|
2017-04-20 20:11:49 +02:00
|
|
|
resource "github_branch_protection" "master" {
|
2017-05-13 08:32:00 +02:00
|
|
|
repository = "${github_repository.test.name}"
|
2017-04-20 20:11:49 +02:00
|
|
|
branch = "master"
|
|
|
|
|
|
|
|
required_status_checks = {
|
|
|
|
include_admins = true
|
|
|
|
strict = true
|
|
|
|
contexts = ["github/foo"]
|
|
|
|
}
|
|
|
|
|
|
|
|
required_pull_request_reviews {
|
|
|
|
include_admins = true
|
|
|
|
}
|
|
|
|
|
|
|
|
restrictions {
|
|
|
|
users = ["%s"]
|
|
|
|
}
|
|
|
|
}
|
2017-05-13 08:32:00 +02:00
|
|
|
`, 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
|
|
|
|
}
|
2017-04-20 20:11:49 +02:00
|
|
|
|
|
|
|
resource "github_branch_protection" "master" {
|
2017-05-13 08:32:00 +02:00
|
|
|
repository = "${github_repository.test.name}"
|
2017-04-20 20:11:49 +02:00
|
|
|
branch = "master"
|
|
|
|
|
|
|
|
required_status_checks = {
|
|
|
|
include_admins = false
|
|
|
|
strict = false
|
|
|
|
contexts = ["github/bar"]
|
|
|
|
}
|
|
|
|
}
|
2017-05-13 08:32:00 +02:00
|
|
|
`, repoName, repoName)
|
|
|
|
}
|