import copy import json import os import unittest from botocore.exceptions import ClientError from mock import Mock import ec2_helper from logging_helper import logging, setup_stream_handler logger = logging.getLogger() logger.addHandler(logging.NullHandler()) here = os.path.dirname(os.path.realpath(__file__)) mocks_dir = os.path.join(here, 'resources', 'mock_data', 'ec2') mock_attrs = {} for file in os.listdir(mocks_dir): if file.endswith('.json'): with open(os.path.join(mocks_dir, file)) as j: mock_attrs['{}.return_value'.format(file.split('.')[0])] = json.loads(j.read()) class TestTerminateInstance(unittest.TestCase): def setUp(self): ec2_helper.ec2 = Mock() def test_terminate_instance(self): logger.debug('TestTerminateInstance.terminate_instance') ec2_helper.terminate_instance('i-abcd123') ec2_helper.ec2.terminate_instances.assert_called()
import boto3 import copy import os from logging_helper import logging from botocore.exceptions import ClientError logger = logging.getLogger() logging.getLogger('boto3').setLevel(logging.WARNING) logging.getLogger('botocore').setLevel(logging.WARNING) # this adds vendored directory to the Python import path here = os.path.dirname(os.path.realpath(__file__)) mocks_dir = os.path.join(here, 'resources', 'mock_data') ec2 = boto3.client('ec2') def terminate_instance(instance_id): ''' Terminates instance_id via the EC2 API No return value ''' logger.info('Terminating EC2 Instance {}'.format(instance_id)) try: ec2.terminate_instances(InstanceIds=[instance_id]) except ClientError as c: if c.response['Error']['Code'] == 'InvalidInstanceID.NotFound': logger.info('{} not found'.format(instance_id)) else: