def test_term_instance_raise_clienterror(self): logger.debug('TestTerminateInstance.test_term_instance_raise_clienterror') self.mock_attrs['terminate_instance_in_auto_scaling_group.side_effect'] = ClientError({ 'Error': { 'Code': 'UnknownError', 'Message': 'Some other error', 'Type': 'Unknown' } }, 'TerminateInstanceInAutoScalingGroup') asg_helper.autoscaling = Mock(**self.mock_attrs) with self.assertRaises(ClientError): asg_helper.terminate_instance('i-abcd123', True)
def test_term_instance_not_found(self): logger.debug('TestTerminateInstance.test_term_instance_not_found') self.mock_attrs['terminate_instance_in_auto_scaling_group.side_effect'] = ClientError({ 'Error': { 'Code': 'ValidationError', 'Message': 'Instance Id not found - No managed instance found for instance ID i-abcd123', 'Type': 'Sender' } }, 'TerminateInstanceInAutoScalingGroup') asg_helper.autoscaling = Mock(**self.mock_attrs) asg_helper.terminate_instance('i-abcd123', True) asg_helper.autoscaling.terminate_instance_in_auto_scaling_group.assert_called()
def process_warning_event(event): event_source = event.get('source') event_detail_type = event.get('detail-type') if event_source != 'aws.ec2' or event_detail_type != 'EC2 Spot Instance Interruption Warning': raise Exception('Malformed event: {}'.format( json.dumps(event, indent=2, default=util.json_dumps_converter))) if event['detail']['instance-action'] != 'terminate': raise Exception('Invalid or unknown event: {}'.format( json.dumps(event, indent=2, default=util.json_dumps_converter))) instance_id = event['detail']['instance-id'] logger.info( 'EC2 Spot Interruption Warning received for {}'.format(instance_id)) if ec2_helper.is_spoptimize_instance(instance_id): logger.info( '{} was launched by spoptimize ... terminating via autoscaling API' .format(instance_id)) asg_helper.terminate_instance(instance_id, decrement_cap=False) else: logger.info('{} was not launched by spoptimize ... ignoring'.format( instance_id))
def test_term_instance_raise_exception(self): logger.debug('TestTerminateInstance.test_term_instance_raise_exception') self.mock_attrs['terminate_instance_in_auto_scaling_group.side_effect'] = Exception('Testing') asg_helper.autoscaling = Mock(**self.mock_attrs) with self.assertRaises(Exception): asg_helper.terminate_instance('i-abcd123', True)
def test_term_instance(self): logger.debug('TestTerminateInstance.test_term_instance') asg_helper.autoscaling = Mock(**self.mock_attrs) asg_helper.terminate_instance('i-abcd123', True) asg_helper.autoscaling.terminate_instance_in_auto_scaling_group.assert_called()