Skip to content

Commit

Permalink
Change approach to use fixtures on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
romanini-ciandt committed Nov 12, 2024
1 parent ac332a1 commit 416b34d
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 26 deletions.
4 changes: 0 additions & 4 deletions examples/monitoring_alerts/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
* If multiple key versions are deleted in less than 5 minutes a single notification will be sent.
*/

terraform {
backend "local" {}
}

locals {
all_keys_filter = "protoPayload.request.@type=\"type.googleapis.com/google.cloud.kms.v1.DestroyCryptoKeyVersionRequest\""
single_key_filter = "${local.all_keys_filter} AND protoPayload.request.name=~\"${values(module.kms.keys)[0]}/.*\""
Expand Down
2 changes: 1 addition & 1 deletion examples/monitoring_alerts/variables.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2018 Google LLC
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
23 changes: 23 additions & 0 deletions test/fixtures/monitoring_alerts_on_project/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

module "monitoring_alert_on_project" {
source = "../../../examples/monitoring_alerts"

monitor_all_keys_in_the_project = true
project_id = var.project_id
email_addresses_to_be_notified = ["[email protected]", "[email protected]"]
}
35 changes: 35 additions & 0 deletions test/fixtures/monitoring_alerts_on_project/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

output "key" {
value = module.monitoring_alert_on_project.key
description = "The version of the created KMS key."
}

output "keyring" {
value = module.monitoring_alert_on_project.keyring
description = "The keyring created."
}

output "project_id" {
value = module.monitoring_alert_on_project.project_id
description = "GCP Project ID where key version was created."
}

output "notification_channel_names" {
value = module.monitoring_alert_on_project.notification_channel_names
description = "Notification channel names."
}
20 changes: 20 additions & 0 deletions test/fixtures/monitoring_alerts_on_project/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

variable "project_id" {
description = "The ID of the project in which to provision resources."
type = string
}
23 changes: 23 additions & 0 deletions test/fixtures/monitoring_alerts_specific_key/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

module "monitoring_alert_specific_key" {
source = "../../../examples/monitoring_alerts"

monitor_all_keys_in_the_project = false
project_id = var.project_id
email_addresses_to_be_notified = ["[email protected]", "[email protected]"]
}
35 changes: 35 additions & 0 deletions test/fixtures/monitoring_alerts_specific_key/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

output "key" {
value = module.monitoring_alert_specific_key.key
description = "The version of the created KMS key."
}

output "keyring" {
value = module.monitoring_alert_specific_key.keyring
description = "The keyring created."
}

output "project_id" {
value = module.monitoring_alert_specific_key.project_id
description = "GCP Project ID where key version was created."
}

output "notification_channel_names" {
value = module.monitoring_alert_specific_key.notification_channel_names
description = "Notification channel names."
}
20 changes: 20 additions & 0 deletions test/fixtures/monitoring_alerts_specific_key/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

variable "project_id" {
description = "The ID of the project in which to provision resources."
type = string
}
28 changes: 7 additions & 21 deletions test/integration/monitoring_alerts/monitoring_alerts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package monitoring_alert
import (
"errors"
"fmt"
"os"
"strings"
"testing"
"time"
Expand All @@ -29,29 +28,16 @@ import (
)

func TestMonitoringAlertKeyVersion(t *testing.T) {
path, _ := os.Getwd()

emailAddresses := []string{"[email protected]", "[email protected]"}

TfInputs := map[string]bool{
"test1": true,
"test2": false,
TfInputs := map[bool]string{
true: "../../fixtures/monitoring_alerts_on_project",
false: "../../fixtures/monitoring_alerts_specific_key",
}

for key, monitor_all_keys_in_the_project := range TfInputs {

statePath := fmt.Sprintf("%s/custom_backend_%s.tfstate", path, key)

vars := map[string]interface{}{
"monitor_all_keys_in_the_project": monitor_all_keys_in_the_project,
"email_addresses_to_be_notified": emailAddresses,
}
for monitor_all_keys_in_the_project, fixture_path := range TfInputs {

kmsAlertT := tft.NewTFBlueprintTest(t,
tft.WithVars(vars),
tft.WithBackendConfig(map[string]interface{}{
"path": statePath,
}),
tft.WithTFDir(fixture_path),
)

kmsAlertT.DefineVerify(func(assert *assert.Assertions) {
Expand All @@ -62,7 +48,7 @@ func TestMonitoringAlertKeyVersion(t *testing.T) {
keyring := kmsAlertT.GetStringOutput("keyring")
notificationChannelNames := kmsAlertT.GetJsonOutput("notification_channel_names").Array()

assert.Len(notificationChannelNames, len(emailAddresses))
assert.Len(notificationChannelNames, 2)
notificationChannelEmailAddresses := []string{}
notificationChannelStringNames := []string{}
for _, notificationChannelName := range notificationChannelNames {
Expand All @@ -71,7 +57,7 @@ func TestMonitoringAlertKeyVersion(t *testing.T) {
assert.Len(monitoringChannel, 1)
notificationChannelEmailAddresses = append(notificationChannelEmailAddresses, monitoringChannel[0].Get("labels.email_address").String())
}
assert.ElementsMatch(emailAddresses, notificationChannelEmailAddresses)
assert.ElementsMatch([]string{"[email protected]", "[email protected]"}, notificationChannelEmailAddresses)

monitoringAlerts := gcloud.Runf(t, "alpha monitoring policies list --project %s", projectId).Array()
assert.Len(monitoringAlerts, 1)
Expand Down

0 comments on commit 416b34d

Please sign in to comment.