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