From bfeea007749a771248ffafc6c6d9693b36577f10 Mon Sep 17 00:00:00 2001 From: Jim Shields Date: Wed, 13 Dec 2017 10:15:40 -0500 Subject: [PATCH 1/2] Fix #1370: Implement suspend_processes in AutoScaling service --- moto/autoscaling/models.py | 5 ++++ moto/autoscaling/responses.py | 22 +++++++++++++++- tests/test_autoscaling/test_autoscaling.py | 29 ++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/moto/autoscaling/models.py b/moto/autoscaling/models.py index af65c2a5..0ebc4c46 100644 --- a/moto/autoscaling/models.py +++ b/moto/autoscaling/models.py @@ -179,6 +179,7 @@ class FakeAutoScalingGroup(BaseModel): self.placement_group = placement_group self.termination_policies = termination_policies + self.suspended_processes = [] self.instance_states = [] self.tags = tags if tags else [] self.set_desired_capacity(desired_capacity) @@ -621,6 +622,10 @@ class AutoScalingBackend(BaseBackend): asg_targets = [{'id': x.instance.id} for x in group.instance_states] self.elbv2_backend.deregister_targets(target_group, (asg_targets)) + def suspend_processes(self, group_name, scaling_processes): + group = self.autoscaling_groups[group_name] + group.suspended_processes = scaling_processes or [] + autoscaling_backends = {} for region, ec2_backend in ec2_backends.items(): diff --git a/moto/autoscaling/responses.py b/moto/autoscaling/responses.py index 9e11299c..c7170e17 100644 --- a/moto/autoscaling/responses.py +++ b/moto/autoscaling/responses.py @@ -283,6 +283,13 @@ class AutoScalingResponse(BaseResponse): template = self.response_template(DETACH_LOAD_BALANCERS_TEMPLATE) return template.render() + def suspend_processes(self): + autoscaling_group_name = self._get_param('AutoScalingGroupName') + scaling_processes = self._get_multi_param('ScalingProcesses.member') + self.autoscaling_backend.suspend_processes(autoscaling_group_name, scaling_processes) + template = self.response_template(SUSPEND_PROCESSES_TEMPLATE) + return template.render() + CREATE_LAUNCH_CONFIGURATION_TEMPLATE = """ @@ -463,7 +470,14 @@ DESCRIBE_AUTOSCALING_GROUPS_TEMPLATE = """ {% endfor %} - + + {% for suspended_process in group.suspended_processes %} + + {{suspended_process}} + + + {% endfor %} + {{ group.name }} {{ group.health_check_type }} 2013-05-06T17:47:15.107Z @@ -644,6 +658,12 @@ DETACH_LOAD_BALANCERS_TEMPLATE = """ + + 7c6e177f-f082-11e1-ac58-3714bEXAMPLE + +""" + SET_INSTANCE_HEALTH_TEMPLATE = """ diff --git a/tests/test_autoscaling/test_autoscaling.py b/tests/test_autoscaling/test_autoscaling.py index 453d1409..f2ee1622 100644 --- a/tests/test_autoscaling/test_autoscaling.py +++ b/tests/test_autoscaling/test_autoscaling.py @@ -1067,3 +1067,32 @@ def test_set_instance_health(): instance1 = response['AutoScalingGroups'][0]['Instances'][0] instance1['HealthStatus'].should.equal('Unhealthy') + +@mock_autoscaling +def test_asg(): + client = boto3.client('autoscaling') + client.create_launch_configuration( + LaunchConfigurationName='lc', + ) + client.create_auto_scaling_group( + LaunchConfigurationName='lc', + AutoScalingGroupName='test-asg', + MinSize=1, + MaxSize=1, + ) + + # Testing something that calls the below... + client.suspend_processes( + AutoScalingGroupName='test-asg', + ScalingProcesses=['Launch'] + ) + + res = client.describe_auto_scaling_groups( + AutoScalingGroupNames=['test-asg'] + ) + launch_suspended = False + for proc in res['AutoScalingGroups'][0]['SuspendedProcesses']: + if proc.get('ProcessName') == 'Launch': + launch_suspended = True + + assert launch_suspended is True From baedbfa8ca058cef407540965af31c46b5a7b569 Mon Sep 17 00:00:00 2001 From: Jim Shields Date: Fri, 9 Mar 2018 17:22:57 -0500 Subject: [PATCH 2/2] Fix test_suspend_processes * Add `region_name` to the client to be consistent with other tests * Add `VPCZoneIdentifier` to the ASG creation (AZ or VPC is required) * Add myself as a contributor --- AUTHORS.md | 1 + tests/test_autoscaling/test_autoscaling.py | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/AUTHORS.md b/AUTHORS.md index 710cf5dc..5152e547 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -50,3 +50,4 @@ Moto is written by Steve Pulec with contributions from: * [Jessie Nadler](https://github.com/nadlerjessie) * [Alex Morken](https://github.com/alexmorken) * [Clive Li](https://github.com/cliveli) +* [Jim Shields](https://github.com/jimjshields) diff --git a/tests/test_autoscaling/test_autoscaling.py b/tests/test_autoscaling/test_autoscaling.py index f2ee1622..f86ca2b8 100644 --- a/tests/test_autoscaling/test_autoscaling.py +++ b/tests/test_autoscaling/test_autoscaling.py @@ -1069,8 +1069,9 @@ def test_set_instance_health(): instance1['HealthStatus'].should.equal('Unhealthy') @mock_autoscaling -def test_asg(): - client = boto3.client('autoscaling') +def test_suspend_processes(): + mocked_networking = setup_networking() + client = boto3.client('autoscaling', region_name='us-east-1') client.create_launch_configuration( LaunchConfigurationName='lc', ) @@ -1079,9 +1080,10 @@ def test_asg(): AutoScalingGroupName='test-asg', MinSize=1, MaxSize=1, + VPCZoneIdentifier=mocked_networking['subnet1'], ) - # Testing something that calls the below... + # When we suspend the 'Launch' process on the ASG client client.suspend_processes( AutoScalingGroupName='test-asg', ScalingProcesses=['Launch'] @@ -1090,6 +1092,8 @@ def test_asg(): res = client.describe_auto_scaling_groups( AutoScalingGroupNames=['test-asg'] ) + + # The 'Launch' process should, in fact, be suspended launch_suspended = False for proc in res['AutoScalingGroups'][0]['SuspendedProcesses']: if proc.get('ProcessName') == 'Launch':