github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/github/resource_github_team_repository_test.go (about)

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