github.com/mehmetalisavas/terraform@v0.7.10/builtin/providers/github/resource_github_repository_collaborator_test.go (about) 1 package github 2 3 import ( 4 "fmt" 5 "os" 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 const expectedPermission string = "admin" 14 15 func TestAccGithubRepositoryCollaborator_basic(t *testing.T) { 16 testCollaborator := os.Getenv("GITHUB_TEST_COLLABORATOR") 17 testAccGithubRepositoryCollaboratorConfig := fmt.Sprintf(` 18 resource "github_repository_collaborator" "test_repo_collaborator" { 19 repository = "%s" 20 username = "%s" 21 permission = "%s" 22 } 23 `, testRepo, testCollaborator, expectedPermission) 24 25 resource.Test(t, resource.TestCase{ 26 PreCheck: func() { testAccPreCheck(t) }, 27 Providers: testAccProviders, 28 CheckDestroy: testAccCheckGithubRepositoryCollaboratorDestroy, 29 Steps: []resource.TestStep{ 30 resource.TestStep{ 31 Config: testAccGithubRepositoryCollaboratorConfig, 32 Check: resource.ComposeTestCheckFunc( 33 testAccCheckGithubRepositoryCollaboratorExists("github_repository_collaborator.test_repo_collaborator"), 34 testAccCheckGithubRepositoryCollaboratorPermission("github_repository_collaborator.test_repo_collaborator"), 35 ), 36 }, 37 }, 38 }) 39 } 40 41 func testAccCheckGithubRepositoryCollaboratorDestroy(s *terraform.State) error { 42 conn := testAccProvider.Meta().(*Organization).client 43 44 for _, rs := range s.RootModule().Resources { 45 if rs.Type != "github_repository_collaborator" { 46 continue 47 } 48 49 o := testAccProvider.Meta().(*Organization).name 50 r, u := parseTwoPartID(rs.Primary.ID) 51 isCollaborator, _, err := conn.Repositories.IsCollaborator(o, r, u) 52 53 if err != nil { 54 return err 55 } 56 57 if isCollaborator { 58 return fmt.Errorf("Repository collaborator still exists") 59 } 60 61 return nil 62 } 63 64 return nil 65 } 66 67 func testAccCheckGithubRepositoryCollaboratorExists(n string) resource.TestCheckFunc { 68 return func(s *terraform.State) error { 69 rs, ok := s.RootModule().Resources[n] 70 if !ok { 71 return fmt.Errorf("Not Found: %s", n) 72 } 73 74 if rs.Primary.ID == "" { 75 return fmt.Errorf("No membership ID is set") 76 } 77 78 conn := testAccProvider.Meta().(*Organization).client 79 o := testAccProvider.Meta().(*Organization).name 80 r, u := parseTwoPartID(rs.Primary.ID) 81 82 isCollaborator, _, err := conn.Repositories.IsCollaborator(o, r, u) 83 84 if err != nil { 85 return err 86 } 87 88 if !isCollaborator { 89 return fmt.Errorf("Repository collaborator does not exist") 90 } 91 92 return nil 93 } 94 } 95 96 func testAccCheckGithubRepositoryCollaboratorPermission(n string) resource.TestCheckFunc { 97 return func(s *terraform.State) error { 98 rs, ok := s.RootModule().Resources[n] 99 if !ok { 100 return fmt.Errorf("Not Found: %s", n) 101 } 102 103 if rs.Primary.ID == "" { 104 return fmt.Errorf("No membership ID is set") 105 } 106 107 conn := testAccProvider.Meta().(*Organization).client 108 o := testAccProvider.Meta().(*Organization).name 109 r, u := parseTwoPartID(rs.Primary.ID) 110 111 collaborators, _, err := conn.Repositories.ListCollaborators(o, r, &github.ListOptions{}) 112 113 if err != nil { 114 return err 115 } 116 117 for _, c := range collaborators { 118 if *c.Login == u { 119 permName, err := getRepoPermission(c.Permissions) 120 121 if err != nil { 122 return err 123 } 124 125 if permName != expectedPermission { 126 return fmt.Errorf("Expected permission %s on repository collaborator, actual permission %s", expectedPermission, permName) 127 } 128 129 return nil 130 } 131 } 132 133 return fmt.Errorf("Repository collaborator did not appear in list of collaborators on repository") 134 } 135 }