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