github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/github/resource_github_team_repository_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 TestAccGithubTeamRepository_basic(t *testing.T) { 15 var repository github.Repository 16 17 randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) 18 repoName := fmt.Sprintf("tf-acc-test-team-%s", acctest.RandString(5)) 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testAccCheckGithubTeamRepositoryDestroy, 24 Steps: []resource.TestStep{ 25 { 26 Config: testAccGithubTeamRepositoryConfig(randString, repoName), 27 Check: resource.ComposeTestCheckFunc( 28 testAccCheckGithubTeamRepositoryExists("github_team_repository.test_team_test_repo", &repository), 29 testAccCheckGithubTeamRepositoryRoleState("pull", &repository), 30 ), 31 }, 32 { 33 Config: testAccGithubTeamRepositoryUpdateConfig(randString, repoName), 34 Check: resource.ComposeTestCheckFunc( 35 testAccCheckGithubTeamRepositoryExists("github_team_repository.test_team_test_repo", &repository), 36 testAccCheckGithubTeamRepositoryRoleState("push", &repository), 37 ), 38 }, 39 }, 40 }) 41 } 42 43 func TestAccGithubTeamRepository_importBasic(t *testing.T) { 44 randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) 45 repoName := fmt.Sprintf("tf-acc-test-team-%s", acctest.RandString(5)) 46 47 resource.Test(t, resource.TestCase{ 48 PreCheck: func() { testAccPreCheck(t) }, 49 Providers: testAccProviders, 50 CheckDestroy: testAccCheckGithubTeamRepositoryDestroy, 51 Steps: []resource.TestStep{ 52 { 53 Config: testAccGithubTeamRepositoryConfig(randString, repoName), 54 }, 55 { 56 ResourceName: "github_team_repository.test_team_test_repo", 57 ImportState: true, 58 ImportStateVerify: true, 59 }, 60 }, 61 }) 62 } 63 64 func TestAccCheckGetPermissions(t *testing.T) { 65 pullMap := map[string]bool{"pull": true, "push": false, "admin": false} 66 pushMap := map[string]bool{"pull": true, "push": true, "admin": false} 67 adminMap := map[string]bool{"pull": true, "push": true, "admin": true} 68 errorMap := map[string]bool{"pull": false, "push": false, "admin": false} 69 70 pull, _ := getRepoPermission(&pullMap) 71 if pull != "pull" { 72 t.Fatalf("Expected pull permission, actual: %s", pull) 73 } 74 75 push, _ := getRepoPermission(&pushMap) 76 if push != "push" { 77 t.Fatalf("Expected push permission, actual: %s", push) 78 } 79 80 admin, _ := getRepoPermission(&adminMap) 81 if admin != "admin" { 82 t.Fatalf("Expected admin permission, actual: %s", admin) 83 } 84 85 errPerm, err := getRepoPermission(&errorMap) 86 if err == nil { 87 t.Fatalf("Expected an error getting permissions, actual: %v", errPerm) 88 } 89 } 90 91 func testAccCheckGithubTeamRepositoryRoleState(role string, repository *github.Repository) resource.TestCheckFunc { 92 return func(s *terraform.State) error { 93 resourceRole, err := getRepoPermission(repository.Permissions) 94 if err != nil { 95 return err 96 } 97 98 if resourceRole != role { 99 return fmt.Errorf("Team repository role %v in resource does match expected state of %v", resourceRole, role) 100 } 101 return nil 102 } 103 } 104 105 func testAccCheckGithubTeamRepositoryExists(n string, repository *github.Repository) resource.TestCheckFunc { 106 return func(s *terraform.State) error { 107 rs, ok := s.RootModule().Resources[n] 108 if !ok { 109 return fmt.Errorf("Not Found: %s", n) 110 } 111 112 if rs.Primary.ID == "" { 113 return fmt.Errorf("No team repository ID is set") 114 } 115 116 conn := testAccProvider.Meta().(*Organization).client 117 t, r := parseTwoPartID(rs.Primary.ID) 118 119 repo, _, err := conn.Organizations.IsTeamRepo(context.TODO(), 120 toGithubID(t), 121 testAccProvider.Meta().(*Organization).name, r) 122 123 if err != nil { 124 return err 125 } 126 *repository = *repo 127 return nil 128 } 129 } 130 131 func testAccCheckGithubTeamRepositoryDestroy(s *terraform.State) error { 132 conn := testAccProvider.Meta().(*Organization).client 133 134 for _, rs := range s.RootModule().Resources { 135 if rs.Type != "github_team_repository" { 136 continue 137 } 138 t, r := parseTwoPartID(rs.Primary.ID) 139 140 repo, resp, err := conn.Organizations.IsTeamRepo(context.TODO(), 141 toGithubID(t), 142 testAccProvider.Meta().(*Organization).name, r) 143 144 if err == nil { 145 if repo != nil && 146 buildTwoPartID(&t, repo.Name) == rs.Primary.ID { 147 return fmt.Errorf("Team repository still exists") 148 } 149 } 150 if resp.StatusCode != 404 { 151 return err 152 } 153 return nil 154 } 155 return nil 156 } 157 158 func testAccGithubTeamRepositoryConfig(randString, repoName string) string { 159 return fmt.Sprintf(` 160 resource "github_team" "test_team" { 161 name = "tf-acc-test-team-repo-%s" 162 description = "Terraform acc test group" 163 } 164 165 resource "github_repository" "test" { 166 name = "%s" 167 } 168 169 resource "github_team_repository" "test_team_test_repo" { 170 team_id = "${github_team.test_team.id}" 171 repository = "${github_repository.test.name}" 172 permission = "pull" 173 } 174 `, randString, repoName) 175 } 176 177 func testAccGithubTeamRepositoryUpdateConfig(randString, repoName string) string { 178 return fmt.Sprintf(` 179 resource "github_team" "test_team" { 180 name = "tf-acc-test-team-repo-%s" 181 description = "Terraform acc test group" 182 } 183 184 resource "github_repository" "test" { 185 name = "%s" 186 } 187 188 resource "github_team_repository" "test_team_test_repo" { 189 team_id = "${github_team.test_team.id}" 190 repository = "${github_repository.test.name}" 191 permission = "push" 192 } 193 `, randString, repoName) 194 }