github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/github/resource_github_team_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 TestAccGithubTeam_basic(t *testing.T) {
    13  	var team github.Team
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckGithubTeamDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccGithubTeamConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckGithubTeamExists("github_team.foo", &team),
    24  					testAccCheckGithubTeamAttributes(&team, "foo", "Terraform acc test group"),
    25  				),
    26  			},
    27  			resource.TestStep{
    28  				Config: testAccGithubTeamUpdateConfig,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckGithubTeamExists("github_team.foo", &team),
    31  					testAccCheckGithubTeamAttributes(&team, "foo2", "Terraform acc test group - updated"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func testAccCheckGithubTeamExists(n string, team *github.Team) resource.TestCheckFunc {
    39  	return func(s *terraform.State) error {
    40  		rs, ok := s.RootModule().Resources[n]
    41  		if !ok {
    42  			return fmt.Errorf("Not Found: %s", n)
    43  		}
    44  
    45  		if rs.Primary.ID == "" {
    46  			return fmt.Errorf("No Team ID is set")
    47  		}
    48  
    49  		conn := testAccProvider.Meta().(*Organization).client
    50  		githubTeam, _, err := conn.Organizations.GetTeam(toGithubID(rs.Primary.ID))
    51  		if err != nil {
    52  			return err
    53  		}
    54  		*team = *githubTeam
    55  		return nil
    56  	}
    57  }
    58  
    59  func testAccCheckGithubTeamAttributes(team *github.Team, name, description string) resource.TestCheckFunc {
    60  	return func(s *terraform.State) error {
    61  		if *team.Name != name {
    62  			return fmt.Errorf("Team name does not match: %s, %s", *team.Name, name)
    63  		}
    64  
    65  		if *team.Description != description {
    66  			return fmt.Errorf("Team description does not match: %s, %s", *team.Description, description)
    67  		}
    68  
    69  		return nil
    70  	}
    71  }
    72  
    73  func testAccCheckGithubTeamDestroy(s *terraform.State) error {
    74  	conn := testAccProvider.Meta().(*Organization).client
    75  
    76  	for _, rs := range s.RootModule().Resources {
    77  		if rs.Type != "github_team" {
    78  			continue
    79  		}
    80  
    81  		team, resp, err := conn.Organizations.GetTeam(toGithubID(rs.Primary.ID))
    82  		if err == nil {
    83  			if team != nil &&
    84  				fromGithubID(team.ID) == rs.Primary.ID {
    85  				return fmt.Errorf("Team still exists")
    86  			}
    87  		}
    88  		if resp.StatusCode != 404 {
    89  			return err
    90  		}
    91  		return nil
    92  	}
    93  	return nil
    94  }
    95  
    96  const testAccGithubTeamConfig = `
    97  resource "github_team" "foo" {
    98  	name = "foo"
    99  	description = "Terraform acc test group"
   100  	privacy = "secret"
   101  }
   102  `
   103  
   104  const testAccGithubTeamUpdateConfig = `
   105  resource "github_team" "foo" {
   106  	name = "foo2"
   107  	description = "Terraform acc test group - updated"
   108  	privacy = "closed"
   109  }
   110  `