def _test_exec_file(self, mock_get_os_utils, filename, exception=False): mock_osutils = mock.MagicMock() mock_part = mock.MagicMock() mock_part.get_filename.return_value = filename mock_get_os_utils.return_value = mock_osutils if exception: mock_osutils.execute_process.side_effect = [Exception] with mock.patch( "cloudbaseinit.plugins.windows.userdataplugins." "shellscript.open", mock.mock_open(), create=True): response = fileexecutils.exec_file(filename) if filename.endswith(".cmd"): mock_osutils.execute_process.assert_called_once_with([filename], True) elif filename.endswith(".sh"): mock_osutils.execute_process.assert_called_once_with( ['bash.exe', filename], False) elif filename.endswith(".py"): mock_osutils.execute_process.assert_called_once_with( ['python.exe', filename], False) elif filename.endswith(".exe"): mock_osutils.execute_process.assert_called_once_with([filename], False) elif filename.endswith(".ps1"): mock_osutils.execute_powershell_script.assert_called_once_with( filename) else: self.assertEqual(0, response)
def _test_exec_file(self, mock_get_os_utils, filename, exception=False): mock_osutils = mock.MagicMock() mock_part = mock.MagicMock() mock_part.get_filename.return_value = filename mock_get_os_utils.return_value = mock_osutils if exception: mock_osutils.execute_process.side_effect = [Exception] with mock.patch("cloudbaseinit.plugins.windows.userdataplugins." "shellscript.open", mock.mock_open(), create=True): response = fileexecutils.exec_file(filename) if filename.endswith(".cmd"): mock_osutils.execute_process.assert_called_once_with( [filename], True) elif filename.endswith(".sh"): mock_osutils.execute_process.assert_called_once_with( ['bash.exe', filename], False) elif filename.endswith(".py"): mock_osutils.execute_process.assert_called_once_with( ['python.exe', filename], False) elif filename.endswith(".exe"): mock_osutils.execute_process.assert_called_once_with( [filename], False) elif filename.endswith(".ps1"): mock_osutils.execute_powershell_script.assert_called_once_with( filename) else: self.assertEqual(0, response)
def execute(self, service, shared_data): if CONF.set_date_url is None: LOG.info('No date_url set, cannot set date') return (base.PLUGIN_EXECUTE_ON_NEXT_BOOT, False) url = CONF.set_date_url date_format = '%d %B %Y %H:%M:%S' LOG.info('Fetching date from: %s' % url) date_str = urllib.urlopen(url).read().strip() date_val = time.strptime(date_str, date_format) windows_date_str = time.strftime('%m-%d-%y', date_val) windows_time_str = time.strftime('%H:%M:%S', date_val) commands = """ date %s time %s """ % (windows_date_str, windows_time_str) target_path = os.path.join(tempfile.gettempdir(), 'setdate.cmd') try: with open(target_path, 'wb') as f: f.write(commands) ret_val = fileexecutils.exec_file(target_path) if ret_val == 0: LOG.info('Set date: %s' % date_str) else: LOG.warning('Failed to set date: %s' % ret_val) except Exception as ex: LOG.warning('An error occurred during set_date execution: \'%s\'' % ex) finally: if os.path.exists(target_path): os.remove(target_path) return (base.PLUGIN_EXECUTION_DONE, False)
def test_exec_file_(self, mock_execute, _): mock_execute.return_value = ( mock.sentinel.out, mock.sentinel.error, 0, ) retval = fileexecutils.exec_file("fake.py") mock_execute.assert_called_once_with() self.assertEqual(0, retval)
def test_exec_file_(self, mock_execute, _): mock_execute.return_value = ( mock.sentinel.out, mock.sentinel.error, 0, ) retval = fileexecutils.exec_file("fake.py") mock_execute.assert_called_once_with() self.assertEqual(0, retval)
def process(self, part): file_name = part.get_filename() target_path = os.path.join(tempfile.gettempdir(), file_name) try: with open(target_path, 'wb') as f: f.write(part.get_payload()) return fileexecutils.exec_file(target_path) except Exception, ex: LOG.warning('An error occurred during user_data execution: \'%s\'' % ex)
def process(self, part): file_name = part.get_filename() target_path = os.path.join(tempfile.gettempdir(), file_name) try: encoding.write_file(target_path, part.get_payload()) return fileexecutils.exec_file(target_path) except Exception as ex: LOG.warning('An error occurred during user_data execution: \'%s\'' % ex) finally: if os.path.exists(target_path): os.remove(target_path)
def process(self, part): file_name = part.get_filename() target_path = os.path.join(tempfile.gettempdir(), file_name) try: encoding.write_file(target_path, part.get_payload()) return fileexecutils.exec_file(target_path) except Exception as ex: LOG.warning( 'An error occurred during user_data execution: \'%s\'' % ex) finally: if os.path.exists(target_path): os.remove(target_path)
def execute(self, service, shared_data): if CONF.local_scripts_path: for file_path in self._get_files_in_dir(CONF.local_scripts_path): fileexecutils.exec_file(file_path) return (base.PLUGIN_EXECUTION_DONE, False)
def execute(self, service, shared_data): if CONF.local_scripts_path: for file_path in self._get_files_in_dir(CONF.local_scripts_path): fileexecutils.exec_file(file_path) return (base.PLUGIN_EXECUTION_DONE, False)
def test_exec_file_fails(self, mock_execute, _): mock_execute.side_effect = ValueError retval = fileexecutils.exec_file("fake.py") mock_execute.assert_called_once_with() self.assertEqual(0, retval)
def test_exec_file_no_executor(self, _): retval = fileexecutils.exec_file("fake.fake") self.assertEqual(0, retval)
def test_exec_file_fails(self, mock_execute, _): mock_execute.side_effect = ValueError retval = fileexecutils.exec_file("fake.py") mock_execute.assert_called_once_with() self.assertEqual(0, retval)
def test_exec_file_no_executor(self, _): retval = fileexecutils.exec_file("fake.fake") self.assertEqual(0, retval)