~dooferlad/juju-ci-tools/juju-ci-tools-addressable-containers

« back to all changes in this revision

Viewing changes to test_jujupy.py

  • Committer: Nate Finch
  • Date: 2015-06-02 03:47:22 UTC
  • mfrom: (968 origin/trunk)
  • mto: This revision was merged to the branch mainline in revision 976.
  • Revision ID: nate.finch@canonical.com-20150602034722-cr3lzq2yb6xdh7gz
merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
    EnvJujuClient,
32
32
    EnvJujuClient22,
33
33
    EnvJujuClient24,
 
34
    EnvJujuClient25,
34
35
    ErroredUnit,
35
36
    GroupReporter,
36
37
    get_local_root,
59
60
    test_case.assertEqual(args, (expected_args,))
60
61
    kwarg_keys = ['env']
61
62
    if assign_stderr:
62
 
        kwarg_keys = ['stderr'] + kwarg_keys
63
 
        test_case.assertEqual(type(kwargs['stderr']), file)
 
63
        with tempfile.TemporaryFile() as example:
 
64
            # 'example' is a pragmatic way of checking file types in py2 and 3.
 
65
            kwarg_keys = ['stderr'] + kwarg_keys
 
66
            test_case.assertIsInstance(kwargs['stderr'], type(example))
64
67
    test_case.assertItemsEqual(kwargs.keys(), kwarg_keys)
65
68
    bin_dir = os.path.dirname(client.full_path)
66
69
    test_case.assertRegexpMatches(kwargs['env']['PATH'],
83
86
        self.pause_mock = patcher.start()
84
87
 
85
88
 
86
 
class TestEnvJujuClient24(ClientTest):
 
89
class TestEnvJujuClient25(ClientTest):
 
90
 
 
91
    client_class = EnvJujuClient25
87
92
 
88
93
    def test__shell_environ_not_cloudsigma(self):
89
 
        client = EnvJujuClient24(
90
 
            SimpleEnvironment('baz', {'type': 'ec2'}), '1.24-foobar', 'path')
 
94
        client = self.client_class(
 
95
            SimpleEnvironment('baz', {'type': 'ec2'}), '1.25-foobar', 'path')
91
96
        env = client._shell_environ()
92
97
        self.assertEqual(env.get(JUJU_DEV_FEATURE_FLAGS, ''), '')
93
98
 
94
99
    def test__shell_environ_cloudsigma(self):
95
 
        client = EnvJujuClient24(
 
100
        client = self.client_class(
96
101
            SimpleEnvironment('baz', {'type': 'cloudsigma'}),
97
 
            '1.24-foobar', 'path')
 
102
            '1.25-foobar', 'path')
98
103
        env = client._shell_environ()
99
104
        "".split()
100
105
        self.assertTrue('cloudsigma' in env[JUJU_DEV_FEATURE_FLAGS].split(","))
101
106
 
102
107
    def test__shell_environ_juju_home(self):
103
108
        client = EnvJujuClient24(
104
 
            SimpleEnvironment('baz', {'type': 'ec2'}), '1.24-foobar', 'path')
 
109
            SimpleEnvironment('baz', {'type': 'ec2'}), '1.25-foobar', 'path')
105
110
        env = client._shell_environ(juju_home='asdf')
106
111
        self.assertEqual(env['JUJU_HOME'], 'asdf')
107
112
 
116
121
 
117
122
    def test__shell_environ_juju_home(self):
118
123
        client = EnvJujuClient22(
119
 
            SimpleEnvironment('baz', {'type': 'ec2'}), '1.24-foobar', 'path')
 
124
            SimpleEnvironment('baz', {'type': 'ec2'}), '1.22-foobar', 'path')
120
125
        env = client._shell_environ(juju_home='asdf')
121
126
        self.assertEqual(env['JUJU_HOME'], 'asdf')
122
127
 
123
128
 
 
129
class TestEnvJujuClient24(TestEnvJujuClient25):
 
130
 
 
131
    client_class = EnvJujuClient25
 
132
 
 
133
 
124
134
class TestEnvJujuClient(ClientTest):
125
135
 
126
136
    def test_get_version(self):
177
187
            yield '1.24-alpha1'
178
188
            yield '1.24.7'
179
189
            yield '1.25.1'
 
190
            yield '1.26.1'
180
191
 
181
192
        context = patch.object(
182
193
            EnvJujuClient, 'get_version',
199
210
            self.assertIs(type(client), EnvJujuClient24)
200
211
            self.assertEqual(client.version, '1.24.7')
201
212
            client = EnvJujuClient.by_version(None)
 
213
            self.assertIs(type(client), EnvJujuClient25)
 
214
            self.assertEqual(client.version, '1.25.1')
 
215
            client = EnvJujuClient.by_version(None)
202
216
            self.assertIs(type(client), EnvJujuClient)
203
 
            self.assertEqual(client.version, '1.25.1')
 
217
            self.assertEqual(client.version, '1.26.1')
204
218
 
205
219
    def test_by_version_path(self):
206
220
        with patch('subprocess.check_output', return_value=' 4.3') as vsn:
231
245
        self.assertEqual((
232
246
            'juju', '--debug', 'bar', '-e', 'foo', 'baz', 'qux'), full)
233
247
 
 
248
    def test_full_args_action(self):
 
249
        env = Environment('foo', '')
 
250
        client = EnvJujuClient(env, None, 'my/juju/bin')
 
251
        full = client._full_args('action bar', False, ('baz', 'qux'))
 
252
        self.assertEqual((
 
253
            'juju', '--show-log', 'action', 'bar', '-e', 'foo', 'baz', 'qux'),
 
254
            full)
 
255
 
234
256
    def test_bootstrap_hpcloud(self):
235
257
        env = Environment('hp', '')
236
258
        with patch.object(env, 'hpcloud', lambda: True):
256
278
            client = EnvJujuClient(env, None, None)
257
279
            with patch.object(client.env, 'joyent', lambda: True):
258
280
                client.bootstrap()
259
 
            mock.assert_called_once_with(client,
260
 
                                         'bootstrap',
261
 
                                         ('--constraints',
262
 
                                          'mem=2G cpu-cores=1'),
263
 
                                         False,
264
 
                                         juju_home=None)
 
281
            mock.assert_called_once_with(
 
282
                client, 'bootstrap', ('--constraints', 'mem=2G cpu-cores=1'),
 
283
                False, juju_home=None)
265
284
 
266
285
    def test_bootstrap_non_sudo(self):
267
286
        env = Environment('foo', '')
412
431
        env = Environment('foo', '')
413
432
        client = EnvJujuClient(env, None, '/foobar/bar')
414
433
        with patch('subprocess.check_output') as sco_mock:
415
 
            client.get_juju_output(env, 'baz')
 
434
            client.get_juju_output('cmd', 'baz')
416
435
        self.assertRegexpMatches(sco_mock.call_args[1]['env']['PATH'],
417
436
                                 r'/foobar\:')
418
437
 
1337
1356
            'services': {
1338
1357
                'jenkins': {
1339
1358
                    'units': {
1340
 
                        'jenkins/1': {'baz': 'qux'}
 
1359
                        'jenkins/1': {
 
1360
                            'subordinates': {
 
1361
                                'sub': {'baz': 'qux'}
 
1362
                            }
 
1363
                        }
1341
1364
                    }
1342
1365
                }
1343
1366
            }
1344
1367
        }, '')
1345
1368
        expected = [
1346
 
            ('1', {'foo': 'bar'}), ('jenkins/1', {'baz': 'qux'})]
 
1369
            ('1', {'foo': 'bar'}),
 
1370
            ('jenkins/1', {'subordinates': {'sub': {'baz': 'qux'}}}),
 
1371
            ('sub', {'baz': 'qux'})]
1347
1372
        self.assertItemsEqual(expected, status.agent_items())
1348
1373
 
1349
1374
    def test_agent_items_containers(self):
1398
1423
 
1399
1424
    def test_get_unit(self):
1400
1425
        status = Status({
 
1426
            'machines': {
 
1427
                '1': {},
 
1428
            },
1401
1429
            'services': {
1402
1430
                'jenkins': {
1403
1431
                    'units': {
1418
1446
        with self.assertRaisesRegexp(KeyError, 'jenkins/3'):
1419
1447
            status.get_unit('jenkins/3')
1420
1448
 
 
1449
    def test_get_subordinate_units(self):
 
1450
        status = Status({
 
1451
            'machines': {
 
1452
                '1': {},
 
1453
            },
 
1454
            'services': {
 
1455
                'ubuntu': {},
 
1456
                'jenkins': {
 
1457
                    'units': {
 
1458
                        'jenkins/1': {
 
1459
                            'subordinates': {
 
1460
                                'chaos-monkey/0': {'agent-state': 'started'},
 
1461
                            }
 
1462
                        }
 
1463
                    }
 
1464
                },
 
1465
                'dummy-sink': {
 
1466
                    'units': {
 
1467
                        'jenkins/2': {
 
1468
                            'subordinates': {
 
1469
                                'chaos-monkey/1': {'agent-state': 'started'}
 
1470
                            }
 
1471
                        },
 
1472
                        'jenkins/3': {
 
1473
                            'subordinates': {
 
1474
                                'chaos-monkey/2': {'agent-state': 'started'}
 
1475
                            }
 
1476
                        }
 
1477
                    }
 
1478
                }
 
1479
            }
 
1480
        }, '')
 
1481
        self.assertEqual(
 
1482
            status.get_subordinate_units('ubuntu'),
 
1483
            [])
 
1484
        self.assertEqual(
 
1485
            status.get_subordinate_units('jenkins'),
 
1486
            [{'chaos-monkey/0': {'agent-state': 'started'}}])
 
1487
        self.assertEqual(
 
1488
            status.get_subordinate_units('dummy-sink'), [
 
1489
                {'chaos-monkey/1': {'agent-state': 'started'}},
 
1490
                {'chaos-monkey/2': {'agent-state': 'started'}}]
 
1491
            )
 
1492
        with self.assertRaisesRegexp(KeyError, 'foo'):
 
1493
            status.get_subordinate_units('foo')
 
1494
 
 
1495
    def test_get_subordinate_units_no_services(self):
 
1496
        status = Status({}, '')
 
1497
        with self.assertRaisesRegexp(KeyError, 'ubuntu'):
 
1498
            status.get_subordinate_units('ubuntu')
 
1499
 
1421
1500
    def test_get_open_ports(self):
1422
1501
        status = Status({
 
1502
            'machines': {
 
1503
                '1': {},
 
1504
            },
1423
1505
            'services': {
1424
1506
                'jenkins': {
1425
1507
                    'units': {
1485
1567
            'services': {
1486
1568
                'jenkins': {
1487
1569
                    'units': {
1488
 
                        'jenkins/1': {'agent-state': 'started'},
 
1570
                        'jenkins/1': {
 
1571
                            'agent-state': 'started',
 
1572
                            'subordinates': {
 
1573
                                'sub1': {
 
1574
                                    'agent-state': 'started'
 
1575
                                }
 
1576
                            }
 
1577
                        },
1489
1578
                        'jenkins/2': {'agent-state': 'started'},
1490
1579
                    }
1491
1580
                }
1816
1905
                         'bundle:~juju-qa/some-bundle'), True
1817
1906
        )
1818
1907
 
 
1908
    def test_deployer_with_bundle_name(self):
 
1909
        client = EnvJujuClient(SimpleEnvironment(None, {'type': 'local'}),
 
1910
                               '1.23-series-arch', None)
 
1911
        with patch.object(EnvJujuClient, 'juju') as mock:
 
1912
            client.deployer('bundle:~juju-qa/some-bundle', 'name')
 
1913
        mock.assert_called_with(
 
1914
            'deployer', ('--debug', '--deploy-delay', '10', '--config',
 
1915
                         'bundle:~juju-qa/some-bundle', 'name'), True
 
1916
        )
 
1917
 
1819
1918
    def test_quickstart_maas(self):
1820
1919
        client = EnvJujuClient(SimpleEnvironment(None, {'type': 'maas'}),
1821
1920
                               '1.23-series-arch', None)