github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 testAccCheckGithubRepositoryExists(n string, repo *github.Repository) resource.TestCheckFunc { 53 return func(s *terraform.State) error { 54 rs, ok := s.RootModule().Resources[n] 55 if !ok { 56 return fmt.Errorf("Not Found: %s", n) 57 } 58 59 repoName := rs.Primary.ID 60 if repoName == "" { 61 return fmt.Errorf("No repository name is set") 62 } 63 64 org := testAccProvider.Meta().(*Organization) 65 conn := org.client 66 gotRepo, _, err := conn.Repositories.Get(org.name, repoName) 67 if err != nil { 68 return err 69 } 70 *repo = *gotRepo 71 return nil 72 } 73 } 74 75 type testAccGithubRepositoryExpectedAttributes struct { 76 Name string 77 Description string 78 Homepage string 79 Private bool 80 HasIssues bool 81 HasWiki bool 82 HasDownloads bool 83 84 DefaultBranch string 85 } 86 87 func testAccCheckGithubRepositoryAttributes(repo *github.Repository, want *testAccGithubRepositoryExpectedAttributes) resource.TestCheckFunc { 88 return func(s *terraform.State) error { 89 90 if *repo.Name != want.Name { 91 return fmt.Errorf("got repo %q; want %q", *repo.Name, want.Name) 92 } 93 if *repo.Description != want.Description { 94 return fmt.Errorf("got description %q; want %q", *repo.Description, want.Description) 95 } 96 if *repo.Homepage != want.Homepage { 97 return fmt.Errorf("got homepage URL %q; want %q", *repo.Homepage, want.Homepage) 98 } 99 if *repo.Private != want.Private { 100 return fmt.Errorf("got private %#v; want %#v", *repo.Private, want.Private) 101 } 102 if *repo.HasIssues != want.HasIssues { 103 return fmt.Errorf("got has issues %#v; want %#v", *repo.HasIssues, want.HasIssues) 104 } 105 if *repo.HasWiki != want.HasWiki { 106 return fmt.Errorf("got has wiki %#v; want %#v", *repo.HasWiki, want.HasWiki) 107 } 108 if *repo.HasDownloads != want.HasDownloads { 109 return fmt.Errorf("got has downloads %#v; want %#v", *repo.HasDownloads, want.HasDownloads) 110 } 111 112 if *repo.DefaultBranch != want.DefaultBranch { 113 return fmt.Errorf("got default branch %q; want %q", *repo.DefaultBranch, want.DefaultBranch) 114 } 115 116 // For the rest of these, we just want to make sure they've been 117 // populated with something that seems somewhat reasonable. 118 if !strings.HasSuffix(*repo.FullName, "/"+want.Name) { 119 return fmt.Errorf("got full name %q; want to end with '/%s'", *repo.FullName, want.Name) 120 } 121 if !strings.HasSuffix(*repo.CloneURL, "/"+want.Name+".git") { 122 return fmt.Errorf("got Clone URL %q; want to end with '/%s.git'", *repo.CloneURL, want.Name) 123 } 124 if !strings.HasPrefix(*repo.CloneURL, "https://") { 125 return fmt.Errorf("got Clone URL %q; want to start with 'https://'", *repo.CloneURL) 126 } 127 if !strings.HasSuffix(*repo.SSHURL, "/"+want.Name+".git") { 128 return fmt.Errorf("got SSH URL %q; want to end with '/%s.git'", *repo.SSHURL, want.Name) 129 } 130 if !strings.HasPrefix(*repo.SSHURL, "git@github.com:") { 131 return fmt.Errorf("got SSH URL %q; want to start with 'git@github.com:'", *repo.SSHURL) 132 } 133 if !strings.HasSuffix(*repo.GitURL, "/"+want.Name+".git") { 134 return fmt.Errorf("got git URL %q; want to end with '/%s.git'", *repo.GitURL, want.Name) 135 } 136 if !strings.HasPrefix(*repo.GitURL, "git://") { 137 return fmt.Errorf("got git URL %q; want to start with 'git://'", *repo.GitURL) 138 } 139 if !strings.HasSuffix(*repo.SVNURL, "/"+want.Name) { 140 return fmt.Errorf("got svn URL %q; want to end with '/%s'", *repo.SVNURL, want.Name) 141 } 142 if !strings.HasPrefix(*repo.SVNURL, "https://") { 143 return fmt.Errorf("got svn URL %q; want to start with 'https://'", *repo.SVNURL) 144 } 145 146 return nil 147 } 148 } 149 150 func testAccCheckGithubRepositoryDestroy(s *terraform.State) error { 151 conn := testAccProvider.Meta().(*Organization).client 152 orgName := testAccProvider.Meta().(*Organization).name 153 154 for _, rs := range s.RootModule().Resources { 155 if rs.Type != "github_repository" { 156 continue 157 } 158 159 gotRepo, resp, err := conn.Repositories.Get(orgName, rs.Primary.ID) 160 if err == nil { 161 if gotRepo != nil && *gotRepo.Name == rs.Primary.ID { 162 return fmt.Errorf("Repository still exists") 163 } 164 } 165 if resp.StatusCode != 404 { 166 return err 167 } 168 return nil 169 } 170 return nil 171 } 172 173 const testAccGithubRepositoryConfig = ` 174 resource "github_repository" "foo" { 175 name = "foo" 176 description = "Terraform acceptance tests" 177 homepage_url = "http://example.com/" 178 179 # So that acceptance tests can be run in a github organization 180 # with no billing 181 private = false 182 183 has_issues = true 184 has_wiki = true 185 has_downloads = true 186 } 187 ` 188 189 const testAccGithubRepositoryUpdateConfig = ` 190 resource "github_repository" "foo" { 191 name = "foo" 192 description = "Terraform acceptance tests!" 193 homepage_url = "http://example.com/" 194 195 # So that acceptance tests can be run in a github organization 196 # with no billing 197 private = false 198 199 has_issues = false 200 has_wiki = false 201 has_downloads = false 202 } 203 `