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

     1  package github
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/google/go-github/github"
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccGithubTeamRepository_basic(t *testing.T) {
    15  	var repository github.Repository
    16  	randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckGithubTeamRepositoryDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: testAccGithubTeamRepositoryConfig(randString, testRepo),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckGithubTeamRepositoryExists("github_team_repository.test_team_test_repo", &repository),
    27  					testAccCheckGithubTeamRepositoryRoleState("pull", &repository),
    28  				),
    29  			},
    30  			{
    31  				Config: testAccGithubTeamRepositoryUpdateConfig(randString, testRepo),
    32  				Check: resource.ComposeTestCheckFunc(
    33  					testAccCheckGithubTeamRepositoryExists("github_team_repository.test_team_test_repo", &repository),
    34  					testAccCheckGithubTeamRepositoryRoleState("push", &repository),
    35  				),
    36  			},
    37  		},
    38  	})
    39  }
    40  
    41  func TestAccGithubTeamRepository_importBasic(t *testing.T) {
    42  	randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
    43  
    44  	resource.Test(t, resource.TestCase{
    45  		PreCheck:     func() { testAccPreCheck(t) },
    46  		Providers:    testAccProviders,
    47  		CheckDestroy: testAccCheckGithubTeamRepositoryDestroy,
    48  		Steps: []resource.TestStep{
    49  			{
    50  				Config: testAccGithubTeamRepositoryConfig(randString, testRepo),
    51  			},
    52  			{
    53  				ResourceName:      "github_team_repository.test_team_test_repo",
    54  				ImportState:       true,
    55  				ImportStateVerify: true,
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func TestAccCheckGetPermissions(t *testing.T) {
    62  	pullMap := map[string]bool{"pull": true, "push": false, "admin": false}
    63  	pushMap := map[string]bool{"pull": true, "push": true, "admin": false}
    64  	adminMap := map[string]bool{"pull": true, "push": true, "admin": true}
    65  	errorMap := map[string]bool{"pull": false, "push": false, "admin": false}
    66  
    67  	pull, _ := getRepoPermission(&pullMap)
    68  	if pull != "pull" {
    69  		t.Fatalf("Expected pull permission, actual: %s", pull)
    70  	}
    71  
    72  	push, _ := getRepoPermission(&pushMap)
    73  	if push != "push" {
    74  		t.Fatalf("Expected push permission, actual: %s", push)
    75  	}
    76  
    77  	admin, _ := getRepoPermission(&adminMap)
    78  	if admin != "admin" {
    79  		t.Fatalf("Expected admin permission, actual: %s", admin)
    80  	}
    81  
    82  	errPerm, err := getRepoPermission(&errorMap)
    83  	if err == nil {
    84  		t.Fatalf("Expected an error getting permissions, actual: %v", errPerm)
    85  	}
    86  }
    87  
    88  func testAccCheckGithubTeamRepositoryRoleState(role string, repository *github.Repository) resource.TestCheckFunc {
    89  	return func(s *terraform.State) error {
    90  		resourceRole, err := getRepoPermission(repository.Permissions)
    91  		if err != nil {
    92  			return err
    93  		}
    94  
    95  		if resourceRole != role {
    96  			return fmt.Errorf("Team repository role %v in resource does match expected state of %v", resourceRole, role)
    97  		}
    98  		return nil
    99  	}
   100  }
   101  
   102  func testAccCheckGithubTeamRepositoryExists(n string, repository *github.Repository) resource.TestCheckFunc {
   103  	return func(s *terraform.State) error {
   104  		rs, ok := s.RootModule().Resources[n]
   105  		if !ok {
   106  			return fmt.Errorf("Not Found: %s", n)
   107  		}
   108  
   109  		if rs.Primary.ID == "" {
   110  			return fmt.Errorf("No team repository ID is set")
   111  		}
   112  
   113  		conn := testAccProvider.Meta().(*Organization).client
   114  		t, r := parseTwoPartID(rs.Primary.ID)
   115  
   116  		repo, _, err := conn.Organizations.IsTeamRepo(context.TODO(),
   117  			toGithubID(t),
   118  			testAccProvider.Meta().(*Organization).name, r)
   119  
   120  		if err != nil {
   121  			return err
   122  		}
   123  		*repository = *repo
   124  		return nil
   125  	}
   126  }
   127  
   128  func testAccCheckGithubTeamRepositoryDestroy(s *terraform.State) error {
   129  	conn := testAccProvider.Meta().(*Organization).client
   130  
   131  	for _, rs := range s.RootModule().Resources {
   132  		if rs.Type != "github_team_repository" {
   133  			continue
   134  		}
   135  		t, r := parseTwoPartID(rs.Primary.ID)
   136  
   137  		repo, resp, err := conn.Organizations.IsTeamRepo(context.TODO(),
   138  			toGithubID(t),
   139  			testAccProvider.Meta().(*Organization).name, r)
   140  
   141  		if err == nil {
   142  			if repo != nil &&
   143  				buildTwoPartID(&t, repo.Name) == rs.Primary.ID {
   144  				return fmt.Errorf("Team repository still exists")
   145  			}
   146  		}
   147  		if resp.StatusCode != 404 {
   148  			return err
   149  		}
   150  		return nil
   151  	}
   152  	return nil
   153  }
   154  
   155  func testAccGithubTeamRepositoryConfig(randString, repoName string) string {
   156  	return fmt.Sprintf(`
   157  resource "github_team" "test_team" {
   158  	name = "tf-acc-test-team-repo-%s"
   159  	description = "Terraform acc test group"
   160  }
   161  
   162  resource "github_team_repository" "test_team_test_repo" {
   163  	team_id = "${github_team.test_team.id}"
   164  	repository = "%s"
   165  	permission = "pull"
   166  }
   167  `, randString, repoName)
   168  }
   169  
   170  func testAccGithubTeamRepositoryUpdateConfig(randString, repoName string) string {
   171  	return fmt.Sprintf(`
   172  resource "github_team" "test_team" {
   173  	name = "tf-acc-test-team-repo-%s"
   174  	description = "Terraform acc test group"
   175  }
   176  
   177  resource "github_team_repository" "test_team_test_repo" {
   178  	team_id = "${github_team.test_team.id}"
   179  	repository = "%s"
   180  	permission = "push"
   181  }
   182  `, randString, repoName)
   183  }