Ejemplo n.º 1
0
 def test_execute_user_data_script(self, mock_get_command, _):
     mock_get_command.return_value.return_value = (
         mock.sentinel.output, mock.sentinel.error, -1
     )
     retval = userdatautils.execute_user_data_script(
         mock.sentinel.user_data)
     self.assertEqual(-1, retval)
Ejemplo n.º 2
0
    def process(self, part):
        file_name = os.path.join(CONF.heat_config_dir, part.get_filename())
        self._check_dir(file_name)

        encoding.write_file(file_name, part.get_payload())

        if part.get_filename() == self._heat_user_data_filename:
            return userdatautils.execute_user_data_script(part.get_payload())
Ejemplo n.º 3
0
    def process(self, part):
        self._check_heat_config_dir()

        file_name = os.path.join(CONF.heat_config_dir, part.get_filename())
        with open(file_name, 'wb') as f:
            f.write(part.get_payload())

        if part.get_filename() == self._heat_user_data_filename:
            return userdatautils.execute_user_data_script(part.get_payload())
Ejemplo n.º 4
0
    def _process_non_multi_part(self, user_data):
        if user_data.startswith('#cloud-config'):
            user_data_plugins = factory.load_plugins()
            cloud_config_plugin = user_data_plugins.get('text/cloud-config')
            ret_val = cloud_config_plugin.process(user_data)
        else:
            ret_val = userdatautils.execute_user_data_script(user_data)

        return self._get_plugin_return_value(ret_val)
Ejemplo n.º 5
0
    def _test_execute_user_data_script(self, mock_get_os_utils,
                                       mock_path_expandvars,
                                       mock_path_exists, mock_path_isdir,
                                       mock_os_remove, mock_gettempdir,
                                       mock_re_search, fake_user_data,
                                       directory_exists):
        mock_osutils = mock.MagicMock()
        mock_gettempdir.return_value = 'fake_temp'
        uuid.uuid4 = mock.MagicMock(return_value='randomID')
        match_instance = mock.MagicMock()
        path = os.path.join('fake_temp', 'randomID')
        args = None
        mock_get_os_utils.return_value = mock_osutils
        if fake_user_data == '^rem cmd\s':
            side_effect = [match_instance]
            number_of_calls = 1
            extension = '.cmd'
            args = [path+extension]
            shell = True
        elif fake_user_data == '^#!/usr/bin/env\spython\s':
            side_effect = [None, match_instance]
            number_of_calls = 2
            extension = '.py'
            args = ['python.exe', path+extension]
            shell = False
        elif fake_user_data == '#!':
            side_effect = [None, None, match_instance]
            number_of_calls = 3
            extension = '.sh'
            args = ['bash.exe', path+extension]
            shell = False
        elif fake_user_data == '#ps1\s':
            side_effect = [None, None, None, match_instance]
            number_of_calls = 4
            extension = '.ps1'
            args = ['powershell.exe', '-ExecutionPolicy', 'RemoteSigned',
                    '-NonInteractive', path+extension]
            shell = False
        else:
            side_effect = [None, None, None, None, match_instance]
            number_of_calls = 5
            extension = '.ps1'
            shell = False
            if directory_exists:
                args = [mock_path_expandvars('%windir%\\sysnative\\'
                                             'WindowsPowerShell\\v1.0\\'
                                             'powershell.exe'),
                        '-ExecutionPolicy',
                        'RemoteSigned', '-NonInteractive', path+extension]
                mock_path_isdir.return_value = True
            else:
                mock_path_isdir.return_value = False

        mock_re_search.side_effect = side_effect

        with mock.patch('cloudbaseinit.plugins.windows.userdatautils.open',
                        mock.mock_open(), create=True):
            response = userdatautils.execute_user_data_script(fake_user_data)
        mock_gettempdir.assert_called_once_with()
        self.assertEqual(mock_re_search.call_count, number_of_calls)
        if args:
            mock_osutils.execute_process.assert_called_with(args, shell)
        if not directory_exists:
            self.assertEqual(response, 0)
        else:
            self.assertEqual(response, None)
Ejemplo n.º 6
0
 def _process_non_multi_part(self, user_data):
     ret_val = userdatautils.execute_user_data_script(user_data)
     return self._get_plugin_return_value(ret_val)
    def _test_execute_user_data_script(self, mock_get_os_utils,
                                       mock_path_expandvars,
                                       mock_path_exists, mock_os_remove,
                                       mock_gettempdir, mock_re_search,
                                       fake_user_data):
        mock_osutils = mock.MagicMock()
        mock_gettempdir.return_value = 'fake_temp'
        uuid.uuid4 = mock.MagicMock(return_value='randomID')
        match_instance = mock.MagicMock()
        path = os.path.join('fake_temp', 'randomID')
        args = None
        powershell = False
        mock_get_os_utils.return_value = mock_osutils
        mock_path_exists.return_value = True
        if fake_user_data == '^rem cmd\s':
            side_effect = [match_instance]
            number_of_calls = 1
            extension = '.cmd'
            args = [path + extension]
            shell = True
        elif fake_user_data == '^#!/usr/bin/env\spython\s':
            side_effect = [None, match_instance]
            number_of_calls = 2
            extension = '.py'
            args = ['python.exe', path + extension]
            shell = False
        elif fake_user_data == '#!':
            side_effect = [None, None, match_instance]
            number_of_calls = 3
            extension = '.sh'
            args = ['bash.exe', path + extension]
            shell = False
        elif fake_user_data == '#ps1_sysnative\s':
            side_effect = [None, None, None, match_instance]
            number_of_calls = 4
            extension = '.ps1'
            sysnative = True
            powershell = True
        elif fake_user_data == '#ps1_x86\s':
            side_effect = [None, None, None, None, match_instance]
            number_of_calls = 5
            extension = '.ps1'
            shell = False
            sysnative = False
            powershell = True
        else:
            side_effect = [None, None, None, None, None]
            number_of_calls = 5

        mock_re_search.side_effect = side_effect

        with mock.patch('cloudbaseinit.plugins.windows.userdatautils.open',
                        mock.mock_open(), create=True):
            response = userdatautils.execute_user_data_script(fake_user_data)
        mock_gettempdir.assert_called_once_with()
        self.assertEqual(mock_re_search.call_count, number_of_calls)
        if args:
            mock_osutils.execute_process.assert_called_with(args, shell)
            mock_os_remove.assert_called_once_with(path + extension)
            self.assertEqual(response, None)
        elif powershell:
            mock_osutils.execute_powershell_script.assert_called_with(
                path + extension, sysnative)
            mock_os_remove.assert_called_once_with(path + extension)
            self.assertEqual(response, None)
        else:
            self.assertEqual(response, 0)
Ejemplo n.º 8
0
    def test_execute_user_data_script_fails(self, mock_get_command, _):
        mock_get_command.return_value.side_effect = ValueError
        retval = userdatautils.execute_user_data_script(
            mock.sentinel.user_data)

        self.assertEqual(0, retval)
Ejemplo n.º 9
0
 def test_execute_user_data_script_no_commands(self, _):
     retval = userdatautils.execute_user_data_script(b"unknown")
     self.assertEqual(0, retval)
Ejemplo n.º 10
0
 def process(self, part):
     if part.get_filename() == self._heat_user_data_filename:
         return userdatautils.execute_user_data_script(part.get_payload())
     else:
         LOG.info("Heat content not supported: %s" % part.get_filename())