def wrap_element_by_master(cib_file, resource_id, master_id=None): cib_file.seek(0) cib_tree = etree.parse(cib_file, etree.XMLParser(huge_tree=True)).getroot() element = cib_tree.find(f'.//*[@id="{resource_id}"]') final_master_id = ( master_id if master_id is not None else f"{resource_id}-master" ) master_element = _xml_to_element( f""" <master id="{final_master_id}"> </master> """ ) element.getparent().append(master_element) master_element.append(element) final_xml = etree_to_str(cib_tree) environ = dict(os.environ) environ["CIB_file"] = cib_file.name runner = CommandRunner( mock.MagicMock(logging.Logger), MockLibraryReportProcessor(), environ ) stdout, stderr, retval = runner.run( [ os.path.join(settings.pacemaker_binaries, "cibadmin"), "--replace", "--scope", "resources", "--xml-pipe", ], stdin_string=final_xml, ) assert retval == 0, ( "Error running wrap_element_by_master:\n" + stderr + "\n" + stdout )
def cmd_runner(self): runner_env = { # make sure to get output of external processes in English and ASCII "LC_ALL": "C", } if self.user_login: runner_env["CIB_user"] = self.user_login if not self.is_cib_live: # Dump CIB data to a temporary file and set it up in the runner. # This way every called pacemaker tool can access the CIB and we # don't need to take care of it every time the runner is called. if not self._cib_data_tmp_file: try: cib_data = self._cib_data self._cib_data_tmp_file = write_tmpfile(cib_data) self.report_processor.process( reports.tmp_file_write( self._cib_data_tmp_file.name, cib_data ) ) except EnvironmentError as e: raise LibraryError(reports.cib_save_tmp_error(str(e))) runner_env["CIB_file"] = self._cib_data_tmp_file.name return CommandRunner(self.logger, self.report_processor, runner_env)
def __can_load_xvm_fence_agent(): try: runner = CommandRunner(logging.getLogger("test"), ReportProcessor()) StonithAgent(runner, "fence_xvm").validate_metadata() return True except: return False
def cmd_runner( self, env: Optional[Mapping[str, str]] = None ) -> CommandRunner: runner_env = { # make sure to get output of external processes in English and ASCII "LC_ALL": "C", } if self.user_login: runner_env["CIB_user"] = self.user_login if not self.is_cib_live: # Dump CIB data to a temporary file and set it up in the runner. # This way every called pacemaker tool can access the CIB and we # don't need to take care of it every time the runner is called. if not self._cib_data_tmp_file: self._cib_data_tmp_file = create_tmp_cib( self.report_processor, self._cib_data ) runner_env["CIB_file"] = self._cib_data_tmp_file.name if env: runner_env.update(env) return CommandRunner(self.logger, self.report_processor, runner_env)
def fixture_to_cib(cib_file, xml): environ = dict(os.environ) environ["CIB_file"] = cib_file runner = CommandRunner(mock.MagicMock(logging.Logger), MockLibraryReportProcessor(), environ) stdout, stderr, retval = runner.run( ["cibadmin", "--create", "--scope", "resources", "--xml-text", xml]) assert retval == 0, ("Error running fixture_to_cib:\n" + stderr + "\n" + stdout)
import logging import os import re # from pcs import utils from pcs.common.tools import is_string from pcs.lib.external import CommandRunner, is_service_enabled from pcs.test.tools.custom_mock import MockLibraryReportProcessor from pcs.test.tools.pcs_unittest import ( mock, skipUnless, ) testdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) runner = CommandRunner(mock.MagicMock(logging.Logger), MockLibraryReportProcessor(), os.environ) def get_test_resource(name): """Return full path to a test resource file specified by name""" return os.path.join(testdir, "resources", name) def cmp3(a, b): # python3 doesn't have the cmp function, this is an official workaround # https://docs.python.org/3.0/whatsnew/3.0.html#ordering-comparisons return (a > b) - (a < b) def compare_version(a, b): if a[0] == b[0]:
def cmd_runner(self): runner_env = dict() if self.user_login: runner_env["CIB_user"] = self.user_login return CommandRunner(self.logger, self.report_processor, runner_env)