• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3# -*- coding: utf-8 -*-
4# -*- Mode: Python
5#
6# Author: Chenxiong Qi
7
8'''Runs tests for the fedabipkgdiff program
9
10This program runs the fedabipkgdiff tool (via the mockfedabipkgdiff
11tool) repeatedly and compares its output against a set of reference
12output files.
13
14'''
15
16import sys
17import os
18import subprocess
19
20FEDABIPKGDIFF = '@abs_top_builddir@/tests/mockfedabipkgdiff'
21TEST_SRC_DIR = '@abs_top_srcdir@/tests'
22TEST_BUILD_DIR = '@abs_top_builddir@/tests'
23INPUT_DIR = '@abs_top_srcdir@/tests/data/test-fedabipkgdiff'
24OUTPUT_DIR = '@abs_top_builddir@/tests/output/test-fedabipkgdiff'
25
26# This variable  is a list of 3-uples.  Here is the meaning of
27# the elements of each 3-uples:
28#
29# (arguments to pass to fedabipkgdiff,
30#  reference output file,
31#  where to store the result of the comparison)
32#
33# Note that after fedabipkgdiff is run (using the arguments that are
34# the first element of the tuple) the result of the comparison is
35# going to be compared against the reference output file, and both
36# must be equal.
37#
38# If a user wants to add a new test, she must add a new tuple to the
39# variable below, and also add a new reference output to the source
40# distribution.
41#
42FEDABIPKGDIFF_TEST_SPECS = [
43    (['--from', 'fc20',  '--to', 'fc23', 'dbus-glib'],
44     'data/test-fedabipkgdiff/test0-from-fc20-to-fc23-dbus-glib-report-0.txt',
45     'output/test-fedabipkgdiff/test0-type-suppr-report-0.txt'),
46
47    (['--from', 'fc20', os.path.join(INPUT_DIR,
48                                     'packages/dbus-glib/0.106/1.fc23/x86_64/'
49                                     'dbus-glib-0.106-1.fc23.x86_64.rpm')],
50     'data/test-fedabipkgdiff/test1-from-fc20-to-dbus-glib-0.106-1.fc23.x86_64-report-0.txt',
51     'output/test-fedabipkgdiff/test1-from-fc20-to-dbus-glib-0.106-1.fc23.x86_64-report-0.txt'),
52
53    (['dbus-glib-0.100.2-2.fc20', 'dbus-glib-0.106-1.fc23'],
54     'data/test-fedabipkgdiff/test2-dbus-glib-0.100.2-2.fc20--dbus-glib-0.106-1.fc23-report-0.txt',
55     'output/test-fedabipkgdiff/test2-dbus-glib-0.100.2-2.fc20--dbus-glib-0.106-1.fc23-report-0.txt'),
56
57    (['dbus-glib-0.100.2-2.fc20.i686', 'dbus-glib-0.106-1.fc23.i686'],
58     'data/test-fedabipkgdiff/test3-dbus-glib-0.100.2-2.fc20.i686--dbus-glib-0.106-1.fc23.i686-report-0.txt',
59     'output/test-fedabipkgdiff/test3-dbus-glib-0.100.2-2.fc20.i686--dbus-glib-0.106-1.fc23.i686-report-0.txt'),
60
61    (['vte291-0.39.1-1.fc22.x86_64', 'vte291-0.39.90-1.fc22.x86_64'],
62     'data/test-fedabipkgdiff/vte291-0.39.1-1.fc22.x86_64--vte291-0.39.90-1.fc22.x86_64-report-0.txt',
63     'output/test-fedabipkgdiff/vte291-0.39.1-1.fc22.x86_64--vte291-0.39.90-1.fc22.x86_64-report-0.txt'),
64
65    ([os.path.join(INPUT_DIR, 'packages/dbus-glib/0.100.2/2.fc20/x86_64/dbus-glib-0.100.2-2.fc20.x86_64.rpm'),
66      os.path.join(INPUT_DIR, 'packages/dbus-glib/0.106/1.fc23/x86_64/dbus-glib-0.106-1.fc23.x86_64.rpm')],
67     'data/test-fedabipkgdiff/test4-glib-0.100.2-2.fc20.x86_64.rpm-glib-0.106-1.fc23.x86_64.rpm-report-0.txt',
68     'output/test-fedabipkgdiff/test4-glib-0.100.2-2.fc20.x86_64.rpm-glib-0.106-1.fc23.x86_64.rpm-report-0.txt'),
69
70    ([os.path.join(INPUT_DIR, 'dbus-glib/dbus-glib-0.100.2-2.fc20.x86_64.rpm'),
71      os.path.join(INPUT_DIR, 'dbus-glib/dbus-glib-0.106-1.fc23.x86_64.rpm')],
72     'data/test-fedabipkgdiff/test5-same-dir-dbus-glib-0.100.2-2.fc20.x86_64--dbus-glib-0.106-1.fc23.x86_64-report-0.txt',
73     'output/test-fedabipkgdiff/test5-same-dir-dbus-glib-0.100.2-2.fc20.x86_64--dbus-glib-0.106-1.fc23.x86_64-report-0.txt'),
74
75    ([os.path.join(INPUT_DIR, 'nss-util/nss-util-3.12.6-1.fc14.x86_64.rpm'),
76      os.path.join(INPUT_DIR, 'nss-util/nss-util-3.24.0-2.0.fc25.x86_64.rpm')],
77     'data/test-fedabipkgdiff/test6-nss-util-3.12.6-1.fc14.x86_64--nss-util-3.24.0-2.0.fc25.x86_64-report-0.txt',
78     'output/test-fedabipkgdiff/test6-nss-util-3.12.6-1.fc14.x86_64--nss-util-3.24.0-2.0.fc25.x86_64-report-0.txt'),
79
80    (['--self-compare', '-a', '--from', 'fc23', 'dbus-glib'],
81     'data/test-fedabipkgdiff/test7-self-compare-from-fc23-dbus-glib-report-0.txt',
82     'output/test-fedabipkgdiff/test7-self-compare-from-fc23-dbus-glib-report-0.txt'),
83]
84
85
86def ensure_output_dir_created():
87    '''Create output dir if it's not already created.'''
88
89    try:
90        os.makedirs(OUTPUT_DIR)
91    except:
92        pass
93
94        if not os.path.exists(OUTPUT_DIR):
95            sys.exit(1)
96
97
98def run_fedabipkgdiff_tests():
99    """Run the fedabipkgdiff tests
100
101    Loop through the test inputs in the FEDABIPKGDIFF_TEST_SPECS global
102    variable and for each of the test input, launch a comparison using
103    mockfedabipkgdiff, which calls fedabipkgdiff by making sure it
104    doesn't use the network.
105
106    The result of the comparison is stored in an output file, and that
107    output file is compared (using GNU diff) against the reference
108    output file specified in the FEDABIPKGDIFF_TEST_SPECS global
109    variable.
110
111    This function returns True iff all comparison specified by
112    FEDABIPKGDIFF_TEST_SPECS succeed.
113    """
114
115    result = True
116    for args, reference_report_path, output_path in FEDABIPKGDIFF_TEST_SPECS:
117        reference_report_path = os.path.join(TEST_SRC_DIR, reference_report_path)
118        output_path = os.path.join(TEST_BUILD_DIR, output_path)
119        cmd = [FEDABIPKGDIFF,
120               '--no-default-suppression',
121               '--show-identical-binaries',
122               '--clean-cache'] + args
123
124        with open(output_path, 'w') as out_file:
125            subprocess.call(cmd, stdout=out_file)
126
127            if not os.path.exists(reference_report_path):
128                sys.stderr.write('file not found: ' + reference_report_path + '\n')
129                return_code = -1
130            else:
131                diffcmd = ['diff', '-u', reference_report_path, output_path]
132                return_code = subprocess.call(diffcmd)
133            if return_code:
134                sys.stderr.write('fedabipkgdiff test failed for ' +
135                                 reference_report_path + '\n')
136                sys.stderr.write('command was: ' + ' '.join(cmd) + '\n');
137            result &=  not return_code
138
139    return result
140
141
142def main():
143    """The main entry point of this program.
144
145    This creates the output directory and launches the tests for the
146    fedabipkgdiff program.
147
148    :return: 0 upon success, 1 otherwise.
149    """
150
151    ensure_output_dir_created()
152    result = 0
153    result = run_fedabipkgdiff_tests()
154    return not result
155
156
157if __name__ == '__main__':
158    exit_code = main()
159    sys.exit(exit_code)
160