~abentley/juju-ci-tools/client-from-config-4

« back to all changes in this revision

Viewing changes to tests/test_industrial_test.py

  • Committer: Aaron Bentley
  • Date: 2016-03-02 16:04:04 UTC
  • mto: This revision was merged to the branch mainline in revision 1307.
  • Revision ID: aaron.bentley@canonical.com-20160302160404-0l1z6lb3oanrgvxo
assess_heterogeneous_control does not tear down with inappropriate client.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
    closing,
6
6
    contextmanager,
7
7
    )
8
 
import json
9
8
import os
10
9
from tempfile import (
11
10
    mkdtemp,
52
51
    EnvJujuClient1X,
53
52
    get_timeout_prefix,
54
53
    JujuData,
55
 
    KVM_MACHINE,
56
 
    LXC_MACHINE,
57
 
    LXD_MACHINE,
58
54
    SimpleEnvironment,
59
55
    Status,
60
56
    _temp_env,
69
65
from tests.test_deploy_stack import FakeBootstrapManager
70
66
from test_jujupy import (
71
67
    assert_juju_call,
72
 
    fake_juju_client,
73
 
    FakePopen,
 
68
    FakeJujuClient,
74
69
    observable_temp_file,
75
70
    )
76
71
from test_substrate import (
147
142
    """
148
143
    kwargs = {}
149
144
    if len(statuses) > 1:
150
 
        kwargs['side_effect'] = (Status.from_text(json.dumps(s))
 
145
        kwargs['side_effect'] = (Status.from_text(yaml.safe_dump(s))
151
146
                                 for s in statuses).next
152
147
    else:
153
 
        kwargs['return_value'] = Status.from_text(json.dumps(statuses[0]))
 
148
        kwargs['return_value'] = Status.from_text(yaml.safe_dump(statuses[0]))
154
149
    if client is not None:
155
150
        return patch.object(client, 'get_status', autospec=True, **kwargs)
156
151
    return patch('jujupy.EnvJujuClient.get_status', autospec=True, **kwargs)
277
272
    def iter_steps(self, client):
278
273
        yield self.stage.as_result()
279
274
        if self.new_path is not None and client.full_path == self.new_path:
280
 
            result_value = self.result[0][2]
 
275
            yield self.stage.as_result(self.result[0][2])
281
276
        else:
282
 
            result_value = self.result[0][1]
283
 
        if isinstance(result_value, BaseException):
284
 
            raise result_value
285
 
        yield self.stage.as_result(result_value)
 
277
            yield self.stage.as_result(self.result[0][1])
286
278
 
287
279
 
288
280
class FakeAttemptClass:
520
512
    @staticmethod
521
513
    @contextmanager
522
514
    def patch_client(by_version):
523
 
        with patch('industrial_test.client_from_config',
524
 
                   side_effect=by_version):
 
515
        with patch('jujupy.EnvJujuClient.by_version', side_effect=by_version):
525
516
            with patch('jujupy.SimpleEnvironment.from_config',
526
517
                       side_effect=lambda x: SimpleEnvironment(x, {})):
527
518
                with patch.object(EnvJujuClient, 'get_full_path',
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=''),
541
 
            None))
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=''),
545
 
            'bar-path'))
 
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', {
560
 
                    'type': 'foo',
561
 
                    'default-series': 'angsty',
562
 
                    'region': 'bar',
563
 
                    'name': 'foo-env-new',
564
 
                    'tools-metadata-url': 'http://example.com',
565
 
                    }, 'foo'),
 
542
            industrial.new_client, (
 
543
                SimpleEnvironment('foo-env-new', {
 
544
                    'tools-metadata-url': 'http://example.com'}),
566
545
                'bar-path')
567
546
            )
568
547
 
572
551
                                  new_agent_url='http://example.com')
573
552
 
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)
 
554
            return debug
577
555
 
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)
582
560
        mit.debug = True
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)
587
565
 
588
566
    def test_update_results(self):
589
567
        mit = MultiIndustrialTest('foo-env', 'bar-path',
651
629
            ]), log_dir, 5, 10)
652
630
 
653
631
        def side_effect(env, full_path=None, debug=False):
654
 
            return fake_juju_client(None, full_path, debug)
 
632
            return FakeJujuClient(None, full_path, debug)
655
633
 
656
634
        with self.patch_client(side_effect):
657
635
            with patch('industrial_test.BootstrapManager',
683
661
            ]), log_dir, 5, 6)
684
662
 
685
663
        def side_effect(env, full_path=None, debug=False):
686
 
            return fake_juju_client(None, full_path, debug)
 
664
            return FakeJujuClient(None, full_path, debug)
687
665
 
688
666
        with self.patch_client(side_effect):
689
667
            with patch('industrial_test.BootstrapManager',
717
695
            log_dir, 5, 4)
718
696
 
719
697
        def side_effect(env, full_path=None, debug=False):
720
 
            return fake_juju_client(None, full_path, debug)
 
698
            return FakeJujuClient(None, full_path, debug)
721
699
 
722
700
        with self.patch_client(side_effect):
723
701
            with patch('industrial_test.BootstrapManager',
847
825
 
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', [])
 
828
            return (x, y)
 
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'}),
862
 
            'new-juju-path'))
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)
865
841
 
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):
 
844
            return debug
 
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)
880
855
 
881
856
    def test_run_stages(self):
882
857
        old_client = FakeEnvJujuClient('old')
891
866
        self.assertEqual(len(cc_mock.mock_calls), 0)
892
867
 
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)
916
889
 
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)
937
908
 
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)
958
927
 
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())
985
954
 
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)
1003
972
                           fake_bootstrap_manager):
1004
973
                    industrial.run_attempt()
1005
974
        self.assertEqual(2, le_mock.call_count)
1006
 
        self.assertEqual('controller-killed',
1007
 
                         old_client._backend.controller_state.state)
1008
 
        self.assertEqual('controller-killed',
1009
 
                         new_client._backend.controller_state.state)
 
975
        self.assertEqual('destroyed', old_client._backing_state.state)
 
976
        self.assertEqual('destroyed', new_client._backing_state.state)
1010
977
 
1011
978
 
1012
979
class TestSteppedStageAttempt(JujuPyTestCase):
1183
1150
            ('bar-id', {'title': 'Bar title', 'report_on': False})]))
1184
1151
 
1185
1152
 
1186
 
def FakeEnvJujuClient(name='steve', version='1.2', full_path='/jbin/juju'):
1187
 
    return EnvJujuClient(
 
1153
class FakeEnvJujuClient(EnvJujuClient):
 
1154
 
 
1155
    def __init__(self, name='steve'):
 
1156
        super(FakeEnvJujuClient, self).__init__(
1188
1157
            JujuData(name, {'type': 'fake', 'region': 'regionx'}),
1189
 
            version, full_path)
 
1158
            '1.2', '/jbin/juju')
1190
1159
 
1191
1160
 
1192
1161
class FakeEnvJujuClient1X(EnvJujuClient1X):
1193
1162
 
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')
1197
1166
 
1198
1167
 
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'))
1213
1182
            statuses = [
1214
1183
                {'machines': {'0': {'agent-state': 'pending'}},
1215
 
                 'applications': {}},
 
1184
                 'services': {}},
1216
1185
                {'machines': {'0': {'agent-state': 'started'}},
1217
 
                 'applicaions': {}},
 
1186
                 'services': {}},
1218
1187
            ]
1219
1188
            popen_mock.return_value.wait.return_value = 0
1220
1189
            self.assertEqual(boot_iter.next(), {'test_id': 'bootstrap'})
1313
1282
            iterator.next()
1314
1283
 
1315
1284
    def test_iter_steps_kill_controller(self):
1316
 
        client = fake_juju_client()
1317
 
        client.bootstrap()
 
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)
1326
1293
 
1327
1294
    @staticmethod
1328
1295
    def get_aws_client():
1452
1419
 
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'})
1465
 
        assert_juju_call(
1466
 
            self,
1467
 
            cc_mock, client,
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'))
1470
1431
        status = {
1471
1432
            'machines': {
1472
1433
                '0': {'controller-member-status': 'has-vote'},
1473
1434
                '1': {'controller-member-status': 'has-vote'},
1474
1435
                '2': {'controller-member-status': 'has-vote'},
1475
1436
                },
1476
 
            'applications': {},
 
1437
            'services': {},
1477
1438
        }
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()
1482
1443
 
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):
1492
 
                ensure_iter.next()
 
1450
            ensure_iter.next()
1493
1451
        status = {
1494
1452
            'machines': {
1495
1453
                '0': {'state-server-member-status': 'has-vote'},
1496
1454
                '1': {'state-server-member-status': 'has-vote'},
1497
1455
                },
1498
 
            'applications': {},
 
1456
            'services': {},
1499
1457
        }
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()
1506
1464
 
1507
1465
class TestDeployManyAttempt(JujuPyTestCase):
1508
1466
 
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)
1516
1474
 
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))
1522
1480
 
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)
1529
 
 
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,
1537
 
                               started_state)
1538
 
 
1539
 
    def do_iter_steps(self, client, machine_type, machine_started,
1540
 
                      unit_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'})
1544
1486
        status = {
1545
 
            'machines': {'0': dict(machine_started)},
1546
 
            'applications': {},
 
1487
            'machines': {'0': {'agent-state': 'started'}},
 
1488
            'services': {},
1547
1489
        }
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)
1556
1497
 
1557
1498
        status = {
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)),
1560
 
            'applications': {},
 
1501
            'services': {},
1561
1502
        }
1562
1503
        with patch_status(client, status):
1563
1504
                self.assertEqual(
1576
1517
            self.assertEqual(deploy_iter.next(),
1577
1518
                             {'test_id': 'deploy-many'})
1578
1519
 
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))
1586
 
        applications = {}
1587
 
        for num, service_name in enumerate(service_names):
1588
 
            foo = {'machine': str(num + 100)}
1589
 
            foo.update(unit_started)
1590
 
            units = {
1591
 
                'foo': foo,
1592
 
                }
1593
 
            applications[service_name] = {'units': units}
 
1527
        services = dict((service_name, {
 
1528
            'units': {
 
1529
                'foo': {'machine': str(num + 100), 'agent-state': 'started'}
 
1530
                }})
 
1531
            for num, service_name in enumerate(service_names))
1594
1532
        status = {
1595
 
            'machines': {'0': dict(machine_started)},
1596
 
            'applications': applications,
 
1533
            'machines': {'0': {'agent-state': 'started'}},
 
1534
            'services': services,
1597
1535
        }
1598
1536
        with patch_status(client, status):
1599
1537
            self.assertEqual(deploy_iter.next(),
1600
1538
                             {'test_id': 'deploy-many', 'result': True})
1601
1539
 
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)
1612
1550
        statuses = [
1613
 
            {'machines': {'100': dict(machine_started)}, 'applications': {}},
1614
 
            {'machines': {}, 'applications': {}},
 
1551
            {'machines': {'100': {'agent-state': 'started'}}, 'services': {}},
 
1552
            {'machines': {}, 'services': {}},
1615
1553
        ]
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)
1631
1569
 
1632
1570
        statuses = [
1633
 
            {'machines': {'1': dict(machine_started)}, 'applications': {}},
1634
 
            {'machines': {}, 'applications': {}},
 
1571
            {'machines': {'1': {'agent-state': 'started'}}, 'services': {}},
 
1572
            {'machines': {}, 'services': {}},
1635
1573
        ]
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'})
1648
1585
        status = {
1649
1586
            'machines': {'0': {'agent-state': 'started'}},
1650
 
            'applications': {},
 
1587
            'services': {},
1651
1588
        }
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)
1660
1596
 
1661
1597
        status = {
1662
1598
            'machines': dict((str(x), {'agent-state': 'started'})
1663
1599
                             for x in range(deploy_many.host_count + 1)),
1664
 
            'applications': {},
 
1600
            'services': {},
1665
1601
        }
1666
1602
        with patch_status(client, status):
1667
1603
                self.assertEqual(
1683
1619
            'machines': {
1684
1620
                '0': {'agent-state': 'pending'},
1685
1621
                },
1686
 
            'applications': {},
 
1622
            'services': {},
1687
1623
        }
1688
1624
        with patch_status(client, status):
1689
1625
            with self.assertRaisesRegexp(
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'})
1700
1635
        status = {
1701
1636
            'machines': {'0': {'agent-state': 'started'}},
1702
 
            'applications': {},
 
1637
            'services': {},
1703
1638
        }
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()
1713
1647
 
1714
1648
        status = {
1715
1649
            'machines': dict((str(x), {'agent-state': 'pending'})
1716
1650
                             for x in range(deploy_many.host_count + 1)),
1717
 
            'applications': {},
 
1651
            'services': {},
1718
1652
        }
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)
1734
1667
 
1735
1668
        status = {
1736
1669
            'machines': dict((str(x), {'agent-state': 'started'})
1737
1670
                             for x in range(deploy_many.host_count + 1)),
1738
 
            'applications': {},
 
1671
            'services': {},
1739
1672
        }
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)
1749
1682
 
1750
 
    def get_wait_until_removed_timeout(self, container_type):
1751
 
        deploy_many = DeployManyAttempt()
1752
 
        client = fake_juju_client()
1753
 
        client.bootstrap()
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):
1758
 
                list(deploy_iter)
1759
 
        return wur_mock.mock_calls[0][2]['timeout']
1760
 
 
1761
 
    def test_wait_until_removed_timeout_lxd(self):
1762
 
        self.assertEqual(60, self.get_wait_until_removed_timeout(LXD_MACHINE))
1763
 
 
1764
 
    def test_wait_until_removed_timeout_lxc(self):
1765
 
        self.assertEqual(30, self.get_wait_until_removed_timeout(LXC_MACHINE))
1766
 
 
1767
1683
 
1768
1684
class TestBackupRestoreAttempt(JujuPyTestCase):
1769
1685
 
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))
1785
1697
 
1786
1698
        def check_output(*args, **kwargs):
1787
 
            if args == ((
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',
 
1700
                         'baz',),):
 
1701
                return 'juju-backup-24.tgz'
1791
1702
            self.assertEqual([], args)
1792
1703
        initial_status = {
1793
1704
            'machines': {'0': {
1797
1708
        }
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,
1806
 
                                      autospec=True):
1807
 
                        with patch('sys.stdout'):
1808
 
                            self.assertEqual(
1809
 
                                iterator.next(),
1810
 
                                {'test_id': 'back-up-restore'})
1811
 
        assert_juju_call(
1812
 
            self,
1813
 
            co_mock,
1814
 
            client,
1815
 
            ('juju', '--show-log', 'create-backup',
1816
 
             '-m', 'steve:{}'.format(controller_client.env.environment)),
1817
 
            0)
 
1715
                    with patch('sys.stdout'):
 
1716
                        self.assertEqual(
 
1717
                            iterator.next(),
 
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'))
 
1731
        assert_juju_call(
 
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'})
1835
1741
            'machines': {
1836
1742
                '0': {'agent-state': 'started'},
1837
1743
                },
1838
 
            'applications': {},
 
1744
            'services': {},
1839
1745
        }
1840
 
        with patch_status(controller_client, final_status) as gs_mock:
 
1746
        with patch_status(client, final_status) as gs_mock:
1841
1747
            self.assertEqual(iterator.next(),
1842
1748
                             {'test_id': 'back-up-restore', 'result': True})
1843
1749
        gs_mock.assert_called_once_with()
1860
1766
            PrepareUpgradeJujuAttempt.factory([], None)
1861
1767
 
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)
1865
 
 
1866
 
        def by_version(path):
1867
 
            return fake_juju_client(client.env, path, client.debug)
1868
 
 
1869
 
        with patch.object(client, 'clone_path_cls', by_version):
1870
 
            bootstrap_client = puj_attempt.get_bootstrap_client(client)
1871
 
 
 
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)
1876
1776
 
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,
1883
1785
                                                future_client)
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'))
1899
1799
                             {'test_id': 'prepare-upgrade-juju'})
1900
1800
        b_status = {
1901
1801
            'machines': {'0': {'agent-state': 'started'}},
1902
 
            'applications': {},
 
1802
            'services': {},
1903
1803
        }
1904
1804
        with patch_status(None, b_status):
1905
1805
            self.assertEqual(
1908
1808
 
1909
1809
    def test_iter_steps_no_previous_client(self):
1910
1810
        uj_attempt = PrepareUpgradeJujuAttempt({})
1911
 
        client = FakeEnvJujuClient(full_path='/present/juju')
 
1811
        client = FakeEnvJujuClient()
 
1812
        client.full_path = '/present/juju'
1912
1813
        uj_iterator = uj_attempt.iter_steps(client)
1913
1814
        with self.assertRaises(CannotUpgradeToClient) as exc_context:
1914
1815
            uj_iterator.next()
1918
1819
class TestUpgradeJujuAttempt(JujuPyTestCase):
1919
1820
 
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())
1927
 
        assert_juju_call(
1928
 
            self,
1929
 
            cc_mock,
1930
 
            future_client,
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()}},
1937
 
            'applications': {},
 
1835
            'services': {},
1938
1836
        }
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)
1951
1849
 
1952
1850
    def test_iter_steps(self):
1953
 
        client = FakeEnvJujuClient(version='2.0.0', full_path='/future/juju')
1954
 
        self._iter_steps(client)
1955
 
 
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)
1960
 
 
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))
1985
 
            option = '-e'
1986
 
        else:
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))
1991
 
            option = '-m'
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))
1993
1872
        status = {
1994
1873
            'machines': {'0': {'agent-state': 'started'}},
1995
 
            'applications': {},
 
1874
            'services': {},
1996
1875
        }
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))
2024
 
        else:
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',
2028
 
                                                  'mycharm')))
 
1899
        assert_juju_call(self, cc_mock, client, (
 
1900
            'juju', '--show-log', 'upgrade-charm', '-m', 'steve',
 
1901
            'mycharm', '--repository', temp_repository))
2029
1902
        status = {
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'],
2033
1906
                }}}},
2034
1907
        }
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)
2200
2073
 
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)
2214
2087
 
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},
2239
2112
            ], steps)
2240
 
 
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)