2016-10-11 19:05:38 +02:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
2017-03-12 15:05:18 +01:00
|
|
|
"context"
|
2016-10-11 19:05:38 +02:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-github/github"
|
2017-03-17 11:47:43 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2016-10-11 19:05:38 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccGithubRepository_basic(t *testing.T) {
|
|
|
|
var repo github.Repository
|
2017-03-17 11:47:43 +01:00
|
|
|
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
|
|
|
|
name := fmt.Sprintf("tf-acc-test-%s", randString)
|
|
|
|
description := fmt.Sprintf("Terraform acceptance tests %s", randString)
|
2016-10-11 19:05:38 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckGithubRepositoryDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-03-12 15:05:18 +01:00
|
|
|
{
|
2017-03-17 11:47:43 +01:00
|
|
|
Config: testAccGithubRepositoryConfig(randString),
|
2016-10-11 19:05:38 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGithubRepositoryExists("github_repository.foo", &repo),
|
|
|
|
testAccCheckGithubRepositoryAttributes(&repo, &testAccGithubRepositoryExpectedAttributes{
|
2017-03-17 11:47:43 +01:00
|
|
|
Name: name,
|
|
|
|
Description: description,
|
2016-10-11 19:05:38 +02:00
|
|
|
Homepage: "http://example.com/",
|
|
|
|
HasIssues: true,
|
|
|
|
HasWiki: true,
|
|
|
|
HasDownloads: true,
|
|
|
|
DefaultBranch: "master",
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
{
|
2017-03-17 11:47:43 +01:00
|
|
|
Config: testAccGithubRepositoryUpdateConfig(randString),
|
2016-10-11 19:05:38 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckGithubRepositoryExists("github_repository.foo", &repo),
|
|
|
|
testAccCheckGithubRepositoryAttributes(&repo, &testAccGithubRepositoryExpectedAttributes{
|
2017-03-17 11:47:43 +01:00
|
|
|
Name: name,
|
|
|
|
Description: "Updated " + description,
|
2016-10-11 19:05:38 +02:00
|
|
|
Homepage: "http://example.com/",
|
|
|
|
DefaultBranch: "master",
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-28 18:30:24 +01:00
|
|
|
func TestAccGithubRepository_importBasic(t *testing.T) {
|
2017-03-17 11:47:43 +01:00
|
|
|
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
|
|
|
|
|
2016-11-28 18:30:24 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckGithubRepositoryDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-03-12 15:05:18 +01:00
|
|
|
{
|
2017-03-17 11:47:43 +01:00
|
|
|
Config: testAccGithubRepositoryConfig(randString),
|
2016-11-28 18:30:24 +01:00
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
{
|
2016-11-28 18:30:24 +01:00
|
|
|
ResourceName: "github_repository.foo",
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-11 19:05:38 +02:00
|
|
|
func testAccCheckGithubRepositoryExists(n string, repo *github.Repository) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not Found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
repoName := rs.Primary.ID
|
|
|
|
if repoName == "" {
|
|
|
|
return fmt.Errorf("No repository name is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
org := testAccProvider.Meta().(*Organization)
|
|
|
|
conn := org.client
|
2017-03-12 15:05:18 +01:00
|
|
|
gotRepo, _, err := conn.Repositories.Get(context.TODO(), org.name, repoName)
|
2016-10-11 19:05:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*repo = *gotRepo
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testAccGithubRepositoryExpectedAttributes struct {
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
Homepage string
|
|
|
|
Private bool
|
|
|
|
HasIssues bool
|
|
|
|
HasWiki bool
|
|
|
|
HasDownloads bool
|
|
|
|
|
|
|
|
DefaultBranch string
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckGithubRepositoryAttributes(repo *github.Repository, want *testAccGithubRepositoryExpectedAttributes) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
|
|
|
if *repo.Name != want.Name {
|
|
|
|
return fmt.Errorf("got repo %q; want %q", *repo.Name, want.Name)
|
|
|
|
}
|
|
|
|
if *repo.Description != want.Description {
|
|
|
|
return fmt.Errorf("got description %q; want %q", *repo.Description, want.Description)
|
|
|
|
}
|
|
|
|
if *repo.Homepage != want.Homepage {
|
|
|
|
return fmt.Errorf("got homepage URL %q; want %q", *repo.Homepage, want.Homepage)
|
|
|
|
}
|
|
|
|
if *repo.Private != want.Private {
|
|
|
|
return fmt.Errorf("got private %#v; want %#v", *repo.Private, want.Private)
|
|
|
|
}
|
|
|
|
if *repo.HasIssues != want.HasIssues {
|
|
|
|
return fmt.Errorf("got has issues %#v; want %#v", *repo.HasIssues, want.HasIssues)
|
|
|
|
}
|
|
|
|
if *repo.HasWiki != want.HasWiki {
|
|
|
|
return fmt.Errorf("got has wiki %#v; want %#v", *repo.HasWiki, want.HasWiki)
|
|
|
|
}
|
|
|
|
if *repo.HasDownloads != want.HasDownloads {
|
|
|
|
return fmt.Errorf("got has downloads %#v; want %#v", *repo.HasDownloads, want.HasDownloads)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *repo.DefaultBranch != want.DefaultBranch {
|
|
|
|
return fmt.Errorf("got default branch %q; want %q", *repo.DefaultBranch, want.DefaultBranch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For the rest of these, we just want to make sure they've been
|
|
|
|
// populated with something that seems somewhat reasonable.
|
|
|
|
if !strings.HasSuffix(*repo.FullName, "/"+want.Name) {
|
|
|
|
return fmt.Errorf("got full name %q; want to end with '/%s'", *repo.FullName, want.Name)
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(*repo.CloneURL, "/"+want.Name+".git") {
|
|
|
|
return fmt.Errorf("got Clone URL %q; want to end with '/%s.git'", *repo.CloneURL, want.Name)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(*repo.CloneURL, "https://") {
|
|
|
|
return fmt.Errorf("got Clone URL %q; want to start with 'https://'", *repo.CloneURL)
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(*repo.SSHURL, "/"+want.Name+".git") {
|
|
|
|
return fmt.Errorf("got SSH URL %q; want to end with '/%s.git'", *repo.SSHURL, want.Name)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(*repo.SSHURL, "git@github.com:") {
|
|
|
|
return fmt.Errorf("got SSH URL %q; want to start with 'git@github.com:'", *repo.SSHURL)
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(*repo.GitURL, "/"+want.Name+".git") {
|
|
|
|
return fmt.Errorf("got git URL %q; want to end with '/%s.git'", *repo.GitURL, want.Name)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(*repo.GitURL, "git://") {
|
|
|
|
return fmt.Errorf("got git URL %q; want to start with 'git://'", *repo.GitURL)
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(*repo.SVNURL, "/"+want.Name) {
|
|
|
|
return fmt.Errorf("got svn URL %q; want to end with '/%s'", *repo.SVNURL, want.Name)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(*repo.SVNURL, "https://") {
|
|
|
|
return fmt.Errorf("got svn URL %q; want to start with 'https://'", *repo.SVNURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckGithubRepositoryDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*Organization).client
|
|
|
|
orgName := testAccProvider.Meta().(*Organization).name
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "github_repository" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-03-12 15:05:18 +01:00
|
|
|
gotRepo, resp, err := conn.Repositories.Get(context.TODO(), orgName, rs.Primary.ID)
|
2016-10-11 19:05:38 +02:00
|
|
|
if err == nil {
|
|
|
|
if gotRepo != nil && *gotRepo.Name == rs.Primary.ID {
|
2017-03-23 10:19:03 +01:00
|
|
|
return fmt.Errorf("Repository %s/%s still exists", orgName, *gotRepo.Name)
|
2016-10-11 19:05:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 404 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-17 11:47:43 +01:00
|
|
|
func testAccGithubRepositoryConfig(randString string) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-10-11 19:05:38 +02:00
|
|
|
resource "github_repository" "foo" {
|
2017-03-17 11:47:43 +01:00
|
|
|
name = "tf-acc-test-%s"
|
|
|
|
description = "Terraform acceptance tests %s"
|
2016-10-11 19:05:38 +02:00
|
|
|
homepage_url = "http://example.com/"
|
|
|
|
|
|
|
|
# So that acceptance tests can be run in a github organization
|
|
|
|
# with no billing
|
|
|
|
private = false
|
|
|
|
|
|
|
|
has_issues = true
|
|
|
|
has_wiki = true
|
|
|
|
has_downloads = true
|
|
|
|
}
|
2017-03-17 11:47:43 +01:00
|
|
|
`, randString, randString)
|
|
|
|
}
|
2016-10-11 19:05:38 +02:00
|
|
|
|
2017-03-17 11:47:43 +01:00
|
|
|
func testAccGithubRepositoryUpdateConfig(randString string) string {
|
|
|
|
return fmt.Sprintf(`
|
2016-10-11 19:05:38 +02:00
|
|
|
resource "github_repository" "foo" {
|
2017-03-17 11:47:43 +01:00
|
|
|
name = "tf-acc-test-%s"
|
|
|
|
description = "Updated Terraform acceptance tests %s"
|
2016-10-11 19:05:38 +02:00
|
|
|
homepage_url = "http://example.com/"
|
|
|
|
|
|
|
|
# So that acceptance tests can be run in a github organization
|
|
|
|
# with no billing
|
|
|
|
private = false
|
|
|
|
|
|
|
|
has_issues = false
|
|
|
|
has_wiki = false
|
|
|
|
has_downloads = false
|
|
|
|
}
|
2017-03-17 11:47:43 +01:00
|
|
|
`, randString, randString)
|
|
|
|
}
|