summaryrefslogtreecommitdiffstats
path: root/src/software/test/util.py
blob: 6532a2bb3d453604028b58e705ed4b534a4a9002 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/python
# -*- Coding:utf-8 -*-
#
# Copyright (C) 2012-2013 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: Radek Novacek <rnovacek@redhat.com>
# Authors: Michal Minar <miminar@redhat.com>
# Authors: Jan Grec <jgrec@redhat.com>

"""
Common test utilities.
"""

import platform
import pywbem
import re
from subprocess import call, check_output

RE_NEVRA = re.compile(
    r'^(?P<name>[^\s]+)-(?P<evra>(?P<epoch>\d+):(?P<ver>[^-\s]+)'
    r'-(?P<rel>[^\s]+)\.(?P<arch>[^.\s]+))$')
RE_NEVRA_OPT_EPOCH = re.compile(
    r'^(?P<name>[^\s]+)-(?P<evra>((?P<epoch>\d+):)?(?P<ver>[^-\s]+)'
    r'-(?P<rel>[^\s]+)\.(?P<arch>[^.\s]+))$')
RE_ENVRA = re.compile(
    r'^(?P<epoch>\d+|\(none\)):(?P<name>[^\s]+)-(?P<ver>[^-\s]+)'
    r'-(?P<rel>[^\s]+)\.(?P<arch>[^.\s]+)$')
RE_REPO = re.compile(
        r'(?:^\*?)(?P<name>[^\s/]+\b)(?!\s+id)', re.MULTILINE | re.IGNORECASE)

DEV_NULL = open('/dev/null', 'w')

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 = str(epoch)
    elif with_epoch.lower() == "not_zero":
        if epoch and str(epoch).lower() not in {"0", "(none)"}:
            estr = str(epoch)
    if len(estr):
        estr += ":"
    return "%s-%s%s-%s.%s" % (name, estr, ver, rel, arch)

def make_evra(epoch, ver, rel, arch):
    """ @return evra string """
    if not epoch or str(epoch).lower() == "(none)":
        epoch = "0"
    return "%s:%s-%s.%s" % (epoch, ver, rel, arch)

def run_yum(*params, **kwargs):
    """
    Runs yum with params and returns its output
    It's here especially to allow pass a repolist argument, that
    specifies list of repositories, to run the command on.
    """
    cmd = ['yum'] + list(params)
    repolist = kwargs.get('repolist', None)
    if repolist is None:
        repolist = []
    if repolist:
        cmd += ['--disablerepo=*']
        cmd += ['--enablerepo='+r for r in repolist]
    return check_output(cmd)

def get_repo_list():
    """
    @return list of software repository names
    """
    repos_str = check_output(['yum', 'repolist', '-q'])
    return RE_REPO.findall(repos_str)

def get_system_architecture():
    """
    @return the system architecture name as seen by rpm
    """
    return check_output(['rpm', '-q', '--qf', '%{ARCH}\n', 'rpm'])

def verify_pkg(name):
    """
    @return True, if package is installed and passes rpm verification check
    """
    return call(["rpm", "--quiet", "-Va", name]) == 0


def is_config_file(pkg, file_path):
    """
    @return True, if file_path is a configuration file of package pkg.
    """
    cmd = ['rpm', '-qc', pkg.name]
    out = check_output(cmd)
    return file_path in set(out.splitlines())   #pylint: disable=E1103

def is_doc_file(pkg, file_path):
    """
    @return True, if file_path is a documentation file of package pkg.
    """
    cmd = ['rpm', '-qd', pkg.name]
    out = check_output(cmd)
    return file_path in set(out.splitlines())   #pylint: disable=E1103

def make_identity_path(pkg, newer=True):
    """
    Make instance name for LMI_SoftwareIdentity from Package.
    """
    return pywbem.CIMInstanceName(
            classname="LMI_SoftwareIdentity",
            namespace="root/cimv2",
            keybindings=pywbem.NocaseDict({
                "InstanceID" : "LMI:LMI_SoftwareIdentity:%s" % pkg.get_nevra(
                    newer=newer)
            }))

def get_target_operating_system():
    """
    :returns: integer corresponding for TargetOperatingProperty of CIM_Check
        for this system
    """
    target_operating_system = 36 # LINUX
    if hasattr(platform, 'linux_distribution') and \
            platform.linux_distribution(
                    full_distribution_name=False)[0].lower() == 'redhat':
        target_operating_system = 79 # RHEL
        if platform.uname()[4].lower() == 'x86_64':
            target_operating_system = 80 # RHEL 64bit
    return pywbem.Uint16(target_operating_system)

def get_installed_packages():
    """
    :returns: list of packages in format: ``NAME-EPOCH:VERSION-RELEASE.ARCH``
    """
    output = check_output(
            [ "/usr/bin/rpm", "-qa", "--qf"
            , "%{NAME}-%{EPOCH}:%{VERSION}-%{RELEASE}.%{ARCH}\n"],
            stderr=DEV_NULL).split()
    package_list = []
    for package in output:
        # Skip all gpg public keys returned by rpm -qa
        if "gpg-pubkey" not in package:
            package_list.append(package.replace("(none)", "0"))
    return package_list

def make_pkg_op(ns, pkg):
    """
    :returns: Object path of ``LMI_SoftwareIdentity``
    :rtype: :py:class:`lmi.shell.LMIInstanceName`
    """
    if not isinstance(pkg, basestring):
        nevra = pkg.nevra
    else:
        nevra = pkg
    return ns.LMI_SoftwareIdentity.new_instance_name({
        "InstanceID" : 'LMI:LMI_SoftwareIdentity:' + nevra
    })