github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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 TestAccCheckGetPermissions(t *testing.T) {
    39  	pullMap := map[string]bool{"pull": true, "push": false, "admin": false}
    40  	pushMap := map[string]bool{"pull": true, "push": true, "admin": false}
    41  	adminMap := map[string]bool{"pull": true, "push": true, "admin": true}
    42  	errorMap := map[string]bool{"pull": false, "push": false, "admin": false}
    43  
    44  	pull, _ := getRepoPermission(&pullMap)
    45  	if pull != "pull" {
    46  		t.Fatalf("Expected pull permission, actual: %s", pull)
    47  	}
    48  
    49  	push, _ := getRepoPermission(&pushMap)
    50  	if push != "push" {
    51  		t.Fatalf("Expected push permission, actual: %s", push)
    52  	}
    53  
    54  	admin, _ := getRepoPermission(&adminMap)
    55  	if admin != "admin" {
    56  		t.Fatalf("Expected admin permission, actual: %s", admin)
    57  	}
    58  
    59  	errPerm, err := getRepoPermission(&errorMap)
    60  	if err == nil {
    61  		t.Fatalf("Expected an error getting permissions, actual: %v", errPerm)
    62  	}
    63  }
    64  
    65  func testAccCheckGithubTeamRepositoryRoleState(role string, repository *github.Repository) resource.TestCheckFunc {
    66  	return func(s *terraform.State) error {
    67  		resourceRole, err := getRepoPermission(repository.Permissions)
    68  		if err != nil {
    69  			return err
    70  		}
    71  
    72  		if resourceRole != role {
    73  			return fmt.Errorf("Team repository role %v in resource does match expected state of %v", resourceRole, role)
    74  		}
    75  		return nil
    76  	}
    77  }
    78  
    79  func testAccCheckGithubTeamRepositoryExists(n string, repository *github.Repository) resource.TestCheckFunc {
    80  	return func(s *terraform.State) error {
    81  		rs, ok := s.RootModule().Resources[n]
    82  		if !ok {
    83  			return fmt.Errorf("Not Found: %s", n)
    84  		}
    85  
    86  		if rs.Primary.ID == "" {
    87  			return fmt.Errorf("No team repository ID is set")
    88  		}
    89  
    90  		conn := testAccProvider.Meta().(*Organization).client
    91  		t, r := parseTwoPartID(rs.Primary.ID)
    92  
    93  		repo, _, err := conn.Organizations.IsTeamRepo(toGithubID(t),
    94  			testAccProvider.Meta().(*Organization).name, r)
    95  
    96  		if err != nil {
    97  			return err
    98  		}
    99  		*repository = *repo
   100  		return nil
   101  	}
   102  }
   103  
   104  func testAccCheckGithubTeamRepositoryDestroy(s *terraform.State) error {
   105  	conn := testAccProvider.Meta().(*Organization).client
   106  
   107  	for _, rs := range s.RootModule().Resources {
   108  		if rs.Type != "github_team_repository" {
   109  			continue
   110  		}
   111  		t, r := parseTwoPartID(rs.Primary.ID)
   112  
   113  		repo, resp, err := conn.Organizations.IsTeamRepo(toGithubID(t),
   114  			testAccProvider.Meta().(*Organization).name, r)
   115  
   116  		if err == nil {
   117  			if repo != nil &&
   118  				buildTwoPartID(&t, repo.Name) == rs.Primary.ID {
   119  				return fmt.Errorf("Team repository still exists")
   120  			}
   121  		}
   122  		if resp.StatusCode != 404 {
   123  			return err
   124  		}
   125  		return nil
   126  	}
   127  	return nil
   128  }
   129  
   130  var testAccGithubTeamRepositoryConfig string = fmt.Sprintf(`
   131  resource "github_team" "test_team" {
   132  	name = "foo"
   133  	description = "Terraform acc test group"
   134  }
   135  
   136  resource "github_team_repository" "test_team_test_repo" {
   137  	team_id = "${github_team.test_team.id}"
   138  	repository = "%s"
   139  	permission = "pull"
   140  }
   141  `, testRepo)
   142  
   143  var testAccGithubTeamRepositoryUpdateConfig string = fmt.Sprintf(`
   144  resource "github_team" "test_team" {
   145  	name = "foo"
   146  	description = "Terraform acc test group"
   147  }
   148  
   149  resource "github_team_repository" "test_team_test_repo" {
   150  	team_id = "${github_team.test_team.id}"
   151  	repository = "%s"
   152  	permission = "push"
   153  }
   154  `, testRepo)