diff --git a/examples/monitoring_alerts/main.tf b/examples/monitoring_alerts/main.tf index e378c20..e80b999 100644 --- a/examples/monitoring_alerts/main.tf +++ b/examples/monitoring_alerts/main.tf @@ -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]}/.*\"" diff --git a/examples/monitoring_alerts/variables.tf b/examples/monitoring_alerts/variables.tf index 28948d0..5cc6e29 100644 --- a/examples/monitoring_alerts/variables.tf +++ b/examples/monitoring_alerts/variables.tf @@ -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. diff --git a/test/fixtures/monitoring_alerts_on_project/main.tf b/test/fixtures/monitoring_alerts_on_project/main.tf new file mode 100644 index 0000000..cbb4339 --- /dev/null +++ b/test/fixtures/monitoring_alerts_on_project/main.tf @@ -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@example.com", "email2@example.com"] +} diff --git a/test/fixtures/monitoring_alerts_on_project/outputs.tf b/test/fixtures/monitoring_alerts_on_project/outputs.tf new file mode 100644 index 0000000..dd076c4 --- /dev/null +++ b/test/fixtures/monitoring_alerts_on_project/outputs.tf @@ -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." +} diff --git a/test/fixtures/monitoring_alerts_on_project/variables.tf b/test/fixtures/monitoring_alerts_on_project/variables.tf new file mode 100644 index 0000000..561b5a3 --- /dev/null +++ b/test/fixtures/monitoring_alerts_on_project/variables.tf @@ -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 +} diff --git a/test/fixtures/monitoring_alerts_specific_key/main.tf b/test/fixtures/monitoring_alerts_specific_key/main.tf new file mode 100644 index 0000000..1b48fd6 --- /dev/null +++ b/test/fixtures/monitoring_alerts_specific_key/main.tf @@ -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@example.com", "email2@example.com"] +} diff --git a/test/fixtures/monitoring_alerts_specific_key/outputs.tf b/test/fixtures/monitoring_alerts_specific_key/outputs.tf new file mode 100644 index 0000000..837746d --- /dev/null +++ b/test/fixtures/monitoring_alerts_specific_key/outputs.tf @@ -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." +} diff --git a/test/fixtures/monitoring_alerts_specific_key/variables.tf b/test/fixtures/monitoring_alerts_specific_key/variables.tf new file mode 100644 index 0000000..561b5a3 --- /dev/null +++ b/test/fixtures/monitoring_alerts_specific_key/variables.tf @@ -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 +} diff --git a/test/integration/monitoring_alerts/monitoring_alerts_test.go b/test/integration/monitoring_alerts/monitoring_alerts_test.go index 90eafd1..f508b0b 100644 --- a/test/integration/monitoring_alerts/monitoring_alerts_test.go +++ b/test/integration/monitoring_alerts/monitoring_alerts_test.go @@ -17,7 +17,6 @@ package monitoring_alert import ( "errors" "fmt" - "os" "strings" "testing" "time" @@ -29,29 +28,16 @@ import ( ) func TestMonitoringAlertKeyVersion(t *testing.T) { - path, _ := os.Getwd() - emailAddresses := []string{"email@example.com", "email2@example.com"} - - 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) { @@ -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 { @@ -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@example.com", "email2@example.com"}, notificationChannelEmailAddresses) monitoringAlerts := gcloud.Runf(t, "alpha monitoring policies list --project %s", projectId).Array() assert.Len(monitoringAlerts, 1)