Skip to content

Commit

Permalink
Adjust test
Browse files Browse the repository at this point in the history
  • Loading branch information
romanini-ciandt committed Nov 12, 2024
1 parent 416b34d commit 375d688
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions test/integration/monitoring_alerts/monitoring_alerts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"
)

func TestMonitoringAlertKeyVersion(t *testing.T) {
Expand Down Expand Up @@ -59,20 +60,37 @@ func TestMonitoringAlertKeyVersion(t *testing.T) {
}
assert.ElementsMatch([]string{"[email protected]", "[email protected]"}, notificationChannelEmailAddresses)

var expectedFilter string
if monitor_all_keys_in_the_project {
expectedFilter = fmt.Sprintf("protoPayload.request.@type=\"type.googleapis.com/google.cloud.kms.v1.DestroyCryptoKeyVersionRequest\"")
} else {
expectedFilter = fmt.Sprintf("protoPayload.request.@type=\"type.googleapis.com/google.cloud.kms.v1.DestroyCryptoKeyVersionRequest\" AND protoPayload.request.name=~\"%s/.*\"", keyVersion)
}

monitoringAlerts := gcloud.Runf(t, "alpha monitoring policies list --project %s", projectId).Array()
assert.Len(monitoringAlerts, 1)
monitoringAlert := monitoringAlerts[0]
alertCondition := monitoringAlerts[0].Get("conditions").Array()
var monitoringAlert gjson.Result
for _, monitoringAlertLoop := range monitoringAlerts {
conditions := monitoringAlertLoop.Get("conditions").Array()
if len(conditions) > 0 && conditions[0].Get("conditionMatchedLog.filter").String() == expectedFilter {
monitoringAlert = monitoringAlertLoop
break
}
}
alertCondition := monitoringAlert.Get("conditions").Array()
assert.Len(alertCondition, 1)

assert.Equal(expectedFilter, alertCondition[0].Get("conditionMatchedLog.filter").String())
notificationChannels := monitoringAlert.Get("notificationChannels").Array()
for _, notificationChannel := range notificationChannels {
assert.Contains(notificationChannelStringNames, notificationChannel.String())
}
assert.Equal("WARNING", monitoringAlert.Get("severity").String())
assert.Equal("300s", monitoringAlert.Get("alertStrategy.notificationRateLimit.period").String())
assert.True(monitoringAlert.Get("enabled").Bool())

if !monitor_all_keys_in_the_project {
time.Sleep(1 * time.Minute)
expectedFilter := fmt.Sprintf("protoPayload.request.@type=\"type.googleapis.com/google.cloud.kms.v1.DestroyCryptoKeyVersionRequest\" AND protoPayload.request.name=~\"%s/.*\"", keyVersion)
assert.Equal(expectedFilter, alertCondition[0].Get("conditionMatchedLog.filter").String())
// Deleting a key will be tested just for a specific key use case in order
// to avoid increasing too much the testing runtime.

gcloud.Runf(t, fmt.Sprintf("kms keys versions destroy 1 --location us-central1 --keyring %s --key alert-key", keyring))
utils.Poll(t, func() (bool, error) {
Expand All @@ -93,13 +111,7 @@ func TestMonitoringAlertKeyVersion(t *testing.T) {
// Timeout will occour after 20 retries of 10 seconds
20,
10*time.Second)
} else {
// Will test just the filter string when monitor_all_keys_in_the_project is true
// in order to avoid increasing too much the test runtime
expectedFilter := fmt.Sprintf("protoPayload.request.@type=\"type.googleapis.com/google.cloud.kms.v1.DestroyCryptoKeyVersionRequest\"")
assert.Equal(expectedFilter, alertCondition[0].Get("conditionMatchedLog.filter").String())
}

})
kmsAlertT.Test()

Expand Down

0 comments on commit 375d688

Please sign in to comment.