github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/github/resource_github_repository_webhook_test.go (about)

     1  package github
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"reflect"
     7  	"strconv"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/google/go-github/github"
    12  	"github.com/hashicorp/terraform/helper/acctest"
    13  	"github.com/hashicorp/terraform/helper/resource"
    14  	"github.com/hashicorp/terraform/terraform"
    15  )
    16  
    17  func TestAccGithubRepositoryWebhook_basic(t *testing.T) {
    18  	randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
    19  	var hook github.Hook
    20  
    21  	resource.Test(t, resource.TestCase{
    22  		PreCheck:     func() { testAccPreCheck(t) },
    23  		Providers:    testAccProviders,
    24  		CheckDestroy: testAccCheckGithubRepositoryWebhookDestroy,
    25  		Steps: []resource.TestStep{
    26  			{
    27  				Config: testAccGithubRepositoryWebhookConfig(randString),
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckGithubRepositoryWebhookExists("github_repository_webhook.foo", fmt.Sprintf("foo-%s", randString), &hook),
    30  					testAccCheckGithubRepositoryWebhookAttributes(&hook, &testAccGithubRepositoryWebhookExpectedAttributes{
    31  						Name:   "web",
    32  						Events: []string{"pull_request"},
    33  						Configuration: map[string]interface{}{
    34  							"url":          "https://google.de/webhook",
    35  							"content_type": "json",
    36  							"insecure_ssl": "1",
    37  						},
    38  						Active: true,
    39  					}),
    40  				),
    41  			},
    42  			{
    43  				Config: testAccGithubRepositoryWebhookUpdateConfig(randString),
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckGithubRepositoryWebhookExists("github_repository_webhook.foo", fmt.Sprintf("foo-%s", randString), &hook),
    46  					testAccCheckGithubRepositoryWebhookAttributes(&hook, &testAccGithubRepositoryWebhookExpectedAttributes{
    47  						Name:   "web",
    48  						Events: []string{"issues"},
    49  						Configuration: map[string]interface{}{
    50  							"url":          "https://google.de/webhooks",
    51  							"content_type": "form",
    52  							"insecure_ssl": "0",
    53  						},
    54  						Active: false,
    55  					}),
    56  				),
    57  			},
    58  		},
    59  	})
    60  }
    61  
    62  func testAccCheckGithubRepositoryWebhookExists(n string, repoName string, hook *github.Hook) resource.TestCheckFunc {
    63  	return func(s *terraform.State) error {
    64  		rs, ok := s.RootModule().Resources[n]
    65  		if !ok {
    66  			return fmt.Errorf("Not Found: %s", n)
    67  		}
    68  
    69  		hookID, _ := strconv.Atoi(rs.Primary.ID)
    70  		if hookID == 0 {
    71  			return fmt.Errorf("No repository name is set")
    72  		}
    73  
    74  		org := testAccProvider.Meta().(*Organization)
    75  		conn := org.client
    76  		getHook, _, err := conn.Repositories.GetHook(context.TODO(), org.name, repoName, hookID)
    77  		if err != nil {
    78  			return err
    79  		}
    80  		*hook = *getHook
    81  		return nil
    82  	}
    83  }
    84  
    85  type testAccGithubRepositoryWebhookExpectedAttributes struct {
    86  	Name          string
    87  	Events        []string
    88  	Configuration map[string]interface{}
    89  	Active        bool
    90  }
    91  
    92  func testAccCheckGithubRepositoryWebhookAttributes(hook *github.Hook, want *testAccGithubRepositoryWebhookExpectedAttributes) resource.TestCheckFunc {
    93  	return func(s *terraform.State) error {
    94  
    95  		if *hook.Name != want.Name {
    96  			return fmt.Errorf("got hook %q; want %q", *hook.Name, want.Name)
    97  		}
    98  		if *hook.Active != want.Active {
    99  			return fmt.Errorf("got hook %t; want %t", *hook.Active, want.Active)
   100  		}
   101  		if !strings.HasPrefix(*hook.URL, "https://") {
   102  			return fmt.Errorf("got http URL %q; want to start with 'https://'", *hook.URL)
   103  		}
   104  		if !reflect.DeepEqual(hook.Events, want.Events) {
   105  			return fmt.Errorf("got hook events %q; want %q", hook.Events, want.Events)
   106  		}
   107  		if !reflect.DeepEqual(hook.Config, want.Configuration) {
   108  			return fmt.Errorf("got hook configuration %q; want %q", hook.Config, want.Configuration)
   109  		}
   110  
   111  		return nil
   112  	}
   113  }
   114  
   115  func testAccCheckGithubRepositoryWebhookDestroy(s *terraform.State) error {
   116  	conn := testAccProvider.Meta().(*Organization).client
   117  	orgName := testAccProvider.Meta().(*Organization).name
   118  
   119  	for _, rs := range s.RootModule().Resources {
   120  		if rs.Type != "github_repository_webhook" {
   121  			continue
   122  		}
   123  
   124  		id, err := strconv.Atoi(rs.Primary.ID)
   125  		if err != nil {
   126  			return err
   127  		}
   128  
   129  		gotHook, resp, err := conn.Repositories.GetHook(context.TODO(), orgName, rs.Primary.Attributes["repository"], id)
   130  		if err == nil {
   131  			if gotHook != nil && *gotHook.ID == id {
   132  				return fmt.Errorf("Webhook still exists")
   133  			}
   134  		}
   135  		if resp.StatusCode != 404 {
   136  			return err
   137  		}
   138  		return nil
   139  	}
   140  	return nil
   141  }
   142  
   143  func testAccGithubRepositoryWebhookConfig(randString string) string {
   144  	return fmt.Sprintf(`
   145      resource "github_repository" "foo" {
   146        name = "foo-%s"
   147        description = "Terraform acceptance tests"
   148        homepage_url = "http://example.com/"
   149  
   150        # So that acceptance tests can be run in a github organization
   151        # with no billing
   152        private = false
   153  
   154        has_issues = true
   155        has_wiki = true
   156        has_downloads = true
   157      }
   158  
   159      resource "github_repository_webhook" "foo" {
   160        depends_on = ["github_repository.foo"]
   161        repository = "foo-%s"
   162  
   163        name = "web"
   164        configuration {
   165          url = "https://google.de/webhook"
   166          content_type = "json"
   167          insecure_ssl = true
   168        }
   169  
   170        events = ["pull_request"]
   171      }
   172      `, randString, randString)
   173  }
   174  
   175  func testAccGithubRepositoryWebhookUpdateConfig(randString string) string {
   176  	return fmt.Sprintf(`
   177  resource "github_repository" "foo" {
   178    name = "foo-%s"
   179    description = "Terraform acceptance tests"
   180    homepage_url = "http://example.com/"
   181  
   182    # So that acceptance tests can be run in a github organization
   183    # with no billing
   184    private = false
   185  
   186    has_issues = true
   187    has_wiki = true
   188    has_downloads = true
   189  }
   190  
   191  resource "github_repository_webhook" "foo" {
   192    depends_on = ["github_repository.foo"]
   193    repository = "foo-%s"
   194  
   195    name = "web"
   196    configuration {
   197      url = "https://google.de/webhooks"
   198      content_type = "form"
   199      insecure_ssl = false
   200    }
   201    active = false
   202  
   203    events = ["issues"]
   204  }
   205  `, randString, randString)
   206  }