github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/github/resource_github_branch_protection_test.go (about)

     1  package github
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/google/go-github/github"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccGithubBranchProtection_basic(t *testing.T) {
    16  	var protection github.Protection
    17  
    18  	rString := acctest.RandString(5)
    19  	repoName := fmt.Sprintf("tf-acc-test-branch-prot-%s", rString)
    20  
    21  	resource.Test(t, resource.TestCase{
    22  		PreCheck:     func() { testAccPreCheck(t) },
    23  		Providers:    testAccProviders,
    24  		CheckDestroy: testAccGithubBranchProtectionDestroy,
    25  		Steps: []resource.TestStep{
    26  			{
    27  				Config: testAccGithubBranchProtectionConfig(repoName),
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckGithubProtectedBranchExists("github_branch_protection.master", repoName+":master", &protection),
    30  					testAccCheckGithubBranchProtectionRequiredStatusChecks(&protection, true, true, []string{"github/foo"}),
    31  					testAccCheckGithubBranchProtectionRestrictions(&protection, []string{testUser}, []string{}),
    32  					resource.TestCheckResourceAttr("github_branch_protection.master", "repository", repoName),
    33  					resource.TestCheckResourceAttr("github_branch_protection.master", "branch", "master"),
    34  					resource.TestCheckResourceAttr("github_branch_protection.master", "required_status_checks.0.include_admins", "true"),
    35  					resource.TestCheckResourceAttr("github_branch_protection.master", "required_status_checks.0.strict", "true"),
    36  					resource.TestCheckResourceAttr("github_branch_protection.master", "required_status_checks.0.contexts.#", "1"),
    37  					resource.TestCheckResourceAttr("github_branch_protection.master", "required_status_checks.0.contexts.0", "github/foo"),
    38  					resource.TestCheckResourceAttr("github_branch_protection.master", "required_pull_request_reviews.0.include_admins", "true"),
    39  					resource.TestCheckResourceAttr("github_branch_protection.master", "restrictions.0.users.#", "1"),
    40  					resource.TestCheckResourceAttr("github_branch_protection.master", "restrictions.0.users.0", testUser),
    41  					resource.TestCheckResourceAttr("github_branch_protection.master", "restrictions.0.teams.#", "0"),
    42  				),
    43  			},
    44  			{
    45  				Config: testAccGithubBranchProtectionUpdateConfig(repoName),
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testAccCheckGithubProtectedBranchExists("github_branch_protection.master", repoName+":master", &protection),
    48  					testAccCheckGithubBranchProtectionRequiredStatusChecks(&protection, false, false, []string{"github/bar"}),
    49  					testAccCheckGithubBranchProtectionNoRestrictionsExist(&protection),
    50  					resource.TestCheckResourceAttr("github_branch_protection.master", "repository", repoName),
    51  					resource.TestCheckResourceAttr("github_branch_protection.master", "branch", "master"),
    52  					resource.TestCheckResourceAttr("github_branch_protection.master", "required_status_checks.0.include_admins", "false"),
    53  					resource.TestCheckResourceAttr("github_branch_protection.master", "required_status_checks.0.strict", "false"),
    54  					resource.TestCheckResourceAttr("github_branch_protection.master", "required_status_checks.0.contexts.#", "1"),
    55  					resource.TestCheckResourceAttr("github_branch_protection.master", "required_status_checks.0.contexts.0", "github/bar"),
    56  					resource.TestCheckResourceAttr("github_branch_protection.master", "required_pull_request_reviews.#", "0"),
    57  					resource.TestCheckResourceAttr("github_branch_protection.master", "restrictions.#", "0"),
    58  				),
    59  			},
    60  		},
    61  	})
    62  }
    63  
    64  func TestAccGithubBranchProtection_importBasic(t *testing.T) {
    65  	rString := acctest.RandString(5)
    66  
    67  	resource.Test(t, resource.TestCase{
    68  		PreCheck:     func() { testAccPreCheck(t) },
    69  		Providers:    testAccProviders,
    70  		CheckDestroy: testAccGithubBranchProtectionDestroy,
    71  		Steps: []resource.TestStep{
    72  			{
    73  				Config: testAccGithubBranchProtectionConfig(rString),
    74  			},
    75  			{
    76  				ResourceName:      "github_branch_protection.master",
    77  				ImportState:       true,
    78  				ImportStateVerify: true,
    79  			},
    80  		},
    81  	})
    82  }
    83  
    84  func testAccCheckGithubProtectedBranchExists(n, id string, protection *github.Protection) resource.TestCheckFunc {
    85  	return func(s *terraform.State) error {
    86  		rs, ok := s.RootModule().Resources[n]
    87  		if !ok {
    88  			return fmt.Errorf("Not Found: %s", n)
    89  		}
    90  
    91  		if rs.Primary.ID != id {
    92  			return fmt.Errorf("Expected ID to be %v, got %v", id, rs.Primary.ID)
    93  		}
    94  
    95  		conn := testAccProvider.Meta().(*Organization).client
    96  		o := testAccProvider.Meta().(*Organization).name
    97  		r, b := parseTwoPartID(rs.Primary.ID)
    98  
    99  		githubProtection, _, err := conn.Repositories.GetBranchProtection(context.TODO(), o, r, b)
   100  		if err != nil {
   101  			return err
   102  		}
   103  
   104  		*protection = *githubProtection
   105  		return nil
   106  	}
   107  }
   108  
   109  func testAccCheckGithubBranchProtectionRequiredStatusChecks(protection *github.Protection, expectedIncludeAdmins bool, expectedStrict bool, expectedContexts []string) resource.TestCheckFunc {
   110  	return func(s *terraform.State) error {
   111  		rsc := protection.RequiredStatusChecks
   112  		if rsc == nil {
   113  			return fmt.Errorf("Expected RequiredStatusChecks to be present, but was nil")
   114  		}
   115  
   116  		if rsc.IncludeAdmins != expectedIncludeAdmins {
   117  			return fmt.Errorf("Expected RequiredStatusChecks.IncludeAdmins to be %v, got %v", expectedIncludeAdmins, rsc.IncludeAdmins)
   118  		}
   119  		if rsc.Strict != expectedStrict {
   120  			return fmt.Errorf("Expected RequiredStatusChecks.Strict to be %v, got %v", expectedStrict, rsc.Strict)
   121  		}
   122  
   123  		if !reflect.DeepEqual(rsc.Contexts, expectedContexts) {
   124  			return fmt.Errorf("Expected RequiredStatusChecks.Contexts to be %v, got %v", expectedContexts, rsc.Contexts)
   125  		}
   126  
   127  		return nil
   128  	}
   129  }
   130  
   131  func testAccCheckGithubBranchProtectionRestrictions(protection *github.Protection, expectedUserLogins []string, expectedTeamNames []string) resource.TestCheckFunc {
   132  	return func(s *terraform.State) error {
   133  		restrictions := protection.Restrictions
   134  		if restrictions == nil {
   135  			return fmt.Errorf("Expected Restrictions to be present, but was nil")
   136  		}
   137  
   138  		userLogins := []string{}
   139  		for _, u := range restrictions.Users {
   140  			userLogins = append(userLogins, *u.Login)
   141  		}
   142  		if !reflect.DeepEqual(userLogins, expectedUserLogins) {
   143  			return fmt.Errorf("Expected Restrictions.Users to be %v, got %v", expectedUserLogins, userLogins)
   144  		}
   145  
   146  		teamLogins := []string{}
   147  		for _, t := range restrictions.Teams {
   148  			teamLogins = append(teamLogins, *t.Name)
   149  		}
   150  		if !reflect.DeepEqual(teamLogins, expectedTeamNames) {
   151  			return fmt.Errorf("Expected Restrictions.Teams to be %v, got %v", expectedTeamNames, teamLogins)
   152  		}
   153  
   154  		return nil
   155  	}
   156  }
   157  
   158  func testAccCheckGithubBranchProtectionNoRestrictionsExist(protection *github.Protection) resource.TestCheckFunc {
   159  	return func(s *terraform.State) error {
   160  		if protection.Restrictions != nil {
   161  			return fmt.Errorf("Expected Restrictions to be nil, but was %v", protection.Restrictions)
   162  		}
   163  
   164  		return nil
   165  
   166  	}
   167  }
   168  
   169  func testAccGithubBranchProtectionDestroy(s *terraform.State) error {
   170  	conn := testAccProvider.Meta().(*Organization).client
   171  
   172  	for _, rs := range s.RootModule().Resources {
   173  		if rs.Type != "github_branch_protection" {
   174  			continue
   175  		}
   176  
   177  		o := testAccProvider.Meta().(*Organization).name
   178  		r, b := parseTwoPartID(rs.Primary.ID)
   179  		protection, res, err := conn.Repositories.GetBranchProtection(context.TODO(), o, r, b)
   180  
   181  		if err == nil {
   182  			if protection != nil {
   183  				return fmt.Errorf("Branch protection still exists")
   184  			}
   185  		}
   186  		if res.StatusCode != 404 {
   187  			return err
   188  		}
   189  		return nil
   190  	}
   191  	return nil
   192  }
   193  
   194  func testAccGithubBranchProtectionConfig(repoName string) string {
   195  	return fmt.Sprintf(`
   196  resource "github_repository" "test" {
   197    name        = "%s"
   198    description = "Terraform Acceptance Test %s"
   199    auto_init   = true
   200  }
   201  
   202  resource "github_branch_protection" "master" {
   203    repository = "${github_repository.test.name}"
   204    branch     = "master"
   205  
   206    required_status_checks = {
   207      include_admins = true
   208      strict         = true
   209      contexts       = ["github/foo"]
   210    }
   211  
   212    required_pull_request_reviews {
   213      include_admins = true
   214    }
   215  
   216    restrictions {
   217      users = ["%s"]
   218    }
   219  }
   220  `, repoName, repoName, testUser)
   221  }
   222  
   223  func testAccGithubBranchProtectionUpdateConfig(repoName string) string {
   224  	return fmt.Sprintf(`
   225  resource "github_repository" "test" {
   226    name        = "%s"
   227    description = "Terraform Acceptance Test %s"
   228    auto_init   = true
   229  }
   230  
   231  resource "github_branch_protection" "master" {
   232    repository = "${github_repository.test.name}"
   233    branch     = "master"
   234  
   235    required_status_checks = {
   236      include_admins = false
   237      strict         = false
   238      contexts       = ["github/bar"]
   239    }
   240  }
   241  `, repoName, repoName)
   242  }