|
| 1 | +import pytest |
| 2 | +from pydantic import ValidationError |
| 3 | + |
| 4 | +from pycfmodel.model.resources.autoscaling_auto_scaling_group import AutoScalingAutoScalingGroup |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture() |
| 8 | +def valid_autoscaling_group(): |
| 9 | + return AutoScalingAutoScalingGroup( |
| 10 | + **{ |
| 11 | + "Type": "AWS::AutoScaling::AutoScalingGroup", |
| 12 | + "Properties": { |
| 13 | + "AutoScalingGroupName": "my-asg", |
| 14 | + "MinSize": "1", |
| 15 | + "MaxSize": "10", |
| 16 | + "DesiredCapacity": "2", |
| 17 | + "LaunchTemplate": { |
| 18 | + "LaunchTemplateId": "lt-1234567890abcdef0", |
| 19 | + "Version": "$Latest", |
| 20 | + }, |
| 21 | + "VPCZoneIdentifier": ["subnet-1234567890abcdef0", "subnet-0987654321fedcba0"], |
| 22 | + }, |
| 23 | + } |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +def test_valid_autoscaling_group_resource(valid_autoscaling_group): |
| 28 | + assert valid_autoscaling_group.Properties.AutoScalingGroupName == "my-asg" |
| 29 | + assert valid_autoscaling_group.Properties.MinSize == "1" |
| 30 | + assert valid_autoscaling_group.Properties.MaxSize == "10" |
| 31 | + assert valid_autoscaling_group.Properties.DesiredCapacity == "2" |
| 32 | + |
| 33 | + |
| 34 | +def test_autoscaling_group_with_launch_configuration(): |
| 35 | + asg = AutoScalingAutoScalingGroup( |
| 36 | + **{ |
| 37 | + "Type": "AWS::AutoScaling::AutoScalingGroup", |
| 38 | + "Properties": { |
| 39 | + "MinSize": "1", |
| 40 | + "MaxSize": "5", |
| 41 | + "LaunchConfigurationName": "my-launch-config", |
| 42 | + "AvailabilityZones": ["us-east-1a", "us-east-1b"], |
| 43 | + }, |
| 44 | + } |
| 45 | + ) |
| 46 | + assert asg.Properties.LaunchConfigurationName == "my-launch-config" |
| 47 | + assert len(asg.Properties.AvailabilityZones) == 2 |
| 48 | + |
| 49 | + |
| 50 | +def test_autoscaling_group_with_mixed_instances_policy(): |
| 51 | + asg = AutoScalingAutoScalingGroup( |
| 52 | + **{ |
| 53 | + "Type": "AWS::AutoScaling::AutoScalingGroup", |
| 54 | + "Properties": { |
| 55 | + "MinSize": "1", |
| 56 | + "MaxSize": "10", |
| 57 | + "VPCZoneIdentifier": ["subnet-123"], |
| 58 | + "MixedInstancesPolicy": { |
| 59 | + "LaunchTemplate": { |
| 60 | + "LaunchTemplateSpecification": { |
| 61 | + "LaunchTemplateId": "lt-123", |
| 62 | + "Version": "$Latest", |
| 63 | + }, |
| 64 | + "Overrides": [ |
| 65 | + {"InstanceType": "t3.micro"}, |
| 66 | + {"InstanceType": "t3.small"}, |
| 67 | + {"InstanceType": "t3.medium"}, |
| 68 | + ], |
| 69 | + }, |
| 70 | + "InstancesDistribution": { |
| 71 | + "OnDemandBaseCapacity": 1, |
| 72 | + "OnDemandPercentageAboveBaseCapacity": 25, |
| 73 | + "SpotAllocationStrategy": "capacity-optimized", |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | + ) |
| 79 | + assert len(asg.Properties.MixedInstancesPolicy.LaunchTemplate.Overrides) == 3 |
| 80 | + assert asg.Properties.MixedInstancesPolicy.InstancesDistribution.OnDemandBaseCapacity == 1 |
| 81 | + |
| 82 | + |
| 83 | +def test_autoscaling_group_with_target_groups(): |
| 84 | + asg = AutoScalingAutoScalingGroup( |
| 85 | + **{ |
| 86 | + "Type": "AWS::AutoScaling::AutoScalingGroup", |
| 87 | + "Properties": { |
| 88 | + "MinSize": "1", |
| 89 | + "MaxSize": "5", |
| 90 | + "LaunchTemplate": { |
| 91 | + "LaunchTemplateId": "lt-123", |
| 92 | + "Version": "$Latest", |
| 93 | + }, |
| 94 | + "VPCZoneIdentifier": ["subnet-123"], |
| 95 | + "TargetGroupARNs": [ |
| 96 | + "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-tg/1234567890123456" |
| 97 | + ], |
| 98 | + }, |
| 99 | + } |
| 100 | + ) |
| 101 | + assert len(asg.Properties.TargetGroupARNs) == 1 |
| 102 | + |
| 103 | + |
| 104 | +def test_autoscaling_group_with_health_check(): |
| 105 | + asg = AutoScalingAutoScalingGroup( |
| 106 | + **{ |
| 107 | + "Type": "AWS::AutoScaling::AutoScalingGroup", |
| 108 | + "Properties": { |
| 109 | + "MinSize": "1", |
| 110 | + "MaxSize": "5", |
| 111 | + "LaunchTemplate": { |
| 112 | + "LaunchTemplateId": "lt-123", |
| 113 | + "Version": "$Latest", |
| 114 | + }, |
| 115 | + "VPCZoneIdentifier": ["subnet-123"], |
| 116 | + "HealthCheckType": "ELB", |
| 117 | + "HealthCheckGracePeriod": 300, |
| 118 | + }, |
| 119 | + } |
| 120 | + ) |
| 121 | + assert asg.Properties.HealthCheckType == "ELB" |
| 122 | + assert asg.Properties.HealthCheckGracePeriod == 300 |
| 123 | + |
| 124 | + |
| 125 | +def test_autoscaling_group_with_lifecycle_hooks(): |
| 126 | + asg = AutoScalingAutoScalingGroup( |
| 127 | + **{ |
| 128 | + "Type": "AWS::AutoScaling::AutoScalingGroup", |
| 129 | + "Properties": { |
| 130 | + "MinSize": "1", |
| 131 | + "MaxSize": "5", |
| 132 | + "LaunchTemplate": { |
| 133 | + "LaunchTemplateId": "lt-123", |
| 134 | + "Version": "$Latest", |
| 135 | + }, |
| 136 | + "VPCZoneIdentifier": ["subnet-123"], |
| 137 | + "LifecycleHookSpecificationList": [ |
| 138 | + { |
| 139 | + "LifecycleHookName": "launch-hook", |
| 140 | + "LifecycleTransition": "autoscaling:EC2_INSTANCE_LAUNCHING", |
| 141 | + "DefaultResult": "CONTINUE", |
| 142 | + "HeartbeatTimeout": 300, |
| 143 | + } |
| 144 | + ], |
| 145 | + }, |
| 146 | + } |
| 147 | + ) |
| 148 | + assert len(asg.Properties.LifecycleHookSpecificationList) == 1 |
| 149 | + assert asg.Properties.LifecycleHookSpecificationList[0].LifecycleHookName == "launch-hook" |
| 150 | + |
| 151 | + |
| 152 | +def test_autoscaling_group_with_notification(): |
| 153 | + asg = AutoScalingAutoScalingGroup( |
| 154 | + **{ |
| 155 | + "Type": "AWS::AutoScaling::AutoScalingGroup", |
| 156 | + "Properties": { |
| 157 | + "MinSize": "1", |
| 158 | + "MaxSize": "5", |
| 159 | + "LaunchTemplate": { |
| 160 | + "LaunchTemplateId": "lt-123", |
| 161 | + "Version": "$Latest", |
| 162 | + }, |
| 163 | + "VPCZoneIdentifier": ["subnet-123"], |
| 164 | + "NotificationConfiguration": { |
| 165 | + "TopicARN": "arn:aws:sns:us-east-1:123456789012:my-topic", |
| 166 | + "NotificationTypes": [ |
| 167 | + "autoscaling:EC2_INSTANCE_LAUNCH", |
| 168 | + "autoscaling:EC2_INSTANCE_TERMINATE", |
| 169 | + ], |
| 170 | + }, |
| 171 | + }, |
| 172 | + } |
| 173 | + ) |
| 174 | + assert asg.Properties.NotificationConfiguration.TopicARN == "arn:aws:sns:us-east-1:123456789012:my-topic" |
| 175 | + |
| 176 | + |
| 177 | +def test_autoscaling_group_with_tags(): |
| 178 | + asg = AutoScalingAutoScalingGroup( |
| 179 | + **{ |
| 180 | + "Type": "AWS::AutoScaling::AutoScalingGroup", |
| 181 | + "Properties": { |
| 182 | + "MinSize": "1", |
| 183 | + "MaxSize": "5", |
| 184 | + "LaunchTemplate": { |
| 185 | + "LaunchTemplateId": "lt-123", |
| 186 | + "Version": "$Latest", |
| 187 | + }, |
| 188 | + "VPCZoneIdentifier": ["subnet-123"], |
| 189 | + "Tags": [ |
| 190 | + {"Key": "Environment", "Value": "production", "PropagateAtLaunch": True}, |
| 191 | + {"Key": "Application", "Value": "web", "PropagateAtLaunch": True}, |
| 192 | + ], |
| 193 | + }, |
| 194 | + } |
| 195 | + ) |
| 196 | + assert len(asg.Properties.Tags) == 2 |
| 197 | + assert asg.Properties.Tags[0].PropagateAtLaunch is True |
| 198 | + |
| 199 | + |
| 200 | +def test_autoscaling_group_with_instance_maintenance_policy(): |
| 201 | + asg = AutoScalingAutoScalingGroup( |
| 202 | + **{ |
| 203 | + "Type": "AWS::AutoScaling::AutoScalingGroup", |
| 204 | + "Properties": { |
| 205 | + "MinSize": "1", |
| 206 | + "MaxSize": "5", |
| 207 | + "LaunchTemplate": { |
| 208 | + "LaunchTemplateId": "lt-123", |
| 209 | + "Version": "$Latest", |
| 210 | + }, |
| 211 | + "VPCZoneIdentifier": ["subnet-123"], |
| 212 | + "InstanceMaintenancePolicy": { |
| 213 | + "MinHealthyPercentage": 90, |
| 214 | + "MaxHealthyPercentage": 120, |
| 215 | + }, |
| 216 | + }, |
| 217 | + } |
| 218 | + ) |
| 219 | + assert asg.Properties.InstanceMaintenancePolicy.MinHealthyPercentage == 90 |
| 220 | + |
| 221 | + |
| 222 | +def test_autoscaling_group_requires_min_size(): |
| 223 | + with pytest.raises(ValidationError): |
| 224 | + AutoScalingAutoScalingGroup( |
| 225 | + **{ |
| 226 | + "Type": "AWS::AutoScaling::AutoScalingGroup", |
| 227 | + "Properties": { |
| 228 | + "MaxSize": "5", |
| 229 | + "LaunchTemplate": { |
| 230 | + "LaunchTemplateId": "lt-123", |
| 231 | + "Version": "$Latest", |
| 232 | + }, |
| 233 | + }, |
| 234 | + } |
| 235 | + ) |
| 236 | + |
| 237 | + |
| 238 | +def test_autoscaling_group_requires_max_size(): |
| 239 | + with pytest.raises(ValidationError): |
| 240 | + AutoScalingAutoScalingGroup( |
| 241 | + **{ |
| 242 | + "Type": "AWS::AutoScaling::AutoScalingGroup", |
| 243 | + "Properties": { |
| 244 | + "MinSize": "1", |
| 245 | + "LaunchTemplate": { |
| 246 | + "LaunchTemplateId": "lt-123", |
| 247 | + "Version": "$Latest", |
| 248 | + }, |
| 249 | + }, |
| 250 | + } |
| 251 | + ) |
0 commit comments