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