| new file mode 100644 |
| index 000000000..09c41242f |
| --- /dev/null |
| +++ b/infra/modules/region-host/tests/health_alarms.tftest.hcl |
| @@ -0,0 +1,213 @@ |
| +# Plan-time tests for the fixed host's EC2 status-check alarms: each one |
| +# keeps its EC2 action and tells someone. mock_provider: no AWS account. |
| +mock_provider "aws" { |
| + mock_data "aws_region" { defaults = { region = "eu-west-1" } } |
| +} |
| + |
| +override_data { |
| + target = data.aws_vpcs.default |
| + values = { ids = ["vpc-0aaaaaaaaaaaaaaaa"] } |
| +} |
| + |
| +override_data { |
| + target = data.aws_caller_identity.current |
| + values = { account_id = "123456789012" } |
| +} |
| + |
| +# The termination queue's ARN feeds an EventBridge target, which the provider |
| +# validates as an ARN; the mock's random string is not one. |
| +override_resource { |
| + target = aws_sqs_queue.terminations |
| + values = { arn = "arn:aws:sqs:eu-west-1:123456789012:carlos-test-host-terminations" } |
| +} |
| + |
| +variables { |
| + name = "carlos-test-ie" |
| + sg_description = "carlos test host" |
| + iam_name = "carlos-test-host" |
| + instance_type = "t4g.small" |
| + root_volume_size = 20 |
| + user_data = "#!/bin/sh\n" |
| + deployment_bucket_arn = "arn:aws:s3:::carlos-test-platform" |
| + data_bucket_arn = "arn:aws:s3:::carlos-test-ie" |
| + ssm_parameter_arn = "arn:aws:ssm:eu-west-1:123456789012:parameter/carlos/*" |
| +} |
| + |
| + |
| +override_data { |
| + target = data.aws_ami.debian_arm64 |
| + values = { id = "ami-0123456789abcdef0" } |
| +} |
| +override_data { |
| + target = data.aws_vpc.host |
| + values = { cidr_block = "10.0.0.0/16" } |
| +} |
| +override_resource { |
| + target = aws_instance.host |
| + values = { id = "i-0123456789abcdef0" } |
| +} |
| +override_resource { |
| + target = aws_security_group.host |
| + values = { id = "sg-0123456789abcdef0" } |
| +} |
| +override_resource { |
| + target = aws_iam_role.host |
| + values = { arn = "arn:aws:iam::123456789012:role/carlos-test-host" } |
| +} |
| +override_resource { |
| + target = aws_eip.host |
| + values = { public_ip = "192.0.2.10" } |
| +} |
| +override_resource { |
| + target = aws_sns_topic.alerts |
| + values = { arn = "arn:aws:sns:eu-west-1:123456789012:carlos-test-ie-alerts" } |
| +} |
| + |
| +run "status_alarms_without_any_topic" { |
| + command = plan |
| + assert { |
| + condition = ( |
| + aws_cloudwatch_metric_alarm.host_auto_recover[0].alarm_actions == toset(["arn:aws:automate:eu-west-1:ec2:recover"]) && |
| + length(aws_cloudwatch_metric_alarm.host_auto_recover[0].ok_actions) == 0 && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].alarm_actions == toset(["arn:aws:automate:eu-west-1:ec2:reboot"]) && |
| + length(aws_cloudwatch_metric_alarm.host_instance_check[0].ok_actions) == 0 |
| + ) |
| + error_message = "with no topic configured the status alarms keep only their EC2 actions" |
| + } |
| + assert { |
| + condition = ( |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].alarm_name == "carlos-test-ie-instance-check" && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].namespace == "AWS/EC2" && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].metric_name == "StatusCheckFailed_Instance" && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].statistic == "Maximum" && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].period == 60 && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].evaluation_periods == 2 && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].datapoints_to_alarm == 2 && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].threshold == 1 && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].comparison_operator == "GreaterThanOrEqualToThreshold" && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].treat_missing_data == "missing" && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].dimensions == tomap({ InstanceId = "i-0123456789abcdef0" }) |
| + ) |
| + error_message = "instance-check alarm must watch this instance's instance status check, 2 of 2 one-minute periods" |
| + } |
| + assert { |
| + condition = ( |
| + aws_cloudwatch_metric_alarm.host_auto_recover[0].metric_name == "StatusCheckFailed_System" && |
| + aws_cloudwatch_metric_alarm.host_auto_recover[0].period == 60 && |
| + aws_cloudwatch_metric_alarm.host_auto_recover[0].evaluation_periods == 2 && |
| + aws_cloudwatch_metric_alarm.host_auto_recover[0].dimensions == tomap({ InstanceId = "i-0123456789abcdef0" }) |
| + ) |
| + error_message = "auto-recover alarm's metric and timing must not change" |
| + } |
| +} |
| + |
| +run "status_alarms_use_the_alerts_email_topic" { |
| + command = plan |
| + variables { |
| + alerts_email = "ops@example.com" |
| + } |
| + assert { |
| + condition = ( |
| + aws_cloudwatch_metric_alarm.host_auto_recover[0].alarm_actions == toset(["arn:aws:automate:eu-west-1:ec2:recover", "arn:aws:sns:eu-west-1:123456789012:carlos-test-ie-alerts"]) && |
| + aws_cloudwatch_metric_alarm.host_auto_recover[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:carlos-test-ie-alerts"]) && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].alarm_actions == toset(["arn:aws:automate:eu-west-1:ec2:reboot", "arn:aws:sns:eu-west-1:123456789012:carlos-test-ie-alerts"]) && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:carlos-test-ie-alerts"]) |
| + ) |
| + error_message = "status alarms must notify the module's alerts topic on alarm and on clear" |
| + } |
| +} |
| + |
| +run "status_alarms_use_the_alerts_topic_arn" { |
| + command = plan |
| + variables { |
| + alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts" |
| + } |
| + assert { |
| + condition = ( |
| + aws_cloudwatch_metric_alarm.host_auto_recover[0].alarm_actions == toset(["arn:aws:automate:eu-west-1:ec2:recover", "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"]) && |
| + aws_cloudwatch_metric_alarm.host_auto_recover[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"]) && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].alarm_actions == toset(["arn:aws:automate:eu-west-1:ec2:reboot", "arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"]) && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:permanent-ha-alerts"]) |
| + ) |
| + error_message = "status alarms must notify the selected alerts topic" |
| + } |
| +} |
| + |
| +# The dev case: a box with no memory alarm and no publish grant, told through |
| +# a topic the deployment owns. |
| +run "health_topic_alone_arms_nothing_else" { |
| + command = plan |
| + variables { |
| + health_alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts" |
| + } |
| + assert { |
| + condition = ( |
| + aws_cloudwatch_metric_alarm.host_auto_recover[0].alarm_actions == toset(["arn:aws:automate:eu-west-1:ec2:recover", "arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts"]) && |
| + aws_cloudwatch_metric_alarm.host_auto_recover[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts"]) && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].alarm_actions == toset(["arn:aws:automate:eu-west-1:ec2:reboot", "arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts"]) && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts"]) |
| + ) |
| + error_message = "status alarms must notify the health topic" |
| + } |
| + assert { |
| + condition = ( |
| + length(aws_cloudwatch_metric_alarm.host_memory_low) == 0 && |
| + length(aws_iam_role_policy.alert_publish) == 0 && |
| + length(aws_sns_topic.alerts) == 0 && |
| + output.alerts_topic_arn == null |
| + ) |
| + error_message = "the health topic must not arm the memory alarm, grant publish, or create a topic" |
| + } |
| +} |
| + |
| +run "health_topic_overrides_the_alerts_topic" { |
| + command = plan |
| + variables { |
| + alerts_email = "ops@example.com" |
| + health_alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts" |
| + } |
| + assert { |
| + condition = ( |
| + aws_cloudwatch_metric_alarm.host_auto_recover[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts"]) && |
| + aws_cloudwatch_metric_alarm.host_instance_check[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts"]) && |
| + aws_cloudwatch_metric_alarm.host_memory_low[0].ok_actions == toset(["arn:aws:sns:eu-west-1:123456789012:carlos-test-ie-alerts"]) |
| + ) |
| + error_message = "the health topic takes the status alarms; the memory alarm keeps the alerts topic" |
| + } |
| +} |
| + |
| +run "auto_recovery_off_arms_neither_status_alarm" { |
| + command = plan |
| + variables { |
| + enable_auto_recovery = false |
| + health_alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts" |
| + } |
| + assert { |
| + condition = length(aws_cloudwatch_metric_alarm.host_auto_recover) == 0 && length(aws_cloudwatch_metric_alarm.host_instance_check) == 0 |
| + error_message = "enable_auto_recovery = false must arm neither status alarm" |
| + } |
| +} |
| + |
| +run "health_topic_wrong_region" { |
| + command = plan |
| + variables { |
| + health_alerts_topic_arn = "arn:aws:sns:eu-west-2:123456789012:dev-ha-alerts" |
| + } |
| + expect_failures = [var.health_alerts_topic_arn] |
| +} |
| + |
| +run "health_topic_wildcard" { |
| + command = plan |
| + variables { |
| + health_alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:dev-ha-*" |
| + } |
| + expect_failures = [var.health_alerts_topic_arn] |
| +} |
| + |
| +run "health_topic_subscription_suffix" { |
| + command = plan |
| + variables { |
| + health_alerts_topic_arn = "arn:aws:sns:eu-west-1:123456789012:dev-ha-alerts:0f1e2d3c-aaaa-bbbb-cccc-123456789012" |
| + } |
| + expect_failures = [var.health_alerts_topic_arn] |
| +} |