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