Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nobl9-adam-szymanski committed Apr 22, 2024
1 parent 6df84a4 commit b201645
Showing 1 changed file with 328 additions and 20 deletions.
348 changes: 328 additions & 20 deletions nobl9/resource_alert_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nobl9

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand All @@ -11,25 +12,274 @@ import (
)

func TestAcc_Nobl9AlertPolicy(t *testing.T) {
cases := []struct {
name string
configFunc func(name string) string
}{
{"alert-policy", testAlertPolicyWithoutAnyAlertMethod},
{"alert-policy-with-cool-down", testAlertPolicyWithCoolDown},
{"alert-policy-with-alert-method", testAlertPolicyWithAlertMethod},
{"alert-policy-with-multi-alert-method", testAlertPolicyWithMultipleAlertMethods},
{"alert-policy-with-multi-alert-method-reverse", testAlertPolicyWithMultipleAlertMethodsReverseOrder},
{"alert-policy-with-time-to-burn-entire-budget", testAlertPolicyWithTimeToBurnEntireBudgetCondition},
{
"alert-policy-with-average-burn-rate-and-alerting-window",
testAlertPolicyWithAverageBurnRateAndAlertingWindow,
/*
cases := []struct {
name string
configFunc func(name string) string
}{
{"alert-policy", testAlertPolicyWithoutAnyAlertMethod},
{"alert-policy-with-cool-down", testAlertPolicyWithCoolDown},
{"alert-policy-with-alert-method", testAlertPolicyWithAlertMethod},
{"alert-policy-with-multi-alert-method", testAlertPolicyWithMultipleAlertMethods},
{"alert-policy-with-multi-alert-method-reverse", testAlertPolicyWithMultipleAlertMethodsReverseOrder},
{"alert-policy-with-time-to-burn-entire-budget", testAlertPolicyWithTimeToBurnEntireBudgetCondition},
{
"alert-policy-with-average-burn-rate-and-alerting-window",
testAlertPolicyWithAverageBurnRateAndAlertingWindow,
},
{"alert-policy-with-average-burn-rate-and-lasts-for", testAlertPolicyWithAverageBurnRateAndLastsFor},
// alert policy with no description
// alert policy with with description
// alert policy with no display name
// alert policy with with display name
// alert policy with no cooldown defined
// alert policy with cooldown the same as default 5m
// alert policy with custom cooldown = 15m
// alert policy with no alert methods
// alert policy with with multiple alert methods
// alert policy with with multiple alert methods reversed
// alert policy with with multiple conditions
// alert policy with with multiple conditions reversed
// alert policy with one condition:
//
// | measurement | lasts_for | alerting_window | value | value_string | op | title |
// | ---------------------- | --------- | ----------------- | ----- | ------------- | ----- | ----------------------------- |
// | burnedBudget | N/A | N/A | 1.0 | N/A | N/A | default operator |
// | burnedBudget | N/A | N/A | 1.0 | N/A | lt | custom operator |
// | burnedBudget | 5m | N/A | 0.0 | N/A | gte | specific lasts_for |
// | averageBurnRate | N/A | N/A | 1.0 | N/A | N/A | default operator |
// | averageBurnRate | N/A | N/A | 1.0 | N/A | gte | default lasts_for (0m) |
// | averageBurnRate | 5m | N/A | 1.0 | N/A | gte | specific lasts_for |
// | averageBurnRate | N/A | 10m | 1.0 | N/A | gte | specific alerting_window |
// | timeToBurnBudget | N/A | N/A | N/A | 72h | N/A | default operator |
// | timeToBurnBudget | N/A | N/A | N/A | 72h | lt | default lasts_for (0m) |
// | timeToBurnBudget | 5m | N/A | N/A | 72h | lt | specific lasts_for |
// | timeToBurnBudget | N/A | 10m | N/A | 72h | lt | specific alerting_window |
// | timeToBurnEntireBudget | N/A | N/A | N/A | 72h | N/A | default operator |
// | timeToBurnEntireBudget | N/A | N/A | N/A | 72h | lte | default lasts_for (0m) |
// | timeToBurnEntireBudget | 5m | N/A | N/A | 72h | lte | specific lasts_for |
// | timeToBurnEntireBudget | N/A | 10m | N/A | 72h | lte | specific alerting_window |
}
// for _, tc := range cases {
// t.Run(tc.name, func(t *testing.T) {
// resource.Test(t, resource.TestCase{
// ProviderFactories: ProviderFactory(),
// CheckDestroy: destroyMultiple(
// []string{"nobl9_alert_policy", "nobl9_alert_method_webhook"},
// []manifest.Kind{manifest.KindAlertPolicy, manifest.KindAlertMethod},
// ),
// Steps: []resource.TestStep{
// {
// Config: tc.configFunc(tc.name),
// Check: CheckObjectCreated("nobl9_alert_policy." + tc.name),
// },
// // make sure that applying the same config results in a no-op plan, regardless of alert_method order
// {
// Config: tc.configFunc(tc.name),
// PlanOnly: true,
// ExpectNonEmptyPlan: false,
// },
// },
// })
// })
// }
*/
for scenario, alertPolicyConfig := range map[string]alertPolicyConfig{
// Test optional description.
"alert policy with no description": {
OverrideDescriptionBlock: ``,
},
{"alert-policy-with-average-burn-rate-and-lasts-for", testAlertPolicyWithAverageBurnRateAndLastsFor},
}
"alert policy with custom description": {
OverrideDescriptionBlock: `description = "test test"`,
},
// Test optional display name.
"alert policy with no display name": {
OverrideDisplayNameBlock: ``,
},
"alert policy with custom display name": {
OverrideDisplayNameBlock: `display_name = "test test"`,
},
// Test optional cooldown.
"alert policy with no cooldown defined": {
OverrideCooldownBlock: ``,
},
"alert policy with custom cooldown": {
OverrideCooldownBlock: `cooldown = "15m"`,
},
// Test multiple conditions order.
"alert policy with multiple conditions": {
OverrideConditionsBlock: `condition {
measurement = "burnedBudget"
value = 0.9
}
condition {
measurement = "averageBurnRate"
value = 3
lasts_for = "1m"
}
condition {
measurement = "timeToBurnBudget"
value_string = "1h"
lasts_for = "300s"
}`,
},
"alert policy with multiple conditions reversed": {
OverrideConditionsBlock: `condition {
measurement = "timeToBurnBudget"
value_string = "1h"
lasts_for = "300s"
}
condition {
measurement = "burnedBudget"
value = 0.9
}
condition {
measurement = "averageBurnRate"
value = 3
lasts_for = "1m"
}`,
},
// Test alert methods
"alert policy with no alert method": {
OverrideAlertMethodsBlock: ``,
},
// Measurement: burnedBudget
"condtion type burned budget with default operator GTE and default lastsFor 0m": {
OverrideConditionsBlock: `condition {
measurement = "burnedBudget"
value = 1.0
}`,
},
"condtion type burned budget with explicit default operator": {
OverrideConditionsBlock: `condition {
measurement = "burnedBudget"
value = 1.0
op = "gte"
}`,
},
"condtion type burned budget with custom operator": {
OverrideConditionsBlock: `condition {
measurement = "burnedBudget"
value = 1.0
op = "lt"
}`,
},
"condtion type burned budget with custom lastsFor": {
OverrideConditionsBlock: `condition {
measurement = "burnedBudget"
value = 1.0
lasts_for = "10m"
}`,
},
// Measurement: averageBurnRate
"condtion type average burn rate with default operator GTE and default lastsFor 0m": {
OverrideConditionsBlock: `condition {
measurement = "averageBurnRate"
value = 1.0
}`,
},
"condtion type average burn rate with explicit default operator": {
OverrideConditionsBlock: `condition {
measurement = "averageBurnRate"
value = 1.0
op = "gte"
}`,
},
"condtion type average burn rate with custom lastsFor": {
OverrideConditionsBlock: `condition {
measurement = "averageBurnRate"
value = 1.0
lasts_for = "10m"
}`,
},
"condtion type average burn rate with custom alertingWindow": {
OverrideConditionsBlock: `condition {
measurement = "averageBurnRate"
value = 1.0
alerting_window = "10m"
}`,
},
// Measurement: timeToBurnBudget
"condtion type time to burn budget with default operator LT and default lastsFor 0m": {
OverrideConditionsBlock: `condition {
measurement = "timeToBurnBudget"
value_string = "6h"
}`,
},
"condtion type time to burn budget with explicit default operator": {
OverrideConditionsBlock: `condition {
measurement = "timeToBurnBudget"
value_string = "6h"
op = "lt"
}`,
},
"condtion type time to burn budget with custom lastsFor": {
OverrideConditionsBlock: `condition {
measurement = "timeToBurnBudget"
value_string = "6h"
lasts_for = "10m"
}`,
},
"condtion type time to burn budget with custom alertingWindow": {
OverrideConditionsBlock: `condition {
measurement = "timeToBurnBudget"
value_string = "6h"
alerting_window = "10m"
}`,
},
// Measurement: timeToBurnEntireBudget
"condtion type time to burn entire budget with default operator LT and default lastsFor 0m": {
OverrideConditionsBlock: `condition {
measurement = "timeToBurnEntireBudget"
value_string = "6h"
}`,
},
"condtion type time to burn entire budget with explicit default operator": {
OverrideConditionsBlock: `condition {
measurement = "timeToBurnEntireBudget"
value_string = "6h"
op = "lte"
}`,
},
"condtion type time to burn entire budget with custom lastsFor": {
OverrideConditionsBlock: `condition {
measurement = "timeToBurnEntireBudget"
value_string = "6h"
lasts_for = "10m"
}`,
},
"condtion type time to burn entire budget with custom alertingWindow": {
OverrideConditionsBlock: `condition {
measurement = "timeToBurnEntireBudget"
value_string = "6h"
alerting_window = "10m"
}`,
},
} {
t.Run(scenario, func(t *testing.T) {
resourceName := strings.Replace(scenario, " ", "_", -1)
alertPolicyName := strings.Replace(scenario, " ", "-", -1)

alertPolicyConfig.Name = alertPolicyName
alertPolicyConfig.ResourceName = resourceName

res := alertPolicyConfig.String()
fmt.Println(res)

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
resource.Test(t, resource.TestCase{
ProviderFactories: ProviderFactory(),
CheckDestroy: destroyMultiple(
Expand All @@ -38,12 +288,12 @@ func TestAcc_Nobl9AlertPolicy(t *testing.T) {
),
Steps: []resource.TestStep{
{
Config: tc.configFunc(tc.name),
Check: CheckObjectCreated("nobl9_alert_policy." + tc.name),
Config: res,
Check: CheckObjectCreated("nobl9_alert_policy." + resourceName),
},
// make sure that applying the same config results in a no-op plan, regardless of alert_method order
{
Config: tc.configFunc(tc.name),
Config: res,
PlanOnly: true,
ExpectNonEmptyPlan: false,
},
Expand All @@ -65,6 +315,64 @@ func destroyMultiple(rsTypes []string, kinds []manifest.Kind) resource.TestCheck
}
}

type alertPolicyConfig struct {
Name string
ResourceName string
Project string

OverrideDisplayNameBlock string
OverrideDescriptionBlock string
OverrideCooldownBlock string
OverrideConditionsBlock string
OverrideAlertMethodsBlock string
}

func (ap alertPolicyConfig) String() string {
b := strings.Builder{}
b.WriteString(fmt.Sprintf(`resource "nobl9_alert_policy" "%s" {`, ap.ResourceName))
b.WriteString("\n")
b.WriteString(fmt.Sprintf(`name = "%s"`, strings.Replace(ap.ResourceName, "_", "-", -1)))
b.WriteString("\n")
b.WriteString(fmt.Sprintf(`project = "%s"`, ap.Project))
b.WriteString("\n")
b.WriteString(`severity = "Low"`)
b.WriteString("\n")
if ap.OverrideCooldownBlock == "" {
b.WriteString(`cooldown = 5m`)
} else {
b.WriteString(ap.OverrideCooldownBlock)
}
b.WriteString("\n")
if ap.OverrideDisplayNameBlock == "" {
b.WriteString(`display_name = "default display name"`)
} else {
b.WriteString(ap.OverrideDisplayNameBlock)
}
b.WriteString("\n")
if ap.OverrideDescriptionBlock == "" {
b.WriteString(`description = "default description"`)
} else {
b.WriteString(ap.OverrideDescriptionBlock)
}
b.WriteString("\n")
if ap.OverrideAlertMethodsBlock != "" {
b.WriteString(ap.OverrideAlertMethodsBlock)
}
b.WriteString("\n")
if ap.OverrideConditionsBlock == "" {
b.WriteString(`condition {
measurement = "burnedBudget"
value = 0.9
}`)
} else {
b.WriteString(ap.OverrideConditionsBlock)
}
b.WriteString("\n")
b.WriteString("}")

return b.String()
}

func testAlertPolicyWithoutAnyAlertMethod(name string) string {
return fmt.Sprintf(`
resource "nobl9_alert_policy" "%s" {
Expand Down

0 comments on commit b201645

Please sign in to comment.