github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/github/resource_github_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 TestAccGithubMembership_basic(t *testing.T) { 14 var membership github.Membership 15 16 testUser := os.Getenv("GITHUB_TEST_USER") 17 testAccGithubMembershipConfig := fmt.Sprintf(` 18 resource "github_membership" "test_org_membership" { 19 username = "%s" 20 role = "member" 21 } 22 `, testUser) 23 24 resource.Test(t, resource.TestCase{ 25 PreCheck: func() { testAccPreCheck(t) }, 26 Providers: testAccProviders, 27 CheckDestroy: testAccCheckGithubMembershipDestroy, 28 Steps: []resource.TestStep{ 29 resource.TestStep{ 30 Config: testAccGithubMembershipConfig, 31 Check: resource.ComposeTestCheckFunc( 32 testAccCheckGithubMembershipExists("github_membership.test_org_membership", &membership), 33 testAccCheckGithubMembershipRoleState("github_membership.test_org_membership", &membership), 34 ), 35 }, 36 }, 37 }) 38 } 39 40 func testAccCheckGithubMembershipDestroy(s *terraform.State) error { 41 conn := testAccProvider.Meta().(*Organization).client 42 43 for _, rs := range s.RootModule().Resources { 44 if rs.Type != "github_membership" { 45 continue 46 } 47 o, u := parseTwoPartID(rs.Primary.ID) 48 49 membership, resp, err := conn.Organizations.GetOrgMembership(u, o) 50 51 if err == nil { 52 if membership != nil && 53 buildTwoPartID(membership.Organization.Login, membership.User.Login) == rs.Primary.ID { 54 return fmt.Errorf("Organization membership still exists") 55 } 56 } 57 if resp.StatusCode != 404 { 58 return err 59 } 60 return nil 61 } 62 return nil 63 } 64 65 func testAccCheckGithubMembershipExists(n string, membership *github.Membership) resource.TestCheckFunc { 66 return func(s *terraform.State) error { 67 rs, ok := s.RootModule().Resources[n] 68 if !ok { 69 return fmt.Errorf("Not Found: %s", n) 70 } 71 72 if rs.Primary.ID == "" { 73 return fmt.Errorf("No membership ID is set") 74 } 75 76 conn := testAccProvider.Meta().(*Organization).client 77 o, u := parseTwoPartID(rs.Primary.ID) 78 79 githubMembership, _, err := conn.Organizations.GetOrgMembership(u, o) 80 if err != nil { 81 return err 82 } 83 *membership = *githubMembership 84 return nil 85 } 86 } 87 88 func testAccCheckGithubMembershipRoleState(n string, membership *github.Membership) resource.TestCheckFunc { 89 return func(s *terraform.State) error { 90 rs, ok := s.RootModule().Resources[n] 91 if !ok { 92 return fmt.Errorf("Not Found: %s", n) 93 } 94 95 if rs.Primary.ID == "" { 96 return fmt.Errorf("No membership ID is set") 97 } 98 99 conn := testAccProvider.Meta().(*Organization).client 100 o, u := parseTwoPartID(rs.Primary.ID) 101 102 githubMembership, _, err := conn.Organizations.GetOrgMembership(u, o) 103 if err != nil { 104 return err 105 } 106 107 resourceRole := membership.Role 108 actualRole := githubMembership.Role 109 110 if *resourceRole != *actualRole { 111 return fmt.Errorf("Membership role %v in resource does match actual state of %v", *resourceRole, *actualRole) 112 } 113 return nil 114 } 115 }