|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from chaosaws.asg.probes import (desired_equals_healthy, |
| 7 | + desired_equals_healthy_tags) |
| 8 | +from chaoslib.exceptions import FailedActivity |
| 9 | + |
| 10 | + |
| 11 | +@patch('chaosaws.asg.probes.aws_client', autospec=True) |
| 12 | +def test_desired_equals_healthy_true(aws_client): |
| 13 | + client = MagicMock() |
| 14 | + aws_client.return_value = client |
| 15 | + asg_names = ['AutoScalingGroup1', 'AutoScalingGroup2'] |
| 16 | + client.describe_auto_scaling_groups.return_value = { |
| 17 | + "AutoScalingGroups": [{ |
| 18 | + "DesiredCapacity": 1, |
| 19 | + "Instances": [{ |
| 20 | + "HealthStatus": "Healthy" |
| 21 | + }] |
| 22 | + }] |
| 23 | + } |
| 24 | + assert desired_equals_healthy(asg_names=asg_names) is True |
| 25 | + |
| 26 | + |
| 27 | +@patch('chaosaws.asg.probes.aws_client', autospec=True) |
| 28 | +def test_desired_equals_healthy_false(aws_client): |
| 29 | + client = MagicMock() |
| 30 | + aws_client.return_value = client |
| 31 | + asg_names = ['AutoScalingGroup1', 'AutoScalingGroup2'] |
| 32 | + client.describe_auto_scaling_groups.return_value = { |
| 33 | + "AutoScalingGroups": [{ |
| 34 | + "DesiredCapacity": 1, |
| 35 | + "Instances": [{ |
| 36 | + "HealthStatus": "Unhealthy" |
| 37 | + }] |
| 38 | + }] |
| 39 | + } |
| 40 | + assert desired_equals_healthy(asg_names=asg_names) is False |
| 41 | + |
| 42 | + |
| 43 | +@patch('chaosaws.asg.probes.aws_client', autospec=True) |
| 44 | +def test_desired_equals_healthy_empty(aws_client): |
| 45 | + client = MagicMock() |
| 46 | + aws_client.return_value = client |
| 47 | + asg_names = ['AutoScalingGroup1', 'AutoScalingGroup2'] |
| 48 | + client.describe_auto_scaling_groups.return_value = { |
| 49 | + "AutoScalingGroups": [ |
| 50 | + ] |
| 51 | + } |
| 52 | + assert desired_equals_healthy(asg_names=asg_names) is False |
| 53 | + |
| 54 | + |
| 55 | +def test_desired_equals_healthy_needs_asg_names(): |
| 56 | + with pytest.raises(FailedActivity) as x: |
| 57 | + desired_equals_healthy([]) |
| 58 | + assert "Non-empty list of auto scaling groups is required" in str(x) |
| 59 | + |
| 60 | + |
| 61 | +def desired_equals_healthy_tags_true_sideeffect(*args, **kwargs): |
| 62 | + if 'AutoScalingGroupNames' not in kwargs: |
| 63 | + return { |
| 64 | + "AutoScalingGroups": [ |
| 65 | + { |
| 66 | + 'AutoScalingGroupName': 'AutoScalingGroup1', |
| 67 | + "DesiredCapacity": 1, |
| 68 | + "Instances": [{ |
| 69 | + "HealthStatus": "Healthy" |
| 70 | + }], |
| 71 | + 'Tags': [{ |
| 72 | + 'ResourceId': 'AutoScalingGroup1', |
| 73 | + 'Key': 'Application', |
| 74 | + 'Value': 'mychaosapp' |
| 75 | + }] |
| 76 | + }, |
| 77 | + { |
| 78 | + 'AutoScalingGroupName': 'AutoScalingGroup2', |
| 79 | + "DesiredCapacity": 1, |
| 80 | + "Instances": [{ |
| 81 | + "HealthStatus": "Unhealthy" |
| 82 | + }], |
| 83 | + 'Tags': [{ |
| 84 | + 'ResourceId': 'AutoScalingGroup1', |
| 85 | + 'Key': 'Application', |
| 86 | + 'Value': 'NOTmychaosapp' |
| 87 | + }] |
| 88 | + }, |
| 89 | + { |
| 90 | + 'AutoScalingGroupName': 'AutoScalingGroup3', |
| 91 | + "DesiredCapacity": 1, |
| 92 | + "Instances": [{ |
| 93 | + "HealthStatus": "Unhealthy" |
| 94 | + }], |
| 95 | + 'Tags': [{ |
| 96 | + 'ResourceId': 'AutoScalingGroup1', |
| 97 | + 'Key': 'NOTApplication', |
| 98 | + 'Value': 'mychaosapp' |
| 99 | + }] |
| 100 | + } |
| 101 | + ] |
| 102 | + } |
| 103 | + else: |
| 104 | + return { |
| 105 | + "AutoScalingGroups": [ |
| 106 | + { |
| 107 | + 'AutoScalingGroupName': 'AutoScalingGroup1', |
| 108 | + "DesiredCapacity": 1, |
| 109 | + "Instances": [{ |
| 110 | + "HealthStatus": "Healthy" |
| 111 | + }], |
| 112 | + 'Tags': [{ |
| 113 | + 'ResourceId': 'AutoScalingGroup1', |
| 114 | + 'Key': 'Application', |
| 115 | + 'Value': 'mychaosapp' |
| 116 | + }] |
| 117 | + } |
| 118 | + ] |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | +@patch('chaosaws.asg.probes.aws_client', autospec=True) |
| 123 | +def test_desired_equals_healthy_tags_true(aws_client): |
| 124 | + client = MagicMock() |
| 125 | + aws_client.return_value = client |
| 126 | + tags = [{'Key': 'Application', 'Value': 'mychaosapp'}] |
| 127 | + client.describe_auto_scaling_groups.side_effect = \ |
| 128 | + desired_equals_healthy_tags_true_sideeffect |
| 129 | + assert desired_equals_healthy_tags(tags=tags) is True |
| 130 | + |
| 131 | + |
| 132 | +@patch('chaosaws.asg.probes.aws_client', autospec=True) |
| 133 | +def test_desired_equals_healthy_tags_false(aws_client): |
| 134 | + client = MagicMock() |
| 135 | + aws_client.return_value = client |
| 136 | + tags = [{'Key': 'Application', 'Value': 'mychaosapp'}] |
| 137 | + client.describe_auto_scaling_groups.return_value = { |
| 138 | + "AutoScalingGroups": [ |
| 139 | + { |
| 140 | + 'AutoScalingGroupName': 'AutoScalingGroup1', |
| 141 | + "DesiredCapacity": 1, |
| 142 | + "Instances": [{ |
| 143 | + "HealthStatus": "Unhealthy" |
| 144 | + }], |
| 145 | + 'Tags': [{ |
| 146 | + 'ResourceId': 'AutoScalingGroup1', |
| 147 | + 'Key': 'Application', |
| 148 | + 'Value': 'mychaosapp' |
| 149 | + }] |
| 150 | + }, |
| 151 | + { |
| 152 | + 'AutoScalingGroupName': 'AutoScalingGroup2', |
| 153 | + "DesiredCapacity": 1, |
| 154 | + "Instances": [{ |
| 155 | + "HealthStatus": "Unhealthy" |
| 156 | + }], |
| 157 | + 'Tags': [{ |
| 158 | + 'ResourceId': 'AutoScalingGroup1', |
| 159 | + 'Key': 'Application', |
| 160 | + 'Value': 'NOTmychaosapp' |
| 161 | + }] |
| 162 | + }, |
| 163 | + { |
| 164 | + 'AutoScalingGroupName': 'AutoScalingGroup3', |
| 165 | + "DesiredCapacity": 1, |
| 166 | + "Instances": [{ |
| 167 | + "HealthStatus": "Unhealthy" |
| 168 | + }], |
| 169 | + 'Tags': [{ |
| 170 | + 'ResourceId': 'AutoScalingGroup1', |
| 171 | + 'Key': 'NOTApplication', |
| 172 | + 'Value': 'mychaosapp' |
| 173 | + }] |
| 174 | + } |
| 175 | + ] |
| 176 | + } |
| 177 | + assert desired_equals_healthy_tags(tags=tags) is False |
0 commit comments