531
522
def test_make_industrial_test(self):
532
523
mit = MultiIndustrialTest('foo-env', 'bar-path', AttemptSuiteFactory([
533
524
DestroyEnvironmentAttempt]), 'log-dir', 5)
534
with self.patch_client(
535
lambda x, y=None, debug=False: fake_juju_client(
536
JujuData(x, {}, juju_home=''), full_path=y)):
525
with self.patch_client(lambda x, y=None, debug=False: (x, y)):
537
526
industrial = mit.make_industrial_test()
538
old_client = industrial.old_client
539
self.assertEqual((old_client.env, old_client.full_path), (
540
JujuData('foo-env-old', {'name': 'foo-env-old'}, juju_home=''),
542
new_client = industrial.new_client
543
self.assertEqual((new_client.env, new_client.full_path), (
544
JujuData('foo-env-new', {'name': 'foo-env-new'}, juju_home=''),
527
self.assertEqual(industrial.old_client,
528
(SimpleEnvironment('foo-env-old', {}), None))
529
self.assertEqual(industrial.new_client,
530
(SimpleEnvironment('foo-env-new', {}), 'bar-path'))
546
531
self.assertEqual(len(industrial.stage_attempts), 1)
547
532
self.assertEqual([mit.stages], [sa.attempt_list for sa in
548
533
industrial.stage_attempts])
551
536
mit = MultiIndustrialTest('foo-env', 'bar-path',
552
537
AttemptSuiteFactory([]), 'log-dir',
553
538
new_agent_url='http://example.com')
554
with self.patch_client(
555
lambda x, y=None, debug=False: fake_juju_client(full_path=y)):
539
with self.patch_client(lambda x, y=None, debug=False: (x, y)):
556
540
industrial = mit.make_industrial_test()
557
541
self.assertEqual(
558
(industrial.new_client.env, industrial.new_client.full_path), (
559
JujuData('foo-env-new', {
561
'default-series': 'angsty',
563
'name': 'foo-env-new',
564
'tools-metadata-url': 'http://example.com',
542
industrial.new_client, (
543
SimpleEnvironment('foo-env-new', {
544
'tools-metadata-url': 'http://example.com'}),
572
551
new_agent_url='http://example.com')
574
553
def side_effect(x, y=None, debug=False):
575
return fake_juju_client(env=JujuData(x, {}, juju_home='x'),
576
full_path=y, debug=debug)
578
556
with self.patch_client(side_effect):
579
557
industrial = mit.make_industrial_test()
580
self.assertEqual(industrial.new_client.debug, False)
581
self.assertEqual(industrial.old_client.debug, False)
558
self.assertEqual(industrial.new_client, False)
559
self.assertEqual(industrial.old_client, False)
583
561
with self.patch_client(side_effect):
584
562
industrial = mit.make_industrial_test()
585
self.assertEqual(industrial.new_client.debug, True)
586
self.assertEqual(industrial.old_client.debug, True)
563
self.assertEqual(industrial.new_client, True)
564
self.assertEqual(industrial.old_client, True)
588
566
def test_update_results(self):
589
567
mit = MultiIndustrialTest('foo-env', 'bar-path',
848
826
def test_from_args(self):
849
827
def side_effect(x, y=None, debug=False):
850
return fake_juju_client(env=JujuData(x, {}), full_path=y)
851
with patch('industrial_test.client_from_config',
852
side_effect=side_effect):
853
industrial = IndustrialTest.from_args(
854
'foo', 'new-juju-path', [])
829
with patch('jujupy.EnvJujuClient.by_version', side_effect=side_effect):
830
with patch('jujupy.SimpleEnvironment.from_config',
831
side_effect=lambda x: SimpleEnvironment(x, {})):
832
industrial = IndustrialTest.from_args(
833
'foo', 'new-juju-path', [])
855
834
self.assertIsInstance(industrial, IndustrialTest)
856
old_client = industrial.old_client
857
self.assertEqual((old_client.env, old_client.full_path), (
858
JujuData('foo-old', {'name': 'foo-old'}), None))
859
new_client = industrial.new_client
860
self.assertEqual((new_client.env, new_client.full_path), (
861
JujuData('foo-new', {'name': 'foo-new'}),
863
self.assertNotEqual(old_client.env.environment,
864
new_client.env.environment)
835
self.assertEqual(industrial.old_client,
836
(SimpleEnvironment('foo-old', {}), None))
837
self.assertEqual(industrial.new_client,
838
(SimpleEnvironment('foo-new', {}), 'new-juju-path'))
839
self.assertNotEqual(industrial.old_client[0].environment,
840
industrial.new_client[0].environment)
866
842
def test_from_args_debug(self):
867
843
def side_effect(x, y=None, debug=False):
868
return fake_juju_client(full_path=y, debug=debug)
869
with patch('industrial_test.client_from_config',
870
side_effect=side_effect):
845
with patch('jujupy.EnvJujuClient.by_version', side_effect=side_effect):
871
846
with patch('jujupy.SimpleEnvironment.from_config'):
872
847
industrial = IndustrialTest.from_args(
873
848
'foo', 'new-juju-path', [], debug=False)
874
self.assertEqual(industrial.old_client.debug, False)
875
self.assertEqual(industrial.new_client.debug, False)
849
self.assertEqual(industrial.old_client, False)
850
self.assertEqual(industrial.new_client, False)
876
851
industrial = IndustrialTest.from_args(
877
852
'foo', 'new-juju-path', [], debug=True)
878
self.assertEqual(industrial.old_client.debug, True)
879
self.assertEqual(industrial.new_client.debug, True)
853
self.assertEqual(industrial.old_client, True)
854
self.assertEqual(industrial.new_client, True)
881
856
def test_run_stages(self):
882
857
old_client = FakeEnvJujuClient('old')
891
866
self.assertEqual(len(cc_mock.mock_calls), 0)
893
868
def test_run_stages_old_fail(self):
894
old_client = fake_juju_client()
895
new_client = fake_juju_client(full_path='bar-path')
869
old_client = FakeJujuClient()
870
new_client = FakeJujuClient(full_path='bar-path')
896
871
industrial = IndustrialTest(old_client, new_client, [
897
872
FakeStepAttempt.from_result(False, True),
898
873
FakeStepAttempt.from_result(True, True)])
909
884
('bootstrap', True, True),
910
885
('prepare-suite', True, True),
911
886
('foo-id', False, True)])
912
self.assertEqual('controller-killed',
913
old_client._backend.controller_state.state)
914
self.assertEqual('controller-killed',
915
new_client._backend.controller_state.state)
887
self.assertEqual('destroyed', old_client._backing_state.state)
888
self.assertEqual('destroyed', new_client._backing_state.state)
917
890
def test_run_stages_new_fail(self):
918
old_client = fake_juju_client()
919
new_client = fake_juju_client(full_path='bar-path')
891
old_client = FakeJujuClient()
892
new_client = FakeJujuClient(full_path='bar-path')
920
893
log_dir = use_context(self, temp_dir())
921
894
suite_factory = AttemptSuiteFactory([
922
895
FakeAttemptClass('foo', True, False, new_path='bar-path'),
930
903
('bootstrap', True, True),
931
904
('prepare-suite', True, True),
932
905
('foo-id', True, False)])
933
self.assertEqual('controller-killed',
934
old_client._backend.controller_state.state)
935
self.assertEqual('controller-killed',
936
new_client._backend.controller_state.state)
906
self.assertEqual('destroyed', old_client._backing_state.state)
907
self.assertEqual('destroyed', new_client._backing_state.state)
938
909
def test_run_stages_both_fail(self):
939
old_client = fake_juju_client()
940
new_client = fake_juju_client()
910
old_client = FakeJujuClient()
911
new_client = FakeJujuClient()
941
912
log_dir = use_context(self, temp_dir())
942
913
suite = AttemptSuiteFactory([
943
914
FakeAttemptClass('foo', False, False),
951
922
('bootstrap', True, True),
952
923
('prepare-suite', True, True),
953
924
('foo-id', False, False)])
954
self.assertEqual('controller-killed',
955
old_client._backend.controller_state.state)
956
self.assertEqual('controller-killed',
957
new_client._backend.controller_state.state)
925
self.assertEqual('destroyed', old_client._backing_state.state)
926
self.assertEqual('destroyed', new_client._backing_state.state)
959
928
def test_run_stages_recover_failure(self):
960
old_client = fake_juju_client()
961
new_client = fake_juju_client()
929
old_client = FakeJujuClient()
930
new_client = FakeJujuClient()
962
931
fsa = FakeStepAttempt([('foo', True, False), ('bar', True, True)])
963
932
industrial = IndustrialTest(old_client, new_client, [
964
933
fsa, FakeStepAttempt.from_result(True, True)])
984
953
list(industrial.run_stages())
986
955
def test_run_attempt(self):
987
old_client = fake_juju_client()
988
new_client = fake_juju_client()
956
old_client = FakeJujuClient()
957
new_client = FakeJujuClient()
989
958
attempt = FakeStepAttempt.from_result(True, True)
990
959
log_dir = use_context(self, temp_dir())
991
960
suite = AttemptSuiteFactory([attempt]).factory([], log_dir, None)
1183
1150
('bar-id', {'title': 'Bar title', 'report_on': False})]))
1186
def FakeEnvJujuClient(name='steve', version='1.2', full_path='/jbin/juju'):
1187
return EnvJujuClient(
1153
class FakeEnvJujuClient(EnvJujuClient):
1155
def __init__(self, name='steve'):
1156
super(FakeEnvJujuClient, self).__init__(
1188
1157
JujuData(name, {'type': 'fake', 'region': 'regionx'}),
1158
'1.2', '/jbin/juju')
1192
1161
class FakeEnvJujuClient1X(EnvJujuClient1X):
1194
def __init__(self, name='steve', version='1.2', full_path='/jbin/juju'):
1163
def __init__(self, name='steve'):
1195
1164
super(FakeEnvJujuClient1X, self).__init__(
1196
SimpleEnvironment(name, {'type': 'fake'}), version, full_path)
1165
SimpleEnvironment(name, {'type': 'fake'}), '1.2', '/jbin/juju')
1199
1168
class TestBootstrapAttempt(JujuPyTestCase):
1209
1178
assert_juju_call(self, popen_mock, client, (
1210
1179
'juju', '--show-log', 'bootstrap', '--constraints', 'mem=2G',
1211
1180
'steve', 'fake/regionx', '--config', config_file.name,
1212
'--default-model', 'steve', '--agent-version', '1.2'))
1181
'--agent-version', '1.2'))
1214
1183
{'machines': {'0': {'agent-state': 'pending'}},
1215
'applications': {}},
1216
1185
{'machines': {'0': {'agent-state': 'started'}},
1219
1188
popen_mock.return_value.wait.return_value = 0
1220
1189
self.assertEqual(boot_iter.next(), {'test_id': 'bootstrap'})
1313
1282
iterator.next()
1315
1284
def test_iter_steps_kill_controller(self):
1316
client = fake_juju_client()
1285
client = FakeJujuClient(jes_enabled=True)
1318
1286
destroy_env = DestroyEnvironmentAttempt()
1319
1287
iterator = iter_steps_validate_info(self, destroy_env, client)
1320
1288
with closing(iterator):
1321
1289
self.assertEqual({'test_id': 'destroy-env'}, iterator.next())
1322
1290
self.assertEqual(iterator.next(), {
1323
1291
'test_id': 'destroy-env', 'result': True})
1324
self.assertEqual('controller-killed',
1325
client._backend.controller_state.state)
1292
self.assertEqual('controller-killed', client._backing_state.state)
1328
1295
def get_aws_client():
1453
1420
def test_iter_steps(self):
1454
1421
client = FakeEnvJujuClient()
1455
controller_client = client.get_controller_client()
1456
1422
ensure_av = EnsureAvailabilityAttempt()
1457
1423
ensure_iter = iter_steps_validate_info(self, ensure_av, client)
1458
1424
self.assertEqual(ensure_iter.next(), {
1459
1425
'test_id': 'ensure-availability-n3'})
1460
1426
with patch('subprocess.check_call') as cc_mock:
1461
with patch.object(client, 'get_controller_client',
1462
return_value=controller_client, autospec=True):
1463
self.assertEqual(ensure_iter.next(), {
1464
'test_id': 'ensure-availability-n3'})
1468
('juju', '--show-log', 'enable-ha', '-m',
1469
'steve:{}'.format(controller_client.env.environment), '-n', '3'))
1427
self.assertEqual(ensure_iter.next(), {
1428
'test_id': 'ensure-availability-n3'})
1429
assert_juju_call(self, cc_mock, client, (
1430
'juju', '--show-log', 'enable-ha', '-m', 'steve', '-n', '3'))
1472
1433
'0': {'controller-member-status': 'has-vote'},
1473
1434
'1': {'controller-member-status': 'has-vote'},
1474
1435
'2': {'controller-member-status': 'has-vote'},
1478
with patch_status(controller_client, status) as gs_mock:
1439
with patch_status(client, status) as gs_mock:
1479
1440
self.assertEqual(ensure_iter.next(), {
1480
1441
'test_id': 'ensure-availability-n3', 'result': True})
1481
gs_mock.assert_called_once_with(controller=True)
1442
gs_mock.assert_called_once_with()
1483
1444
def test_iter_steps_failure(self):
1484
1445
client = FakeEnvJujuClient()
1486
1447
ensure_iter = iter_steps_validate_info(self, ensure_av, client)
1487
1448
ensure_iter.next()
1488
1449
with patch('subprocess.check_call'):
1489
controller_client = client.get_controller_client()
1490
with patch.object(client, 'get_controller_client',
1491
return_value=controller_client, autospec=True):
1495
1453
'0': {'state-server-member-status': 'has-vote'},
1496
1454
'1': {'state-server-member-status': 'has-vote'},
1500
with patch_status(controller_client, status) as gs_mock:
1458
with patch_status(client, status) as gs_mock:
1501
1459
with self.assertRaisesRegexp(
1502
1460
Exception, 'Timed out waiting for voting to be enabled.'):
1503
1461
ensure_iter.next()
1507
1465
class TestDeployManyAttempt(JujuPyTestCase):
1509
def predict_add_machine_calls(self, deploy_many, machine_type):
1467
def predict_add_machine_calls(self, deploy_many):
1510
1468
for host in range(1, deploy_many.host_count + 1):
1511
1469
for container in range(deploy_many.container_count):
1512
target = '{}:{}'.format(machine_type, host)
1470
target = 'lxc:{}'.format(host)
1513
1471
service = 'ubuntu{}x{}'.format(host, container)
1514
yield ('juju', '--show-log', 'deploy', '-m', 'steve:steve',
1515
'ubuntu', service, '--to', target, '--series', 'angsty')
1472
yield ('juju', '--show-log', 'deploy', '-m', 'steve', '--to',
1473
target, 'ubuntu', service)
1517
1475
def predict_remove_machine_calls(self, deploy_many):
1518
1476
total_guests = deploy_many.host_count * deploy_many.container_count
1519
1477
for guest in range(100, total_guests + 100):
1520
yield ('juju', '--show-log', 'remove-machine', '-m', 'steve:steve',
1478
yield ('juju', '--show-log', 'remove-machine', '-m', 'steve',
1521
1479
'--force', str(guest))
1523
1481
def test_iter_steps(self):
1524
machine_started = {'juju-status': {'current': 'idle'}}
1525
unit_started = {'agent-status': {'current': 'idle'}}
1526
client = FakeEnvJujuClient()
1527
client.env.config['default-series'] = 'angsty'
1528
self.do_iter_steps(client, LXD_MACHINE, machine_started, unit_started)
1530
def test_iter_steps_1x(self):
1531
started_state = {'agent-state': 'started'}
1532
client = FakeEnvJujuClient()
1533
with patch.object(EnvJujuClient, 'supported_container_types',
1534
frozenset([KVM_MACHINE, LXC_MACHINE])):
1535
client.env.config['default-series'] = 'angsty'
1536
self.do_iter_steps(client, LXC_MACHINE, started_state,
1539
def do_iter_steps(self, client, machine_type, machine_started,
1482
client = FakeEnvJujuClient()
1541
1483
deploy_many = DeployManyAttempt(9, 11)
1542
1484
deploy_iter = iter_steps_validate_info(self, deploy_many, client)
1543
1485
self.assertEqual(deploy_iter.next(), {'test_id': 'add-machine-many'})
1545
'machines': {'0': dict(machine_started)},
1487
'machines': {'0': {'agent-state': 'started'}},
1548
1490
with patch_status(client, status):
1549
1491
with patch('subprocess.check_call') as mock_cc:
1551
1493
{'test_id': 'add-machine-many'})
1552
1494
for index in range(deploy_many.host_count):
1553
1495
assert_juju_call(self, mock_cc, client, (
1554
'juju', '--show-log', 'add-machine',
1555
'-m', 'steve:steve'), index)
1496
'juju', '--show-log', 'add-machine', '-m', 'steve'), index)
1558
'machines': dict((str(x), dict(machine_started))
1499
'machines': dict((str(x), {'agent-state': 'started'})
1559
1500
for x in range(deploy_many.host_count + 1)),
1562
1503
with patch_status(client, status):
1563
1504
self.assertEqual(
1576
1517
self.assertEqual(deploy_iter.next(),
1577
1518
{'test_id': 'deploy-many'})
1579
calls = self.predict_add_machine_calls(deploy_many, machine_type)
1520
calls = self.predict_add_machine_calls(deploy_many)
1580
1521
for num, args in enumerate(calls):
1581
1522
assert_juju_call(self, mock_cc, client, args, num)
1582
1523
service_names = []
1583
1524
for host in range(1, deploy_many.host_count + 1):
1584
1525
for container in range(deploy_many.container_count):
1585
1526
service_names.append('ubuntu{}x{}'.format(host, container))
1587
for num, service_name in enumerate(service_names):
1588
foo = {'machine': str(num + 100)}
1589
foo.update(unit_started)
1593
applications[service_name] = {'units': units}
1527
services = dict((service_name, {
1529
'foo': {'machine': str(num + 100), 'agent-state': 'started'}
1531
for num, service_name in enumerate(service_names))
1595
'machines': {'0': dict(machine_started)},
1596
'applications': applications,
1533
'machines': {'0': {'agent-state': 'started'}},
1534
'services': services,
1598
1536
with patch_status(client, status):
1599
1537
self.assertEqual(deploy_iter.next(),
1600
1538
{'test_id': 'deploy-many', 'result': True})
1602
1540
self.assertEqual(deploy_iter.next(),
1603
{'test_id': 'remove-machine-many-container'})
1541
{'test_id': 'remove-machine-many-lxc'})
1604
1542
with patch_status(client, status):
1605
1543
with patch('subprocess.check_call') as mock_cc:
1606
1544
self.assertEqual(
1607
1545
deploy_iter.next(),
1608
{'test_id': 'remove-machine-many-container'})
1546
{'test_id': 'remove-machine-many-lxc'})
1609
1547
calls = self.predict_remove_machine_calls(deploy_many)
1610
1548
for num, args in enumerate(calls):
1611
1549
assert_juju_call(self, mock_cc, client, args, num)
1613
{'machines': {'100': dict(machine_started)}, 'applications': {}},
1614
{'machines': {}, 'applications': {}},
1551
{'machines': {'100': {'agent-state': 'started'}}, 'services': {}},
1552
{'machines': {}, 'services': {}},
1616
1554
with patch_status(client, *statuses) as status_mock:
1617
1555
self.assertEqual(
1618
1556
deploy_iter.next(),
1619
{'test_id': 'remove-machine-many-container', 'result': True})
1557
{'test_id': 'remove-machine-many-lxc', 'result': True})
1620
1558
self.assertEqual(2, status_mock.call_count)
1621
1559
self.assertEqual(deploy_iter.next(), {
1622
1560
'test_id': 'remove-machine-many-instance'})
1626
1564
{'test_id': 'remove-machine-many-instance'})
1627
1565
for num in range(deploy_many.host_count):
1628
1566
assert_juju_call(self, mock_cc, client, (
1629
'juju', '--show-log', 'remove-machine', '-m', 'steve:steve',
1567
'juju', '--show-log', 'remove-machine', '-m', 'steve',
1630
1568
str(num + 1)), num)
1633
{'machines': {'1': dict(machine_started)}, 'applications': {}},
1634
{'machines': {}, 'applications': {}},
1571
{'machines': {'1': {'agent-state': 'started'}}, 'services': {}},
1572
{'machines': {}, 'services': {}},
1636
1574
with patch_status(client, *statuses) as status_mock:
1637
1575
self.assertEqual(
1642
1580
def test_iter_step_failure(self):
1643
1581
deploy_many = DeployManyAttempt()
1644
1582
client = FakeEnvJujuClient()
1645
client.env.config['default-series'] = 'angsty'
1646
1583
deploy_iter = iter_steps_validate_info(self, deploy_many, client)
1647
1584
self.assertEqual(deploy_iter.next(), {'test_id': 'add-machine-many'})
1649
1586
'machines': {'0': {'agent-state': 'started'}},
1652
1589
with patch_status(client, status):
1653
1590
with patch('subprocess.check_call') as mock_cc:
1655
1592
{'test_id': 'add-machine-many'})
1656
1593
for index in range(deploy_many.host_count):
1657
1594
assert_juju_call(self, mock_cc, client, (
1658
'juju', '--show-log', 'add-machine',
1659
'-m', 'steve:steve'), index)
1595
'juju', '--show-log', 'add-machine', '-m', 'steve'), index)
1662
1598
'machines': dict((str(x), {'agent-state': 'started'})
1663
1599
for x in range(deploy_many.host_count + 1)),
1666
1602
with patch_status(client, status):
1667
1603
self.assertEqual(
1694
1630
def test_iter_step_add_machine_failure(self):
1695
1631
deploy_many = DeployManyAttempt()
1696
1632
client = FakeEnvJujuClient()
1697
client.env.config['default-series'] = 'angsty'
1698
1633
deploy_iter = iter_steps_validate_info(self, deploy_many, client)
1699
1634
self.assertEqual(deploy_iter.next(), {'test_id': 'add-machine-many'})
1701
1636
'machines': {'0': {'agent-state': 'started'}},
1704
1639
with patch_status(client, status) as gs_mock:
1705
1640
with patch('subprocess.check_call') as mock_cc:
1707
1642
{'test_id': 'add-machine-many'})
1708
1643
for index in range(deploy_many.host_count):
1709
1644
assert_juju_call(self, mock_cc, client, (
1710
'juju', '--show-log', 'add-machine',
1711
'-m', 'steve:steve'), index)
1645
'juju', '--show-log', 'add-machine', '-m', 'steve'), index)
1712
1646
gs_mock.assert_called_once_with()
1715
1649
'machines': dict((str(x), {'agent-state': 'pending'})
1716
1650
for x in range(deploy_many.host_count + 1)),
1719
1653
with patch_status(client, status) as gs_mock:
1720
1654
self.assertEqual(deploy_iter.next(),
1726
1660
deploy_iter.next())
1727
1661
for x in range(deploy_many.host_count):
1728
1662
assert_juju_call(self, mock_cc, client, (
1729
'juju', '--show-log', 'remove-machine', '-m', 'steve:steve',
1663
'juju', '--show-log', 'remove-machine', '-m', 'steve',
1730
1664
'--force', str((x + 1))), x * 2)
1731
1665
assert_juju_call(self, mock_cc, client, (
1732
'juju', '--show-log', 'add-machine',
1733
'-m', 'steve:steve'), x * 2 + 1)
1666
'juju', '--show-log', 'add-machine', '-m', 'steve'), x * 2 + 1)
1736
1669
'machines': dict((str(x), {'agent-state': 'started'})
1737
1670
for x in range(deploy_many.host_count + 1)),
1740
1673
with patch_status(client, status) as gs_mock:
1741
1674
self.assertEqual({'test_id': 'ensure-machines', 'result': True},
1743
1676
self.assertEqual({'test_id': 'deploy-many'}, deploy_iter.next())
1744
1677
with patch('subprocess.check_call') as mock_cc:
1745
1678
self.assertEqual({'test_id': 'deploy-many'}, deploy_iter.next())
1746
calls = self.predict_add_machine_calls(deploy_many, LXD_MACHINE)
1679
calls = self.predict_add_machine_calls(deploy_many)
1747
1680
for num, args in enumerate(calls):
1748
1681
assert_juju_call(self, mock_cc, client, args, num)
1750
def get_wait_until_removed_timeout(self, container_type):
1751
deploy_many = DeployManyAttempt()
1752
client = fake_juju_client()
1754
deploy_iter = iter_steps_validate_info(self, deploy_many, client)
1755
with patch('industrial_test.wait_until_removed') as wur_mock:
1756
with patch.object(client, 'preferred_container',
1757
return_value=container_type):
1759
return wur_mock.mock_calls[0][2]['timeout']
1761
def test_wait_until_removed_timeout_lxd(self):
1762
self.assertEqual(60, self.get_wait_until_removed_timeout(LXD_MACHINE))
1764
def test_wait_until_removed_timeout_lxc(self):
1765
self.assertEqual(30, self.get_wait_until_removed_timeout(LXC_MACHINE))
1768
1684
class TestBackupRestoreAttempt(JujuPyTestCase):
1775
1691
def test_iter_steps(self):
1776
1692
br_attempt = BackupRestoreAttempt()
1777
1693
client = FakeEnvJujuClient()
1778
aws_env = get_aws_env()
1779
client.env.environment = aws_env.environment
1780
client.env.config = aws_env.config
1781
client.env.juju_home = aws_env.juju_home
1782
controller_client = client.get_controller_client()
1694
client.env = get_aws_env()
1783
1695
environ = dict(os.environ)
1784
1696
environ.update(get_euca_env(client.env.config))
1786
1698
def check_output(*args, **kwargs):
1788
'juju', '--show-log', 'create-backup', '-m',
1789
'steve:{}'.format(controller_client.env.environment),),):
1790
return FakePopen('juju-backup-24.tgz', '', 0)
1699
if args == (('juju', '--show-log', 'create-backup', '-m',
1701
return 'juju-backup-24.tgz'
1791
1702
self.assertEqual([], args)
1792
1703
initial_status = {
1793
1704
'machines': {'0': {
1798
1709
iterator = iter_steps_validate_info(self, br_attempt, client)
1799
1710
self.assertEqual(iterator.next(), {'test_id': 'back-up-restore'})
1800
with patch_status(controller_client, initial_status) as gs_mock:
1801
with patch('subprocess.Popen',
1711
with patch_status(client, initial_status) as gs_mock:
1712
with patch('subprocess.check_output',
1802
1713
side_effect=check_output) as co_mock:
1803
1714
with patch('subprocess.check_call') as cc_mock:
1804
with patch.object(client, 'get_controller_client',
1805
return_value=controller_client,
1807
with patch('sys.stdout'):
1810
{'test_id': 'back-up-restore'})
1815
('juju', '--show-log', 'create-backup',
1816
'-m', 'steve:{}'.format(controller_client.env.environment)),
1715
with patch('sys.stdout'):
1718
{'test_id': 'back-up-restore'})
1719
assert_juju_call(self, co_mock, client, (
1720
'juju', '--show-log', 'create-backup', '-m', 'baz'), 0)
1818
1721
self.assertEqual(
1819
1722
cc_mock.mock_calls[0],
1820
1723
call(['euca-terminate-instances', 'asdf'], env=environ))
1823
1726
self.assertEqual(iterator.next(),
1824
1727
{'test_id': 'back-up-restore'})
1825
1728
pn_mock.assert_called_with('Closed.')
1826
with patch.object(controller_client, 'restore_backup') as rb_mock:
1729
with patch('subprocess.Popen') as po_mock:
1827
1730
self.assertEqual(iterator.next(), {'test_id': 'back-up-restore'})
1828
rb_mock.assert_called_once_with(
1829
os.path.abspath('juju-backup-24.tgz'))
1732
self, po_mock, client, (
1733
'juju', '--show-log', 'restore', '-m', 'baz',
1734
os.path.abspath('juju-backup-24.tgz')))
1735
po_mock.return_value.wait.return_value = 0
1830
1736
with patch('os.unlink') as ul_mock:
1831
1737
self.assertEqual(iterator.next(),
1832
1738
{'test_id': 'back-up-restore'})
1860
1766
PrepareUpgradeJujuAttempt.factory([], None)
1862
1768
def test_get_bootstrap_client(self):
1863
client = fake_juju_client(full_path='c', debug=True)
1769
client = FakeJujuClient(full_path='c', debug=True)
1864
1770
puj_attempt = PrepareUpgradeJujuAttempt.factory(['a', 'b', 'c'], None)
1866
def by_version(path):
1867
return fake_juju_client(client.env, path, client.debug)
1869
with patch.object(client, 'clone_path_cls', by_version):
1870
bootstrap_client = puj_attempt.get_bootstrap_client(client)
1771
bootstrap_client = puj_attempt.get_bootstrap_client(client)
1872
1772
self.assertIsNot(bootstrap_client, client)
1873
1773
self.assertIs(client.debug, bootstrap_client.debug)
1874
1774
self.assertIs(client.env, bootstrap_client.env)
1875
1775
self.assertEqual('b', bootstrap_client.full_path)
1877
1777
def test_iter_steps(self):
1878
future_client = FakeEnvJujuClient(full_path='/future/juju')
1879
present_client = FakeEnvJujuClient(full_path='/present/juju')
1778
future_client = FakeEnvJujuClient()
1779
future_client.full_path = '/future/juju'
1780
present_client = FakeEnvJujuClient()
1781
present_client.full_path = '/present/juju'
1880
1782
puj_attempt = PrepareUpgradeJujuAttempt(
1881
1783
{future_client.full_path: present_client.full_path})
1882
1784
puj_iterator = iter_steps_validate_info(self, puj_attempt,
1884
1786
with patch('subprocess.check_output', return_value='2.0-alpha3-a-b'):
1885
with patch('industrial_test.client_from_config',
1886
return_value=future_client):
1887
self.assertEqual({'test_id': 'prepare-upgrade-juju'},
1888
puj_iterator.next())
1787
self.assertEqual({'test_id': 'prepare-upgrade-juju'},
1788
puj_iterator.next())
1889
1789
with observable_temp_file() as config_file:
1890
1790
with patch('subprocess.Popen') as po_mock:
1891
1791
self.assertEqual({'test_id': 'prepare-upgrade-juju'},
1892
1792
puj_iterator.next())
1893
assert_juju_call(self, po_mock, future_client, (
1793
assert_juju_call(self, po_mock, present_client, (
1894
1794
'juju', '--show-log', 'bootstrap', '--constraints', 'mem=2G',
1895
1795
'steve', 'fake/regionx', '--config', config_file.name,
1896
1796
'--agent-version', '2.0-alpha3'))
1918
1819
class TestUpgradeJujuAttempt(JujuPyTestCase):
1920
1821
def test_iter_steps(self):
1921
future_client = FakeEnvJujuClient(full_path='/future/juju')
1822
future_client = FakeEnvJujuClient()
1823
future_client.full_path = '/future/juju'
1922
1824
uj_attempt = UpgradeJujuAttempt()
1923
1825
uj_iterator = iter_steps_validate_info(self, uj_attempt, future_client)
1924
1826
self.assertEqual(uj_iterator.next(), {'test_id': 'upgrade-juju'})
1925
1827
with patch('subprocess.check_call') as cc_mock:
1926
1828
self.assertEqual({'test_id': 'upgrade-juju'}, uj_iterator.next())
1931
('juju', '--show-log', 'upgrade-juju',
1932
'-m', 'steve:steve', '--version',
1933
future_client.get_matching_agent_version()))
1829
assert_juju_call(self, cc_mock, future_client, (
1830
'juju', '--show-log', 'upgrade-juju', '-m', 'steve', '--version',
1831
future_client.get_matching_agent_version()))
1934
1832
version_status = {
1935
1833
'machines': {'0': {
1936
1834
'agent-version': future_client.get_matching_agent_version()}},
1939
1837
with patch_status(None, version_status):
1940
1838
self.assertEqual({'test_id': 'upgrade-juju', 'result': True},
1950
1848
self.assertEqual(0o755, mode & 0o777)
1952
1850
def test_iter_steps(self):
1953
client = FakeEnvJujuClient(version='2.0.0', full_path='/future/juju')
1954
self._iter_steps(client)
1956
def test_iter_steps_juju_1x(self):
1957
client = FakeEnvJujuClient1X(version='1.25.0',
1958
full_path='/future/juju')
1959
self._iter_steps(client)
1961
def _iter_steps(self, client):
1962
self.assertEqual(client.full_path, '/future/juju')
1851
client = FakeEnvJujuClient()
1852
client.full_path = '/future/juju'
1963
1853
uc_attempt = UpgradeCharmAttempt()
1964
1854
uc_iterator = iter_steps_validate_info(self, uc_attempt, client)
1965
1855
self.assertEqual(uc_iterator.next(),
1976
1866
self.assertEqual(metadata['name'], 'mycharm')
1977
1867
self.assertIn('summary', metadata)
1978
1868
self.assertIn('description', metadata)
1979
self.assertEqual(['trusty'], metadata['series'])
1980
if client.version.startswith('1.'):
1981
charm_path = os.path.join('local:trusty', 'mycharm')
1982
assert_juju_call(self, cc_mock, client, (
1983
'juju', '--show-log', 'deploy', '-e', 'steve', charm_path,
1984
'--repository', temp_repository))
1987
charm_path = os.path.join(temp_repository, 'trusty', 'mycharm')
1988
assert_juju_call(self, cc_mock, client, (
1989
'juju', '--show-log', 'deploy',
1990
'-m', 'steve:steve', charm_path))
1992
self.assertNotIn('min-juju-version', metadata)
1869
assert_juju_call(self, cc_mock, client, (
1870
'juju', '--show-log', 'deploy', '-m', 'steve',
1871
'local:trusty/mycharm', '--repository', temp_repository))
1994
1873
'machines': {'0': {'agent-state': 'started'}},
1997
1876
with patch_status(client, status):
1998
1877
self.assertEqual(uc_iterator.next(),
2017
1896
self.assertEqual(uc_iterator.next(), {'test_id': 'upgrade-charm'})
2018
1897
with patch('subprocess.check_call') as cc_mock:
2019
1898
self.assertEqual(uc_iterator.next(), {'test_id': 'upgrade-charm'})
2020
if client.version.startswith('1.'):
2021
assert_juju_call(self, cc_mock, client, (
2022
'juju', '--show-log', 'upgrade-charm', option, 'steve',
2023
'mycharm', '--repository', temp_repository))
2025
assert_juju_call(self, cc_mock, client, (
2026
'juju', '--show-log', 'upgrade-charm', option, 'steve:steve',
2027
'mycharm', '--path', os.path.join(temp_repository, 'trusty',
1899
assert_juju_call(self, cc_mock, client, (
1900
'juju', '--show-log', 'upgrade-charm', '-m', 'steve',
1901
'mycharm', '--repository', temp_repository))
2030
1903
'machines': {'0': {'agent-state': 'started'}},
2031
'applications': {'mycharm': {'units': {'mycharm/0': {
1904
'services': {'mycharm': {'units': {'mycharm/0': {
2032
1905
'open-ports': ['42/tcp', '34/tcp'],
2190
2063
factory = AttemptSuiteFactory([], bootstrap_attempt=fake_bootstrap)
2191
2064
attempt_suite = AttemptSuite(factory, None, 'asdf', None)
2192
2065
with self.iter_steps_cxt(attempt_suite) as (mock_ibms, mock_bm):
2193
client = fake_juju_client()
2066
client = FakeJujuClient()
2194
2067
attempt_suite.iter_steps(client)
2195
2068
mock_bm.assert_called_once_with(
2196
2069
'name', client, client, agent_stream=None, agent_url=None,
2197
bootstrap_host=None, jes_enabled=True, keep_env=True,
2198
log_dir='qux-1', machines=[], permanent=True,
2070
bootstrap_host=None, jes_enabled=False, keep_env=True,
2071
log_dir='qux-1', machines=[], permanent=False,
2199
2072
region=None, series=None)
2201
2074
def test_iter_steps_agent_stream(self):
2203
2076
factory = AttemptSuiteFactory([], bootstrap_attempt=fake_bootstrap)
2204
2077
attempt_suite = AttemptSuite(factory, None, 'asdf', 'bar-stream')
2205
2078
with self.iter_steps_cxt(attempt_suite) as (mock_ibms, mock_bm):
2206
client = fake_juju_client()
2079
client = FakeJujuClient()
2207
2080
iterator = attempt_suite.iter_steps(client)
2208
2081
self.assertEqual(iterator, mock_ibms.return_value)
2209
2082
mock_bm.assert_called_once_with(
2210
2083
'name', client, client, agent_stream='bar-stream', agent_url=None,
2211
bootstrap_host=None, jes_enabled=True, keep_env=True,
2212
log_dir='qux-1', machines=[], permanent=True,
2084
bootstrap_host=None, jes_enabled=False, keep_env=True,
2085
log_dir='qux-1', machines=[], permanent=False,
2213
2086
region=None, series=None)
2215
2088
def test__iter_bs_manager_steps(self):
2219
2092
factory = AttemptSuiteFactory([fake_1, fake_2],
2220
2093
bootstrap_attempt=fake_bootstrap)
2221
2094
attempt_suite = AttemptSuite(factory, None, None, None)
2222
client = fake_juju_client()
2095
client = FakeJujuClient()
2223
2096
bs_manager = FakeBootstrapManager(client)
2224
2097
steps = list(attempt_suite._iter_bs_manager_steps(
2225
2098
bs_manager, client, fake_bootstrap(), True))
2237
2110
{'test_id': 'substrate-clean'},
2238
2111
{'test_id': 'substrate-clean', 'result': True},
2241
def test__iter_bs_manager_steps_teardown_in_runtime(self):
2242
fake_bootstrap = FakeAttemptClass('fake-bootstrap', '1', '2')
2243
fake_1 = FakeAttemptClass('fake-1', Exception('fake exception'), '2')
2244
factory = AttemptSuiteFactory([fake_1],
2245
bootstrap_attempt=fake_bootstrap)
2246
attempt_suite = AttemptSuite(factory, None, None, None)
2247
client = fake_juju_client()
2248
bs_manager = FakeBootstrapManager(client, keep_env=True)
2249
with self.assertRaisesRegexp(Exception, 'fake exception'):
2250
list(attempt_suite._iter_bs_manager_steps(
2251
bs_manager, client, fake_bootstrap(), True))
2252
self.assertIs(True, bs_manager.torn_down)