terraform/builtin/providers/github/resource_github_repository_...

233 lines
7.1 KiB
Go
Raw Normal View History

package github
import (
"context"
"fmt"
"strings"
"testing"
"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccGithubRepository_basic(t *testing.T) {
var repo github.Repository
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
name := fmt.Sprintf("tf-acc-test-%s", randString)
description := fmt.Sprintf("Terraform acceptance tests %s", randString)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGithubRepositoryDestroy,
Steps: []resource.TestStep{
{
Config: testAccGithubRepositoryConfig(randString),
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubRepositoryExists("github_repository.foo", &repo),
testAccCheckGithubRepositoryAttributes(&repo, &testAccGithubRepositoryExpectedAttributes{
Name: name,
Description: description,
Homepage: "http://example.com/",
HasIssues: true,
HasWiki: true,
HasDownloads: true,
DefaultBranch: "master",
}),
),
},
{
Config: testAccGithubRepositoryUpdateConfig(randString),
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubRepositoryExists("github_repository.foo", &repo),
testAccCheckGithubRepositoryAttributes(&repo, &testAccGithubRepositoryExpectedAttributes{
Name: name,
Description: "Updated " + description,
Homepage: "http://example.com/",
DefaultBranch: "master",
}),
),
},
},
})
}
provider/github: supports importing resources (#10382) ``` === RUN TestProvider --- PASS: TestProvider (0.00s) === RUN TestProvider_impl --- PASS: TestProvider_impl (0.00s) === RUN TestAccGithubIssueLabel_basic --- PASS: TestAccGithubIssueLabel_basic (0.91s) === RUN TestAccGithubIssueLabel_importBasic --- PASS: TestAccGithubIssueLabel_importBasic (0.41s) === RUN TestAccGithubMembership_basic --- PASS: TestAccGithubMembership_basic (0.84s) === RUN TestAccGithubMembership_importBasic --- PASS: TestAccGithubMembership_importBasic (0.53s) === RUN TestAccGithubRepositoryCollaborator_basic --- PASS: TestAccGithubRepositoryCollaborator_basic (0.64s) === RUN TestAccGithubRepositoryCollaborator_importBasic --- PASS: TestAccGithubRepositoryCollaborator_importBasic (0.74s) === RUN TestAccGithubRepository_basic --- PASS: TestAccGithubRepository_basic (1.54s) === RUN TestAccGithubRepository_importBasic --- PASS: TestAccGithubRepository_importBasic (0.77s) === RUN TestAccGithubTeamMembership_basic --- PASS: TestAccGithubTeamMembership_basic (1.59s) === RUN TestAccGithubTeamMembership_importBasic --- PASS: TestAccGithubTeamMembership_importBasic (0.95s) === RUN TestAccGithubTeamRepository_basic --- PASS: TestAccGithubTeamRepository_basic (1.45s) === RUN TestAccGithubTeamRepository_importBasic --- PASS: TestAccGithubTeamRepository_importBasic (0.75s) === RUN TestAccCheckGetPermissions --- PASS: TestAccCheckGetPermissions (0.00s) === RUN TestAccGithubTeam_basic --- PASS: TestAccGithubTeam_basic (0.79s) === RUN TestAccGithubTeam_importBasic --- PASS: TestAccGithubTeam_importBasic (0.54s) === RUN TestAccGithubUtilRole_validation --- PASS: TestAccGithubUtilRole_validation (0.00s) === RUN TestAccGithubUtilTwoPartID --- PASS: TestAccGithubUtilTwoPartID (0.00s) PASS ok github.com/hashicorp/terraform/builtin/providers/github 12.455s ```
2016-11-28 18:30:24 +01:00
func TestAccGithubRepository_importBasic(t *testing.T) {
randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
provider/github: supports importing resources (#10382) ``` === RUN TestProvider --- PASS: TestProvider (0.00s) === RUN TestProvider_impl --- PASS: TestProvider_impl (0.00s) === RUN TestAccGithubIssueLabel_basic --- PASS: TestAccGithubIssueLabel_basic (0.91s) === RUN TestAccGithubIssueLabel_importBasic --- PASS: TestAccGithubIssueLabel_importBasic (0.41s) === RUN TestAccGithubMembership_basic --- PASS: TestAccGithubMembership_basic (0.84s) === RUN TestAccGithubMembership_importBasic --- PASS: TestAccGithubMembership_importBasic (0.53s) === RUN TestAccGithubRepositoryCollaborator_basic --- PASS: TestAccGithubRepositoryCollaborator_basic (0.64s) === RUN TestAccGithubRepositoryCollaborator_importBasic --- PASS: TestAccGithubRepositoryCollaborator_importBasic (0.74s) === RUN TestAccGithubRepository_basic --- PASS: TestAccGithubRepository_basic (1.54s) === RUN TestAccGithubRepository_importBasic --- PASS: TestAccGithubRepository_importBasic (0.77s) === RUN TestAccGithubTeamMembership_basic --- PASS: TestAccGithubTeamMembership_basic (1.59s) === RUN TestAccGithubTeamMembership_importBasic --- PASS: TestAccGithubTeamMembership_importBasic (0.95s) === RUN TestAccGithubTeamRepository_basic --- PASS: TestAccGithubTeamRepository_basic (1.45s) === RUN TestAccGithubTeamRepository_importBasic --- PASS: TestAccGithubTeamRepository_importBasic (0.75s) === RUN TestAccCheckGetPermissions --- PASS: TestAccCheckGetPermissions (0.00s) === RUN TestAccGithubTeam_basic --- PASS: TestAccGithubTeam_basic (0.79s) === RUN TestAccGithubTeam_importBasic --- PASS: TestAccGithubTeam_importBasic (0.54s) === RUN TestAccGithubUtilRole_validation --- PASS: TestAccGithubUtilRole_validation (0.00s) === RUN TestAccGithubUtilTwoPartID --- PASS: TestAccGithubUtilTwoPartID (0.00s) PASS ok github.com/hashicorp/terraform/builtin/providers/github 12.455s ```
2016-11-28 18:30:24 +01:00
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGithubRepositoryDestroy,
Steps: []resource.TestStep{
{
Config: testAccGithubRepositoryConfig(randString),
provider/github: supports importing resources (#10382) ``` === RUN TestProvider --- PASS: TestProvider (0.00s) === RUN TestProvider_impl --- PASS: TestProvider_impl (0.00s) === RUN TestAccGithubIssueLabel_basic --- PASS: TestAccGithubIssueLabel_basic (0.91s) === RUN TestAccGithubIssueLabel_importBasic --- PASS: TestAccGithubIssueLabel_importBasic (0.41s) === RUN TestAccGithubMembership_basic --- PASS: TestAccGithubMembership_basic (0.84s) === RUN TestAccGithubMembership_importBasic --- PASS: TestAccGithubMembership_importBasic (0.53s) === RUN TestAccGithubRepositoryCollaborator_basic --- PASS: TestAccGithubRepositoryCollaborator_basic (0.64s) === RUN TestAccGithubRepositoryCollaborator_importBasic --- PASS: TestAccGithubRepositoryCollaborator_importBasic (0.74s) === RUN TestAccGithubRepository_basic --- PASS: TestAccGithubRepository_basic (1.54s) === RUN TestAccGithubRepository_importBasic --- PASS: TestAccGithubRepository_importBasic (0.77s) === RUN TestAccGithubTeamMembership_basic --- PASS: TestAccGithubTeamMembership_basic (1.59s) === RUN TestAccGithubTeamMembership_importBasic --- PASS: TestAccGithubTeamMembership_importBasic (0.95s) === RUN TestAccGithubTeamRepository_basic --- PASS: TestAccGithubTeamRepository_basic (1.45s) === RUN TestAccGithubTeamRepository_importBasic --- PASS: TestAccGithubTeamRepository_importBasic (0.75s) === RUN TestAccCheckGetPermissions --- PASS: TestAccCheckGetPermissions (0.00s) === RUN TestAccGithubTeam_basic --- PASS: TestAccGithubTeam_basic (0.79s) === RUN TestAccGithubTeam_importBasic --- PASS: TestAccGithubTeam_importBasic (0.54s) === RUN TestAccGithubUtilRole_validation --- PASS: TestAccGithubUtilRole_validation (0.00s) === RUN TestAccGithubUtilTwoPartID --- PASS: TestAccGithubUtilTwoPartID (0.00s) PASS ok github.com/hashicorp/terraform/builtin/providers/github 12.455s ```
2016-11-28 18:30:24 +01:00
},
{
provider/github: supports importing resources (#10382) ``` === RUN TestProvider --- PASS: TestProvider (0.00s) === RUN TestProvider_impl --- PASS: TestProvider_impl (0.00s) === RUN TestAccGithubIssueLabel_basic --- PASS: TestAccGithubIssueLabel_basic (0.91s) === RUN TestAccGithubIssueLabel_importBasic --- PASS: TestAccGithubIssueLabel_importBasic (0.41s) === RUN TestAccGithubMembership_basic --- PASS: TestAccGithubMembership_basic (0.84s) === RUN TestAccGithubMembership_importBasic --- PASS: TestAccGithubMembership_importBasic (0.53s) === RUN TestAccGithubRepositoryCollaborator_basic --- PASS: TestAccGithubRepositoryCollaborator_basic (0.64s) === RUN TestAccGithubRepositoryCollaborator_importBasic --- PASS: TestAccGithubRepositoryCollaborator_importBasic (0.74s) === RUN TestAccGithubRepository_basic --- PASS: TestAccGithubRepository_basic (1.54s) === RUN TestAccGithubRepository_importBasic --- PASS: TestAccGithubRepository_importBasic (0.77s) === RUN TestAccGithubTeamMembership_basic --- PASS: TestAccGithubTeamMembership_basic (1.59s) === RUN TestAccGithubTeamMembership_importBasic --- PASS: TestAccGithubTeamMembership_importBasic (0.95s) === RUN TestAccGithubTeamRepository_basic --- PASS: TestAccGithubTeamRepository_basic (1.45s) === RUN TestAccGithubTeamRepository_importBasic --- PASS: TestAccGithubTeamRepository_importBasic (0.75s) === RUN TestAccCheckGetPermissions --- PASS: TestAccCheckGetPermissions (0.00s) === RUN TestAccGithubTeam_basic --- PASS: TestAccGithubTeam_basic (0.79s) === RUN TestAccGithubTeam_importBasic --- PASS: TestAccGithubTeam_importBasic (0.54s) === RUN TestAccGithubUtilRole_validation --- PASS: TestAccGithubUtilRole_validation (0.00s) === RUN TestAccGithubUtilTwoPartID --- PASS: TestAccGithubUtilTwoPartID (0.00s) PASS ok github.com/hashicorp/terraform/builtin/providers/github 12.455s ```
2016-11-28 18:30:24 +01:00
ResourceName: "github_repository.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
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
gotRepo, _, err := conn.Repositories.Get(context.TODO(), org.name, repoName)
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
}
gotRepo, resp, err := conn.Repositories.Get(context.TODO(), orgName, rs.Primary.ID)
if err == nil {
if gotRepo != nil && *gotRepo.Name == rs.Primary.ID {
return fmt.Errorf("Repository %s/%s still exists", orgName, *gotRepo.Name)
}
}
if resp.StatusCode != 404 {
return err
}
return nil
}
return nil
}
func testAccGithubRepositoryConfig(randString string) string {
return fmt.Sprintf(`
resource "github_repository" "foo" {
name = "tf-acc-test-%s"
description = "Terraform acceptance tests %s"
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
}
`, randString, randString)
}
func testAccGithubRepositoryUpdateConfig(randString string) string {
return fmt.Sprintf(`
resource "github_repository" "foo" {
name = "tf-acc-test-%s"
description = "Updated Terraform acceptance tests %s"
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
}
`, randString, randString)
}