github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/github/resource_github_repository_test.go (about) 1 package github 2 3 import ( 4 "fmt" 5 "strings" 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 TestAccGithubRepository_basic(t *testing.T) { 14 var repo github.Repository 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckGithubRepositoryDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccGithubRepositoryConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckGithubRepositoryExists("github_repository.foo", &repo), 25 testAccCheckGithubRepositoryAttributes(&repo, &testAccGithubRepositoryExpectedAttributes{ 26 Name: "foo", 27 Description: "Terraform acceptance tests", 28 Homepage: "http://example.com/", 29 HasIssues: true, 30 HasWiki: true, 31 HasDownloads: true, 32 DefaultBranch: "master", 33 }), 34 ), 35 }, 36 resource.TestStep{ 37 Config: testAccGithubRepositoryUpdateConfig, 38 Check: resource.ComposeTestCheckFunc( 39 testAccCheckGithubRepositoryExists("github_repository.foo", &repo), 40 testAccCheckGithubRepositoryAttributes(&repo, &testAccGithubRepositoryExpectedAttributes{ 41 Name: "foo", 42 Description: "Terraform acceptance tests!", 43 Homepage: "http://example.com/", 44 DefaultBranch: "master", 45 }), 46 ), 47 }, 48 }, 49 }) 50 } 51 52 func TestAccGithubRepository_importBasic(t *testing.T) { 53 resource.Test(t, resource.TestCase{ 54 PreCheck: func() { testAccPreCheck(t) }, 55 Providers: testAccProviders, 56 CheckDestroy: testAccCheckGithubRepositoryDestroy, 57 Steps: []resource.TestStep{ 58 resource.TestStep{ 59 Config: testAccGithubRepositoryConfig, 60 }, 61 resource.TestStep{ 62 ResourceName: "github_repository.foo", 63 ImportState: true, 64 ImportStateVerify: true, 65 }, 66 }, 67 }) 68 } 69 70 func testAccCheckGithubRepositoryExists(n string, repo *github.Repository) resource.TestCheckFunc { 71 return func(s *terraform.State) error { 72 rs, ok := s.RootModule().Resources[n] 73 if !ok { 74 return fmt.Errorf("Not Found: %s", n) 75 } 76 77 repoName := rs.Primary.ID 78 if repoName == "" { 79 return fmt.Errorf("No repository name is set") 80 } 81 82 org := testAccProvider.Meta().(*Organization) 83 conn := org.client 84 gotRepo, _, err := conn.Repositories.Get(org.name, repoName) 85 if err != nil { 86 return err 87 } 88 *repo = *gotRepo 89 return nil 90 } 91 } 92 93 type testAccGithubRepositoryExpectedAttributes struct { 94 Name string 95 Description string 96 Homepage string 97 Private bool 98 HasIssues bool 99 HasWiki bool 100 HasDownloads bool 101 102 DefaultBranch string 103 } 104 105 func testAccCheckGithubRepositoryAttributes(repo *github.Repository, want *testAccGithubRepositoryExpectedAttributes) resource.TestCheckFunc { 106 return func(s *terraform.State) error { 107 108 if *repo.Name != want.Name { 109 return fmt.Errorf("got repo %q; want %q", *repo.Name, want.Name) 110 } 111 if *repo.Description != want.Description { 112 return fmt.Errorf("got description %q; want %q", *repo.Description, want.Description) 113 } 114 if *repo.Homepage != want.Homepage { 115 return fmt.Errorf("got homepage URL %q; want %q", *repo.Homepage, want.Homepage) 116 } 117 if *repo.Private != want.Private { 118 return fmt.Errorf("got private %#v; want %#v", *repo.Private, want.Private) 119 } 120 if *repo.HasIssues != want.HasIssues { 121 return fmt.Errorf("got has issues %#v; want %#v", *repo.HasIssues, want.HasIssues) 122 } 123 if *repo.HasWiki != want.HasWiki { 124 return fmt.Errorf("got has wiki %#v; want %#v", *repo.HasWiki, want.HasWiki) 125 } 126 if *repo.HasDownloads != want.HasDownloads { 127 return fmt.Errorf("got has downloads %#v; want %#v", *repo.HasDownloads, want.HasDownloads) 128 } 129 130 if *repo.DefaultBranch != want.DefaultBranch { 131 return fmt.Errorf("got default branch %q; want %q", *repo.DefaultBranch, want.DefaultBranch) 132 } 133 134 // For the rest of these, we just want to make sure they've been 135 // populated with something that seems somewhat reasonable. 136 if !strings.HasSuffix(*repo.FullName, "/"+want.Name) { 137 return fmt.Errorf("got full name %q; want to end with '/%s'", *repo.FullName, want.Name) 138 } 139 if !strings.HasSuffix(*repo.CloneURL, "/"+want.Name+".git") { 140 return fmt.Errorf("got Clone URL %q; want to end with '/%s.git'", *repo.CloneURL, want.Name) 141 } 142 if !strings.HasPrefix(*repo.CloneURL, "https://") { 143 return fmt.Errorf("got Clone URL %q; want to start with 'https://'", *repo.CloneURL) 144 } 145 if !strings.HasSuffix(*repo.SSHURL, "/"+want.Name+".git") { 146 return fmt.Errorf("got SSH URL %q; want to end with '/%s.git'", *repo.SSHURL, want.Name) 147 } 148 if !strings.HasPrefix(*repo.SSHURL, "git@github.com:") { 149 return fmt.Errorf("got SSH URL %q; want to start with 'git@github.com:'", *repo.SSHURL) 150 } 151 if !strings.HasSuffix(*repo.GitURL, "/"+want.Name+".git") { 152 return fmt.Errorf("got git URL %q; want to end with '/%s.git'", *repo.GitURL, want.Name) 153 } 154 if !strings.HasPrefix(*repo.GitURL, "git://") { 155 return fmt.Errorf("got git URL %q; want to start with 'git://'", *repo.GitURL) 156 } 157 if !strings.HasSuffix(*repo.SVNURL, "/"+want.Name) { 158 return fmt.Errorf("got svn URL %q; want to end with '/%s'", *repo.SVNURL, want.Name) 159 } 160 if !strings.HasPrefix(*repo.SVNURL, "https://") { 161 return fmt.Errorf("got svn URL %q; want to start with 'https://'", *repo.SVNURL) 162 } 163 164 return nil 165 } 166 } 167 168 func testAccCheckGithubRepositoryDestroy(s *terraform.State) error { 169 conn := testAccProvider.Meta().(*Organization).client 170 orgName := testAccProvider.Meta().(*Organization).name 171 172 for _, rs := range s.RootModule().Resources { 173 if rs.Type != "github_repository" { 174 continue 175 } 176 177 gotRepo, resp, err := conn.Repositories.Get(orgName, rs.Primary.ID) 178 if err == nil { 179 if gotRepo != nil && *gotRepo.Name == rs.Primary.ID { 180 return fmt.Errorf("Repository still exists") 181 } 182 } 183 if resp.StatusCode != 404 { 184 return err 185 } 186 return nil 187 } 188 return nil 189 } 190 191 const testAccGithubRepositoryConfig = ` 192 resource "github_repository" "foo" { 193 name = "foo" 194 description = "Terraform acceptance tests" 195 homepage_url = "http://example.com/" 196 197 # So that acceptance tests can be run in a github organization 198 # with no billing 199 private = false 200 201 has_issues = true 202 has_wiki = true 203 has_downloads = true 204 } 205 ` 206 207 const testAccGithubRepositoryUpdateConfig = ` 208 resource "github_repository" "foo" { 209 name = "foo" 210 description = "Terraform acceptance tests!" 211 homepage_url = "http://example.com/" 212 213 # So that acceptance tests can be run in a github organization 214 # with no billing 215 private = false 216 217 has_issues = false 218 has_wiki = false 219 has_downloads = false 220 } 221 `