From 3031847bf3289abfdcd6a8cf3051a5e59fcdd0e8 Mon Sep 17 00:00:00 2001 From: brookesargent Date: Tue, 7 Jan 2025 07:45:10 -0500 Subject: [PATCH] tests for variable removal --- internal/provider/burn_alert_resource_test.go | 141 +++++++++++++++++- internal/provider/trigger_resource_test.go | 84 +++++++++++ 2 files changed, 219 insertions(+), 6 deletions(-) diff --git a/internal/provider/burn_alert_resource_test.go b/internal/provider/burn_alert_resource_test.go index 92c9fb83..4566faa3 100644 --- a/internal/provider/burn_alert_resource_test.go +++ b/internal/provider/burn_alert_resource_test.go @@ -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", @@ -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", @@ -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), @@ -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), @@ -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), @@ -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" @@ -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) } @@ -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" diff --git a/internal/provider/trigger_resource_test.go b/internal/provider/trigger_resource_test.go index 1f54dbd3..b665f0cc 100644 --- a/internal/provider/trigger_resource_test.go +++ b/internal/provider/trigger_resource_test.go @@ -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"), @@ -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", @@ -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 {