Ejemplo n.º 1
0
from pcs_test.tools.misc import create_patcher
from pcs_test.tools.xml import etree_to_str

from pcs.common import report_codes
from pcs.common.fencing_topology import (
    TARGET_TYPE_NODE,
    TARGET_TYPE_REGEXP,
    TARGET_TYPE_ATTRIBUTE,
)
from pcs.lib import reports
from pcs.lib.errors import LibraryError, ReportItemSeverity as severity
from pcs.lib.pacemaker.state import ClusterState

from pcs.lib.cib import fencing_topology as lib

patch_lib = create_patcher("pcs.lib.cib.fencing_topology")

# pylint: disable=too-few-public-methods, anomalous-backslash-in-string
# pylint: disable=protected-access, no-self-use


class CibMixin:
    @staticmethod
    def get_cib():
        return etree.fromstring("""
            <cib><configuration>
            <fencing-topology>
                <fencing-level
                    id="fl1" index="1" devices="d1,d2" target="nodeA"
                />
                <fencing-level
Ejemplo n.º 2
0
from pcs_test.tools.misc import create_patcher

from pcs.common.reports import ReportItemSeverity as severity
from pcs.common.reports import codes as report_codes
from pcs.lib.cib.tools import IdProvider
from pcs.lib.env import LibraryEnvironment
from pcs.lib.errors import LibraryError

from pcs.lib.commands import node as lib

# pylint: disable=protected-access

mocked_cib = etree.fromstring("<cib />")

patch_env = partial(mock.patch.object, LibraryEnvironment)
patch_command = create_patcher("pcs.lib.commands.node")

create_env = partial(
    LibraryEnvironment,
    mock.MagicMock(logging.Logger),
    MockLibraryReportProcessor(),
)


def fixture_node(order_num):
    node = mock.MagicMock(attrs=mock.MagicMock())
    node.attrs.name = "node-{0}".format(order_num)
    return node


class StandbyMaintenancePassParameters(TestCase):
Ejemplo n.º 3
0
from pcs_test.tools import fixture
from pcs_test.tools.assertions import assert_raise_library_error
from pcs_test.tools.command_env import get_env_tools
from pcs_test.tools.custom_mock import MockLibraryReportProcessor
from pcs_test.tools.misc import (
    create_patcher,
    get_test_resource as rc,
)

from pcs.common import report_codes
from pcs.lib.corosync.config_facade import ConfigFacade as CorosyncConfigFacade
from pcs.lib.env import LibraryEnvironment
from pcs.lib.errors import ReportItemSeverity as severity


patch_env = create_patcher("pcs.lib.env")
patch_env_object = partial(mock.patch.object, LibraryEnvironment)


class LibraryEnvironmentTest(TestCase):
    def setUp(self):
        self.mock_logger = mock.MagicMock(logging.Logger)
        self.mock_reporter = MockLibraryReportProcessor()

    def test_logger(self):
        env = LibraryEnvironment(self.mock_logger, self.mock_reporter)
        self.assertEqual(self.mock_logger, env.logger)

    def test_report_processor(self):
        env = LibraryEnvironment(self.mock_logger, self.mock_reporter)
        self.assertEqual(self.mock_reporter, env.report_processor)
Ejemplo n.º 4
0
    FileAlreadyExists,
    FileMetadata,
    RawFile,
    RawFileError,
)

from pcs_test.tools.misc import create_patcher

FILE_TYPE_CODE = "file type code"
FILE_PATH = "file path"
FILE_OWNER = "owner name"
FILE_GROUP = "group name"
FILE_PERMISSIONS = "file permissions"
FILE_IS_BINARY = False

patch_file = create_patcher("pcs.common.file")


def fixture_metadata(
    owner=FILE_OWNER,
    group=FILE_GROUP,
    permissions=FILE_PERMISSIONS,
    binary=FILE_IS_BINARY,
):
    return FileMetadata(
        FILE_TYPE_CODE, FILE_PATH, owner, group, permissions, binary
    )


class RawFileExists(TestCase):
    @patch_file("os.path.exists")
Ejemplo n.º 5
0
from unittest import mock, TestCase

from pcs_test.tools.assertions import assert_raise_library_error
from pcs_test.tools.command_env import get_env_tools
from pcs_test.tools.misc import create_patcher
from pcs_test.tools.misc import get_test_resource as rc
from pcs_test.tools.xml import get_xml_manipulation_creator_from_file
from pcs_test.tier0.lib.misc import get_mocked_env

from pcs.common import report_codes
from pcs.lib.commands.constraint import ticket as ticket_command
from pcs.lib.errors import ReportItemSeverity as severities

patch_commands = create_patcher("pcs.lib.commands.constraint.ticket")

class CreateTest(TestCase):
    def setUp(self):
        self.create_cib = get_xml_manipulation_creator_from_file(
            rc("cib-empty.xml")
        )

    def test_sucess_create(self):
        env_assist, config = get_env_tools(test_case=self)
        (config
            .runner.cib.load(
                resources="""
                    <resources>
                        <primitive id="resourceA" class="service" type="exim"/>
                    </resources>
                """
            )
Ejemplo n.º 6
0
from unittest import mock, TestCase

from pcs_test.tools.misc import create_patcher
from pcs_test.tools.assertions import (assert_raise_library_error,
                                       assert_report_item_list_equal)
from pcs_test.tools.custom_mock import MockLibraryReportProcessor

from pcs.common import report_codes
from pcs.lib import env_file
from pcs.lib.errors import ReportItemSeverity as severities

# pylint: disable=no-self-use

patch_env_file = create_patcher(env_file)

FILE_PATH = "/path/to.file"
MISSING_PATH = "/no/existing/file.path"
CONF_PATH = "/etc/booth/some-name.conf"


class GhostFileInit(TestCase):
    def test_is_not_binary_default(self):
        ghost_file = env_file.GhostFile("some role", content=None)
        self.assertFalse(ghost_file.export()["is_binary"])

    def test_accepts_is_binary_attribute(self):
        ghost_file = env_file.GhostFile("some role",
                                        content=None,
                                        is_binary=True)
        self.assertTrue(ghost_file.export()["is_binary"])
Ejemplo n.º 7
0
from unittest import mock, TestCase
from lxml import etree

from pcs_test.tools import fixture
from pcs_test.tools.assertions import assert_report_item_list_equal
from pcs_test.tools.custom_mock import MockLibraryReportProcessor
from pcs_test.tools.misc import create_patcher

from pcs.common import report_codes
from pcs.lib.cib.resource import operations
from pcs.common.reports import ReportItemSeverity as severities
from pcs.lib.validate import ValuePair

# pylint: disable=no-self-use

patch_operations = create_patcher("pcs.lib.cib.resource.operations")


@patch_operations("get_remaining_defaults")
@patch_operations("complete_all_intervals")
@patch_operations("validate_different_intervals")
@patch_operations("validate_operation_list")
@patch_operations("normalized_to_operations")
@patch_operations("operations_to_normalized")
class Prepare(TestCase):
    def test_prepare(self, operations_to_normalized, normalized_to_operations,
                     validate_operation_list, validate_different_intervals,
                     complete_all_intervals, get_remaining_defaults):
        validate_operation_list.return_value = ["options_report"]
        validate_different_intervals.return_value = [
            "different_interval_report"
Ejemplo n.º 8
0
    TARGET_TYPE_REGEXP,
)
from pcs.common.tools import Version
from pcs.lib.env import LibraryEnvironment

from pcs.lib.commands import fencing_topology as lib

# pylint: disable=no-self-use

create_lib_env = partial(
    LibraryEnvironment,
    mock.MagicMock(logging.Logger),
    MockLibraryReportProcessor()
)
patch_env = partial(mock.patch.object, LibraryEnvironment)
patch_command = create_patcher("pcs.lib.commands.fencing_topology")


@patch_command("cib_fencing_topology.add_level")
@patch_command("get_resources")
@patch_command("get_fencing_topology")
@patch_env("push_cib")
@patch_command("ClusterState")
@patch_command("get_cluster_status_xml")
@patch_env("get_cib")
@patch_env("cmd_runner", lambda self: "mocked cmd_runner")
class AddLevel(TestCase):
    def prepare_mocks(
        self, mock_get_cib, mock_status_xml, mock_status, mock_get_topology,
        mock_get_resources
    ):
Ejemplo n.º 9
0
from pcs_test.tools.xml import etree_to_str

from pcs.common import report_codes
from pcs.common.fencing_topology import (
    TARGET_TYPE_NODE,
    TARGET_TYPE_REGEXP,
    TARGET_TYPE_ATTRIBUTE,
)
from pcs.lib import reports
from pcs.lib.errors import LibraryError, ReportItemSeverity as severity
from pcs.lib.pacemaker.state import ClusterState

from pcs.lib.cib import fencing_topology as lib


patch_lib = create_patcher("pcs.lib.cib.fencing_topology")

# pylint: disable=too-few-public-methods, anomalous-backslash-in-string
# pylint: disable=protected-access, no-self-use


class CibMixin:
    @staticmethod
    def get_cib():
        return etree.fromstring("""
            <cib><configuration>
            <fencing-topology>
                <fencing-level
                    id="fl1" index="1" devices="d1,d2" target="nodeA"
                />
                <fencing-level
Ejemplo n.º 10
0
import os.path
from unittest import mock, TestCase

from pcs_test.tools.assertions import (
    assert_raise_library_error,
    assert_report_item_list_equal,
)
from pcs_test.tools.custom_mock import MockLibraryReportProcessor
from pcs_test.tools.misc import create_patcher

from pcs.common import report_codes, env_file_role_codes as file_roles
from pcs.lib.booth import config_files
from pcs.lib.errors import ReportItemSeverity as severities
from pcs.settings import booth_config_dir as BOOTH_CONFIG_DIR

patch_config_files = create_patcher("pcs.lib.booth.config_files")


@mock.patch("os.path.isdir")
@mock.patch("os.listdir")
@mock.patch("os.path.isfile")
class GetAllConfigsFileNamesTest(TestCase):
    def test_booth_config_dir_is_no_dir(self, mock_is_file, mock_listdir,
                                        mock_isdir):
        mock_isdir.return_value = False
        self.assertEqual([], config_files.get_all_configs_file_names())
        mock_isdir.assert_called_once_with(BOOTH_CONFIG_DIR)
        self.assertEqual(0, mock_is_file.call_count)
        self.assertEqual(0, mock_listdir.call_count)

    def test_success(self, mock_is_file, mock_listdir, mock_isdir):
Ejemplo n.º 11
0
from unittest import mock, TestCase

from pcs_test.tools.misc import create_patcher, create_setup_patch_mixin

from pcs.cli.common import env_file
from pcs.common import report_codes
from pcs.lib.errors import ReportItem

patch_env_file = create_patcher(env_file)
SetupPatchMixin = create_setup_patch_mixin(patch_env_file)

FILE_PATH = "/path/to/local/file"

class Write(TestCase, SetupPatchMixin):
    def setUp(self):
        self.mock_open = mock.mock_open()
        self.mock_error = self.setup_patch("console_report.error")

    def assert_params_causes_calls(self, env_file_dict, calls, path=FILE_PATH):
        with patch_env_file("open", self.mock_open, create=True):
            env_file.write(env_file_dict, path)
        self.assertEqual(self.mock_open.mock_calls, calls)

    def test_sucessfully_write(self):
        self.assert_params_causes_calls(
            {"content": "filecontent"},
            [
                mock.call(FILE_PATH, "w"),
                mock.call().write("filecontent"),
                mock.call().close(),
            ]
Ejemplo n.º 12
0
import os.path
from unittest import mock, TestCase

from pcs_test.tools.assertions import (
    assert_raise_library_error,
    assert_report_item_list_equal,
)
from pcs_test.tools.custom_mock import MockLibraryReportProcessor
from pcs_test.tools.misc import create_patcher

from pcs.common import report_codes, env_file_role_codes as file_roles
from pcs.lib.booth import config_files
from pcs.lib.errors import ReportItemSeverity as severities
from pcs.settings import booth_config_dir as BOOTH_CONFIG_DIR

patch_config_files = create_patcher("pcs.lib.booth.config_files")

@mock.patch("os.path.isdir")
@mock.patch("os.listdir")
@mock.patch("os.path.isfile")
class GetAllConfigsFileNamesTest(TestCase):
    def test_booth_config_dir_is_no_dir(
        self, mock_is_file, mock_listdir, mock_isdir
    ):
        mock_isdir.return_value = False
        self.assertEqual([], config_files.get_all_configs_file_names())
        mock_isdir.assert_called_once_with(BOOTH_CONFIG_DIR)
        self.assertEqual(0, mock_is_file.call_count)
        self.assertEqual(0, mock_listdir.call_count)

    def test_success(self, mock_is_file, mock_listdir, mock_isdir):
Ejemplo n.º 13
0
)

# pylint: disable=no-self-use, unused-argument, protected-access

def _booth_env_fixture(name):
    booth_env = mock.MagicMock()
    booth_env.name = name
    return booth_env


def _env_fixture(booth_name):
    env = mock.MagicMock(spec_set=LibraryEnvironment)
    env.booth = _booth_env_fixture(booth_name)
    return env

patch_commands = create_patcher("pcs.lib.commands.booth")

@mock.patch("pcs.lib.tools.generate_binary_key", return_value=b"key value")
@mock.patch("pcs.lib.commands.booth.build", return_value="config content")
@mock.patch("pcs.lib.booth.config_structure.validate_peers")
class ConfigSetupTest(TestCase):
    def test_successfuly_build_and_write_to_std_path(
        self, mock_validate_peers, mock_build, mock_generate_binary_key
    ):
        env = _env_fixture("booth_name")
        commands.config_setup(
            env,
            booth_configuration=[
                {"key": "site", "value": "1.1.1.1", "details": []},
                {"key": "arbitrator", "value": "2.2.2.2", "details": []},
            ],
Ejemplo n.º 14
0
                    'REMOTE_ADDR':
                    None,  # It requires complicated request args
                    'REMOTE_HOST': 'pcsd-host:2224',
                    'REQUEST_METHOD': 'POST',
                    'REQUEST_PATH': '/pcsd/uri',
                    'REQUEST_URI': 'http://pcsd-host:2224/pcsd/uri',
                    'SCRIPT_NAME': '',
                    'SERVER_NAME': 'pcsd-host',
                    'SERVER_PORT': 2224,
                    'SERVER_PROTOCOL': 'HTTP/1.0',
                    'rack.input': 'post-key=post-value'
                }
            })


patch_ruby_pcsd = create_patcher(ruby_pcsd)


class RunRuby(AsyncTestCase):
    def setUp(self):
        self.stdout = ""
        self.stderr = ""
        self.exit_status = 0
        self.request = self.create_request()
        self.wrapper = create_wrapper()
        patcher = mock.patch.object(self.wrapper, "send_to_ruby",
                                    self.send_to_ruby)
        self.addCleanup(patcher.stop)
        patcher.start()
        super().setUp()
Ejemplo n.º 15
0
                    'QUERY_STRING': '',
                    'REMOTE_ADDR': None, # It requires complicated request args
                    'REMOTE_HOST': 'pcsd-host:2224',
                    'REQUEST_METHOD': 'POST',
                    'REQUEST_PATH': '/pcsd/uri',
                    'REQUEST_URI': 'http://pcsd-host:2224/pcsd/uri',
                    'SCRIPT_NAME': '',
                    'SERVER_NAME': 'pcsd-host',
                    'SERVER_PORT': 2224,
                    'SERVER_PROTOCOL': 'HTTP/1.0',
                    'rack.input': 'post-key=post-value'
                }
            }
        )

patch_ruby_pcsd = create_patcher(ruby_pcsd)

class RunRuby(AsyncTestCase):
    def setUp(self):
        self.stdout = ""
        self.stderr = ""
        self.exit_status = 0
        self.request = self.create_request()
        self.wrapper = create_wrapper()
        patcher = mock.patch.object(
            self.wrapper,
            "send_to_ruby",
            self.send_to_ruby
        )
        self.addCleanup(patcher.stop)
        patcher.start()
Ejemplo n.º 16
0
from unittest import mock, TestCase

from pcs_test.tools.command_env import get_env_tools
from pcs_test.tools.misc import create_patcher

from pcs.common.reports import ReportItemSeverity as severities
from pcs.common.reports import codes as report_codes
from pcs.lib.commands.constraint import ticket as ticket_command

patch_commands = create_patcher("pcs.lib.commands.constraint.ticket")


class CreateTest(TestCase):
    def test_sucess_create(self):
        env_assist, config = get_env_tools(test_case=self)
        (config.runner.cib.load(resources="""
                    <resources>
                        <primitive id="resourceA" class="service" type="exim"/>
                    </resources>
                """).env.push_cib(optional_in_conf="""
                    <constraints>
                        <rsc_ticket
                            id="ticket-ticketA-resourceA-Master"
                            rsc="resourceA"
                            rsc-role="Master"
                            ticket="ticketA"
                            loss-policy="fence"
                        />
                    </constraints>
                """))
Ejemplo n.º 17
0
    TARGET_TYPE_ATTRIBUTE,
)
from pcs.common.tools import Version
from pcs.lib.env import LibraryEnvironment

from pcs.lib.commands import fencing_topology as lib

# pylint: disable=no-self-use

create_lib_env = partial(
    LibraryEnvironment,
    mock.MagicMock(logging.Logger),
    MockLibraryReportProcessor()
)
patch_env = partial(mock.patch.object, LibraryEnvironment)
patch_command = create_patcher("pcs.lib.commands.fencing_topology")


@patch_command("cib_fencing_topology.add_level")
@patch_command("get_resources")
@patch_command("get_fencing_topology")
@patch_env("push_cib")
@patch_command("ClusterState")
@patch_command("get_cluster_status_xml")
@patch_env("get_cib")
@patch_env("cmd_runner", lambda self: "mocked cmd_runner")
class AddLevel(TestCase):
    def prepare_mocks(
        self, mock_get_cib, mock_status_xml, mock_status, mock_get_topology,
        mock_get_resources
    ):
Ejemplo n.º 18
0
from unittest import mock, TestCase

from pcs_test.tools.assertions import assert_raise_library_error
from pcs_test.tools.custom_mock import MockLibraryReportProcessor
from pcs_test.tools.misc import (
    create_patcher,
    get_test_resource as rc,
)

from pcs.common import file_type_codes
from pcs.common.reports import ReportItemSeverity as severity
from pcs.common.reports import codes as report_codes
from pcs.lib.env import LibraryEnvironment


patch_env = create_patcher("pcs.lib.env")
patch_env_object = partial(mock.patch.object, LibraryEnvironment)


class LibraryEnvironmentTest(TestCase):
    def setUp(self):
        self.mock_logger = mock.MagicMock(logging.Logger)
        self.mock_reporter = MockLibraryReportProcessor()

    def test_logger(self):
        env = LibraryEnvironment(self.mock_logger, self.mock_reporter)
        self.assertEqual(self.mock_logger, env.logger)

    def test_report_processor(self):
        env = LibraryEnvironment(self.mock_logger, self.mock_reporter)
        self.assertEqual(self.mock_reporter, env.report_processor)
Ejemplo n.º 19
0
from functools import partial
from unittest import mock, TestCase
from lxml import etree

from pcs_test.tools.assertions import assert_report_item_list_equal
from pcs_test.tools.custom_mock import MockLibraryReportProcessor
from pcs_test.tools.misc import create_patcher

from pcs.common import report_codes
from pcs.lib.cib.resource import operations
from pcs.lib.errors import ReportItemSeverity as severities
from pcs.lib.validate import ValuePair

# pylint: disable=no-self-use

patch_operations = create_patcher("pcs.lib.cib.resource.operations")

@patch_operations("get_remaining_defaults")
@patch_operations("complete_all_intervals")
@patch_operations("validate_different_intervals")
@patch_operations("validate_operation_list")
@patch_operations("normalized_to_operations")
@patch_operations("operations_to_normalized")
class Prepare(TestCase):
    def test_prepare(
        self, operations_to_normalized, normalized_to_operations,
        validate_operation_list, validate_different_intervals,
        complete_all_intervals, get_remaining_defaults
    ):
        validate_operation_list.return_value = ["options_report"]
        validate_different_intervals.return_value = [