Example #1
0
from pcs.test.tools.assertions import (
    ExtendedAssertionsMixin,
    assert_raise_library_error,
    assert_xml_equal,
)
from pcs.test.tools.misc import create_patcher
from pcs.test.tools.pcs_unittest import TestCase, mock
from pcs.test.tools.xml import XmlManipulation

from pcs.common import report_codes
from pcs.lib import resource_agent as lib_ra
from pcs.lib.errors import ReportItemSeverity as severity, LibraryError
from pcs.lib.external import CommandRunner

patch_agent = create_patcher("pcs.lib.resource_agent")

class ListResourceAgentsStandardsTest(TestCase):
    def test_success_and_filter_stonith_out(self):
        mock_runner = mock.MagicMock(spec_set=CommandRunner)
        agents = [
            "ocf",
            "lsb",
            "service",
            "systemd",
            "nagios",
            "stonith",
        ]
        # retval is number of providers found
        mock_runner.run.return_value = (
            "\n".join(agents) + "\n",
Example #2
0
from pcs.test.tools.custom_mock import MockLibraryReportProcessor
from pcs.test.tools.misc import get_test_resource as rc, create_patcher
from pcs.test.tools.pcs_unittest import mock

from pcs.lib.env import LibraryEnvironment
from pcs.common import report_codes
from pcs.lib import reports
from pcs.lib.cluster_conf_facade import ClusterConfFacade
from pcs.lib.corosync.config_facade import ConfigFacade as CorosyncConfigFacade
from pcs.lib.errors import (
    LibraryError,
    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)
Example #3
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()
Example #4
0
from pcs.test.tools.assertions import assert_raise_library_error
from pcs.test.tools.custom_mock import MockLibraryReportProcessor
from pcs.test.tools.pcs_unittest import mock, TestCase
from pcs.test.tools.misc import create_patcher

from pcs.common import report_codes
from pcs.lib.env import LibraryEnvironment
from pcs.lib.errors import ReportItemSeverity as severity, LibraryError

from pcs.lib.commands import node as lib


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):
    def setUp(self):
        self.lib_env = "lib_env"
Example #5
0
)

import grp
import os
import pwd
from pcs.test.tools.pcs_unittest import TestCase

from pcs import settings
from pcs.common import report_codes
from pcs.lib.booth import env
from pcs.lib.errors import ReportItemSeverity as severities
from pcs.test.tools.assertions import assert_raise_library_error
from pcs.test.tools.misc import get_test_resource as rc, create_patcher
from pcs.test.tools.pcs_unittest import mock

patch_env = create_patcher("pcs.lib.booth.env")


class GetConfigFileNameTest(TestCase):
    @patch_env("os.path.exists")
    def test_refuse_when_name_starts_with_slash(self, mock_path_exists):
        mock_path_exists.return_value = True
        assert_raise_library_error(
            lambda: env.get_config_file_name("/booth"),
            (severities.ERROR, report_codes.BOOTH_INVALID_NAME, {
                "name": "/booth",
                "reason": "contains illegal character '/'",
            }),
        )

Example #6
0
from pcs.common.tools import Version
from pcs.lib.env import LibraryEnvironment
from pcs.test.tools.misc import create_patcher
from pcs.test.tools.custom_mock import MockLibraryReportProcessor

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
    ):
Example #7
0
    unicode_literals,
)

import os.path
from pcs.test.tools.pcs_unittest import TestCase

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
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.test.tools.pcs_unittest import mock

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):
Example #8
0
from pcs import settings
from pcs.common import report_codes
from pcs.lib.env import LibraryEnvironment
from pcs.lib.node import NodeAddresses
from pcs.lib.errors import LibraryError, ReportItemSeverity as Severities
from pcs.lib.commands import booth as commands
from pcs.lib.external import (
    NodeCommunicator,
    CommandRunner,
    EnableServiceError,
    DisableServiceError,
    StartServiceError,
    StopServiceError
)

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

@mock.patch("pcs.lib.booth.config_files.generate_key", return_value="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_key
    ):
        env = mock.MagicMock()
        commands.config_setup(
            env,
            booth_configuration=[
                {"key": "site", "value": "1.1.1.1", "details": []},
                {"key": "arbitrator", "value": "2.2.2.2", "details": []},
            ],
Example #9
0
from unittest import mock, TestCase

from pcs.cli.common import env_file
from pcs.test.tools.misc import create_patcher, create_setup_patch_mixin

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

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(),
            ]
Example #10
0
)

from pcs.test.tools.pcs_unittest import TestCase
from pcs.test.tools.pcs_unittest import mock
from pcs.test.tools.misc import create_patcher

from pcs.common import report_codes
from pcs.lib.commands.constraint import ticket as ticket_command
from pcs.lib.errors import ReportItemSeverity as severities
from pcs.lib.test.misc import get_mocked_env
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 get_test_resource as rc
from pcs.test.tools.xml import get_xml_manipulation_creator_from_file

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>
                """).env.push_cib(optional_in_conf="""
                    <constraints>
Example #11
0
from pcs.test.tools.misc import create_patcher

from pcs import settings
from pcs.common import report_codes, env_file_role_codes as file_roles
from pcs.lib.env import LibraryEnvironment
from pcs.lib.errors import LibraryError, ReportItemSeverity as Severities
from pcs.lib.commands import booth as commands
from pcs.lib.external import (
    CommandRunner,
    EnableServiceError,
    DisableServiceError,
    StartServiceError,
    StopServiceError
)

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

@mock.patch("pcs.lib.tools.generate_key", return_value="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_key
    ):
        env = mock.MagicMock()
        commands.config_setup(
            env,
            booth_configuration=[
                {"key": "site", "value": "1.1.1.1", "details": []},
                {"key": "arbitrator", "value": "2.2.2.2", "details": []},
            ],
Example #12
0
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.pcs_unittest import mock
from pcs.test.tools.misc import create_patcher

from pcs.common import report_codes
from pcs.lib.external import NodeCommunicator, NodeAuthenticationException
from pcs.lib.node import NodeAddresses, NodeAddressesList
from pcs.lib.errors import ReportItemSeverity as severity

import pcs.lib.nodes_task as lib

patch_nodes_task = create_patcher(lib)


class DistributeCorosyncConfTest(TestCase):
    def setUp(self):
        self.mock_reporter = MockLibraryReportProcessor()
        self.mock_communicator = "mock node communicator"

    @patch_nodes_task("corosync_live")
    def test_success(self, mock_corosync_live):
        conf_text = "test conf text"
        nodes = ["node1", "node2"]
        node_addrs_list = NodeAddressesList(
            [NodeAddresses(addr) for addr in nodes])
        mock_corosync_live.set_remote_corosync_conf = mock.MagicMock()
Example #13
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

from pcs.common import report_codes
from pcs.lib.env import LibraryEnvironment
from pcs.lib.errors import ReportItemSeverity as severity, LibraryError

from pcs.lib.commands import node as lib

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):
    def setUp(self):
        self.lib_env = "lib_env"
        self.nodes = "nodes"
Example #14
0
from __future__ import absolute_import, division, print_function, unicode_literals

from pcs.test.tools.pcs_unittest import TestCase
from pcs.test.tools.pcs_unittest import mock
from pcs.test.tools.misc import create_patcher

from pcs.common import report_codes
from pcs.lib.commands.constraint import ticket as ticket_command
from pcs.lib.errors import ReportItemSeverity as severities
from pcs.lib.test.misc import get_mocked_env
from pcs.test.tools.assertions import assert_xml_equal, assert_raise_library_error
from pcs.test.tools.misc import get_test_resource as rc
from pcs.test.tools.xml import get_xml_manipulation_creator_from_file

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):
        resource_xml = '<primitive id="resourceA" class="service" type="exim"/>'
        cib = self.create_cib().append_to_first_tag_name("resources", resource_xml)

        env = get_mocked_env(cib_data=str(cib))
        ticket_command.create(env, "ticketA", "resourceA", {"loss-policy": "fence", "rsc-role": "master"})

        assert_xml_equal(
            env._get_cib_xml(),
            str(
Example #15
0
    unicode_literals,
)

from functools import partial
from lxml import etree

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
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.test.tools.pcs_unittest import TestCase, mock

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"
    assert_xml_equal,
)
from pcs.test.tools.custom_mock import MockLibraryReportProcessor
from pcs.test.tools.misc import create_patcher
from pcs.test.tools.pcs_unittest import TestCase#, mock
from pcs.test.tools.xml import etree_to_str

from pcs.common.fencing_topology import (
    TARGET_TYPE_NODE,
    TARGET_TYPE_REGEXP,
    TARGET_TYPE_ATTRIBUTE,
)
from pcs.lib.cib import fencing_topology as lib


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


class CibMixin(object):
    def get_cib(self):
        return etree.fromstring("""
            <fencing-topology>
                <fencing-level
                    id="fl1" index="1" devices="d1,d2" target="nodeA"
                />
                <fencing-level
                    id="fl2" index="2" devices="d3" target="nodeA"
                />
                <fencing-level
                    id="fl3" index="1" devices="d2,d1" target="nodeB"
                />
Example #17
0
    assert_raise_library_error,
    assert_report_item_list_equal,
    assert_xml_equal,
)
from pcs.test.tools.custom_mock import MockLibraryReportProcessor
from pcs.test.tools.misc import create_patcher
from pcs.test.tools.xml import etree_to_str

from pcs.common.fencing_topology import (
    TARGET_TYPE_NODE,
    TARGET_TYPE_REGEXP,
    TARGET_TYPE_ATTRIBUTE,
)
from pcs.lib.cib import fencing_topology as lib

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


class CibMixin(object):
    def get_cib(self):
        return etree.fromstring("""
            <fencing-topology>
                <fencing-level
                    id="fl1" index="1" devices="d1,d2" target="nodeA"
                />
                <fencing-level
                    id="fl2" index="2" devices="d3" target="nodeA"
                />
                <fencing-level
                    id="fl3" index="1" devices="d2,d1" target="nodeB"
                />
Example #18
0
from functools import partial
import logging

from pcs.common.fencing_topology import TARGET_TYPE_REGEXP, TARGET_TYPE_ATTRIBUTE
from pcs.lib.env import LibraryEnvironment
from pcs.test.tools.misc import create_patcher
from pcs.test.tools.pcs_unittest import mock, TestCase
from pcs.test.tools.custom_mock import MockLibraryReportProcessor

from pcs.lib.commands import fencing_topology as lib


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):
        mock_get_cib.return_value = "mocked cib"
        mock_status_xml.return_value = "mock get_cluster_status_xml"
        mock_status.return_value = mock.MagicMock(node_section=mock.MagicMock(nodes="nodes"))
    unicode_literals,
)

from functools import partial

from pcs.common import report_codes
from pcs.lib.cib.resource import operations
from pcs.lib.errors import ReportItemSeverity as severities
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.test.tools.pcs_unittest import TestCase, mock
from pcs.lib.validate import ValuePair


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

@patch_operations("OPERATION_OPTIONS_VALIDATORS", [])
@patch_operations("get_remaining_defaults")
@patch_operations("complete_all_intervals")
@patch_operations("validate_different_intervals")
@patch_operations("validate.value_in")
@patch_operations("validate_operation")
class Prepare(TestCase):
    def test_prepare(
        self, validate_operation, validate_value_in,
        validate_different_intervals, complete_all_intervals,
        get_remaining_defaults
    ):
        validate_operation.side_effect = lambda operation, validator_list: [
            operation["name"].normalized #values comes here in ValuePairs