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