github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/github/resource_github_repository_test.go (about)

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