File: test-built-packages.py

package info (click to toggle)
pmdk 1.13.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 28,944 kB
  • sloc: ansic: 126,815; sh: 21,543; cpp: 9,413; python: 5,893; makefile: 3,119; perl: 2,294; pascal: 1,442
file content (276 lines) | stat: -rw-r--r-- 10,422 bytes parent folder | download
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!usr/bin/env python3
#
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2018-2023, Intel Corporation

"""
This module includes tests that check if all packages in PMDK library are
built correctly.
Tests check:
-if all required packages are built,
-if built packages are consistent with names of libraries read from
 .so files and other elements (tools and "PMDK").

Required arguments:
-r <PMDK_path>    the PMDK library root path.

Optional arguments:
--skip-daxio      to be set if daxio was not built (e.g., if NDCTL_ENABLE was set to 'n')
"""

from os import listdir, path, linesep
from collections import namedtuple
import distro
import unittest
import xmlrunner
import sys
import re

PACKAGES_INFO = namedtuple('packages_info',
                           'basic devel debug debuginfo debug_debuginfo')
PMDK_VERSION = ''
SYSTEM_ARCHITECTURE = ''


def get_package_version_and_system_architecture(pmdk_path):
    """
    Returns packages version and system architecture from names of directories
    from packages directory.
    """
    os_distro=distro.id()
    if os_distro != 'ubuntu':
        pkg_directory = path.join(pmdk_path, 'rpm')
    else:
        pkg_directory = path.join(pmdk_path, 'dpkg')

    version = ''
    architecture = ''
    for elem in listdir(pkg_directory):
        if os_distro != 'ubuntu':
            if '.src.rpm' in elem:
                # looks for the version number of rpm package in rpm package name
                version = re.search(r'[\s]*pmdk-([\S]+).src.rpm', elem).group(1)
            else:
                architecture = elem

        else:
            if '.changes' in elem:
                # looks for the version number of packages in package name
                version = re.search(r'pmdk_*(.+)_(.+).changes', elem).group(1)
                architecture = re.search(r'pmdk_*(.+)_(.+).changes', elem).group(2)
    return version, architecture


def get_built_packages(pmdk_pkg_path):
    """
    Returns built packages.
    """
    packages = listdir(pmdk_pkg_path)
    return packages


def get_libraries_names_from_so_files(pmdk_path, is_pmdk_debuginfo):
    """
    Returns names of libraries from .so files, and information which packages
    should be built for individual libraries.
    """
    libraries_from_so_files = dict()
    path_to_so_files = path.join(pmdk_path, 'src', 'nondebug')

    for elem in listdir(path_to_so_files):
        if elem.endswith('.so') and elem.startswith('lib'):
            library_name = elem.split('.')[0]
            if is_pmdk_debuginfo:
                libraries_from_so_files[library_name] =\
                    PACKAGES_INFO(basic=True, devel=True, debug=True,
                                  debuginfo=False, debug_debuginfo=False)
            else:
                libraries_from_so_files[library_name] =\
                    PACKAGES_INFO(basic=True, devel=True, debug=True,
                                  debuginfo=True, debug_debuginfo=True)
    return libraries_from_so_files


def get_names_of_packages(packages_info):
    """
    Returns names of packages, that should be built.
    """
    packages = []
    pkg_ext = ''
    separator = ''
    os_distro=distro.id()
    if os_distro != 'ubuntu':
        types = ['-', '-debug-', '-devel-', '-debuginfo-', '-debug-debuginfo-']
        pkg_ext = '.rpm'
        separator ='.'

    elif os_distro == 'ubuntu':
        types = ['_', '-dev_']
        pkg_ext = '.deb'
        separator = '_'

    for elem in packages_info:
        sets_of_information = zip(packages_info[elem], types)
        for kit in sets_of_information:
            if kit[0] and os_distro == 'opensuse':
                if elem.startswith('lib') and (kit[1] == '-' or kit[1] == '-debuginfo-'):
                    package_name = elem + str(1) + kit[1] + PMDK_VERSION + separator + \
                        SYSTEM_ARCHITECTURE + pkg_ext
                    packages.append(package_name)
                elif kit[0]:
                    package_name = elem + kit[1] + PMDK_VERSION + separator + \
                        SYSTEM_ARCHITECTURE + pkg_ext
                    packages.append(package_name)

            elif kit[0] and not os_distro == 'opensuse':
                package_name = elem + kit[1] + PMDK_VERSION + separator +\
                    SYSTEM_ARCHITECTURE + pkg_ext
                packages.append(package_name)
    return packages


def check_existence_of_pmdk_debuginfo_package(pmdk_debuginfo_package_name, built_packages):
    """
    Checks if 'pmdk-debuginfo' package is built
    """
    is_pmdk_debuginfo_package = False
    if pmdk_debuginfo_package_name in built_packages:
        is_pmdk_debuginfo_package = True
    return is_pmdk_debuginfo_package


def find_missing_packages(packages_path, pmdk_path, pmdk_debuginfo_package_name):
    """
    Checks if names of built packages are the same as names of packages,
    which should be built and returns missing packages. Tools are taken
    into account.
    """
    built_packages = get_built_packages(packages_path)
    is_pmdk_debuginfo =\
        check_existence_of_pmdk_debuginfo_package(pmdk_debuginfo_package_name, built_packages)
    tools = {
        'pmempool': PACKAGES_INFO(basic=True, devel=False, debug=False,
                                  debuginfo=True, debug_debuginfo=False),
        'pmreorder': PACKAGES_INFO(basic=True, devel=False, debug=False,
                                   debuginfo=False, debug_debuginfo=False)
    }
    if not skip_daxio:
        tools['daxio'] = PACKAGES_INFO(basic=True, devel=False, debug=False,
                                       debuginfo=True, debug_debuginfo=False)

    tools_packages = get_names_of_packages(tools)
    missing_tools_packages = [
        elem for elem in tools_packages if elem not in built_packages]
    libraries = get_libraries_names_from_so_files(pmdk_path, is_pmdk_debuginfo)
    library_packages = get_names_of_packages(libraries)
    missing_library_packages = [
        elem for elem in library_packages if elem not in built_packages]
    missing_packages = missing_library_packages + missing_tools_packages
    return missing_packages


def find_missing_libraries_and_tools(packages_path, pmdk_path, pmdk_debuginfo_package_name, library_name_pattern):
    """
    Checks if names of functions from .so files are the same as names of
    functions extracted from built packages and returns missing functions.
    Others pkg (tools and "PMDK") are taken into account.
    """
    others_pkg = ['pmempool', 'daxio', 'pmdk', 'pmreorder']
    built_packages = get_built_packages(packages_path)
    is_pmdk_debuginfo =\
        check_existence_of_pmdk_debuginfo_package(pmdk_debuginfo_package_name, built_packages)
    libraries = get_libraries_names_from_so_files(pmdk_path, is_pmdk_debuginfo)

    missing_elements = []
    # looks for the name of library/others in package name
    for elem in listdir(packages_path):
        library_name = re.search(library_name_pattern, elem).group(1)
        if library_name.endswith('1'):
            library_name = library_name.replace('1','')
        if library_name not in libraries.keys() and library_name not in\
                others_pkg and library_name not in missing_elements:
            missing_elements.append(library_name)
    return missing_elements


def parse_argument(argument_option):
    """
    Parses an option from the command line.
    """
    index = sys.argv.index(argument_option)
    try:
        argument = sys.argv[index+1]
    except IndexError:
        print('ERROR: Invalid argument!')
        print(__doc__)
        print(unittest.main.__doc__)
    else:
        sys.argv.pop(index)
        sys.argv.pop(index)
        return argument


class TestBuildPackages(unittest.TestCase):

    def test_completeness_of_built_packages(self):
        """
        Checks if all packages are built.
        """
        missing_packages =\
            find_missing_packages(packages_path, pmdk_path, pmdk_debuginfo_package_name)
        error_msg = linesep + 'List of missing packages:'

        for package in missing_packages:
            error_msg += linesep + package
        self.assertFalse(missing_packages, error_msg)

    def test_completeness_of_name_of_libraries_and_tools(self):
        """
        Checks if names of functions from .so files and other elements (tools
        and "PMDK") are the same as functions/other elements extracted from
        the name of built packages.
        """
        os_distro=distro.id()
        missing_elements =\
        find_missing_libraries_and_tools(packages_path, pmdk_path, pmdk_debuginfo_package_name,library_name_pattern)
        error_msg = linesep +\
            'List of missing libraries and other elements (tools and "PMDK"):'
        for elem in missing_elements:
            error_msg += linesep + elem

        self.assertFalse(missing_elements, error_msg)


if __name__ == '__main__':
    path_argument = '-r'
    daxio_build_argument = "--skip-daxio"
    skip_daxio = False
    if '-h' in sys.argv or '--help' in sys.argv:
        print(__doc__)
        unittest.main()
    elif path_argument in sys.argv:
        pmdk_path = parse_argument(path_argument)
        if daxio_build_argument in sys.argv:
            skip_daxio = True
            index = sys.argv.index(daxio_build_argument)
            sys.argv.pop(index)
        if pmdk_path:
            PMDK_VERSION, SYSTEM_ARCHITECTURE =\
                get_package_version_and_system_architecture(pmdk_path)
            os_distro=distro.id()
            if os_distro != 'ubuntu':
                packages_path = path.join(pmdk_path, 'rpm', SYSTEM_ARCHITECTURE)
                pmdk_debuginfo_package_name = 'pmdk-debuginfo-' + PMDK_VERSION + '.' + SYSTEM_ARCHITECTURE + '.rpm'
                library_name_pattern = r'[\s]*([2a-zA-Z+\d]+)-'
            else:
                packages_path = path.join(pmdk_path, 'dpkg')
                pmdk_debuginfo_package_name = 'pmdk-debuginfo-' + PMDK_VERSION + '.' + '.deb'
                library_name_pattern = r'^([2a-zA-Z]+)[-_].*$'
            unittest.main(
                testRunner=xmlrunner.XMLTestRunner(output='test-reports'),
                # these make sure that some options that are not applicable
                # remain hidden from the help menu.
                failfast=False, buffer=False, catchbreak=False)
    else:
        print(__doc__)
        print(unittest.main.__doc__)