github.com/jrperritt/terraform@v0.1.1-0.20170525065507-96f391dafc38/builtin/providers/gitlab/resource_gitlab_deploy_key_test.go (about)

     1  package gitlab
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	"github.com/xanzy/go-gitlab"
    12  )
    13  
    14  func TestAccGitlabDeployKey_basic(t *testing.T) {
    15  	var deployKey gitlab.DeployKey
    16  	rInt := acctest.RandInt()
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckGitlabDeployKeyDestroy,
    22  		Steps: []resource.TestStep{
    23  			// Create a project and deployKey with default options
    24  			{
    25  				Config: testAccGitlabDeployKeyConfig(rInt),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckGitlabDeployKeyExists("gitlab_deploy_key.foo", &deployKey),
    28  					testAccCheckGitlabDeployKeyAttributes(&deployKey, &testAccGitlabDeployKeyExpectedAttributes{
    29  						Title: fmt.Sprintf("deployKey-%d", rInt),
    30  						Key:   "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCj13ozEBZ0s4el4k6mYqoyIKKKMh9hHY0sAYqSPXs2zGuVFZss1P8TPuwmdXVjHR7TiRXwC49zDrkyWJgiufggYJ1VilOohcMOODwZEJz+E5q4GCfHuh90UEh0nl8B2R0Uoy0LPeg93uZzy0hlHApsxRf/XZJz/1ytkZvCtxdllxfImCVxJReMeRVEqFCTCvy3YuJn0bce7ulcTFRvtgWOpQsr6GDK8YkcCCv2eZthVlrEwy6DEpAKTRiRLGgUj4dPO0MmO4cE2qD4ualY01PhNORJ8Q++I+EtkGt/VALkecwFuBkl18/gy+yxNJHpKc/8WVVinDeFrd/HhiY9yU0d richardc@tamborine.example.1",
    31  					}),
    32  				),
    33  			},
    34  			// Update the project deployKey to toggle all the values to their inverse
    35  			{
    36  				Config: testAccGitlabDeployKeyUpdateConfig(rInt),
    37  				Check: resource.ComposeTestCheckFunc(
    38  					testAccCheckGitlabDeployKeyExists("gitlab_deploy_key.foo", &deployKey),
    39  					testAccCheckGitlabDeployKeyAttributes(&deployKey, &testAccGitlabDeployKeyExpectedAttributes{
    40  						Title: fmt.Sprintf("modifiedDeployKey-%d", rInt),
    41  						Key:   "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6pSke2kb7YBjo65xDKegbOQsAtnMupRcFxXji7L1iXivGwORq0qpC2xzbhez5jk1WgPckEaNv2/Bz0uEW6oSIXw1KT1VN2WzEUfQCbpNyZPtn4iV3nyl6VQW/Nd1SrxiFJtH1H4vu+eCo4McMXTjuBBD06fiJNrHaSw734LjQgqtXWJuVym9qS5MqraZB7wDwTQwSM6kslL7KTgmo3ONsTLdb2zZhv6CS+dcFKinQo7/ttTmeMuXGbPOVuNfT/bePVIN1MF1TislHa2L2dZdGeoynNJT4fVPjA2Xl6eHWh4ySbvnfPznASsjBhP0n/QKprYJ/5fQShdBYBcuQiIMd richardc@tamborine.example.2",
    42  					}),
    43  				),
    44  			},
    45  			// Update the project deployKey to toggle the options back
    46  			{
    47  				Config: testAccGitlabDeployKeyConfig(rInt),
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckGitlabDeployKeyExists("gitlab_deploy_key.foo", &deployKey),
    50  					testAccCheckGitlabDeployKeyAttributes(&deployKey, &testAccGitlabDeployKeyExpectedAttributes{
    51  						Title: fmt.Sprintf("deployKey-%d", rInt),
    52  						Key:   "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCj13ozEBZ0s4el4k6mYqoyIKKKMh9hHY0sAYqSPXs2zGuVFZss1P8TPuwmdXVjHR7TiRXwC49zDrkyWJgiufggYJ1VilOohcMOODwZEJz+E5q4GCfHuh90UEh0nl8B2R0Uoy0LPeg93uZzy0hlHApsxRf/XZJz/1ytkZvCtxdllxfImCVxJReMeRVEqFCTCvy3YuJn0bce7ulcTFRvtgWOpQsr6GDK8YkcCCv2eZthVlrEwy6DEpAKTRiRLGgUj4dPO0MmO4cE2qD4ualY01PhNORJ8Q++I+EtkGt/VALkecwFuBkl18/gy+yxNJHpKc/8WVVinDeFrd/HhiY9yU0d richardc@tamborine.example.1",
    53  					}),
    54  				),
    55  			},
    56  		},
    57  	})
    58  }
    59  
    60  func testAccCheckGitlabDeployKeyExists(n string, deployKey *gitlab.DeployKey) resource.TestCheckFunc {
    61  	return func(s *terraform.State) error {
    62  		rs, ok := s.RootModule().Resources[n]
    63  		if !ok {
    64  			return fmt.Errorf("Not Found: %s", n)
    65  		}
    66  
    67  		deployKeyID, err := strconv.Atoi(rs.Primary.ID)
    68  		if err != nil {
    69  			return err
    70  		}
    71  		repoName := rs.Primary.Attributes["project"]
    72  		if repoName == "" {
    73  			return fmt.Errorf("No project ID is set")
    74  		}
    75  		conn := testAccProvider.Meta().(*gitlab.Client)
    76  
    77  		gotDeployKey, _, err := conn.DeployKeys.GetDeployKey(repoName, deployKeyID)
    78  		if err != nil {
    79  			return err
    80  		}
    81  		*deployKey = *gotDeployKey
    82  		return nil
    83  	}
    84  }
    85  
    86  type testAccGitlabDeployKeyExpectedAttributes struct {
    87  	Title   string
    88  	Key     string
    89  	CanPush bool
    90  }
    91  
    92  func testAccCheckGitlabDeployKeyAttributes(deployKey *gitlab.DeployKey, want *testAccGitlabDeployKeyExpectedAttributes) resource.TestCheckFunc {
    93  	return func(s *terraform.State) error {
    94  		if deployKey.Title != want.Title {
    95  			return fmt.Errorf("got title %q; want %q", deployKey.Title, want.Title)
    96  		}
    97  
    98  		if deployKey.Key != want.Key {
    99  			return fmt.Errorf("got key %q; want %q", deployKey.Key, want.Key)
   100  		}
   101  
   102  		if deployKey.CanPush != nil && *deployKey.CanPush != want.CanPush {
   103  			return fmt.Errorf("got can_push %t; want %t", *deployKey.CanPush, want.CanPush)
   104  		}
   105  
   106  		return nil
   107  	}
   108  }
   109  
   110  func testAccCheckGitlabDeployKeyDestroy(s *terraform.State) error {
   111  	conn := testAccProvider.Meta().(*gitlab.Client)
   112  
   113  	for _, rs := range s.RootModule().Resources {
   114  		if rs.Type != "gitlab_project" {
   115  			continue
   116  		}
   117  		deployKeyID, err := strconv.Atoi(rs.Primary.ID)
   118  		project := rs.Primary.Attributes["project"]
   119  
   120  		gotDeployKey, resp, err := conn.DeployKeys.GetDeployKey(project, deployKeyID)
   121  		if err == nil {
   122  			if gotDeployKey != nil && fmt.Sprintf("%d", gotDeployKey.ID) == rs.Primary.ID {
   123  				return fmt.Errorf("Deploy key still exists")
   124  			}
   125  		}
   126  		if resp.StatusCode != 404 {
   127  			return err
   128  		}
   129  		return nil
   130  	}
   131  	return nil
   132  }
   133  
   134  func testAccGitlabDeployKeyConfig(rInt int) string {
   135  	return fmt.Sprintf(`
   136  resource "gitlab_project" "foo" {
   137    name = "foo-%d"
   138    description = "Terraform acceptance tests"
   139  
   140    # So that acceptance tests can be run in a gitlab organization
   141    # with no billing
   142    visibility_level = "public"
   143  }
   144  
   145  resource "gitlab_deploy_key" "foo" {
   146    project = "${gitlab_project.foo.id}"
   147    title = "deployKey-%d"
   148    key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCj13ozEBZ0s4el4k6mYqoyIKKKMh9hHY0sAYqSPXs2zGuVFZss1P8TPuwmdXVjHR7TiRXwC49zDrkyWJgiufggYJ1VilOohcMOODwZEJz+E5q4GCfHuh90UEh0nl8B2R0Uoy0LPeg93uZzy0hlHApsxRf/XZJz/1ytkZvCtxdllxfImCVxJReMeRVEqFCTCvy3YuJn0bce7ulcTFRvtgWOpQsr6GDK8YkcCCv2eZthVlrEwy6DEpAKTRiRLGgUj4dPO0MmO4cE2qD4ualY01PhNORJ8Q++I+EtkGt/VALkecwFuBkl18/gy+yxNJHpKc/8WVVinDeFrd/HhiY9yU0d richardc@tamborine.example.1"
   149  }
   150    `, rInt, rInt)
   151  }
   152  
   153  func testAccGitlabDeployKeyUpdateConfig(rInt int) string {
   154  	return fmt.Sprintf(`
   155  resource "gitlab_project" "foo" {
   156    name = "foo-%d"
   157    description = "Terraform acceptance tests"
   158  
   159    # So that acceptance tests can be run in a gitlab organization
   160    # with no billing
   161    visibility_level = "public"
   162  }
   163  
   164  resource "gitlab_deploy_key" "foo" {
   165    project = "${gitlab_project.foo.id}"
   166    title = "modifiedDeployKey-%d"
   167    key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6pSke2kb7YBjo65xDKegbOQsAtnMupRcFxXji7L1iXivGwORq0qpC2xzbhez5jk1WgPckEaNv2/Bz0uEW6oSIXw1KT1VN2WzEUfQCbpNyZPtn4iV3nyl6VQW/Nd1SrxiFJtH1H4vu+eCo4McMXTjuBBD06fiJNrHaSw734LjQgqtXWJuVym9qS5MqraZB7wDwTQwSM6kslL7KTgmo3ONsTLdb2zZhv6CS+dcFKinQo7/ttTmeMuXGbPOVuNfT/bePVIN1MF1TislHa2L2dZdGeoynNJT4fVPjA2Xl6eHWh4ySbvnfPznASsjBhP0n/QKprYJ/5fQShdBYBcuQiIMd richardc@tamborine.example.2"
   168  }
   169    `, rInt, rInt)
   170  }