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

« back to all changes in this revision

Viewing changes to tests/test_deploy_stack.py

  • Committer: Christopher Lee
  • Date: 2016-10-30 23:17:23 UTC
  • mfrom: (1697 trunk)
  • mto: This revision was merged to the branch mainline in revision 1698.
  • Revision ID: chris.lee@canonical.com-20161030231723-weuexhcfcp9jkz0i
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
    EnvJujuClient25,
63
63
    get_cache_path,
64
64
    get_timeout_prefix,
65
 
    get_timeout_path,
66
65
    JujuData,
67
66
    KILL_CONTROLLER,
68
67
    SimpleEnvironment,
1344
1343
            client.env.juju_home)
1345
1344
        return client
1346
1345
 
1347
 
    def test_bootstrap_context_tear_down(self):
 
1346
    def test_bootstrap_context_kill(self):
1348
1347
        client = fake_juju_client()
1349
1348
        client.env.juju_home = use_context(self, temp_dir())
1350
1349
        initial_home = client.env.juju_home
1352
1351
            'foobar', client, client, None, [], None, None, None, None,
1353
1352
            client.env.juju_home, False, False, False)
1354
1353
 
1355
 
        def check_config(client_, jes_enabled, try_jes=False):
 
1354
        def check_config(try_jes=False):
1356
1355
            self.assertEqual(0, client.is_jes_enabled.call_count)
1357
1356
            jenv_path = get_jenv_path(client.env.juju_home, 'foobar')
1358
1357
            self.assertFalse(os.path.exists(jenv_path))
1361
1360
            self.assertNotEqual(initial_home, client.env.juju_home)
1362
1361
 
1363
1362
        ije_cxt = patch.object(client, 'is_jes_enabled')
1364
 
        with patch('deploy_stack.tear_down',
1365
 
                   side_effect=check_config) as td_mock, ije_cxt:
 
1363
        with patch('jujupy.EnvJujuClient.kill_controller',
 
1364
                   side_effect=check_config) as kill_mock, ije_cxt:
1366
1365
            with bs_manager.bootstrap_context([]):
1367
 
                td_mock.assert_called_once_with(client, False, try_jes=True)
 
1366
                pass
 
1367
        kill_mock.assert_called_once_with()
1368
1368
 
1369
1369
    def test_bootstrap_context_tear_down_jenv(self):
1370
1370
        client = self.make_client()
1378
1378
            'foobar', client, client, None, [], None, None, None, None,
1379
1379
            client.env.juju_home, False, False, False)
1380
1380
 
1381
 
        def check_config(client_, jes_enabled, try_jes=False):
 
1381
        def check_config(try_jes=False):
1382
1382
            self.assertEqual(0, client.is_jes_enabled.call_count)
1383
1383
            self.assertTrue(os.path.isfile(jenv_path))
1384
1384
            environments_path = get_environments_path(client.env.juju_home)
1385
1385
            self.assertFalse(os.path.exists(environments_path))
1386
1386
            self.assertEqual(initial_home, client.env.juju_home)
1387
1387
 
1388
 
        with patch('deploy_stack.tear_down',
1389
 
                   side_effect=check_config) as td_mock:
 
1388
        with patch.object(client, 'kill_controller',
 
1389
                          side_effect=check_config) as kill_mock:
1390
1390
            with bs_manager.bootstrap_context([]):
1391
 
                td_mock.assert_called_once_with(client, False, try_jes=False)
 
1391
                pass
 
1392
        kill_mock.assert_called_once_with()
1392
1393
 
1393
1394
    def test_bootstrap_context_tear_down_client(self):
1394
1395
        client = self.make_client()
1398
1399
            'foobar', client, tear_down_client, None, [], None, None, None,
1399
1400
            None, client.env.juju_home, False, False, False)
1400
1401
 
1401
 
        def check_config(client_, jes_enabled, try_jes=False):
1402
 
            self.assertEqual(0, client.is_jes_enabled.call_count)
1403
 
            tear_down_client.is_jes_enabled.assert_called_once_with()
 
1402
        def check_config(try_jes=False):
 
1403
            self.assertIsFalse(client.is_jes_enabled.called)
 
1404
            self.assertIsFalse(tear_down_client.is_jes_enabled.called)
1404
1405
 
1405
 
        with patch('deploy_stack.tear_down',
1406
 
                   side_effect=check_config) as td_mock:
1407
 
            with bs_manager.bootstrap_context([]):
1408
 
                td_mock.assert_called_once_with(tear_down_client,
1409
 
                                                False, try_jes=True)
 
1406
        with patch.object(tear_down_client, 'kill_controller',
 
1407
                          side_effect=check_config) as kill_mock:
 
1408
            with patch('deploy_stack.BootstrapManager.tear_down') as td_mock:
 
1409
                with bs_manager.bootstrap_context([]):
 
1410
                    pass
 
1411
        kill_mock.assert_called_once_with()
 
1412
        self.assertIsFalse(td_mock.called)
1410
1413
 
1411
1414
    def test_bootstrap_context_tear_down_client_jenv(self):
1412
1415
        client = self.make_client()
1422
1425
            None, [], None, None, None, None, client.env.juju_home, False,
1423
1426
            False, False)
1424
1427
 
1425
 
        def check_config(client_, jes_enabled, try_jes=False):
1426
 
            self.assertEqual(0, client.is_jes_enabled.call_count)
1427
 
            tear_down_client.is_jes_enabled.assert_called_once_with()
 
1428
        def check_config(try_jes=False):
 
1429
            self.assertIsFalse(client.is_jes_enabled.called)
 
1430
            self.assertIsFalse(tear_down_client.is_jes_enabled.called)
1428
1431
 
1429
 
        with patch('deploy_stack.tear_down',
1430
 
                   side_effect=check_config) as td_mock:
 
1432
        with patch.object(tear_down_client, 'kill_controller',
 
1433
                          side_effect=check_config) as kill_mock:
1431
1434
            with bs_manager.bootstrap_context([]):
1432
 
                td_mock.assert_called_once_with(tear_down_client, False,
1433
 
                                                try_jes=False)
 
1435
                kill_mock.assert_called_once_with()
1434
1436
 
1435
1437
    def test_bootstrap_context_no_set_home(self):
1436
1438
        orig_home = get_juju_home()
1515
1517
                        raise fake_exception
1516
1518
                self.assertIs(fake_exception, exc.exception.exception)
1517
1519
 
 
1520
    def test_tear_down(self):
 
1521
        client = fake_juju_client()
 
1522
        with patch.object(client, 'tear_down') as tear_down_mock:
 
1523
            with temp_dir() as log_dir:
 
1524
                bs_manager = BootstrapManager(
 
1525
                    'foobar', client, client, None, [], None, None, None,
 
1526
                    None, log_dir, False, False, jes_enabled=False)
 
1527
                bs_manager.tear_down()
 
1528
        tear_down_mock.assert_called_once_with()
 
1529
 
1518
1530
    def test_tear_down_requires_same_env(self):
1519
1531
        client = self.make_client()
1520
1532
        client.env.juju_home = 'foobar'
1524
1536
            'foobar', client, tear_down_client,
1525
1537
            None, [], None, None, None, None, client.env.juju_home, False,
1526
1538
            False, False)
1527
 
 
1528
 
        def check_home(foo, bar, try_jes):
1529
 
            self.assertEqual(client.env.juju_home,
1530
 
                             tear_down_client.env.juju_home)
1531
 
 
1532
1539
        with self.assertRaisesRegexp(AssertionError,
1533
1540
                                     'Tear down client needs same env'):
1534
 
            with patch('deploy_stack.tear_down', autospec=True,
1535
 
                       side_effect=check_home):
 
1541
            with patch.object(client, 'destroy_controller',
 
1542
                              autospec=True) as destroy_mock:
1536
1543
                bs_manager.tear_down()
1537
1544
        self.assertEqual('barfoo', tear_down_client.env.juju_home)
 
1545
        self.assertIsFalse(destroy_mock.called)
1538
1546
 
1539
1547
    def test_dump_all_no_jes_one_model(self):
1540
1548
        client = fake_juju_client()
1863
1871
            models = [{'name': 'controller'}, {'name': 'bar'}]
1864
1872
        self.addContext(patch.object(client, '_get_models',
1865
1873
                                     return_value=models, autospec=True))
1866
 
        c_mock = self.addContext(patch('subprocess.call', autospec=True,
1867
 
                                 return_value=0))
1868
 
        juju_name = os.path.basename(client.full_path)
1869
1874
        if jes:
1870
1875
            output = jes
1871
1876
        else:
1873
1878
        po_count = 0
1874
1879
        with patch('subprocess.Popen', autospec=True,
1875
1880
                   return_value=FakePopen(output, '', 0)) as po_mock:
1876
 
            yield
 
1881
            with patch('deploy_stack.BootstrapManager.tear_down',
 
1882
                       autospec=True) as tear_down_mock:
 
1883
                with patch.object(client, 'kill_controller',
 
1884
                                  autospec=True) as kill_mock:
 
1885
                    yield
1877
1886
        self.assertEqual(po_count, po_mock.call_count)
1878
1887
        dal_mock.assert_called_once_with()
1879
 
        if keep_env:
1880
 
            tear_down_count = 1
1881
 
        else:
1882
 
            tear_down_count = 2
1883
 
        for call_index in range(tear_down_count):
1884
 
            if jes:
1885
 
                assert_juju_call(
1886
 
                    self, c_mock, client, get_timeout_prefix(600) + (
1887
 
                        juju_name, '--show-log', jes, 'bar', '-y'), call_index)
1888
 
            else:
1889
 
                assert_juju_call(
1890
 
                    self, c_mock, client, get_timeout_prefix(600) + (
1891
 
                        juju_name, '--show-log', 'destroy-environment', 'bar',
1892
 
                        '-y'), call_index)
1893
 
        self.assertEqual(tear_down_count, c_mock.call_count)
 
1888
        tear_down_count = 0 if keep_env else 1
 
1889
        self.assertEqual(1, kill_mock.call_count)
 
1890
        self.assertEqual(tear_down_count, tear_down_mock.call_count)
1894
1891
 
1895
1892
    def test_bootstrap_context(self):
1896
1893
        cc_mock = self.addContext(patch('subprocess.check_call'))
2062
2059
        self.addContext(patch('deploy_stack.get_machine_dns_name',
2063
2060
                              return_value='foo'))
2064
2061
        self.addContext(patch('subprocess.check_call'))
2065
 
        call_mock = self.addContext(patch('subprocess.call', return_value=0))
 
2062
        tear_down_mock = self.addContext(
 
2063
            patch('deploy_stack.BootstrapManager.tear_down', autospec=True))
 
2064
        kill_mock = self.addContext(
 
2065
            patch('jujupy.EnvJujuClient.kill_controller', autospec=True))
2066
2066
        po_mock = self.addContext(patch(
2067
2067
            'subprocess.Popen', autospec=True,
2068
2068
            return_value=FakePopen('kill-controller', '', 0)))
2084
2084
        self.assertEqual(call_args[0].get_address(), 'baz')
2085
2085
        self.assertEqual(call_args[1], 'log_dir')
2086
2086
        al_mock.assert_called_once_with('log_dir')
2087
 
        timeout_path = get_timeout_path()
2088
 
        assert_juju_call(self, call_mock, client, (
2089
 
            sys.executable, timeout_path, '600.00', '--',
2090
 
            'path', '--show-log', 'kill-controller', 'bar', '-y'
2091
 
            ), 0)
2092
 
        assert_juju_call(self, call_mock, client, (
2093
 
            sys.executable, timeout_path, '600.00', '--',
2094
 
            'path', '--show-log', 'kill-controller', 'bar', '-y'
2095
 
            ), 1)
2096
 
        self.assertEqual(2, call_mock.call_count)
 
2087
        self.assertEqual(0, tear_down_mock.call_count)
 
2088
        self.assertEqual(2, kill_mock.call_count)
2097
2089
        self.assertEqual(0, po_mock.call_count)
2098
2090
 
2099
2091
    def test_with_bootstrap_failure_non_jes(self):
2106
2098
        self.addContext(patch('deploy_stack.get_machine_dns_name',
2107
2099
                              return_value='foo'))
2108
2100
        self.addContext(patch('subprocess.check_call'))
2109
 
        call_mock = self.addContext(patch('subprocess.call', return_value=0))
 
2101
        tear_down_mock = self.addContext(
 
2102
            patch('deploy_stack.BootstrapManager.tear_down', autospec=True))
 
2103
        kill_mock = self.addContext(
 
2104
            patch.object(client, 'kill_controller', autospec=True))
2110
2105
        po_mock = self.addContext(patch('subprocess.Popen', autospec=True,
2111
2106
                                        return_value=FakePopen('', '', 0)))
2112
2107
        self.addContext(patch('deploy_stack.wait_for_port'))
2127
2122
        self.assertEqual(call_args[0].get_address(), 'baz')
2128
2123
        self.assertEqual(call_args[1], 'log_dir')
2129
2124
        al_mock.assert_called_once_with('log_dir')
2130
 
        timeout_path = get_timeout_path()
2131
 
        assert_juju_call(self, call_mock, client, (
2132
 
            sys.executable, timeout_path, '600.00', '--',
2133
 
            'path', '--show-log', 'destroy-environment', 'bar', '-y'
2134
 
            ), 0)
2135
 
        assert_juju_call(self, call_mock, client, (
2136
 
            sys.executable, timeout_path, '600.00', '--',
2137
 
            'path', '--show-log', 'destroy-environment', 'bar', '-y'
2138
 
            ), 1)
2139
 
        self.assertEqual(2, call_mock.call_count)
 
2125
        self.assertEqual(0, tear_down_mock.call_count)
 
2126
        self.assertEqual(2, kill_mock.call_count)
2140
2127
        self.assertEqual(0, po_mock.call_count)
2141
2128
 
2142
2129
    def test_jes(self):