github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/github/resource_github_team_membership_test.go (about)

     1  package github
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/google/go-github/github"
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccGithubTeamMembership_basic(t *testing.T) {
    15  	var membership github.Membership
    16  	randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
    17  
    18  	testAccGithubTeamMembershipUpdateConfig := fmt.Sprintf(`
    19  		resource "github_membership" "test_org_membership" {
    20  			username = "%s"
    21  			role = "member"
    22  		}
    23  
    24  		resource "github_team" "test_team" {
    25  			name = "tf-acc-test-team-membership-%s"
    26  			description = "Terraform acc test group"
    27  		}
    28  
    29  		resource "github_team_membership" "test_team_membership" {
    30  			team_id = "${github_team.test_team.id}"
    31  			username = "%s"
    32  			role = "maintainer"
    33  		}
    34  	`, testUser, randString, testUser)
    35  
    36  	resource.Test(t, resource.TestCase{
    37  		PreCheck:     func() { testAccPreCheck(t) },
    38  		Providers:    testAccProviders,
    39  		CheckDestroy: testAccCheckGithubTeamMembershipDestroy,
    40  		Steps: []resource.TestStep{
    41  			{
    42  				Config: testAccGithubTeamMembershipConfig(randString, testUser),
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckGithubTeamMembershipExists("github_team_membership.test_team_membership", &membership),
    45  					testAccCheckGithubTeamMembershipRoleState("github_team_membership.test_team_membership", "member", &membership),
    46  				),
    47  			},
    48  			{
    49  				Config: testAccGithubTeamMembershipUpdateConfig,
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckGithubTeamMembershipExists("github_team_membership.test_team_membership", &membership),
    52  					testAccCheckGithubTeamMembershipRoleState("github_team_membership.test_team_membership", "maintainer", &membership),
    53  				),
    54  			},
    55  		},
    56  	})
    57  }
    58  
    59  func TestAccGithubTeamMembership_importBasic(t *testing.T) {
    60  	randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
    61  
    62  	resource.Test(t, resource.TestCase{
    63  		PreCheck:     func() { testAccPreCheck(t) },
    64  		Providers:    testAccProviders,
    65  		CheckDestroy: testAccCheckGithubTeamMembershipDestroy,
    66  		Steps: []resource.TestStep{
    67  			{
    68  				Config: testAccGithubTeamMembershipConfig(randString, testUser),
    69  			},
    70  			{
    71  				ResourceName:      "github_team_membership.test_team_membership",
    72  				ImportState:       true,
    73  				ImportStateVerify: true,
    74  			},
    75  		},
    76  	})
    77  }
    78  
    79  func testAccCheckGithubTeamMembershipDestroy(s *terraform.State) error {
    80  	conn := testAccProvider.Meta().(*Organization).client
    81  
    82  	for _, rs := range s.RootModule().Resources {
    83  		if rs.Type != "github_team_membership" {
    84  			continue
    85  		}
    86  
    87  		t, u := parseTwoPartID(rs.Primary.ID)
    88  		membership, resp, err := conn.Organizations.GetTeamMembership(context.TODO(), toGithubID(t), u)
    89  		if err == nil {
    90  			if membership != nil {
    91  				return fmt.Errorf("Team membership still exists")
    92  			}
    93  		}
    94  		if resp.StatusCode != 404 {
    95  			return err
    96  		}
    97  		return nil
    98  	}
    99  	return nil
   100  }
   101  
   102  func testAccCheckGithubTeamMembershipExists(n string, membership *github.Membership) resource.TestCheckFunc {
   103  	return func(s *terraform.State) error {
   104  		rs, ok := s.RootModule().Resources[n]
   105  		if !ok {
   106  			return fmt.Errorf("Not Found: %s", n)
   107  		}
   108  
   109  		if rs.Primary.ID == "" {
   110  			return fmt.Errorf("No team membership ID is set")
   111  		}
   112  
   113  		conn := testAccProvider.Meta().(*Organization).client
   114  		t, u := parseTwoPartID(rs.Primary.ID)
   115  
   116  		teamMembership, _, err := conn.Organizations.GetTeamMembership(context.TODO(), toGithubID(t), u)
   117  
   118  		if err != nil {
   119  			return err
   120  		}
   121  		*membership = *teamMembership
   122  		return nil
   123  	}
   124  }
   125  
   126  func testAccCheckGithubTeamMembershipRoleState(n, expected string, membership *github.Membership) resource.TestCheckFunc {
   127  	return func(s *terraform.State) error {
   128  		rs, ok := s.RootModule().Resources[n]
   129  		if !ok {
   130  			return fmt.Errorf("Not Found: %s", n)
   131  		}
   132  
   133  		if rs.Primary.ID == "" {
   134  			return fmt.Errorf("No team membership ID is set")
   135  		}
   136  
   137  		conn := testAccProvider.Meta().(*Organization).client
   138  		t, u := parseTwoPartID(rs.Primary.ID)
   139  
   140  		teamMembership, _, err := conn.Organizations.GetTeamMembership(context.TODO(), toGithubID(t), u)
   141  		if err != nil {
   142  			return err
   143  		}
   144  
   145  		resourceRole := membership.Role
   146  		actualRole := teamMembership.Role
   147  
   148  		if *resourceRole != expected {
   149  			return fmt.Errorf("Team membership role %v in resource does match expected state of %v", *resourceRole, expected)
   150  		}
   151  
   152  		if *resourceRole != *actualRole {
   153  			return fmt.Errorf("Team membership role %v in resource does match actual state of %v", *resourceRole, *actualRole)
   154  		}
   155  		return nil
   156  	}
   157  }
   158  
   159  func testAccGithubTeamMembershipConfig(randString, username string) string {
   160  	return fmt.Sprintf(`
   161    resource "github_membership" "test_org_membership" {
   162      username = "%s"
   163      role = "member"
   164    }
   165  
   166    resource "github_team" "test_team" {
   167      name = "tf-acc-test-team-membership-%s"
   168      description = "Terraform acc test group"
   169    }
   170  
   171    resource "github_team_membership" "test_team_membership" {
   172      team_id = "${github_team.test_team.id}"
   173      username = "%s"
   174      role = "member"
   175    }
   176  `, username, randString, username)
   177  }