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