opentofu/builtin/providers/github/resource_github_organization_webhook_test.go
Raphael Randschau 6991ceb2e2 provider/github: add repository_webhook resource (#12924)
* provider/github: add repository_webhook resource

`repository_webhook` can be used to manage webhooks for repositories.
It is currently limited to organization repositories only.

The changeset includes both documentation and tests.
The tests are green:

```
make testacc TEST=./builtin/providers/github
TESTARGS='-run=TestAccGithubRepositoryWebhook_basic'
==> Checking that code complies with gofmt requirements...
go generate $(go list ./... | grep -v /terraform/vendor/)
2017/03/21 16:20:07 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/github -v
-run=TestAccGithubRepositoryWebhook_basic -timeout 120m
=== RUN   TestAccGithubRepositoryWebhook_basic
--- PASS: TestAccGithubRepositoryWebhook_basic (5.10s)
PASS
ok      github.com/hashicorp/terraform/builtin/providers/github    5.113s
```

* provider/github: add github_organization_webhook

the `github_organization_webhook` resource is similar to the
`github_repository_webhook` resource, but it manages webhooks for an
organization.

the tests are green:

```
make testacc TEST=./builtin/providers/github
TESTARGS='-run=TestAccGithubOrganizationWebhook'
==> Checking that code complies with gofmt requirements...
go generate $(go list ./... | grep -v /terraform/vendor/)
2017/03/21 17:23:33 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/github -v
-run=TestAccGithubOrganizationWebhook -timeout 120m
=== RUN   TestAccGithubOrganizationWebhook_basic
--- PASS: TestAccGithubOrganizationWebhook_basic (2.09s)
PASS
ok      github.com/hashicorp/terraform/builtin/providers/github    2.109s
```
2017-03-23 11:21:45 +02:00

167 lines
4.4 KiB
Go

package github
import (
"context"
"fmt"
"reflect"
"strconv"
"strings"
"testing"
"github.com/google/go-github/github"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccGithubOrganizationWebhook_basic(t *testing.T) {
var hook github.Hook
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGithubOrganizationWebhookDestroy,
Steps: []resource.TestStep{
{
Config: testAccGithubOrganizationWebhookConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubOrganizationWebhookExists("github_organization_webhook.foo", &hook),
testAccCheckGithubOrganizationWebhookAttributes(&hook, &testAccGithubOrganizationWebhookExpectedAttributes{
Name: "web",
Events: []string{"pull_request"},
Configuration: map[string]interface{}{
"url": "https://google.de/webhook",
"content_type": "json",
"insecure_ssl": "1",
},
Active: true,
}),
),
},
{
Config: testAccGithubOrganizationWebhookUpdateConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubOrganizationWebhookExists("github_organization_webhook.foo", &hook),
testAccCheckGithubOrganizationWebhookAttributes(&hook, &testAccGithubOrganizationWebhookExpectedAttributes{
Name: "web",
Events: []string{"issues"},
Configuration: map[string]interface{}{
"url": "https://google.de/webhooks",
"content_type": "form",
"insecure_ssl": "0",
},
Active: false,
}),
),
},
},
})
}
func testAccCheckGithubOrganizationWebhookExists(n string, hook *github.Hook) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not Found: %s", n)
}
hookID, _ := strconv.Atoi(rs.Primary.ID)
if hookID == 0 {
return fmt.Errorf("No repository name is set")
}
org := testAccProvider.Meta().(*Organization)
conn := org.client
getHook, _, err := conn.Organizations.GetHook(context.TODO(), org.name, hookID)
if err != nil {
return err
}
*hook = *getHook
return nil
}
}
type testAccGithubOrganizationWebhookExpectedAttributes struct {
Name string
Events []string
Configuration map[string]interface{}
Active bool
}
func testAccCheckGithubOrganizationWebhookAttributes(hook *github.Hook, want *testAccGithubOrganizationWebhookExpectedAttributes) resource.TestCheckFunc {
return func(s *terraform.State) error {
if *hook.Name != want.Name {
return fmt.Errorf("got hook %q; want %q", *hook.Name, want.Name)
}
if *hook.Active != want.Active {
return fmt.Errorf("got hook %t; want %t", *hook.Active, want.Active)
}
if !strings.HasPrefix(*hook.URL, "https://") {
return fmt.Errorf("got http URL %q; want to start with 'https://'", *hook.URL)
}
if !reflect.DeepEqual(hook.Events, want.Events) {
return fmt.Errorf("got hook events %q; want %q", hook.Events, want.Events)
}
if !reflect.DeepEqual(hook.Config, want.Configuration) {
return fmt.Errorf("got hook configuration %q; want %q", hook.Config, want.Configuration)
}
return nil
}
}
func testAccCheckGithubOrganizationWebhookDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*Organization).client
orgName := testAccProvider.Meta().(*Organization).name
for _, rs := range s.RootModule().Resources {
if rs.Type != "github_organization_webhook" {
continue
}
id, err := strconv.Atoi(rs.Primary.ID)
if err != nil {
return err
}
gotHook, resp, err := conn.Organizations.GetHook(context.TODO(), orgName, id)
if err == nil {
if gotHook != nil && *gotHook.ID == id {
return fmt.Errorf("Webhook still exists")
}
}
if resp.StatusCode != 404 {
return err
}
return nil
}
return nil
}
const testAccGithubOrganizationWebhookConfig = `
resource "github_organization_webhook" "foo" {
name = "web"
configuration {
url = "https://google.de/webhook"
content_type = "json"
insecure_ssl = true
}
events = ["pull_request"]
}
`
const testAccGithubOrganizationWebhookUpdateConfig = `
resource "github_organization_webhook" "foo" {
name = "web"
configuration {
url = "https://google.de/webhooks"
content_type = "form"
insecure_ssl = false
}
active = false
events = ["issues"]
}
`