summaryrefslogtreecommitdiffstats
path: root/src/software/test/common.py
blob: d88b2bbc4e809a81af7c7600417fe71d2310ba9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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<name>.+)-(?P<evra>(?P<epoch>\d+):(?P<ver>[^-]+)'
                r'-(?P<rel>.+)\.(?P<arch>[^.]+))$')

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))