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