|
8 | 8 | desired_equals_healthy, desired_equals_healthy_tags, has_subnets,
|
9 | 9 | is_scaling_in_progress, wait_desired_equals_healthy, process_is_suspended,
|
10 | 10 | wait_desired_equals_healthy_tags, wait_desired_not_equals_healthy_tags,
|
11 |
| - describe_auto_scaling_groups) |
| 11 | + describe_auto_scaling_groups, instance_count_by_health) |
12 | 12 | from chaoslib.exceptions import FailedActivity
|
13 | 13 |
|
14 | 14 |
|
@@ -137,24 +137,30 @@ def test_desired_equals_healthy_tags_true(aws_client):
|
137 | 137 | client = MagicMock()
|
138 | 138 | aws_client.return_value = client
|
139 | 139 | tags = [{'Key': 'Application', 'Value': 'mychaosapp'}]
|
140 |
| - client.describe_auto_scaling_groups.return_value = \ |
141 |
| - { |
142 |
| - "AutoScalingGroups": [ |
143 |
| - { |
144 |
| - 'AutoScalingGroupName': 'AutoScalingGroup1', |
145 |
| - "DesiredCapacity": 1, |
146 |
| - "Instances": [{ |
147 |
| - "HealthStatus": "Healthy", |
148 |
| - "LifecycleState": "InService" |
149 |
| - }], |
150 |
| - 'Tags': [{ |
151 |
| - 'ResourceId': 'AutoScalingGroup1', |
152 |
| - 'Key': 'Application', |
153 |
| - 'Value': 'mychaosapp' |
154 |
| - }] |
155 |
| - } |
156 |
| - ] |
157 |
| - } |
| 140 | + client.get_paginator.return_value.paginate.return_value = [{ |
| 141 | + "Tags": [{ |
| 142 | + "ResourceId": "AutoScalingGroup1", |
| 143 | + "ResourceType": "auto-scaling-group", |
| 144 | + "Key": "Application", |
| 145 | + "Value": "mychaosapp"}]}] |
| 146 | + |
| 147 | + client.describe_auto_scaling_groups.return_value = { |
| 148 | + "AutoScalingGroups": [ |
| 149 | + { |
| 150 | + 'AutoScalingGroupName': 'AutoScalingGroup1', |
| 151 | + "DesiredCapacity": 1, |
| 152 | + "Instances": [{ |
| 153 | + "HealthStatus": "Healthy", |
| 154 | + "LifecycleState": "InService" |
| 155 | + }], |
| 156 | + 'Tags': [{ |
| 157 | + 'ResourceId': 'AutoScalingGroup1', |
| 158 | + 'Key': 'Application', |
| 159 | + 'Value': 'mychaosapp' |
| 160 | + }] |
| 161 | + } |
| 162 | + ] |
| 163 | + } |
158 | 164 | client.get_paginator.return_value.paginate.return_value = [{
|
159 | 165 | "AutoScalingGroups": [
|
160 | 166 | {
|
@@ -896,3 +902,139 @@ def test_describe_auto_scaling_groups_tags(aws_client):
|
896 | 902 | describe_auto_scaling_groups(tags=tags)
|
897 | 903 | client.describe_auto_scaling_groups.assert_called_with(
|
898 | 904 | AutoScalingGroupNames=["AutoScalingGroup-A", "AutoScalingGroup-B"])
|
| 905 | + |
| 906 | + |
| 907 | +@patch('chaosaws.asg.probes.aws_client', autospec=True) |
| 908 | +def test_instance_healthy_count_names(aws_client): |
| 909 | + client = MagicMock() |
| 910 | + aws_client.return_value = client |
| 911 | + asg_names = ['AutoScalingGroup-A'] |
| 912 | + client.describe_auto_scaling_groups.return_value = { |
| 913 | + "AutoScalingGroups": [{ |
| 914 | + "AutoScalingGroupName": "AutoScalingGroup-A", |
| 915 | + "Instances": [ |
| 916 | + { |
| 917 | + "InstanceId": "i-012345678901", |
| 918 | + "HealthStatus": "Healthy" |
| 919 | + }, |
| 920 | + { |
| 921 | + "InstanceId": "i-012345678902", |
| 922 | + "HealthStatus": "Healthy" |
| 923 | + }, |
| 924 | + { |
| 925 | + "InstanceId": "i-012345678903", |
| 926 | + "HealthStatus": "Unhealthy" |
| 927 | + }]}]} |
| 928 | + response = instance_count_by_health(asg_names) |
| 929 | + client.describe_auto_scaling_groups.assert_called_with( |
| 930 | + AutoScalingGroupNames=asg_names) |
| 931 | + assert response == 2 |
| 932 | + |
| 933 | + |
| 934 | +@patch('chaosaws.asg.probes.aws_client', autospec=True) |
| 935 | +def test_instance_healthy_count_tags(aws_client): |
| 936 | + client = MagicMock() |
| 937 | + aws_client.return_value = client |
| 938 | + tags = [{'Key': 'TestKey', 'Value': 'TestValue'}] |
| 939 | + client.get_paginator.return_value.paginate.return_value = [{ |
| 940 | + "Tags": [{ |
| 941 | + "ResourceId": "AutoScalingGroup-A", |
| 942 | + "ResourceType": "auto-scaling-group", |
| 943 | + "Key": "TestKey", |
| 944 | + "Value": "TestValue"}]}] |
| 945 | + client.describe_auto_scaling_groups.return_value = { |
| 946 | + "AutoScalingGroups": [{ |
| 947 | + "AutoScalingGroupName": "AutoScalingGroup-A", |
| 948 | + "Instances": [ |
| 949 | + { |
| 950 | + "InstanceId": "i-012345678901", |
| 951 | + "HealthStatus": "Healthy" |
| 952 | + }, |
| 953 | + { |
| 954 | + "InstanceId": "i-012345678902", |
| 955 | + "HealthStatus": "Healthy" |
| 956 | + }, |
| 957 | + { |
| 958 | + "InstanceId": "i-012345678903", |
| 959 | + "HealthStatus": "Unhealthy" |
| 960 | + }], |
| 961 | + "Tags": [{ |
| 962 | + "ResourceId": "AutoScalingGroup-A", |
| 963 | + "Key": "TestKey", |
| 964 | + "Value": "TestValue"}] |
| 965 | + }]} |
| 966 | + response = instance_count_by_health(tags=tags) |
| 967 | + client.get_paginator.return_value.paginate.assert_called_with( |
| 968 | + Filters=[{'Name': 'TestKey', 'Values': ['TestValue']}]) |
| 969 | + client.describe_auto_scaling_groups.assert_called_with( |
| 970 | + AutoScalingGroupNames=["AutoScalingGroup-A"]) |
| 971 | + assert response == 2 |
| 972 | + |
| 973 | + |
| 974 | +@patch('chaosaws.asg.probes.aws_client', autospec=True) |
| 975 | +def test_instance_unhealthy_count_names(aws_client): |
| 976 | + client = MagicMock() |
| 977 | + aws_client.return_value = client |
| 978 | + asg_names = ['AutoScalingGroup-A'] |
| 979 | + client.describe_auto_scaling_groups.return_value = { |
| 980 | + "AutoScalingGroups": [{ |
| 981 | + "AutoScalingGroupName": "AutoScalingGroup-A", |
| 982 | + "Instances": [ |
| 983 | + { |
| 984 | + "InstanceId": "i-012345678901", |
| 985 | + "HealthStatus": "Healthy" |
| 986 | + }, |
| 987 | + { |
| 988 | + "InstanceId": "i-012345678902", |
| 989 | + "HealthStatus": "Healthy" |
| 990 | + }, |
| 991 | + { |
| 992 | + "InstanceId": "i-012345678903", |
| 993 | + "HealthStatus": "Unhealthy" |
| 994 | + } |
| 995 | + ]}]} |
| 996 | + response = instance_count_by_health(asg_names, count_healthy=False) |
| 997 | + client.describe_auto_scaling_groups.assert_called_with( |
| 998 | + AutoScalingGroupNames=asg_names) |
| 999 | + assert response == 1 |
| 1000 | + |
| 1001 | + |
| 1002 | +@patch('chaosaws.asg.probes.aws_client', autospec=True) |
| 1003 | +def test_instance_unhealthy_count_tags(aws_client): |
| 1004 | + client = MagicMock() |
| 1005 | + aws_client.return_value = client |
| 1006 | + tags = [{'Key': 'TestKey', 'Value': 'TestValue'}] |
| 1007 | + client.get_paginator.return_value.paginate.return_value = [{ |
| 1008 | + "Tags": [{ |
| 1009 | + "ResourceId": "AutoScalingGroup-A", |
| 1010 | + "ResourceType": "auto-scaling-group", |
| 1011 | + "Key": "TestKey", |
| 1012 | + "Value": "TestValue"}]}] |
| 1013 | + |
| 1014 | + client.describe_auto_scaling_groups.return_value = { |
| 1015 | + "AutoScalingGroups": [{ |
| 1016 | + "AutoScalingGroupName": "AutoScalingGroup-A", |
| 1017 | + "Instances": [ |
| 1018 | + { |
| 1019 | + "InstanceId": "i-012345678901", |
| 1020 | + "HealthStatus": "Healthy" |
| 1021 | + }, |
| 1022 | + { |
| 1023 | + "InstanceId": "i-012345678902", |
| 1024 | + "HealthStatus": "Healthy" |
| 1025 | + }, |
| 1026 | + { |
| 1027 | + "InstanceId": "i-012345678903", |
| 1028 | + "HealthStatus": "Unhealthy" |
| 1029 | + }], |
| 1030 | + "Tags": [{ |
| 1031 | + "ResourceId": "AutoScalingGroup-A", |
| 1032 | + "Key": "TestKey", |
| 1033 | + "Value": "TestValue"}] |
| 1034 | + }]} |
| 1035 | + response = instance_count_by_health(tags=tags, count_healthy=False) |
| 1036 | + client.get_paginator.return_value.paginate.assert_called_with( |
| 1037 | + Filters=[{'Name': 'TestKey', 'Values': ['TestValue']}]) |
| 1038 | + client.describe_auto_scaling_groups.assert_called_with( |
| 1039 | + AutoScalingGroupNames=["AutoScalingGroup-A"]) |
| 1040 | + assert response == 1 |
0 commit comments