Skip to content

Commit

Permalink
tests for variable removal
Browse files Browse the repository at this point in the history
  • Loading branch information
brookesargent committed Jan 7, 2025
1 parent 8fff1ab commit 3031847
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 6 deletions.
141 changes: 135 additions & 6 deletions internal/provider/burn_alert_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ func TestAcc_BurnAlertResource_exhaustionTimeBasicWebhookRecipient(t *testing.T)
Config: testAccConfigBurnAlertExhaustionTime_basicWebhookRecipient(exhaustionMinutes, dataset, sloID, "info"),
Check: testAccEnsureSuccessExhaustionTimeAlertWithWebhookRecip(t, burnAlert, exhaustionMinutes, sloID, "info"),
},
// Update - remove variables
{
Config: testAccConfigBurnAlertExhaustionTime_basicWebhookRecipient(exhaustionMinutes, dataset, sloID, ""),
Check: testAccEnsureSuccessExhaustionTimeAlertWithWebhookRecip(t, burnAlert, exhaustionMinutes, sloID, ""),
},
// Import
{
ResourceName: "honeycombio_burn_alert.test",
Expand Down Expand Up @@ -222,6 +227,11 @@ func TestAcc_BurnAlertResource_budgetRateBasicWebhookRecipient(t *testing.T) {
Config: testAccConfigBurnAlertBudgetRate_basicWebhookRecipient(budgetRateWindowMinutes, budgetRateDecreasePercent, dataset, sloID, "info"),
Check: testAccEnsureSuccessBudgetRateAlertWithWebhookRecip(t, burnAlert, budgetRateWindowMinutes, budgetRateDecreasePercent, sloID, "info"),
},
// Update - remove variables
{
Config: testAccConfigBurnAlertBudgetRate_basicWebhookRecipient(budgetRateWindowMinutes, budgetRateDecreasePercent, dataset, sloID, ""),
Check: testAccEnsureSuccessBudgetRateAlertWithWebhookRecip(t, burnAlert, budgetRateWindowMinutes, budgetRateDecreasePercent, sloID, ""),
},
// Import
{
ResourceName: "honeycombio_burn_alert.test",
Expand Down Expand Up @@ -618,6 +628,28 @@ func testAccEnsureSuccessExhaustionTimeAlert(t *testing.T, burnAlert *client.Bur

// Checks that the exhaustion time burn alert exists, has the correct attributes, and has the correct state
func testAccEnsureSuccessExhaustionTimeAlertWithWebhookRecip(t *testing.T, burnAlert *client.BurnAlert, exhaustionMinutes int, sloID, varValue string) resource.TestCheckFunc {
if varValue == "" {
return resource.ComposeAggregateTestCheckFunc(
// Check that the burn alert exists
testAccEnsureBurnAlertExists(t, "honeycombio_burn_alert.test", burnAlert),

// Check that the burn alert has the correct attributes
testAccEnsureAttributesCorrectExhaustionTime(burnAlert, exhaustionMinutes, sloID),

// Check that the burn alert has the correct values in state
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "slo_id", sloID),
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "description", testBADescription),
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "alert_type", "exhaustion_time"),
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "exhaustion_minutes", fmt.Sprintf("%d", exhaustionMinutes)),
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "recipient.#", "1"),
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "recipient.0.notification_details.#", "0"),

// Budget rate attributes should not be set
resource.TestCheckNoResourceAttr("honeycombio_burn_alert.test", "budget_rate_window_minutes"),
resource.TestCheckNoResourceAttr("honeycombio_burn_alert.test", "budget_rate_decrease_percent"),
)
}

return resource.ComposeAggregateTestCheckFunc(
// Check that the burn alert exists
testAccEnsureBurnAlertExists(t, "honeycombio_burn_alert.test", burnAlert),
Expand All @@ -632,6 +664,7 @@ func testAccEnsureSuccessExhaustionTimeAlertWithWebhookRecip(t *testing.T, burnA
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "exhaustion_minutes", fmt.Sprintf("%d", exhaustionMinutes)),
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "recipient.#", "1"),
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "recipient.0.notification_details.#", "1"),
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "recipient.0.notification_details.0.variable.#", "1"),
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "recipient.0.notification_details.0.variable.0.name", "severity"),
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "recipient.0.notification_details.0.variable.0.value", varValue),

Expand Down Expand Up @@ -666,6 +699,28 @@ func testAccEnsureSuccessBudgetRateAlert(t *testing.T, burnAlert *client.BurnAle

// Checks that the budget rate burn alert exists, has the correct attributes, and has the correct state
func testAccEnsureSuccessBudgetRateAlertWithWebhookRecip(t *testing.T, burnAlert *client.BurnAlert, budgetRateWindowMinutes int, budgetRateDecreasePercent float64, sloID, varValue string) resource.TestCheckFunc {
if varValue == "" {
return resource.ComposeAggregateTestCheckFunc(
// Check that the burn alert exists
testAccEnsureBurnAlertExists(t, "honeycombio_burn_alert.test", burnAlert),

// Check that the burn alert has the correct attributes
testAccEnsureAttributesCorrectBudgetRate(burnAlert, budgetRateWindowMinutes, budgetRateDecreasePercent, sloID),

// Check that the burn alert has the correct values in state
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "slo_id", sloID),
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "description", testBADescription),
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "alert_type", "budget_rate"),
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "budget_rate_window_minutes", fmt.Sprintf("%d", budgetRateWindowMinutes)),
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "budget_rate_decrease_percent", helper.FloatToPercentString(budgetRateDecreasePercent)),
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "recipient.#", "1"),
resource.TestCheckResourceAttr("honeycombio_burn_alert.test", "recipient.0.notification_details.#", "0"),

// Exhaustion time attributes should not be set
resource.TestCheckNoResourceAttr("honeycombio_burn_alert.test", "exhaustion_minutes"),
)
}

return resource.ComposeAggregateTestCheckFunc(
// Check that the burn alert exists
testAccEnsureBurnAlertExists(t, "honeycombio_burn_alert.test", burnAlert),
Expand Down Expand Up @@ -876,6 +931,42 @@ func testAccConfigBurnAlertExhaustionTime_basicWebhookRecipient(exhaustionMinute
"description": " {{ .Description }}",
}
EOT`

if variableValue == "" {
return fmt.Sprintf(`
resource "honeycombio_webhook_recipient" "test" {
name = "test"
url = "http://example.com"
header {
name = "Authorization"
value = "Bearer abc123"
}
variable {
name = "severity"
default_value = "critical"
}
template {
type = "exhaustion_time"
body = %[5]s
}
}
resource "honeycombio_burn_alert" "test" {
exhaustion_minutes = %[1]d
dataset = "%[2]s"
slo_id = "%[3]s"
description = "%[4]s"
recipient {
id = honeycombio_webhook_recipient.test.id
}
}`, exhaustionMinutes, dataset, sloID, testBADescription, tmplBody)
}

return fmt.Sprintf(`
resource "honeycombio_webhook_recipient" "test" {
name = "test"
Expand Down Expand Up @@ -906,13 +997,13 @@ resource "honeycombio_burn_alert" "test" {
recipient {
id = honeycombio_webhook_recipient.test.id
notification_details {
variable {
name = "severity"
value = "%[6]s"
}
}
variable {
name = "severity"
value = "%[6]s"
}
}
}
}`, exhaustionMinutes, dataset, sloID, testBADescription, tmplBody, variableValue)
}
Expand Down Expand Up @@ -1009,6 +1100,44 @@ func testAccConfigBurnAlertBudgetRate_basicWebhookRecipient(budgetRateWindowMinu
"description": " {{ .Description }}",
}
EOT`

if variableValue == "" {
return fmt.Sprintf(`
resource "honeycombio_webhook_recipient" "test" {
name = "test"
url = "http://example.com"
header {
name = "Authorization"
value = "Bearer abc123"
}
variable {
name = "severity"
default_value = "critical"
}
template {
type = "budget_rate"
body = %[6]s
}
}
resource "honeycombio_burn_alert" "test" {
alert_type = "budget_rate"
description = "%[5]s"
budget_rate_window_minutes = %[1]d
budget_rate_decrease_percent = %[2]s
dataset = "%[3]s"
slo_id = "%[4]s"
recipient {
id = honeycombio_webhook_recipient.test.id
}
}`, budgetRateWindowMinutes, helper.FloatToPercentString(budgetRateDecreasePercent), dataset, sloID, testBADescription, tmplBody)
}

return fmt.Sprintf(`
resource "honeycombio_webhook_recipient" "test" {
name = "test"
Expand Down
84 changes: 84 additions & 0 deletions internal/provider/trigger_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func TestAcc_TriggerResource(t *testing.T) {
resource.TestCheckResourceAttr("honeycombio_trigger.test", "frequency", "600"),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "recipient.#", "1"),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "recipient.0.notification_details.#", "1"),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "recipient.0.notification_details.0.variable.#", "1"),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "recipient.0.notification_details.0.variable.0.name", "severity"),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "recipient.0.notification_details.0.variable.0.value", "info"),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "threshold.0.exceeded_limit", "1"),
Expand All @@ -102,6 +103,33 @@ func TestAcc_TriggerResource(t *testing.T) {
// then update the variable value from info -> critical
{
Config: testAccConfigBasicTriggerTestWithWebhookRecip(dataset, name, "critical"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccEnsureTriggerExists(t, "honeycombio_trigger.test"),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "name", name),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "frequency", "600"),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "recipient.#", "1"),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "recipient.0.notification_details.#", "1"),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "recipient.0.notification_details.0.variable.#", "1"),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "recipient.0.notification_details.0.variable.0.name", "severity"),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "recipient.0.notification_details.0.variable.0.value", "critical"),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "threshold.0.exceeded_limit", "1"),
resource.TestCheckResourceAttrPair("honeycombio_trigger.test", "query_id", "honeycombio_query.test", "id"),
resource.TestCheckNoResourceAttr("honeycombio_trigger.test", "query_json"),
),
},
// remove variables
{
Config: testAccConfigBasicTriggerTestWithWebhookRecip(dataset, name, ""),
Check: resource.ComposeAggregateTestCheckFunc(
testAccEnsureTriggerExists(t, "honeycombio_trigger.test"),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "name", name),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "frequency", "600"),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "recipient.#", "1"),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "recipient.0.notification_details.#", "0"),
resource.TestCheckResourceAttr("honeycombio_trigger.test", "threshold.0.exceeded_limit", "1"),
resource.TestCheckResourceAttrPair("honeycombio_trigger.test", "query_id", "honeycombio_query.test", "id"),
resource.TestCheckNoResourceAttr("honeycombio_trigger.test", "query_json"),
),
},
{
ResourceName: "honeycombio_trigger.test",
Expand Down Expand Up @@ -881,6 +909,62 @@ func testAccConfigBasicTriggerTestWithWebhookRecip(dataset, name, varValue strin
}
EOT`

if varValue == "" {
return fmt.Sprintf(`
data "honeycombio_query_specification" "test" {
calculation {
op = "AVG"
column = "duration_ms"
}
time_range = 1200
}
resource "honeycombio_query" "test" {
dataset = "%[1]s"
query_json = data.honeycombio_query_specification.test.json
}
resource "honeycombio_webhook_recipient" "test" {
name = "test"
url = "http://example.com"
header {
name = "Authorization"
value = "Bearer abc123"
}
variable {
name = "severity"
default_value = "critical"
}
template {
type = "trigger"
body = %[3]s
}
}
resource "honeycombio_trigger" "test" {
name = "%[2]s"
dataset = "%[1]s"
description = "My nice description"
query_id = honeycombio_query.test.id
threshold {
op = ">"
value = 100
}
frequency = data.honeycombio_query_specification.test.time_range / 2
recipient {
id = honeycombio_webhook_recipient.test.id
}
}`, dataset, name, tmplBody)
}

return fmt.Sprintf(`
data "honeycombio_query_specification" "test" {
calculation {
Expand Down

0 comments on commit 3031847

Please sign in to comment.