1
from argparse import Namespace
7
from clean_resources import (
13
import clean_resources
14
from tests import TestCase
15
from tests.test_substrate import get_aws_env
20
class CleanResources(TestCase):
22
def test_parse_args_default(self):
23
args = parse_args(['default-aws'])
24
self.assertEqual(args, Namespace(all_regions=False, env='default-aws',
27
def test_parse_args_all_regions(self):
28
args = parse_args(['default-aws', '--verbose', '--all-regions'])
29
self.assertEqual(args, Namespace(all_regions=True, env='default-aws',
32
def test_get_regions(self):
34
config = {'region': 'foo'}
35
args = Namespace(all_regions=False)
37
regions = get_regions(args, env)
38
self.assertEqual(regions, ['foo'])
40
def test_get_regions_all_regions(self):
41
args = Namespace(all_regions=True)
42
supported_regions = {'ap-southeast-1', 'ap-southeast-2',
43
'us-west-2', 'us-east-1', 'us-west-1',
44
'sa-east-1', 'ap-northeast-1', 'eu-west-1'}
45
all_regions = set(get_regions(args, None))
46
self.assertTrue(all_regions.issuperset(supported_regions))
48
def test_clean_all_regions(self):
49
args = Namespace(all_regions=True)
50
self.asses_clean(all_region=True,
51
call_count=len(get_regions(args, None)))
53
def test_clean_single_region(self):
54
self.asses_clean(all_region=False, call_count=1)
56
def asses_clean(self, all_region, call_count):
57
args = Namespace(env='foo', verbose=0, all_regions=all_region)
58
clean_resources.AWSAccount = MagicMock(spec=['manager_from_config'])
59
with patch('clean_resources.SimpleEnvironment.from_config',
60
return_value=get_aws_env()) as cr_mock:
63
clean_resources.AWSAccount.manager_from_config.call_count,
65
mfc_mock = clean_resources.AWSAccount.manager_from_config.return_value
66
ctx_mock = mfc_mock.__enter__.return_value
67
self.assertEqual(ctx_mock.iter_security_groups.call_count, call_count)
69
ctx_mock.iter_instance_security_groups.call_count, call_count)
71
ctx_mock.delete_detached_interfaces.call_count, call_count)
73
ctx_mock.destroy_security_groups.call_count, call_count)
74
cr_mock.assert_called_once_with('foo')
77
args = Namespace(env='foo', verbose=0, all_regions=True)
78
with patch('clean_resources.parse_args', autospec=True,
79
return_value=args) as pa_mock:
80
with patch('clean_resources.clean', autospec=True) as cln_mock:
82
pa_mock.assert_called_once_with()
83
cln_mock.assert_called_once_with(args)