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

« back to all changes in this revision

Viewing changes to tests/test_scale_out.py

  • Committer: Aaron Bentley
  • Date: 2015-08-19 15:07:08 UTC
  • mto: This revision was merged to the branch mainline in revision 1069.
  • Revision ID: aaron.bentley@canonical.com-20150819150708-88xesx4iardg12b4
Wait for proc to exit after signalling.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from argparse import Namespace
2
 
from contextlib import contextmanager
3
 
import logging
4
 
from mock import (
5
 
    call,
6
 
    patch,
7
 
)
8
 
from unittest import TestCase
9
 
 
10
 
from jujupy import (
11
 
    EnvJujuClient,
12
 
    JujuData,
13
 
)
14
 
from scale_out import (
15
 
    deploy_charms,
16
 
    get_service_name,
17
 
    parse_args,
18
 
    scale_out,
19
 
    scaleout_setup,
20
 
)
21
 
 
22
 
 
23
 
def fake_EnvJujuClient(env, path=None, debug=None):
24
 
    return EnvJujuClient(env=env, version='1.2.3.4', full_path=path)
25
 
 
26
 
 
27
 
class TestScaleOut(TestCase):
28
 
 
29
 
    @contextmanager
30
 
    def fake_client_cxt(self):
31
 
        env = JujuData('foo', {})
32
 
        client = fake_EnvJujuClient(env)
33
 
        bv_cxt = patch('scale_out.client_from_config',
34
 
                       return_value=client)
35
 
        with bv_cxt as bv_mock:
36
 
            yield (client, env, bv_mock)
37
 
 
38
 
    def test_parse_args(self):
39
 
        args = parse_args(
40
 
            ['env', '/path/juju', 'logs', 'temp_name', 'foo', 'bar'])
41
 
        expected = Namespace(
42
 
            agent_stream=None,
43
 
            agent_url=None,
44
 
            bootstrap_host=None,
45
 
            charms=['foo', 'bar'],
46
 
            debug=False,
47
 
            env='env',
48
 
            juju_bin='/path/juju',
49
 
            keep_env=False,
50
 
            logs='logs',
51
 
            machine=[],
52
 
            region=None,
53
 
            series=None,
54
 
            temp_env_name='temp_name',
55
 
            upload_tools=False,
56
 
            verbose=logging.INFO,
57
 
            deadline=None,
58
 
        )
59
 
        self.assertEqual(args, expected)
60
 
 
61
 
    @patch('scale_out.boot_context', autospec=True)
62
 
    @patch('jujupy.EnvJujuClient.add_ssh_machines', autospec=True)
63
 
    def test_scaleout_setup(
64
 
            self,
65
 
            add_ssh_machines_func,
66
 
            boot_context_func):
67
 
 
68
 
        args = Namespace(
69
 
            agent_stream=None,
70
 
            agent_url=None,
71
 
            bootstrap_host=None,
72
 
            charms='ubuntu',
73
 
            debug=False,
74
 
            env='test_env',
75
 
            juju_bin='/path/juju',
76
 
            keep_env=False,
77
 
            logs='/tmp/logs',
78
 
            machine=['0'],
79
 
            region=None,
80
 
            series=None,
81
 
            temp_env_name='temp_name',
82
 
            upload_tools=False,
83
 
            verbose=logging.INFO,
84
 
            deadline=None,
85
 
        )
86
 
 
87
 
        with self.fake_client_cxt() as (fake_client, fake_env, bv_mock):
88
 
            with scaleout_setup(args) as client:
89
 
                pass
90
 
        boot_context_func.assert_called_once_with(
91
 
            args.temp_env_name,
92
 
            client,
93
 
            args.bootstrap_host,
94
 
            args.machine,
95
 
            'trusty',
96
 
            args.agent_url,
97
 
            args.agent_stream,
98
 
            args.logs,
99
 
            args.keep_env,
100
 
            args.upload_tools,
101
 
            region=args.region)
102
 
        bv_mock.assert_called_once_with('test_env', '/path/juju', False,
103
 
                                        soft_deadline=None)
104
 
        add_ssh_machines_func.assert_called_once_with(client, ['0'])
105
 
        self.assertIs(client, fake_client)
106
 
 
107
 
    def test_scaleout_setup_sets_series(self):
108
 
 
109
 
        args = Namespace(
110
 
            agent_stream=None,
111
 
            agent_url=None,
112
 
            bootstrap_host=None,
113
 
            charms='ubuntu some_other_charm',
114
 
            debug=False,
115
 
            env='test_env',
116
 
            juju_bin='/path/juju',
117
 
            keep_env=False,
118
 
            logs='/tmp/logs',
119
 
            machine=['0'],
120
 
            region=None,
121
 
            series='my_series',
122
 
            temp_env_name='temp_name',
123
 
            upload_tools=False,
124
 
            verbose=logging.INFO,
125
 
            deadline=None,
126
 
        )
127
 
 
128
 
        with self.fake_client_cxt():
129
 
            with patch('scale_out.boot_context', autospec=True) as bc_mock:
130
 
                with patch('jujupy.EnvJujuClient.add_ssh_machines',
131
 
                           autospec=True):
132
 
                    with scaleout_setup(args) as client:
133
 
                        pass
134
 
        bc_mock.assert_called_once_with(
135
 
            args.temp_env_name,
136
 
            client,
137
 
            args.bootstrap_host,
138
 
            args.machine,
139
 
            'my_series',
140
 
            args.agent_url,
141
 
            args.agent_stream,
142
 
            args.logs,
143
 
            args.keep_env,
144
 
            args.upload_tools,
145
 
            region=args.region)
146
 
 
147
 
    def test_deploy_charms(self):
148
 
        with self.fake_client_cxt() as (client, env, bv_mock):
149
 
            with patch.object(EnvJujuClient, 'deploy') as d_mock:
150
 
                with patch.object(EnvJujuClient,
151
 
                                  'wait_for_started') as wfs_mock:
152
 
                    deploy_charms(client, ['ubuntu', 'mysql'])
153
 
        expected = [call('ubuntu', service='ubuntu'),
154
 
                    call('mysql', service='mysql')]
155
 
        self.assertEqual(d_mock.mock_calls, expected)
156
 
        wfs_mock.assert_called_once_with()
157
 
 
158
 
    def test_deploy_charms_local(self):
159
 
        with self.fake_client_cxt() as (client, env, bv_mock):
160
 
            with patch.object(EnvJujuClient, 'deploy') as d_mock:
161
 
                with patch.object(EnvJujuClient,
162
 
                                  'wait_for_started') as wfs_mock:
163
 
                    deploy_charms(client, ['local:foo', 'local:bar'])
164
 
        expected = [call('local:foo', service='foo'),
165
 
                    call('local:bar', service='bar')]
166
 
        self.assertEqual(d_mock.mock_calls, expected)
167
 
        wfs_mock.assert_called_once_with()
168
 
 
169
 
    def test_scale_out(self):
170
 
        with self.fake_client_cxt() as (client, env, bv_mock):
171
 
            with patch.object(EnvJujuClient, 'juju') as j_mock:
172
 
                with patch.object(EnvJujuClient,
173
 
                                  'wait_for_started') as wfs_mock:
174
 
                    scale_out(client, 'ubuntu')
175
 
        j_mock.assert_called_once_with('add-unit', ('ubuntu', '-n', '5'))
176
 
        wfs_mock.assert_called_once_with()
177
 
 
178
 
    def test_get_service_name(self):
179
 
        charms = [('charm-name', 'charm-name'),
180
 
                  ('charm-name-21', 'charm-name-21'),
181
 
                  ('series/charm-name-13', 'charm-name-13'),
182
 
                  ('local:charm-name', 'charm-name'),
183
 
                  ('cs:~user/charm-name', 'charm-name'),
184
 
                  ('cs:charm-name-2-1', 'charm-name-2-1'),
185
 
                  ('lp:~user/some/path/to/charm-name', 'charm-name')]
186
 
        for charm, expected in charms:
187
 
            result = get_service_name(charm)
188
 
            self.assertEqual(result, expected)