github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/gitlab/resource_gitlab_project_hook_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 TestAccGitlabProjectHook_basic(t *testing.T) {
    15  	var hook gitlab.ProjectHook
    16  	rInt := acctest.RandInt()
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckGitlabProjectHookDestroy,
    22  		Steps: []resource.TestStep{
    23  			// Create a project and hook with default options
    24  			{
    25  				Config: testAccGitlabProjectHookConfig(rInt),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckGitlabProjectHookExists("gitlab_project_hook.foo", &hook),
    28  					testAccCheckGitlabProjectHookAttributes(&hook, &testAccGitlabProjectHookExpectedAttributes{
    29  						URL:                   fmt.Sprintf("https://example.com/hook-%d", rInt),
    30  						PushEvents:            true,
    31  						EnableSSLVerification: true,
    32  					}),
    33  				),
    34  			},
    35  			// Update the project hook to toggle all the values to their inverse
    36  			{
    37  				Config: testAccGitlabProjectHookUpdateConfig(rInt),
    38  				Check: resource.ComposeTestCheckFunc(
    39  					testAccCheckGitlabProjectHookExists("gitlab_project_hook.foo", &hook),
    40  					testAccCheckGitlabProjectHookAttributes(&hook, &testAccGitlabProjectHookExpectedAttributes{
    41  						URL:                   fmt.Sprintf("https://example.com/hook-%d", rInt),
    42  						PushEvents:            false,
    43  						IssuesEvents:          true,
    44  						MergeRequestsEvents:   true,
    45  						TagPushEvents:         true,
    46  						NoteEvents:            true,
    47  						BuildEvents:           true,
    48  						PipelineEvents:        true,
    49  						WikiPageEvents:        true,
    50  						EnableSSLVerification: false,
    51  					}),
    52  				),
    53  			},
    54  			// Update the project hook to toggle the options back
    55  			{
    56  				Config: testAccGitlabProjectHookConfig(rInt),
    57  				Check: resource.ComposeTestCheckFunc(
    58  					testAccCheckGitlabProjectHookExists("gitlab_project_hook.foo", &hook),
    59  					testAccCheckGitlabProjectHookAttributes(&hook, &testAccGitlabProjectHookExpectedAttributes{
    60  						URL:                   fmt.Sprintf("https://example.com/hook-%d", rInt),
    61  						PushEvents:            true,
    62  						EnableSSLVerification: true,
    63  					}),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func testAccCheckGitlabProjectHookExists(n string, hook *gitlab.ProjectHook) resource.TestCheckFunc {
    71  	return func(s *terraform.State) error {
    72  		rs, ok := s.RootModule().Resources[n]
    73  		if !ok {
    74  			return fmt.Errorf("Not Found: %s", n)
    75  		}
    76  
    77  		hookID, err := strconv.Atoi(rs.Primary.ID)
    78  		if err != nil {
    79  			return err
    80  		}
    81  		repoName := rs.Primary.Attributes["project"]
    82  		if repoName == "" {
    83  			return fmt.Errorf("No project ID is set")
    84  		}
    85  		conn := testAccProvider.Meta().(*gitlab.Client)
    86  
    87  		gotHook, _, err := conn.Projects.GetProjectHook(repoName, hookID)
    88  		if err != nil {
    89  			return err
    90  		}
    91  		*hook = *gotHook
    92  		return nil
    93  	}
    94  }
    95  
    96  type testAccGitlabProjectHookExpectedAttributes struct {
    97  	URL                   string
    98  	PushEvents            bool
    99  	IssuesEvents          bool
   100  	MergeRequestsEvents   bool
   101  	TagPushEvents         bool
   102  	NoteEvents            bool
   103  	BuildEvents           bool
   104  	PipelineEvents        bool
   105  	WikiPageEvents        bool
   106  	EnableSSLVerification bool
   107  }
   108  
   109  func testAccCheckGitlabProjectHookAttributes(hook *gitlab.ProjectHook, want *testAccGitlabProjectHookExpectedAttributes) resource.TestCheckFunc {
   110  	return func(s *terraform.State) error {
   111  		if hook.URL != want.URL {
   112  			return fmt.Errorf("got url %q; want %q", hook.URL, want.URL)
   113  		}
   114  
   115  		if hook.EnableSSLVerification != want.EnableSSLVerification {
   116  			return fmt.Errorf("got enable_ssl_verification %t; want %t", hook.EnableSSLVerification, want.EnableSSLVerification)
   117  		}
   118  
   119  		if hook.PushEvents != want.PushEvents {
   120  			return fmt.Errorf("got push_events %t; want %t", hook.PushEvents, want.PushEvents)
   121  		}
   122  
   123  		if hook.IssuesEvents != want.IssuesEvents {
   124  			return fmt.Errorf("got issues_events %t; want %t", hook.IssuesEvents, want.IssuesEvents)
   125  		}
   126  
   127  		if hook.MergeRequestsEvents != want.MergeRequestsEvents {
   128  			return fmt.Errorf("got merge_requests_events %t; want %t", hook.MergeRequestsEvents, want.MergeRequestsEvents)
   129  		}
   130  
   131  		if hook.TagPushEvents != want.TagPushEvents {
   132  			return fmt.Errorf("got tag_push_events %t; want %t", hook.TagPushEvents, want.TagPushEvents)
   133  		}
   134  
   135  		if hook.NoteEvents != want.NoteEvents {
   136  			return fmt.Errorf("got note_events %t; want %t", hook.NoteEvents, want.NoteEvents)
   137  		}
   138  
   139  		if hook.BuildEvents != want.BuildEvents {
   140  			return fmt.Errorf("got build_events %t; want %t", hook.BuildEvents, want.BuildEvents)
   141  		}
   142  
   143  		if hook.PipelineEvents != want.PipelineEvents {
   144  			return fmt.Errorf("got pipeline_events %t; want %t", hook.PipelineEvents, want.PipelineEvents)
   145  		}
   146  
   147  		if hook.WikiPageEvents != want.WikiPageEvents {
   148  			return fmt.Errorf("got wiki_page_events %t; want %t", hook.WikiPageEvents, want.WikiPageEvents)
   149  		}
   150  
   151  		return nil
   152  	}
   153  }
   154  
   155  func testAccCheckGitlabProjectHookDestroy(s *terraform.State) error {
   156  	conn := testAccProvider.Meta().(*gitlab.Client)
   157  
   158  	for _, rs := range s.RootModule().Resources {
   159  		if rs.Type != "gitlab_project" {
   160  			continue
   161  		}
   162  
   163  		gotRepo, resp, err := conn.Projects.GetProject(rs.Primary.ID)
   164  		if err == nil {
   165  			if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
   166  				return fmt.Errorf("Repository still exists")
   167  			}
   168  		}
   169  		if resp.StatusCode != 404 {
   170  			return err
   171  		}
   172  		return nil
   173  	}
   174  	return nil
   175  }
   176  
   177  func testAccGitlabProjectHookConfig(rInt int) string {
   178  	return fmt.Sprintf(`
   179  resource "gitlab_project" "foo" {
   180    name = "foo-%d"
   181    description = "Terraform acceptance tests"
   182  
   183    # So that acceptance tests can be run in a gitlab organization
   184    # with no billing
   185    visibility_level = "public"
   186  }
   187  
   188  resource "gitlab_project_hook" "foo" {
   189  	project = "${gitlab_project.foo.id}"
   190  	url = "https://example.com/hook-%d"
   191  }
   192  	`, rInt, rInt)
   193  }
   194  
   195  func testAccGitlabProjectHookUpdateConfig(rInt int) string {
   196  	return fmt.Sprintf(`
   197  resource "gitlab_project" "foo" {
   198    name = "foo-%d"
   199    description = "Terraform acceptance tests"
   200  
   201    # So that acceptance tests can be run in a gitlab organization
   202    # with no billing
   203    visibility_level = "public"
   204  }
   205  
   206  resource "gitlab_project_hook" "foo" {
   207  	project = "${gitlab_project.foo.id}"
   208  	url = "https://example.com/hook-%d"
   209  	enable_ssl_verification = false
   210  	push_events = false
   211  	issues_events = true
   212  	merge_requests_events = true
   213  	tag_push_events = true
   214  	note_events = true
   215  	build_events = true
   216  	pipeline_events = true
   217  	wiki_page_events = true
   218  }
   219  	`, rInt, rInt)
   220  }