<commit_before>from mock import (
    ANY,
    patch
)
from unittest import TestCase

from jujupy import (
    EnvJujuClient,
    SimpleEnvironment,
    )
from quickstart_deploy import QuickstartTest


class FakeEnvJujuClient(EnvJujuClient):

    def __init__(self, name='steve'):
        super(FakeEnvJujuClient, self).__init__(
            SimpleEnvironment(name, {'type': 'fake'}), '1.2', '/jbin/juju')

    def quickstart(self, *args, **kwargs):
        # Suppress stdout for juju commands.
        with patch('sys.stdout'):
            return super(FakeEnvJujuClient, self).quickstart(*args, **kwargs)

    def wait_for_deploy_started(self, *args, **kwargs):
        # Suppress stdout for juju commands.
        with patch('sys.stdout'):
            return super(FakeEnvJujuClient, self).wait_for_deploy_started(
                *args, **kwargs)

    def destroy_environment(self, *args, **kwargs):
        # Suppress stdout for juju commands.
        with patch('sys.stdout'):
            return super(FakeEnvJujuClient, self).destroy_environment(
                *args, **kwargs)


class TestQuickstartTest(TestCase):

    def test_from_args(self):
        side_effect = lambda x, y=None, debug=False: (x, y)
        with patch('jujupy.EnvJujuClient.by_version', side_effect=side_effect):
            with patch('jujupy.SimpleEnvironment.from_config',
                       side_effect=lambda x: SimpleEnvironment(x, {})):
                quickstart = QuickstartTest.from_args(
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
                    '/tmp/bundle.yaml', 2
                )
        self.assertIs(type(quickstart), QuickstartTest)
        self.assertEqual(quickstart.client[0].environment, 'temp_env_name')
        self.assertIs(quickstart.client[1], '/foo/bin/juju')
        self.assertEqual(quickstart.bundle_path, '/tmp/bundle.yaml')
        self.assertEqual(quickstart.log_dir, '/tmp/tmp')
        self.assertEqual(quickstart.service_count, 2)

    def test_from_args_agent_url(self):
        side_effect = lambda x, y=None, debug=False: (x, y)
        with patch('jujupy.EnvJujuClient.by_version', side_effect=side_effect):
            with patch('jujupy.SimpleEnvironment.from_config',
                       side_effect=lambda x: SimpleEnvironment(x, {})):
                quickstart = QuickstartTest.from_args(
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
                    '/tmp/bundle.yaml', 2, agent_url='http://agent_url.com'
                )
        self.assertEqual(quickstart.client[0].config['agent_url'],
                         'http://agent_url.com')

    def test_from_args_series(self):
        side_effect = lambda x, y=None, debug=False: (x, y)
        with patch('jujupy.EnvJujuClient.by_version', side_effect=side_effect):
            with patch('jujupy.SimpleEnvironment.from_config',
                       side_effect=lambda x: SimpleEnvironment(x, {})):
                quickstart = QuickstartTest.from_args(
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
                    '/tmp/bundle.yaml', 2, series='precise'
                )
        self.assertEqual(quickstart.client[0].config['series'],
                         'precise')

    def test_from_args_debug(self):
        with patch('jujupy.EnvJujuClient.get_version',
                   side_effect=lambda x, juju_path=None: ''):
            with patch('jujupy.SimpleEnvironment.from_config',
                       side_effect=lambda x: SimpleEnvironment(x, {})):
                quickstart = QuickstartTest.from_args(
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
                    '/tmp/bundle.yaml', 2, debug_flag=True
                )
        self.assertEqual(quickstart.client.debug, True)

    def test_run_finally(self):
        client = FakeEnvJujuClient()
        quickstart = QuickstartTest(client, '/tmp/bundle.yaml', '/tmp/logs', 2)
        with patch.object(client, 'destroy_environment') as qs_mock:
            with patch('quickstart_deploy.safe_print_status') as ps_mock:
                with patch.object(quickstart, 'iter_steps'):
                    quickstart.run()
        qs_mock.assert_called_once_with(delete_jenv=True)
        ps_mock.assert_called_once_with(client)

    def test_iter_steps(self):
        client = FakeEnvJujuClient()
        quickstart = QuickstartTest(client, '/tmp/bundle.yaml', '/tmp/logs', 2)
        steps = quickstart.iter_steps()
        with patch.object(client, 'quickstart') as qs_mock:
            # Test first yield
            step = steps.next()
        qs_mock.assert_called_once_with('/tmp/bundle.yaml')
        expected = {'juju-quickstart': 'Returned from quickstart'}
        self.assertEqual(expected, step)
        with patch('quickstart_deploy.get_machine_dns_name',
                   return_value='mocked_name') as dns_mock:
            # Test second yield
            step = steps.next()
        dns_mock.assert_called_once_with(client, 0)
        self.assertEqual('mocked_name', step['bootstrap_host'])
        with patch.object(client, 'wait_for_deploy_started') as wds_mock:
            # Test third yield
            step = steps.next()
        wds_mock.assert_called_once_with(2)
        self.assertEqual('Deploy stated', step['deploy_started'])
        with patch.object(client, 'wait_for_started') as ws_mock:
            # Test forth yield
            step = steps.next()
        ws_mock.assert_called_once_with(ANY)
        self.assertEqual('All Agents started', step['agents_started'])<commit_msg>Drop use of FakeEnvJujuClient in test_quickstart_deploy.py and add an exception test for QuickstartTest.run().<commit_after>from mock import (
    ANY,
    patch
)
from unittest import TestCase

from jujupy import (
    EnvJujuClient,
    SimpleEnvironment,
    )
from quickstart_deploy import QuickstartTest


class TestQuickstartTest(TestCase):

    def test_from_args(self):
        side_effect = lambda x, y=None, debug=False: (x, y)
        with patch('jujupy.EnvJujuClient.by_version', side_effect=side_effect):
            with patch('jujupy.SimpleEnvironment.from_config',
                       side_effect=lambda x: SimpleEnvironment(x, {})):
                quickstart = QuickstartTest.from_args(
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
                    '/tmp/bundle.yaml', 2
                )
        self.assertIs(type(quickstart), QuickstartTest)
        self.assertEqual(quickstart.client[0].environment, 'temp_env_name')
        self.assertIs(quickstart.client[1], '/foo/bin/juju')
        self.assertEqual(quickstart.bundle_path, '/tmp/bundle.yaml')
        self.assertEqual(quickstart.log_dir, '/tmp/tmp')
        self.assertEqual(quickstart.service_count, 2)

    def test_from_args_agent_url(self):
        side_effect = lambda x, y=None, debug=False: (x, y)
        with patch('jujupy.EnvJujuClient.by_version', side_effect=side_effect):
            with patch('jujupy.SimpleEnvironment.from_config',
                       side_effect=lambda x: SimpleEnvironment(x, {})):
                quickstart = QuickstartTest.from_args(
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
                    '/tmp/bundle.yaml', 2, agent_url='http://agent_url.com'
                )
        self.assertEqual(quickstart.client[0].config['agent_url'],
                         'http://agent_url.com')

    def test_from_args_series(self):
        side_effect = lambda x, y=None, debug=False: (x, y)
        with patch('jujupy.EnvJujuClient.by_version', side_effect=side_effect):
            with patch('jujupy.SimpleEnvironment.from_config',
                       side_effect=lambda x: SimpleEnvironment(x, {})):
                quickstart = QuickstartTest.from_args(
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
                    '/tmp/bundle.yaml', 2, series='precise'
                )
        self.assertEqual(quickstart.client[0].config['series'],
                         'precise')

    def test_from_args_debug(self):
        with patch('jujupy.EnvJujuClient.get_version',
                   side_effect=lambda x, juju_path=None: ''):
            with patch('jujupy.SimpleEnvironment.from_config',
                       side_effect=lambda x: SimpleEnvironment(x, {})):
                quickstart = QuickstartTest.from_args(
                    'base_env', 'temp_env_name', '/foo/bin/juju', '/tmp/tmp',
                    '/tmp/bundle.yaml', 2, debug_flag=True
                )
        self.assertEqual(quickstart.client.debug, True)

    def test_run_finally(self):
        client = EnvJujuClient(
            SimpleEnvironment('foo', {'type': 'local'}), '1.234-76', None)
        quickstart = QuickstartTest(client, '/tmp/bundle.yaml', '/tmp/logs', 2)
        with patch.object(client, 'destroy_environment') as qs_mock:
            with patch('quickstart_deploy.safe_print_status') as ps_mock:
                with patch.object(quickstart, 'iter_steps'):
                    quickstart.run()
        qs_mock.assert_called_once_with(delete_jenv=True)
        ps_mock.assert_called_once_with(client)

    def test_run_exception(self):
        def fake_iter_steps():
            yield {'bootstrap_host': 'foo'}
            raise Exception()
        client = EnvJujuClient(
            SimpleEnvironment('foo', {'type': 'local'}), '1.234-76', None)
        quickstart = QuickstartTest(client, '/tmp/bundle.yaml', '/tmp/logs', 2)
        with patch.object(client, 'destroy_environment') as qs_mock:
            with patch('quickstart_deploy.safe_print_status') as ps_mock:
                with patch('quickstart_deploy.dump_env_logs') as dl_mock:
                    with patch.object(quickstart, 'iter_steps',
                                      side_effect=fake_iter_steps):
                        quickstart.run()
        dl_mock.assert_called_once_with(client, 'foo', '/tmp/logs')
        qs_mock.assert_called_once_with(delete_jenv=True)
        ps_mock.assert_called_once_with(client)

    def test_iter_steps(self):
        client = EnvJujuClient(
            SimpleEnvironment('foo', {'type': 'local'}), '1.234-76', None)
        quickstart = QuickstartTest(client, '/tmp/bundle.yaml', '/tmp/logs', 2)
        steps = quickstart.iter_steps()
        with patch.object(client, 'quickstart') as qs_mock:
            # Test first yield
            step = steps.next()
        qs_mock.assert_called_once_with('/tmp/bundle.yaml')
        expected = {'juju-quickstart': 'Returned from quickstart'}
        self.assertEqual(expected, step)
        with patch('quickstart_deploy.get_machine_dns_name',
                   return_value='mocked_name') as dns_mock:
            # Test second yield
            step = steps.next()
        dns_mock.assert_called_once_with(client, 0)
        self.assertEqual('mocked_name', step['bootstrap_host'])
        with patch.object(client, 'wait_for_deploy_started') as wds_mock:
            # Test third yield
            step = steps.next()
        wds_mock.assert_called_once_with(2)
        self.assertEqual('Deploy stated', step['deploy_started'])
        with patch.object(client, 'wait_for_started') as ws_mock:
            # Test forth yield
            step = steps.next()
        ws_mock.assert_called_once_with(ANY)
        self.assertEqual('All Agents started', step['agents_started'])
{'fraction_non_alphanumeric': 0.1185391789418635, 'fraction_numerical': 0.004096574566373224, 'mean_word_length': 2.422010140172979, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2942120', 'n_tokens_mistral': 3493, 'n_tokens_neox': 3315, 'n_words': 588}