github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/github/resource_github_team_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/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccGithubTeam_basic(t *testing.T) {
    15  	var team github.Team
    16  	randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
    17  	name := fmt.Sprintf("tf-acc-test-%s", randString)
    18  	updatedName := fmt.Sprintf("tf-acc-test-updated-%s", randString)
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckGithubTeamDestroy,
    24  		Steps: []resource.TestStep{
    25  			{
    26  				Config: testAccGithubTeamConfig(randString),
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckGithubTeamExists("github_team.foo", &team),
    29  					testAccCheckGithubTeamAttributes(&team, name, "Terraform acc test group"),
    30  				),
    31  			},
    32  			{
    33  				Config: testAccGithubTeamUpdateConfig(randString),
    34  				Check: resource.ComposeTestCheckFunc(
    35  					testAccCheckGithubTeamExists("github_team.foo", &team),
    36  					testAccCheckGithubTeamAttributes(&team, updatedName, "Terraform acc test group - updated"),
    37  				),
    38  			},
    39  		},
    40  	})
    41  }
    42  
    43  func TestAccGithubTeam_importBasic(t *testing.T) {
    44  	randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
    45  
    46  	resource.Test(t, resource.TestCase{
    47  		PreCheck:     func() { testAccPreCheck(t) },
    48  		Providers:    testAccProviders,
    49  		CheckDestroy: testAccCheckGithubTeamDestroy,
    50  		Steps: []resource.TestStep{
    51  			{
    52  				Config: testAccGithubTeamConfig(randString),
    53  			},
    54  			{
    55  				ResourceName:      "github_team.foo",
    56  				ImportState:       true,
    57  				ImportStateVerify: true,
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func testAccCheckGithubTeamExists(n string, team *github.Team) resource.TestCheckFunc {
    64  	return func(s *terraform.State) error {
    65  		rs, ok := s.RootModule().Resources[n]
    66  		if !ok {
    67  			return fmt.Errorf("Not Found: %s", n)
    68  		}
    69  
    70  		if rs.Primary.ID == "" {
    71  			return fmt.Errorf("No Team ID is set")
    72  		}
    73  
    74  		conn := testAccProvider.Meta().(*Organization).client
    75  		githubTeam, _, err := conn.Organizations.GetTeam(context.TODO(), toGithubID(rs.Primary.ID))
    76  		if err != nil {
    77  			return err
    78  		}
    79  		*team = *githubTeam
    80  		return nil
    81  	}
    82  }
    83  
    84  func testAccCheckGithubTeamAttributes(team *github.Team, name, description string) resource.TestCheckFunc {
    85  	return func(s *terraform.State) error {
    86  		if *team.Name != name {
    87  			return fmt.Errorf("Team name does not match: %s, %s", *team.Name, name)
    88  		}
    89  
    90  		if *team.Description != description {
    91  			return fmt.Errorf("Team description does not match: %s, %s", *team.Description, description)
    92  		}
    93  
    94  		return nil
    95  	}
    96  }
    97  
    98  func testAccCheckGithubTeamDestroy(s *terraform.State) error {
    99  	conn := testAccProvider.Meta().(*Organization).client
   100  
   101  	for _, rs := range s.RootModule().Resources {
   102  		if rs.Type != "github_team" {
   103  			continue
   104  		}
   105  
   106  		team, resp, err := conn.Organizations.GetTeam(context.TODO(), toGithubID(rs.Primary.ID))
   107  		if err == nil {
   108  			if team != nil &&
   109  				fromGithubID(team.ID) == rs.Primary.ID {
   110  				return fmt.Errorf("Team still exists")
   111  			}
   112  		}
   113  		if resp.StatusCode != 404 {
   114  			return err
   115  		}
   116  		return nil
   117  	}
   118  	return nil
   119  }
   120  
   121  func testAccGithubTeamConfig(randString string) string {
   122  	return fmt.Sprintf(`
   123  resource "github_team" "foo" {
   124  	name = "tf-acc-test-%s"
   125  	description = "Terraform acc test group"
   126  	privacy = "secret"
   127  }
   128  `, randString)
   129  }
   130  
   131  func testAccGithubTeamUpdateConfig(randString string) string {
   132  	return fmt.Sprintf(`
   133  resource "github_team" "foo" {
   134  	name = "tf-acc-test-updated-%s"
   135  	description = "Terraform acc test group - updated"
   136  	privacy = "closed"
   137  }
   138  `, randString)
   139  }