~andrewjbeach/juju-ci-tools/make-local-patcher

« back to all changes in this revision

Viewing changes to tests/test_clean_resources.py

  • Committer: Aaron Bentley
  • Date: 2014-02-24 17:18:29 UTC
  • mto: This revision was merged to the branch mainline in revision 252.
  • Revision ID: aaron.bentley@canonical.com-20140224171829-sz644yhoygu7m9dm
Use tags to identify and shut down instances.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from argparse import Namespace
2
 
from mock import (
3
 
    MagicMock,
4
 
    patch,
5
 
)
6
 
 
7
 
from clean_resources import (
8
 
    clean,
9
 
    get_regions,
10
 
    main,
11
 
    parse_args,
12
 
)
13
 
import clean_resources
14
 
from tests import TestCase
15
 
from tests.test_substrate import get_aws_env
16
 
 
17
 
__metaclass__ = type
18
 
 
19
 
 
20
 
class CleanResources(TestCase):
21
 
 
22
 
    def test_parse_args_default(self):
23
 
        args = parse_args(['default-aws'])
24
 
        self.assertEqual(args, Namespace(all_regions=False, env='default-aws',
25
 
                                         verbose=0))
26
 
 
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',
30
 
                                         verbose=1))
31
 
 
32
 
    def test_get_regions(self):
33
 
        class FakeEnv:
34
 
            config = {'region': 'foo'}
35
 
        args = Namespace(all_regions=False)
36
 
        env = FakeEnv()
37
 
        regions = get_regions(args, env)
38
 
        self.assertEqual(regions, ['foo'])
39
 
 
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))
47
 
 
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)))
52
 
 
53
 
    def test_clean_single_region(self):
54
 
        self.asses_clean(all_region=False, call_count=1)
55
 
 
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:
61
 
            clean(args)
62
 
        self.assertEqual(
63
 
            clean_resources.AWSAccount.manager_from_config.call_count,
64
 
            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)
68
 
        self.assertEqual(
69
 
            ctx_mock.iter_instance_security_groups.call_count, call_count)
70
 
        self.assertEqual(
71
 
            ctx_mock.delete_detached_interfaces.call_count, call_count)
72
 
        self.assertEqual(
73
 
            ctx_mock.destroy_security_groups.call_count, call_count)
74
 
        cr_mock.assert_called_once_with('foo')
75
 
 
76
 
    def test_main(self):
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:
81
 
                main()
82
 
        pa_mock.assert_called_once_with()
83
 
        cln_mock.assert_called_once_with(args)