File: oslo_run_pre_release_tests

package info (click to toggle)
python-oslotest 1%3A4.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 412 kB
  • sloc: python: 970; sh: 95; makefile: 21
file content (209 lines) | stat: -rwxr-xr-x 6,646 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
#!/usr/bin/env python
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
"""Run unit tests for projects that use a library.
"""

import glob
import os
import subprocess
import sys

from oslo_config import cfg
from oslotest.tools import config as tconfig
from pbr import packaging
import pkg_resources


def find_all_projects(repo_root):
    """Scan the checked out repositories for all available projects.
    """
    pattern = os.path.join(repo_root, 'openstack/*')
    candidates = glob.glob(pattern)
    prefix_len = len(repo_root)
    return [
        c[prefix_len:].lstrip('/')
        for c in candidates
        if os.path.isdir(c)
    ]


def find_consuming_projects(lib_name, repo_root, projects):
    """Filter the list of projects to only include entries that use the library.
    """
    for p in projects:
        consumer = False
        for base in packaging.get_requirements_files():
            req_file = os.path.join(repo_root, p, base)
            for req in packaging.parse_requirements([req_file]):
                try:
                    parsed_req = pkg_resources.Requirement.parse(req)
                    req_name = parsed_req.project_name
                except ValueError:
                    continue
                if req_name == lib_name:
                    consumer = True
                    yield p
                    break
            if consumer:
                break


def main():
    conf = tconfig.get_config_parser()
    conf.register_cli_opt(
        cfg.StrOpt(
            'library-under-test',
            short='l',
            default='',
            help=('the name of the library being tested; '
                  'defaults to current dir'),
        )
    )
    conf.register_cli_opt(
        cfg.BoolOpt(
            'update',
            short='u',
            default=False,
            help='update consumers before running tests',
        )
    )
    conf.register_cli_opt(
        cfg.BoolOpt(
            'verbose',
            short='v',
            default=False,
            help='print verbose output',
        )
    )
    conf.register_cli_opt(
        cfg.StrOpt(
            'ref',
            short='r',
            default='HEAD',
            help='the commit reference to test; defaults to HEAD',
        )
    )
    conf.register_cli_opt(
        cfg.MultiStrOpt(
            'env',
            short='e',
            default=['py27', 'pep8'],
            help=('the name of the tox environment to test; '
                  'defaults to py27 and pep8'),
        )
    )
    conf.register_cli_opt(
        cfg.MultiStrOpt(
            'consumer',
            positional=True,
            default=[],
            help='the name of a project to test with; may be repeated',
        )
    )
    tconfig.parse_arguments(conf)

    repo_root = os.path.expanduser(conf.repo_root)

    # Figure out which library is being tested
    lib_name = conf.library_under_test
    if not lib_name:
        if conf.verbose:
            print('finding library name')
        lib_name = subprocess.check_output(
            ['python', 'setup.py', '--name']
        ).strip()
        lib_dir = os.getcwd()
    else:
        lib_dir = os.path.join(repo_root, 'openstack', lib_name)
    print('testing %s in %s' % (lib_name, lib_dir))

    projects = set(conf.consumer)
    if not projects:
        # TODO(dhellmann): Need to update this to look at gerrit, so
        # we can check out the projects we want to test with.
        if conf.verbose:
            print('defaulting to all projects under %s/openstack' % repo_root)
        projects = find_all_projects(repo_root)

    # Filter out projects that do not require the library under test
    before = len(projects)
    projects = list(find_consuming_projects(lib_name, repo_root, projects))
    after = len(projects)
    if (after < before) and conf.verbose:
        print('ignoring %s projects that do not use %s'
              % (before - after, lib_name))

    projects = list(sorted(projects))
    if not projects:
        print('ERROR: found no projects using %s' % lib_name)
        return 1
    if conf.verbose:
        print('preparing to test %s projects' % after)

    # Make sure the lib being tested is set to the reference intended.
    if conf.ref != 'HEAD':
        if conf.verbose:
            print('ensuring %s is updated to %s' % (lib_name, conf.ref))
        subprocess.check_call(
            ['git', 'checkout', conf.ref],
            cwd=lib_dir,
        )

    git_quiet = ['-q'] if not conf.verbose else []

    failures = []
    for p in projects:
        if conf.verbose:
            print()
        proj_dir = os.path.join(repo_root, p)
        if conf.update:
            if conf.verbose:
                print('updating %s with "git pull"' % p)
            subprocess.Popen(
                ['git', 'pull'] + git_quiet,
                cwd=proj_dir,
            ).communicate()
        p_log_name = p.split('/')[-1].replace('.', '-')
        for e in conf.env:
            log_name = 'cross-test-%s-%s.log' % (p_log_name, e)
            with open(log_name, 'w') as log_file:
                print('testing %s in %s, logging to %s' % (e, p, log_name),
                      end=' ')
                sys.stdout.flush()
                command = ['oslo_run_cross_tests', proj_dir, e]
                log_file.write('running: %s\n' % ' '.join(command))
                log_file.flush()  # since Popen is going to use the fd directly
                cmd = subprocess.Popen(
                    command,
                    cwd=lib_dir,
                    stdout=log_file,
                    stderr=log_file
                )
                cmd.communicate()
                log_file.write('\nexit code: %s\n' % cmd.returncode)
                if cmd.returncode:
                    print('FAIL')
                    failures.append((p, e, cmd.returncode))
                else:
                    print('PASS')

    if failures:
        print('\nFAILED %d jobs' % len(failures))
        return 1
    print('\nPASSED all jobs')
    return 0


if __name__ == '__main__':
    sys.exit(main())