Skip to content

Commit

Permalink
chore: add recipients sweeper (#516)
Browse files Browse the repository at this point in the history
Adds
[Sweepers](https://developer.hashicorp.com/terraform/plugin/testing/acceptance-tests/sweepers)
to the project with an initial sweeper for recipients (which seem to
cause the most test failures/conflicts).

This also attempts to get all recipients named with a `test.` prefix
(`#test.` for Slack recipients) establishing the convention that if a
thing's name starts with `test.` is can be "swept".

- Closes #446
  • Loading branch information
jharley authored Jul 30, 2024
1 parent 33da866 commit ef618d2
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 251 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ jobs:
TF_ACC_TERRAFORM_VERSION: ${{ env.TERRAFORM_VERSION }}
run: go test -v -coverprofile=tf-coverage.txt -covermode=atomic ./internal/... ./honeycombio/...

- name: Cleanup Dangling Resources
if: ${{ always() }}
timeout-minutes: 5
env:
HONEYCOMB_API_KEY: ${{ secrets.HONEYCOMB_API_KEY }}
HONEYCOMB_KEY_ID: ${{ secrets.HONEYCOMB_KEY_ID }}
HONEYCOMB_KEY_SECRET: ${{ secrets.HONEYCOMB_KEY_SECRET }}
HONEYCOMB_DATASET: testacc
run: make sweep

- name: Generate Coverage Report
uses: codecov/[email protected]
with:
Expand Down Expand Up @@ -142,6 +152,17 @@ jobs:
TF_ACC_TERRAFORM_VERSION: ${{ env.TERRAFORM_VERSION }}
run: go test -v -coverprofile=tf-coverage.txt -covermode=atomic ./internal/... ./honeycombio/...

- name: Cleanup Dangling Resources
if: ${{ always() }}
timeout-minutes: 5
env:
HONEYCOMB_API_ENDPOINT: https://api.eu1.honeycomb.io
HONEYCOMB_API_KEY: ${{ secrets.HONEYCOMB_API_KEY_EU }}
HONEYCOMB_KEY_ID: ${{ secrets.HONEYCOMB_KEY_ID_EU }}
HONEYCOMB_KEY_SECRET: ${{ secrets.HONEYCOMB_KEY_SECRET_EU }}
HONEYCOMB_DATASET: testacc
run: make sweep

- name: Generate Coverage Report
uses: codecov/[email protected]
with:
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ linters:
- unconvert
- unparam
- unused
- vet
- govet

run:
# Prevent false positive timeouts in CI
Expand Down
33 changes: 9 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
.PHONY: build testacc lint sweep
default: testacc

build:
go build -o terraform-provider-honeycombio

testacc:
TF_ACC=1 go test -v ./...

fmt:
goimports -l -w .
go mod tidy
terraform fmt --recursive

# Terraform 0.13+ only: build the repository and install the provider in one of
# the local mirror directories following the new fileystem layout. Additionally,
# we have to specify a version.
#
# https://www.terraform.io/docs/commands/cli-config.html#implied-local-mirror-directories
# https://www.terraform.io/upgrade-guides/0-13.html#new-filesystem-layout-for-local-copies-of-providers

version = 99.0.0
os_arch = $(shell go env GOOS)_$(shell go env GOARCH)
provider_path = registry.terraform.io/honeycombio/honeycombio/$(version)/$(os_arch)/

install_macos:
go build -o terraform-provider-honeycombio_$(version)

mkdir -p ~/Library/Application\ Support/io.terraform/plugins/$(provider_path)
cp terraform-provider-honeycombio_$(version) ~/Library/Application\ Support/io.terraform/plugins/$(provider_path)
lint:
golangci-lint run

uninstall_macos:
rm -r ~/Library/Application\ Support/io.terraform/plugins/registry.terraform.io/honeycombio
sweep:
# the sweep flag requires a string to be passed, but it is not used
@echo "WARNING: This will destroy resources. Use only in development teams."
go test ./internal/provider -v -timeout 5m -sweep=env

.PHONY: build testacc install
2 changes: 1 addition & 1 deletion client/burn_alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestBurnAlerts(t *testing.T) {

c := newTestClient(t)
dataset := testDataset(t)
testAlertEmail := test.RandomString(8) + "@example.com"
testAlertEmail := test.RandomEmail()

sli, err := c.DerivedColumns.Create(ctx, dataset, &client.DerivedColumn{
Alias: test.RandomStringWithPrefix("test.", 8),
Expand Down
5 changes: 3 additions & 2 deletions client/query_annotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/honeycombio/terraform-provider-honeycombio/client"
"github.com/honeycombio/terraform-provider-honeycombio/internal/helper/test"
)

func TestQueryAnnotations(t *testing.T) {
Expand All @@ -33,7 +34,7 @@ func TestQueryAnnotations(t *testing.T) {

t.Run("Create", func(t *testing.T) {
data := &client.QueryAnnotation{
Name: "Query created by a test",
Name: test.RandomStringWithPrefix("test.", 20),
Description: "This derived column is created by a test",
QueryID: *query.ID,
}
Expand Down Expand Up @@ -62,7 +63,7 @@ func TestQueryAnnotations(t *testing.T) {
// change all the fields to test
data := &client.QueryAnnotation{
ID: queryAnnotation.ID,
Name: "This is a new name for the query created by a test",
Name: test.RandomStringWithPrefix("test.", 20),
Description: "This is a new description",
QueryID: *query.ID,
}
Expand Down
4 changes: 2 additions & 2 deletions client/recipient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestRecipientsEmail(t *testing.T) {
data := &client.Recipient{
Type: client.RecipientTypeEmail,
Details: client.RecipientDetails{
EmailAddress: test.RandomString(8) + "@example.com",
EmailAddress: test.RandomEmail(),
},
}
now := time.Now()
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestRecipientsEmail(t *testing.T) {
})

t.Run("Update", func(t *testing.T) {
rcpt.Details.EmailAddress = "[email protected]"
rcpt.Details.EmailAddress = test.RandomEmail()
now := time.Now()
result, err := c.Recipients.Update(ctx, rcpt)

Expand Down
11 changes: 7 additions & 4 deletions client/v2/api_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

hnyclient "github.com/honeycombio/terraform-provider-honeycombio/client"
"github.com/honeycombio/terraform-provider-honeycombio/internal/helper"
"github.com/honeycombio/terraform-provider-honeycombio/internal/helper/test"
)

func TestClient_APIKeys(t *testing.T) {
Expand All @@ -20,8 +21,9 @@ func TestClient_APIKeys(t *testing.T) {
env := newTestEnvironment(ctx, t, c)

// create a new key
keyName := test.RandomStringWithPrefix("test.", 10)
k, err := c.APIKeys.Create(ctx, &APIKey{
Name: helper.ToPtr("test key"),
Name: helper.ToPtr(keyName),
KeyType: "ingest",
Environment: &Environment{
ID: env.ID,
Expand All @@ -33,7 +35,7 @@ func TestClient_APIKeys(t *testing.T) {
require.NoError(t, err)
assert.NotEmpty(t, k.ID)
assert.NotEmpty(t, k.Secret)
assert.Equal(t, "test key", *k.Name)
assert.Equal(t, keyName, *k.Name)
assert.False(t, *k.Disabled)
assert.True(t, k.Permissions.CreateDatasets)

Expand All @@ -50,14 +52,15 @@ func TestClient_APIKeys(t *testing.T) {
assert.WithinDuration(t, k.Timestamps.UpdatedAt, key.Timestamps.UpdatedAt, 5*time.Second)

// update the key's name and disable it
keyName = test.RandomStringWithPrefix("test.", 10)
key, err = c.APIKeys.Update(ctx, &APIKey{
ID: k.ID,
Name: helper.ToPtr("new name"),
Name: helper.ToPtr(keyName),
Disabled: helper.ToPtr(true),
})
require.NoError(t, err)
assert.Equal(t, k.ID, key.ID)
assert.Equal(t, "new name", *key.Name)
assert.Equal(t, keyName, *key.Name)
assert.True(t, *key.Disabled)
assert.WithinDuration(t, time.Now(), key.Timestamps.UpdatedAt, time.Second)

Expand Down
66 changes: 32 additions & 34 deletions honeycombio/data_source_recipient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

honeycombio "github.com/honeycombio/terraform-provider-honeycombio/client"
"github.com/honeycombio/terraform-provider-honeycombio/internal/helper/test"
)

func TestAccDataSourceHoneycombioRecipient_basic(t *testing.T) {
Expand All @@ -21,53 +22,53 @@ func TestAccDataSourceHoneycombioRecipient_basic(t *testing.T) {
{
Type: honeycombio.RecipientTypeEmail,
Details: honeycombio.RecipientDetails{
EmailAddress: "[email protected]",
EmailAddress: test.RandomEmail(),
},
},
{
Type: honeycombio.RecipientTypeEmail,
Details: honeycombio.RecipientDetails{
EmailAddress: "[email protected]",
EmailAddress: test.RandomEmail(),
},
},
{
Type: honeycombio.RecipientTypeSlack,
Details: honeycombio.RecipientDetails{
SlackChannel: "#acctest",
SlackChannel: test.RandomStringWithPrefix("#test.", 12),
},
},
{
Type: honeycombio.RecipientTypeSlack,
Details: honeycombio.RecipientDetails{
SlackChannel: "#tmp-acctest",
SlackChannel: test.RandomStringWithPrefix("#test.", 12),
},
},
{
Type: honeycombio.RecipientTypePagerDuty,
Details: honeycombio.RecipientDetails{
PDIntegrationKey: "6f05176bf1c7a1adb6ee516521770ec4",
PDIntegrationName: "My Important Service",
PDIntegrationKey: test.RandomString(32),
PDIntegrationName: test.RandomStringWithPrefix("test.", 20),
},
},
{
Type: honeycombio.RecipientTypePagerDuty,
Details: honeycombio.RecipientDetails{
PDIntegrationKey: "6f05176bf1b7a1adb6ee516521770ac0",
PDIntegrationName: "My Other Important Service",
PDIntegrationKey: test.RandomString(32),
PDIntegrationName: test.RandomStringWithPrefix("test.", 20),
},
},
{
Type: honeycombio.RecipientTypeWebhook,
Details: honeycombio.RecipientDetails{
WebhookName: "My Notifications Hook",
WebhookSecret: "s0s3kret!",
WebhookName: test.RandomStringWithPrefix("test.", 16),
WebhookSecret: test.RandomString(20),
WebhookURL: "https://my.webhook.dev.corp.io",
},
},
{
Type: honeycombio.RecipientTypeMSTeams,
Details: honeycombio.RecipientDetails{
WebhookName: "My Teams Channel",
WebhookName: test.RandomStringWithPrefix("test.", 16),
WebhookURL: "https://outlook.office.com/webhook/12345",
},
},
Expand All @@ -92,57 +93,57 @@ func TestAccDataSourceHoneycombioRecipient_basic(t *testing.T) {
ProtoV5ProviderFactories: testAccProtoV5ProviderFactory,
Steps: []resource.TestStep{
{
Config: testAccRecipientWithDeprecatedTarget("email", "[email protected]"),
Check: resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "address", "[email protected]"),
Config: testAccRecipientWithDeprecatedTarget("email", testRecipients[0].Details.EmailAddress),
Check: resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "address", testRecipients[0].Details.EmailAddress),
},
{
Config: testAccRecipientWithDeprecatedTarget("slack", "#acctest"),
Check: resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "channel", "#acctest"),
Config: testAccRecipientWithDeprecatedTarget("slack", testRecipients[2].Details.SlackChannel),
Check: resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "channel", testRecipients[2].Details.SlackChannel),
},
{
Config: testAccRecipientWithDeprecatedTarget("email", "[email protected]"),
ExpectError: regexp.MustCompile("your recipient query returned no results."),
},
{
Config: testAccRecipientWithFilterValue("email", "address", "[email protected]"),
Check: resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "address", "[email protected]"),
Config: testAccRecipientWithFilterValue("email", "address", testRecipients[1].Details.EmailAddress),
Check: resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "address", testRecipients[1].Details.EmailAddress),
},
{
Config: testAccRecipientWithFilterValue("email", "address", "[email protected]"),
ExpectError: regexp.MustCompile("your recipient query returned no results."),
},
{
Config: testAccRecipientWithFilterValue("pagerduty", "integration_name", "My Important Service"),
Config: testAccRecipientWithFilterValue("pagerduty", "integration_name", testRecipients[4].Details.PDIntegrationName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "integration_name", "My Important Service"),
resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "integration_key", "6f05176bf1c7a1adb6ee516521770ec4"),
resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "integration_name", testRecipients[4].Details.PDIntegrationName),
resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "integration_key", testRecipients[4].Details.PDIntegrationKey),
),
},
{
Config: testAccRecipientWithFilterRegex("webhook", "url", ".*dev.corp.io"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "name", "My Notifications Hook"),
resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "secret", "s0s3kret!"),
resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "name", testRecipients[6].Details.WebhookName),
resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "secret", testRecipients[6].Details.WebhookSecret),
resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "url", "https://my.webhook.dev.corp.io"),
),
},
{
Config: testAccRecipientWithFilterValue("msteams", "name", "My Teams Channel"),
Config: testAccRecipientWithFilterValue("msteams", "name", testRecipients[7].Details.WebhookName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "name", "My Teams Channel"),
resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "name", testRecipients[7].Details.WebhookName),
resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "url", "https://outlook.office.com/webhook/12345"),
),
},
{
Config: testAccRecipientWithFilterValue("slack", "channel", "#tmp-acctest"),
Check: resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "channel", "#tmp-acctest"),
Config: testAccRecipientWithFilterValue("slack", "channel", testRecipients[3].Details.SlackChannel),
Check: resource.TestCheckResourceAttr("data.honeycombio_recipient.test", "channel", testRecipients[3].Details.SlackChannel),
},
{
Config: testAccRecipientWithFilterRegex("email", "address", "^acctest*"),
Config: testAccRecipientWithFilterRegex("email", "address", ".*@example.com"),
ExpectError: regexp.MustCompile("your recipient query returned more than one result. Please try a more specific search criteria."),
},
{
Config: testAccRecipientWithFilterRegex("pagerduty", "integration_name", "^.*Important Service$"),
Config: testAccRecipientWithFilterRegex("pagerduty", "integration_name", ".*"),
ExpectError: regexp.MustCompile("your recipient query returned more than one result. Please try a more specific search criteria."),
},
},
Expand All @@ -154,8 +155,7 @@ func testAccRecipientWithDeprecatedTarget(recipientType, target string) string {
data "honeycombio_recipient" "test" {
type = "%s"
target = "%s"
}
`, recipientType, target)
}`, recipientType, target)
}

func testAccRecipientWithFilterValue(recipientType, filterName, filterValue string) string {
Expand All @@ -167,8 +167,7 @@ data "honeycombio_recipient" "test" {
name = "%s"
value = "%s"
}
}
`, recipientType, filterName, filterValue)
}`, recipientType, filterName, filterValue)
}

func testAccRecipientWithFilterRegex(recipientType, filterName, filterRegex string) string {
Expand All @@ -180,6 +179,5 @@ data "honeycombio_recipient" "test" {
name = "%s"
value_regex = "%s"
}
}
`, recipientType, filterName, filterRegex)
}`, recipientType, filterName, filterRegex)
}
Loading

0 comments on commit ef618d2

Please sign in to comment.