From a1a66adc32c0ed4a649b0cb9ccf6e52862c648da Mon Sep 17 00:00:00 2001 From: Jan Pokorný Date: Thu, 22 May 2014 16:22:48 +0200 Subject: utils_cib: unit for CIB-related stuff + test counterpart MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan Pokorný --- tests/utils_cib.py | 24 +++++++++++++++++++++++ utils_cib.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 tests/utils_cib.py create mode 100644 utils_cib.py diff --git a/tests/utils_cib.py b/tests/utils_cib.py new file mode 100644 index 0000000..9052dd1 --- /dev/null +++ b/tests/utils_cib.py @@ -0,0 +1,24 @@ +# -*- coding: UTF-8 -*- +# Copyright 2014 Red Hat, Inc. +# Part of clufter project +# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt) +"""Testing CIB helpers""" +__author__ = "Jan Pokorný " + +import unittest + +import _bootstrap # known W402, required + +from clufter.utils_cib import ResourceSpec + +class TestResourceSpec(unittest.TestCase): + def test_xsl_attrs_ocf(self): + rs = ResourceSpec('ocf:heartbeat:Filesystem') + self.assertTrue(rs.res_class == 'ocf') + self.assertTrue(rs.res_provider == 'heartbeat') + self.assertTrue(rs.res_type == 'Filesystem') + + def test_xsl_attrs_systemd(self): + rs = ResourceSpec('systemd:smb') + self.assertTrue(rs.res_class == 'systemd') + self.assertTrue(rs.res_type == 'smb') diff --git a/utils_cib.py b/utils_cib.py new file mode 100644 index 0000000..de2bba1 --- /dev/null +++ b/utils_cib.py @@ -0,0 +1,56 @@ +# -*- coding: UTF-8 -*- +# Copyright 2014 Red Hat, Inc. +# Part of clufter project +# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt) +"""CIB helpers, mainly used in the filter definitions""" +__author__ = "Jan Pokorný " + +from .error import ClufterError + +class ResourceSpecError(ClufterError): + pass + + +class ResourceSpec(object): + """Representation of simplified resource specification""" + # see also pcs/resource.py:resource_list_available + # get_full_ra_type + # note however that Pacemaker internally prefers this notation: + # class + class == "ocf"?("::" + provider):"" + ":" + type + # (i.e., double colon), as per + # pacemaker/lib/pengine/native.c:{native_print_xml,native_print, + # get_rscs_brief} + def __init__(self, spec): + split = spec.replace('::', ':', 1).split(':', 3) + try: + self._type = split.pop() + maybe_class = split.pop() + self._class = maybe_class if not(split) else split.pop() + self._provider = maybe_class if self._class == 'ocf' else None + except IndexError: + raise ResourceSpecError("Invalid spec: {0}".format(spec)) + + @property + def res_class(self): + return self._class + + @property + def res_provider(self): + if self._class != 'ocf': + raise ResourceSpecError("No provider for class `ocf'") + return self._provider + + @property + def res_type(self): + return self._type + + @property + def xsl_attrs(self): + ret = ('{0}' + .format(self._class)) + if self._provider is not None: + ret += ('{0}' + .format(self._provider)) + ret += ('{0}' + .format(self._type)) + return ret -- cgit