github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 TestAccGithubTeam_importBasic(t *testing.T) { 39 resource.Test(t, resource.TestCase{ 40 PreCheck: func() { testAccPreCheck(t) }, 41 Providers: testAccProviders, 42 CheckDestroy: testAccCheckGithubTeamDestroy, 43 Steps: []resource.TestStep{ 44 resource.TestStep{ 45 Config: testAccGithubTeamConfig, 46 }, 47 resource.TestStep{ 48 ResourceName: "github_team.foo", 49 ImportState: true, 50 ImportStateVerify: true, 51 }, 52 }, 53 }) 54 } 55 56 func testAccCheckGithubTeamExists(n string, team *github.Team) resource.TestCheckFunc { 57 return func(s *terraform.State) error { 58 rs, ok := s.RootModule().Resources[n] 59 if !ok { 60 return fmt.Errorf("Not Found: %s", n) 61 } 62 63 if rs.Primary.ID == "" { 64 return fmt.Errorf("No Team ID is set") 65 } 66 67 conn := testAccProvider.Meta().(*Organization).client 68 githubTeam, _, err := conn.Organizations.GetTeam(toGithubID(rs.Primary.ID)) 69 if err != nil { 70 return err 71 } 72 *team = *githubTeam 73 return nil 74 } 75 } 76 77 func testAccCheckGithubTeamAttributes(team *github.Team, name, description string) resource.TestCheckFunc { 78 return func(s *terraform.State) error { 79 if *team.Name != name { 80 return fmt.Errorf("Team name does not match: %s, %s", *team.Name, name) 81 } 82 83 if *team.Description != description { 84 return fmt.Errorf("Team description does not match: %s, %s", *team.Description, description) 85 } 86 87 return nil 88 } 89 } 90 91 func testAccCheckGithubTeamDestroy(s *terraform.State) error { 92 conn := testAccProvider.Meta().(*Organization).client 93 94 for _, rs := range s.RootModule().Resources { 95 if rs.Type != "github_team" { 96 continue 97 } 98 99 team, resp, err := conn.Organizations.GetTeam(toGithubID(rs.Primary.ID)) 100 if err == nil { 101 if team != nil && 102 fromGithubID(team.ID) == rs.Primary.ID { 103 return fmt.Errorf("Team still exists") 104 } 105 } 106 if resp.StatusCode != 404 { 107 return err 108 } 109 return nil 110 } 111 return nil 112 } 113 114 const testAccGithubTeamConfig = ` 115 resource "github_team" "foo" { 116 name = "foo" 117 description = "Terraform acc test group" 118 privacy = "secret" 119 } 120 ` 121 122 const testAccGithubTeamUpdateConfig = ` 123 resource "github_team" "foo" { 124 name = "foo2" 125 description = "Terraform acc test group - updated" 126 privacy = "closed" 127 } 128 `