# # Copyright (C) 2012 Red Hat, Inc. All rights reserved. # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # # Authors: Michal Minar # from collections import namedtuple import os import pywbem import re import subprocess import unittest SCHEMA="http" HOSTNAME="localhost" PORT=5988 USER='' PASSWORD='' Package = namedtuple("Package", "name, epoch, ver, rel, arch, " "updatable, up_epoch, up_ver, up_rel") # This is a database of packages used in tests. If you modify this, please # ensure, that all corresponding rpm packages are present in test directory. packages = [ Package(*args) for args in ( ( "python-xlib-doc", "0", "0.15", "0.6.rc1.fc17", "noarch", False , None, None, None) , ( "slv2", "0", "0.6.6", "8.fc17", "x86_64", True , "0", "0.6.6", "9.fc17") ) ] pkg_files = ( ( "bash-completion-1:2.0-1.fc17.noarch", ( "/usr/share/bash-completion/completions/vgs" # symlink , "/usr/share/doc/bash-completion-2.0/README" # file , "/usr/share/doc/bash-completion-2.0" # directory ) , ) , ) re_nevra = re.compile(r'^(?P.+)-(?P(?P\d+):(?P[^-]+)' r'-(?P.+)\.(?P[^.]+))$') def make_nevra(name, epoch, ver, rel, arch, with_epoch='NOT_ZERO'): """ @param with_epoch may be one of: "NOT_ZERO" - include epoch only if it's not zero "ALWAYS" - include epoch always "NEVER" - do not include epoch at all """ estr = '' if with_epoch.lower() == "always": estr = epoch elif with_epoch.lower() == "not_zero": if epoch != "0": estr = epoch if len(estr): estr += ":" return "%s-%s%s-%s.%s" % (name, estr, ver, rel, arch) def remove_pkg(name, *args): subprocess.call(["rpm", "--quiet"] + list(args) + ["-e", name]) def install_pkg(*args, **kwargs): if len(args) > 1 or len(kwargs) > 0: if kwargs.has_key('nevra'): m = re_nevra.match(kwargs['nevra']) args = [ m.group(k) for k in ( 'name', 'epoch', 'ver', 'rel', 'arch') ] nevra = make_nevra(*args, with_epoch="NEVER") rpm_name = nevra + ".rpm" if os.path.exists(rpm_name): subprocess.call(["rpm", "--quiet", "-i", rpm_name]) return subprocess.call(["yum", "-q", "-y", "install", args[0]]) def is_installed(*args, **kwargs): if len(args) == 1: return subprocess.call(["rpm", "--quiet", "-q", args[0]]) == 0 else: if kwargs.has_key("nevra"): nevra = kwargs["nevra"] name = re_nevra.match(nevra).group('name') else: nevra = make_nevra(*args) name = args[0] try: out = subprocess.check_output( ["rpm", "-q", "--qf", "%{NEVRA}", name]) return out == nevra except subprocess.CalledProcessError: return False def verify_pkg(name): return subprocess.call(["rpm", "--quiet", "-Va", name]) == 0 class SoftwareBaseTestCase(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.url = "%s://%s:%d" % (SCHEMA, HOSTNAME, PORT) self.conn = pywbem.WBEMConnection(self.url, (USER, PASSWORD)) self.op = pywbem.CIMInstanceName( namespace="root/cimv2", classname=self.CLASS_NAME) def tearDown(self): del self.conn def assertIsSubclass(self, cls, base_cls): if not isinstance(cls, basestring): raise TypeError("cls must be a string") if not isinstance(base_cls, basestring): raise TypeError("base_cls must be a string") return self.assertTrue(pywbem.is_subclass(self.conn, "root/cimv2", base_cls, cls))