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