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

« back to all changes in this revision

Viewing changes to test_assess_heterogeneous_control.py

  • Committer: Curtis Hovey
  • Date: 2015-06-11 19:35:22 UTC
  • mto: This revision was merged to the branch mainline in revision 983.
  • Revision ID: curtis@canonical.com-20150611193522-o2nqkqb04o2i75wv
Remove euca_dump_logs because it has not been used this year.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
__metaclass__ = type
 
2
 
1
3
from argparse import Namespace
2
4
import os
3
5
from unittest import TestCase
4
6
 
5
 
from mock import (
6
 
    MagicMock,
7
 
    patch,
8
 
)
 
7
from mock import patch
9
8
 
10
9
from assess_heterogeneous_control import (
11
 
    assess_heterogeneous,
12
 
    check_series,
 
10
    dumping_env,
13
11
    get_clients,
14
12
    parse_args,
15
 
    test_control_heterogeneous,
16
 
)
17
 
from fakejuju import (
18
 
    fake_juju_client,
19
 
    fake_juju_client_optional_jes,
20
 
)
 
13
    upload_heterogeneous,
 
14
    )
21
15
from jujupy import (
 
16
    EnvJujuClient,
 
17
    SimpleEnvironment,
22
18
    _temp_env,
23
 
)
24
 
from tests.test_deploy_stack import FakeBootstrapManager
25
 
 
26
 
__metaclass__ = type
 
19
    )
 
20
 
 
21
 
 
22
class TestDumping_env(TestCase):
 
23
 
 
24
    def test_dumping_env_exception(self):
 
25
        client = EnvJujuClient(SimpleEnvironment('env'), '5', '4')
 
26
        with patch.object(client, 'destroy_environment') as de_mock:
 
27
            with patch('subprocess.check_call') as cc_mock:
 
28
                with patch('deploy_stack.copy_remote_logs') as crl_mock:
 
29
                    with self.assertRaises(ValueError):
 
30
                        with dumping_env(client, 'foo', 'bar'):
 
31
                            raise ValueError
 
32
        crl_mock.assert_called_once_with('foo', 'bar')
 
33
        cc_mock.assert_called_once_with(['gzip', '-f'])
 
34
        de_mock.assert_called_once_with()
 
35
 
 
36
    def test_dumping_env_success(self):
 
37
        client = EnvJujuClient(SimpleEnvironment('env'), '5', '4')
 
38
        with patch.object(client, 'destroy_environment') as de_mock:
 
39
            with patch('subprocess.check_call') as cc_mock:
 
40
                with patch('deploy_stack.copy_remote_logs') as crl_mock:
 
41
                    with dumping_env(client, 'foo', 'bar'):
 
42
                        pass
 
43
        crl_mock.assert_called_once_with('foo', 'bar')
 
44
        cc_mock.assert_called_once_with(['gzip', '-f'])
 
45
        self.assertEqual(de_mock.call_count, 0)
27
46
 
28
47
 
29
48
class TestParseArgs(TestCase):
33
52
        self.assertEqual(args, Namespace(
34
53
            initial='a', other='b', base_environment='c',
35
54
            environment_name='d', log_dir='e', debug=False,
36
 
            upload_tools=False, agent_url=None, agent_stream=None, series=None,
 
55
            upload_tools=False, agent_url=None,
37
56
            user=os.environ.get('JENKINS_USER'),
38
57
            password=os.environ.get('JENKINS_PASSWORD')))
39
58
 
44
63
        self.assertEqual(args.user, 'my name')
45
64
        self.assertEqual(args.password, 'fake pass')
46
65
 
47
 
    def test_parse_args_agent_stream(self):
48
 
        args = parse_args(['a', 'b', 'c', 'd', 'e',
49
 
                           '--agent-stream', 'proposed',
50
 
                           '--user', 'my name', '--password', 'fake pass'])
51
 
        self.assertEqual(args.agent_stream, 'proposed')
52
 
        self.assertEqual(args.user, 'my name')
53
 
        self.assertEqual(args.password, 'fake pass')
54
 
 
55
 
    def test_parse_args_series(self):
56
 
        args = parse_args(['a', 'b', 'c', 'd', 'e', '--series', 'trusty',
57
 
                           '--user', 'my name', '--password', 'fake pass'])
58
 
        self.assertEqual(args.series, 'trusty')
59
 
        self.assertEqual(args.user, 'my name')
60
 
        self.assertEqual(args.password, 'fake pass')
61
 
 
62
66
 
63
67
class TestGetClients(TestCase):
64
68
 
72
76
        with _temp_env({'environments': {'baz': {}}}):
73
77
            with patch('subprocess.check_output', lambda x: boo[x]):
74
78
                initial, other, released = get_clients('foo', 'bar', 'baz',
75
 
                                                       True, 'quxx')
 
79
                                                       'qux', True, 'quxx')
76
80
        self.assertEqual(initial.env, other.env)
77
81
        self.assertEqual(initial.env, released.env)
78
 
        self.assertNotIn('tools-metadata-url', initial.env.config)
 
82
        self.assertEqual(initial.env.config['tools-metadata-url'], 'quxx')
79
83
        self.assertEqual(initial.full_path, os.path.abspath('foo'))
80
84
        self.assertEqual(other.full_path, os.path.abspath('bar'))
81
85
        self.assertEqual(released.full_path, '/usr/bun/juju')
82
86
 
83
 
    def test_get_clients_different_env(self):
84
 
        boo = {
85
 
            ('foo', '--version'): '1.18.73',
86
 
            ('bar', '--version'): '1.18.74',
87
 
            ('juju', '--version'): '2.0',
88
 
            ('which', 'juju'): '/usr/bun/juju'
89
 
            }
90
 
        with _temp_env({'environments': {'baz': {}}}):
91
 
            with patch('subprocess.check_output', lambda x: boo[x]):
92
 
                with patch('jujupy.JujuData.load_yaml'):
93
 
                    initial, other, teardown = get_clients('foo', 'bar', 'baz',
94
 
                                                           True, 'quxx')
95
 
        self.assertIs(initial, teardown)
96
 
 
97
 
    def test_old_released(self):
98
 
        boo = {
99
 
            ('foo', '--version'): '2.1',
100
 
            ('bar', '--version'): '2.0',
101
 
            ('juju', '--version'): '1.18',
102
 
            ('which', 'juju'): '/usr/bun/juju'
103
 
            }
104
 
        with _temp_env({'environments': {'baz': {}}}):
105
 
            with patch('subprocess.check_output', lambda x: boo[x]):
106
 
                with patch('jujupy.JujuData.load_yaml'):
107
 
                    initial, other, teardown = get_clients('foo', 'bar', 'baz',
108
 
                                                           True, 'quxx')
109
 
        self.assertIs(initial, teardown)
110
 
 
111
87
    def test_get_clients_no_agent(self):
112
88
        with _temp_env({'environments': {'baz': {}}}):
113
89
            with patch('subprocess.check_output', return_value='1.18.73'):
114
90
                initial, other, released = get_clients('foo', 'bar', 'baz',
115
 
                                                       True, None)
 
91
                                                       'qux', True, None)
116
92
        self.assertTrue('tools-metadata-url' not in initial.env.config)
117
93
 
118
94
 
119
 
class TestAssessHeterogeneous(TestCase):
120
 
 
121
 
    @patch('assess_heterogeneous_control.BootstrapManager')
122
 
    @patch('assess_heterogeneous_control.test_control_heterogeneous',
123
 
           autospec=True)
124
 
    @patch('assess_heterogeneous_control.get_clients', autospec=True)
125
 
    def test_assess_heterogeneous(self, gc_mock, ch_mock, bm_mock):
126
 
        initial = MagicMock()
127
 
        gc_mock.return_value = (
128
 
            initial, 'other_client', 'released_client')
129
 
        assess_heterogeneous(
130
 
            'initial', 'other', 'base_env', 'environment_name', 'log_dir',
131
 
            False, False, 'agent_url', 'agent_stream', 'series')
132
 
        gc_mock.assert_called_once_with(
133
 
            'initial', 'other', 'base_env', False, 'agent_url')
134
 
        is_jes_enabled = initial.is_jes_enabled.return_value
135
 
        bm_mock.assert_called_once_with(
136
 
            'environment_name', initial, 'released_client',
137
 
            agent_stream='agent_stream', agent_url='agent_url',
138
 
            bootstrap_host=None, jes_enabled=is_jes_enabled, keep_env=False,
139
 
            log_dir='log_dir', machines=[], permanent=is_jes_enabled,
140
 
            region=None, series='series')
141
 
        ch_mock.assert_called_once_with(
142
 
            bm_mock.return_value, 'other_client', False)
143
 
 
144
 
 
145
 
class TestTestControlHeterogeneous(TestCase):
146
 
 
147
 
    def test_test_control_heterogeneous(self):
148
 
        client = fake_juju_client_optional_jes(jes_enabled=False)
149
 
        bs_manager = FakeBootstrapManager(client)
150
 
        # Prevent teardown
151
 
        bs_manager.tear_down_client = MagicMock()
152
 
        bs_manager.tear_down_client.destroy_environment.return_value = 0
153
 
        with patch.object(client, 'kill_controller'):
154
 
            test_control_heterogeneous(bs_manager, client, True)
155
 
        models = client._backend.controller_state.models
156
 
        model_state = models[client.model_name]
157
 
        self.assertEqual(model_state.exposed, {'sink2', 'dummy-sink'})
158
 
        self.assertEqual(model_state.machines, {'0', '1', '2'})
159
 
        self.assertEqual(client.env.juju_home, 'foo')
160
 
 
161
 
    def test_same_home(self):
162
 
        initial_client = fake_juju_client(version='1.25')
163
 
        other_client = fake_juju_client(
164
 
            env=initial_client.env,
165
 
            _backend=initial_client._backend.clone(version='2.0.0'))
166
 
        bs_manager = FakeBootstrapManager(initial_client)
167
 
        bs_manager.permanent = True
168
 
        test_control_heterogeneous(bs_manager, other_client, True)
169
 
        self.assertEqual(initial_client.env.juju_home,
170
 
                         other_client.env.juju_home)
171
 
 
172
 
 
173
 
class TestCheckSeries(TestCase):
174
 
 
175
 
    def test_check_series(self):
176
 
        client = fake_juju_client()
177
 
        client.bootstrap()
178
 
        check_series(client)
179
 
 
180
 
    def test_check_series_xenial(self):
181
 
        client = MagicMock(spec=["get_juju_output"])
182
 
        client.get_juju_output.return_value = "Codename:        xenial"
183
 
        check_series(client, 1, 'xenial')
184
 
 
185
 
    def test_check_series_calls(self):
186
 
        client = MagicMock(spec=["get_juju_output"])
187
 
        with patch.object(client, 'get_juju_output',
188
 
                          return_value="Codename:       xenial") as gjo_mock:
189
 
            check_series(client, 2, 'xenial')
190
 
        gjo_mock.assert_called_once_with('ssh', 2, 'lsb_release', '-c')
191
 
 
192
 
    def test_check_series_exceptionl(self):
193
 
        client = fake_juju_client()
194
 
        client.bootstrap()
195
 
        with self.assertRaisesRegexp(
196
 
                AssertionError, 'Series is angsty, not xenial'):
197
 
            check_series(client, '0', 'xenial')
 
95
class TestUploadHeterogeneous(TestCase):
 
96
 
 
97
    def test_upload_heterogeneous(self):
 
98
        args = Namespace(user='foo', password='bar')
 
99
        env_ctx = patch(
 
100
            'upload_hetero_control.HUploader.upload_by_env_build_number')
 
101
        with patch('upload_hetero_control.get_s3_access',
 
102
                   return_value=('name', 'pass')) as a_mock:
 
103
            with patch('upload_hetero_control.S3Connection') as c_mock:
 
104
                with env_ctx as u_mock:
 
105
                    upload_heterogeneous(args)
 
106
        a_mock.assert_called_once_with()
 
107
        c_mock.assert_called_once_with('name', 'pass')
 
108
        u_mock.assert_called_once_with()