Skip to content

Commit

Permalink
r/burn_alert: Schema validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lafentres committed Nov 15, 2023
1 parent ce93a96 commit 215ddc0
Showing 1 changed file with 148 additions and 0 deletions.
148 changes: 148 additions & 0 deletions internal/provider/burn_alert_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,105 @@ func TestAcc_BurnAlertResource_Import_validateImportID(t *testing.T) {
})
}

func TestAcc_BurnAlertResource_validateDefault(t *testing.T) {
dataset, sloID := burnAlertAccTestSetup(t)

resource.Test(t, resource.TestCase{
PreCheck: testAccPreCheck(t),
ProtoV5ProviderFactories: testAccProtoV5MuxServerFactory,
CheckDestroy: testAccEnsureBurnAlertDestroyed(t),
Steps: []resource.TestStep{
{
Config: testAccConfigBurnAlertDefault_basic(-1, dataset, sloID, "info"),
ExpectError: regexp.MustCompile(`exhaustion_minutes value must be at least`),
},
{
Config: testAccConfigBurnAlertDefault_validateAttributesWhenAlertTypeIsExhaustionTime(dataset, sloID),
ExpectError: regexp.MustCompile(`argument "exhaustion_minutes" is required`),
},
{
Config: testAccConfigBurnAlertDefault_validateAttributesWhenAlertTypeIsExhaustionTime(dataset, sloID),
ExpectError: regexp.MustCompile(`"budget_rate_window_minutes": must not be configured when "alert_type"`),
},
{
Config: testAccConfigBurnAlertDefault_validateAttributesWhenAlertTypeIsExhaustionTime(dataset, sloID),
ExpectError: regexp.MustCompile(`"budget_rate_decrease_percent": must not be configured when`),
},
},
})
}

func TestAcc_BurnAlertResource_validateExhaustionTime(t *testing.T) {
dataset, sloID := burnAlertAccTestSetup(t)

resource.Test(t, resource.TestCase{
PreCheck: testAccPreCheck(t),
ProtoV5ProviderFactories: testAccProtoV5MuxServerFactory,
CheckDestroy: testAccEnsureBurnAlertDestroyed(t),
Steps: []resource.TestStep{
{
Config: testAccConfigBurnAlertExhaustionTime_basic(-1, dataset, sloID, "info"),
ExpectError: regexp.MustCompile(`exhaustion_minutes value must be at least`),
},
{
Config: testAccConfigBurnAlertExhaustionTime_validateAttributesWhenAlertTypeIsExhaustionTime(dataset, sloID),
ExpectError: regexp.MustCompile(`argument "exhaustion_minutes" is required`),
},
{
Config: testAccConfigBurnAlertExhaustionTime_validateAttributesWhenAlertTypeIsExhaustionTime(dataset, sloID),
ExpectError: regexp.MustCompile(`"budget_rate_window_minutes": must not be configured when "alert_type"`),
},
{
Config: testAccConfigBurnAlertExhaustionTime_validateAttributesWhenAlertTypeIsExhaustionTime(dataset, sloID),
ExpectError: regexp.MustCompile(`"budget_rate_decrease_percent": must not be configured when`),
},
},
})
}

func TestAcc_BurnAlertResource_validateBudgetRate(t *testing.T) {
dataset, sloID := burnAlertAccTestSetup(t)

budgetRateWindowMinutes := 60
budgetRateDecreasePercent := float64(1)

resource.Test(t, resource.TestCase{
PreCheck: testAccPreCheck(t),
ProtoV5ProviderFactories: testAccProtoV5MuxServerFactory,
CheckDestroy: testAccEnsureBurnAlertDestroyed(t),
Steps: []resource.TestStep{
{
Config: testAccConfigBurnAlertBudgetRate_basic(0, budgetRateDecreasePercent, dataset, sloID, "info"),
ExpectError: regexp.MustCompile(`budget_rate_window_minutes value must be at least`),
},
{
Config: testAccConfigBurnAlertBudgetRate_basic(budgetRateWindowMinutes, float64(0), dataset, sloID, "info"),
ExpectError: regexp.MustCompile(`budget_rate_decrease_percent value must be at least`),
},
{
Config: testAccConfigBurnAlertBudgetRate_basic(budgetRateWindowMinutes, float64(200), dataset, sloID, "info"),
ExpectError: regexp.MustCompile(`budget_rate_decrease_percent value must be at most`),
},
{
Config: testAccConfigBurnAlertBudgetRate_basic(budgetRateWindowMinutes, float64(1.123456789), dataset, sloID, "info"),
ExpectError: regexp.MustCompile(`budget_rate_decrease_percent precision for value must be at most`),
},
{
Config: testAccConfigBurnAlertBudgetRate_validateAttributesWhenAlertTypeIsBudgetRate(dataset, sloID),
ExpectError: regexp.MustCompile(`argument "budget_rate_decrease_percent" is required`),
},
{
Config: testAccConfigBurnAlertBudgetRate_validateAttributesWhenAlertTypeIsBudgetRate(dataset, sloID),
ExpectError: regexp.MustCompile(`argument "budget_rate_window_minutes" is required`),
},
{
Config: testAccConfigBurnAlertBudgetRate_validateAttributesWhenAlertTypeIsBudgetRate(dataset, sloID),
ExpectError: regexp.MustCompile(`"exhaustion_minutes": must not be configured when "alert_type"`),
},
},
})
}

// Checks that the exhaustion time burn alert exists, has the correct attributes, and has the correct state
func testAccEnsureSuccessExhaustionTimeAlert(t *testing.T, burnAlert *client.BurnAlert, exhaustionMinutes int, pagerdutySeverity, sloID string) resource.TestCheckFunc {
return resource.ComposeAggregateTestCheckFunc(
Expand Down Expand Up @@ -466,6 +565,22 @@ resource "honeycombio_burn_alert" "test" {
}`, exhaustionMinutes, dataset, sloID, pdseverity)
}

func testAccConfigBurnAlertDefault_validateAttributesWhenAlertTypeIsExhaustionTime(dataset, sloID string) string {
return fmt.Sprintf(`
resource "honeycombio_burn_alert" "test" {
budget_rate_window_minutes = 60
budget_rate_decrease_percent = 1
dataset = "%[1]s"
slo_id = "%[2]s"
recipient {
type = "email"
target = "[email protected]"
}
}`, dataset, sloID)
}

func testAccConfigBurnAlertExhaustionTime_basic(exhaustionMinutes int, dataset, sloID, pdseverity string) string {
return fmt.Sprintf(`
resource "honeycombio_pagerduty_recipient" "test" {
Expand All @@ -490,6 +605,23 @@ resource "honeycombio_burn_alert" "test" {
}`, exhaustionMinutes, dataset, sloID, pdseverity)
}

func testAccConfigBurnAlertExhaustionTime_validateAttributesWhenAlertTypeIsExhaustionTime(dataset, sloID string) string {
return fmt.Sprintf(`
resource "honeycombio_burn_alert" "test" {
alert_type = "exhaustion_time"
budget_rate_window_minutes = 60
budget_rate_decrease_percent = 1
dataset = "%[1]s"
slo_id = "%[2]s"
recipient {
type = "email"
target = "[email protected]"
}
}`, dataset, sloID)
}

func testAccConfigBurnAlertBudgetRate_basic(budgetRateWindowMinutes int, budgetRateDecreasePercent float64, dataset, sloID, pdseverity string) string {
return fmt.Sprintf(`
resource "honeycombio_pagerduty_recipient" "test" {
Expand Down Expand Up @@ -539,3 +671,19 @@ resource "honeycombio_burn_alert" "test" {
}
}`, dataset, sloID)
}

func testAccConfigBurnAlertBudgetRate_validateAttributesWhenAlertTypeIsBudgetRate(dataset, sloID string) string {
return fmt.Sprintf(`
resource "honeycombio_burn_alert" "test" {
alert_type = "budget_rate"
exhaustion_minutes = 60
dataset = "%[1]s"
slo_id = "%[2]s"
recipient {
type = "email"
target = "[email protected]"
}
}`, dataset, sloID)
}

0 comments on commit 215ddc0

Please sign in to comment.