Exemple #1
0
 def test_genty_args_yields_args_and_kwargs(self, args_tuple, kwargs_dict):
     gargs = genty_args(*args_tuple, **kwargs_dict)
     for fruit, color in six.iteritems(kwargs_dict):
         self.assertIn('{0}={1}'.format(fruit, repr(color)), gargs)
     for arg in args_tuple:
         formatted_arg = format_arg(arg)
         self.assertIn(formatted_arg, gargs)
 def test_genty_args_yields_args_and_kwargs(self, args_tuple, kwargs_dict):
     gargs = genty_args(*args_tuple, **kwargs_dict)
     for fruit, color in kwargs_dict.iteritems():
         self.assertIn('{}={}'.format(fruit, repr(color)), gargs)
     for arg in args_tuple:
         formatted_arg = repr(arg) if isinstance(arg, basestring) else unicode(arg)
         self.assertIn(formatted_arg, gargs)
class RotUnicodeUtilsTest(TestCase):
    """Tests for :mod:`box.util.rotunicode.utils`."""
    @classmethod
    def setUpClass(cls):
        super(RotUnicodeUtilsTest, cls).setUpClass()
        codecs.register(RotUnicode.search_function)

    @genty_dataset(
        genty_args('plain', 'ҏľȁȉń'),
        genty_args('plain', 'ҏľȁȉń', extension=False),
        genty_args('.extension', '.ȅхƭȅńŝȉőń'),
        genty_args('.extension', '.ȅхƭȅńŝȉőń', extension=False),
        genty_args('plain.txt', 'ҏľȁȉń.txt'),
        genty_args(
            'plain.txt',
            'ҏľȁȉń.txt',
            extension=False,
        ),
        genty_args('plain.txt', 'ҏľȁȉń.ƭхƭ', extension=True),
        genty_args('two.ext.sions', 'ƭŵő.ȅхƭ.sions'),
        genty_args('two.ext.sions', 'ƭŵő.ȅхƭ.sions', extension=False),
        genty_args('two.ext.sions', 'ƭŵő.ȅхƭ.ŝȉőńŝ', extension=True),
    )
    def test_ruencode_encodes_string_using_rotunicode(
        self,
        source,
        target,
        extension=None,
    ):
        encoded_source = ruencode(source) if extension is None else ruencode(
            source, extension=extension)
        self.assertEqual(
            target,
            encoded_source,
        )

    def test_rudecode_decodes_string_using_rotunicode(self):
        self.assertEqual(
            'Hello World!',
            rudecode('Ĥȅľľő Ŵőŕľď!'),
        )
Exemple #4
0
class TestDeploySubcommand(BaseUnitTestCase):
    def setUp(self):
        super().setUp()
        self.patch('app.subcommands.deploy_subcommand.fs.tar_directory')

    def test_binaries_tar_raises_exception_if_running_from_source(self):
        deploy_subcommand = DeploySubcommand()
        with self.assertRaisesRegex(SystemExit, '1'):
            deploy_subcommand._binaries_tar('python main.py deploy', '~/.clusterrunner/dist')

    def test_binaries_doesnt_raise_exception_if_running_from_bin(self):
        self.patch('os.path.isfile').return_value = True
        deploy_subcommand = DeploySubcommand()
        deploy_subcommand._binaries_tar('clusterrunner', '~/.clusterrunner/dist')

    def test_deploy_binaries_and_conf_deploys_both_conf_and_binary_for_remote_host(self):
        mock_DeployTarget = self.patch('app.subcommands.deploy_subcommand.DeployTarget')
        mock_DeployTarget_instance = mock_DeployTarget.return_value
        deploy_subcommand = DeploySubcommand()
        deploy_subcommand._deploy_binaries_and_conf(
            'remote_host', 'username', 'exec', '/path/to/exec', '/path/to/conf')
        self.assertTrue(mock_DeployTarget_instance.deploy_binary.called)
        self.assertTrue(mock_DeployTarget_instance.deploy_conf.called)

    @genty_dataset(
        # expect to deploy the binary but not the conf when the current executable path is not the same
        # as the target executable path but the current conf path is the same as the target conf path
        same_conf_path_different_exe_path=genty_args(
            current_executable=join(expanduser('~'), '.clusterrunner', 'dist', 'clusterrunner2'),
            in_use_conf_path=join(expanduser('~'), '.clusterrunner', 'clusterrunner.conf'),
            expect_deploy_conf=False,
            expect_deploy_binary=True,
        ),
        # expect not to deploy the binary or the conf when the current executable path is the same
        # as the target executable path and the current conf path is the same as the target conf path
        same_conf_path_same_exe_path=genty_args(
            current_executable=join(expanduser('~'), '.clusterrunner', 'dist', 'clusterrunner'),
            in_use_conf_path=join(expanduser('~'), '.clusterrunner', 'clusterrunner.conf'),
            expect_deploy_conf=False,
            expect_deploy_binary=False,
        ),
        # expect to deploy the conf but not the binary when the current conf path is not the same
        # as the target conf path but the current binary path is the same as the target binary path
        different_conf_path_same_exe_path=genty_args(
            current_executable=join(expanduser('~'), '.clusterrunner', 'dist', 'clusterrunner'),
            in_use_conf_path=join(expanduser('~'), '.clusterrunner', 'clusterrunner2.conf'),
            expect_deploy_conf=True,
            expect_deploy_binary=False,
        ),
        # expect to deploy the binary and the conf when the current executable path is not the same
        # as the target executable path and the current conf path is not the same as the target conf path
        different_conf_path_different_exe_path=genty_args(
            current_executable=join(expanduser('~'), '.clusterrunner', 'dist', 'clusterrunner2'),
            in_use_conf_path=join(expanduser('~'), '.clusterrunner', 'clusterrunner2.conf'),
            expect_deploy_conf=True,
            expect_deploy_binary=True,
        ),
    )
    def test_deploy_binaries_and_conf_behaves_properly_if_conf_or_binary_is_in_use_on_localhost(
            self,
            current_executable,
            in_use_conf_path,
            expect_deploy_conf,
            expect_deploy_binary,
    ):
        mock_DeployTarget = self.patch('app.subcommands.deploy_subcommand.DeployTarget')
        mock_DeployTarget_instance = mock_DeployTarget.return_value
        deploy_subcommand = DeploySubcommand()
        deploy_subcommand._deploy_binaries_and_conf(
            'localhost',
            'username',
            current_executable,
            join(expanduser('~'), '.clusterrunner', 'clusterrunner.tgz'),
            in_use_conf_path
        )
        self.assertEqual(expect_deploy_binary, mock_DeployTarget_instance.deploy_binary.called)
        self.assertEqual(expect_deploy_conf, mock_DeployTarget_instance.deploy_conf.called)

    def test_non_registered_slaves_returns_empty_list_if_all_registered(self):
        registered_hosts = ['host_1', 'host_2']
        slaves_to_validate = ['host_1', 'host_2']

        def get_host_id(*args, **kwargs):
            if args[0] == 'host_1':
                return 'host_id_1'
            elif args[0] == 'host_2':
                return 'host_id_2'
            else:
                return 'blah'

        old_host_id = Network.get_host_id
        Network.get_host_id = get_host_id
        deploy_subcommand = DeploySubcommand()
        non_registered = deploy_subcommand._non_registered_slaves(registered_hosts, slaves_to_validate)
        Network.get_host_id = old_host_id
        self.assertEquals(0, len(non_registered))

    def test_non_registered_slaves_returns_non_registered_slaves(self):
        registered_hosts = ['host_1', 'host_3']
        slaves_to_validate = ['host_1', 'host_2', 'host_3', 'host_4']

        def get_host_id(*args, **kwargs):
            if args[0] == 'host_1':
                return 'host_id_1'
            elif args[0] == 'host_2':
                return 'host_id_2'
            elif args[0] == 'host_3':
                return 'host_id_3'
            elif args[0] == 'host_4':
                return 'host_id_4'
            else:
                return 'blah'

        self.patch('app.util.network.Network.get_host_id', new=get_host_id)
        deploy_subcommand = DeploySubcommand()
        non_registered = deploy_subcommand._non_registered_slaves(registered_hosts, slaves_to_validate)
        self.assertEquals(len(non_registered), 2)
        self.assertTrue('host_2' in non_registered)
        self.assertTrue('host_4' in non_registered)

    def test_non_registered_slaves_returns_empty_list_with_slaves_with_same_host_ids_but_different_names(self):
        registered_hosts = ['host_1_alias', 'host_2_alias']
        slaves_to_validate = ['host_1', 'host_2']

        def get_host_id(*args, **kwargs):
            if args[0] == 'host_1':
                return 'host_id_1'
            elif args[0] == 'host_2':
                return 'host_id_2'
            elif args[0] == 'host_1_alias':
                return 'host_id_1'
            elif args[0] == 'host_2_alias':
                return 'host_id_2'
            else:
                return 'blah'

        self.patch('app.util.network.Network.get_host_id', new=get_host_id)
        deploy_subcommand = DeploySubcommand()
        non_registered = deploy_subcommand._non_registered_slaves(registered_hosts, slaves_to_validate)
        self.assertEquals(0, len(non_registered))

    @genty_dataset(
        valid_deployment=genty_args(
            slaves_to_validate=['slave_host_1', 'slave_host_2'],
            connected_slaves=['slave_host_1', 'slave_host_2'],
            host_name_to_uid={
                'slave_host_1': 'host_1_id',
                'slave_host_2': 'host_2_id',
            },
            is_valid=True,
        ),
        host_mismatch=genty_args(
            slaves_to_validate=['slave_host_1', 'slave_host_2'],
            connected_slaves=['slave_host_3', 'slave_host_2'],
            host_name_to_uid={
                'slave_host_2': 'host_2_id',
            },
            is_valid=False,
        ),
        number_of_slaves_not_match=genty_args(
            slaves_to_validate=['slave_host_1'],
            connected_slaves=['slave_host_1', 'slave_host_2'],
            host_name_to_uid={
                'slave_host_1': 'host_1_id',
            },
            is_valid=False,
        ),
        valid_deployment_different_host_names_with_same_host_id=genty_args(
            slaves_to_validate=['slave_host_1', 'slave_host_2'],
            connected_slaves=['slave_host_1_alias', 'slave_host_2'],
            host_name_to_uid={
                'slave_host_1': 'host_1_id',
                'slave_host_1_alias': 'host_1_id',
                'slave_host_2': 'host_2_id',
            },
            is_valid=True,
        ),
    )
    def test_validate_deployment_checks_each_slave_is_connected(
            self,
            slaves_to_validate,
            connected_slaves,
            host_name_to_uid,
            is_valid,
    ):
        def get_host_id(host):
            if host in host_name_to_uid:
                return host_name_to_uid[host]
            else:
                return str(uuid.uuid4())

        self.patch('app.util.network.Network.get_host_id', new=get_host_id)

        deploy_subcommand = DeploySubcommand()
        deploy_subcommand._registered_slave_hostnames = MagicMock(return_value=connected_slaves)
        deploy_subcommand._SLAVE_REGISTRY_TIMEOUT_SEC = 1
        deploy_subcommand._non_registered_slaves = MagicMock()
        validate = partial(deploy_subcommand._validate_successful_deployment, 'master_host_url', slaves_to_validate)
        if not is_valid:
            with self.assertRaises(SystemExit):
                validate()
        else:
            validate()
Exemple #5
0
 def builder(self):
     return genty_args(42, named='named_arg')
class TestProcessUtils(BaseUnitTestCase):
    @genty_dataset(
        str_cmd_on_windows=(
            'set FOO=1 && echo !FOO!',
            'nt',
            ['cmd', '/V', '/C', 'set FOO=1 && echo !FOO!'],
        ),
        list_cmd_on_windows=(
            ['set', 'FOO=1', '&&', 'echo', '!FOO!'],
            'nt',
            ['cmd', '/V', '/C', 'set', 'FOO=1', '&&', 'echo', '!FOO!'],
        ),
        str_cmd_on_posix=(
            'export FOO=1; echo $FOO',
            'posix',
            'export FOO=1; echo $FOO',
        ),
        list_cmd_on_posix=(
            ['export', 'FOO=1;', 'echo', '$FOO'],
            'posix',
            ['export', 'FOO=1;', 'echo', '$FOO'],
        ),
    )
    def test_Popen_with_deplayed_expansion(self, input_cmd, os_name,
                                           expected_final_cmd):
        # Arrange
        mock_os = self.patch('app.util.process_utils.os')
        mock_os.name = os_name
        mock_subprocess_popen = self.patch('subprocess.Popen')

        # Act
        Popen_with_delayed_expansion(input_cmd)

        # Assert
        mock_subprocess_popen.assert_called_once_with(expected_final_cmd)

    @genty_dataset(
        windows=genty_args(
            name='FOO',
            value='1',
            os_name='nt',
            expected_command='set FOO=1&&',
        ),
        posix=genty_args(
            name='BAR',
            value='2',
            os_name='posix',
            expected_command='export BAR="2";',
        ),
    )
    def test_get_environment_variable_setter_command(self, name, value,
                                                     os_name,
                                                     expected_command):
        # Arrange
        mock_os = self.patch('app.util.process_utils.os')
        mock_os.name = os_name

        # Act
        command = get_environment_variable_setter_command(name, value)

        # Assert
        self.assertEqual(command, expected_command)
Exemple #7
0
    _USER = '******'

    @genty_dataset(async_and_err_on_failure=(NotImplementedError, True, True))
    def test_exec_command_raises_expected_error(self, expected_error, async,
                                                error_on_failure):
        client = ShellClient(self._HOST, self._USER)
        with self.assertRaises(expected_error):
            client.exec_command('foo',
                                async=async,
                                error_on_failure=error_on_failure)

    @genty_dataset(
        successful_copy_with_error_on_failure=genty_args(
            source='source0',
            dest='dest0',
            error_on_failure=True,
            copy_successful=True,
            expect_runtime_error=False,
        ),
        failed_copy_with_error_on_failure=genty_args(
            source='source1',
            dest='dest1',
            error_on_failure=True,
            copy_successful=False,
            expect_runtime_error=True,
        ),
        failed_copy_without_error_on_failure=genty_args(
            source='source2',
            dest='dest2',
            error_on_failure=False,
            copy_successful=False,
    _HOST = 'host'
    _USER = '******'

    @genty_dataset(
        async_and_err_on_failure=(NotImplementedError, True, True)
    )
    def test_exec_command_raises_expected_error(self, expected_error, async, error_on_failure):
        client = ShellClient(self._HOST, self._USER)
        with self.assertRaises(expected_error):
            client.exec_command('foo', async=async, error_on_failure=error_on_failure)

    @genty_dataset(
        successful_copy_with_error_on_failure=genty_args(
            source='source0',
            dest='dest0',
            error_on_failure=True,
            copy_successful=True,
            expect_runtime_error=False,
        ),
        failed_copy_with_error_on_failure=genty_args(
            source='source1',
            dest='dest1',
            error_on_failure=True,
            copy_successful=False,
            expect_runtime_error=True,
        ),
        failed_copy_without_error_on_failure=genty_args(
            source='source2',
            dest='dest2',
            error_on_failure=False,
            copy_successful=False,
 def test_genty_args_yields_kwargs(self, kwargs_dict):
     gargs = genty_args(**kwargs_dict)
     for fruit, color in kwargs_dict.iteritems():
         self.assertIn('{}={}'.format(fruit, repr(color)), gargs)
Exemple #10
0
 def test_genty_args_yields_formatted_args(self, *args):
     gargs = genty_args(*args)
     self.assertItemsEqual(
         gargs,
         (repr(arg) if isinstance(arg, basestring) else unicode(arg) for arg in args),
     )
Exemple #11
0
 def test_genty_args_saves_args_and_kwargs(self, args_tuple, kwargs_dict):
     gargs = genty_args(*args_tuple, **kwargs_dict)
     self.assertItemsEqual(gargs.args, args_tuple)
     self.assertItemsEqual(gargs.kwargs, kwargs_dict)
Exemple #12
0
 def test_genty_args_saves_kwargs(self, kwargs_dict):
     gargs = genty_args(**kwargs_dict)
     self.assertItemsEqual(gargs.kwargs, kwargs_dict)
Exemple #13
0
 def test_genty_args_saves_args(self, *args):
     gargs = genty_args(*args)
     self.assertItemsEqual(gargs.args, args)
Exemple #14
0
 def test_genty_args_yields_formatted_args(self, *args):
     gargs = genty_args(*args)
     self.assertItemsEqual(
         gargs,
         (format_arg(arg) for arg in args),
     )