File: load.py

package info (click to toggle)
python-stestr 4.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 852 kB
  • sloc: python: 6,303; makefile: 32; sh: 12
file content (372 lines) | stat: -rw-r--r-- 13,296 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# 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.

"""Load data into a repository."""


import errno
import functools
import os
import sys

from cliff import command
import subunit
import testtools

from stestr import output
from stestr.repository import abstract as repository
from stestr.repository import util
from stestr import results
from stestr import subunit_trace
from stestr import user_config
from stestr import utils


class Load(command.Command):
    """Load a subunit stream into a repository.

    Failing tests are shown on the console and a summary of the stream
    is printed at the end.

    Without --subunit, the process exit code will be non-zero if the test
    run was not successful. With --subunit, the process exit code is
    non-zero if the subunit stream could not be generated successfully.
    The test results and run status are included in the subunit stream, so
    the stream should be used to determining the result of the run instead
    of the exit code when using the --subunit flag.
    """

    def get_parser(self, prog_name):
        parser = super().get_parser(prog_name)
        parser.add_argument(
            "files",
            nargs="*",
            default=False,
            help="The subunit v2 stream files to load into the" " repository",
        )
        parser.add_argument(
            "--force-init",
            action="store_true",
            default=False,
            help="Initialise the repository if it does not " "exist already",
        )
        parser.add_argument(
            "--subunit",
            action="store_true",
            default=False,
            help="Display results in subunit format.",
        )
        parser.add_argument(
            "--id",
            "-i",
            default=None,
            help="Append the stream into an existing entry in " "the repository",
        )
        parser.add_argument(
            "--subunit-trace",
            action="store_true",
            default=False,
            help="Display the loaded stream through the " "subunit-trace output filter",
        )
        parser.add_argument(
            "--color",
            action="store_true",
            default=False,
            help="Enable color output in the subunit-trace "
            "output, if subunit-trace output is enabled. If "
            "subunit-trace is disable this does nothing.",
        )
        parser.add_argument(
            "--abbreviate",
            action="store_true",
            dest="abbreviate",
            help="Print one character status for each test",
        )
        parser.add_argument(
            "--suppress-attachments",
            action="store_true",
            dest="suppress_attachments",
            help="If set do not print stdout or stderr "
            "attachment contents on a successful test "
            "execution",
        )
        parser.add_argument(
            "--all-attachments",
            action="store_true",
            dest="all_attachments",
            help="If set print all text attachment contents on"
            " a successful test execution",
        )
        parser.add_argument(
            "--show-binary-attachments",
            action="store_true",
            dest="show_binary_attachments",
            help="If set, show non-text attachments. This is "
            "generally only useful for debug purposes.",
        )
        return parser

    def take_action(self, parsed_args):
        user_conf = user_config.get_user_config(self.app_args.user_config)
        args = parsed_args
        if args.suppress_attachments and args.all_attachments:
            msg = (
                "The --suppress-attachments and --all-attachments "
                "options are mutually exclusive, you can not use both "
                "at the same time"
            )
            print(msg)
            sys.exit(1)
        if getattr(user_conf, "load", False):
            force_init = args.force_init or user_conf.load.get("force-init", False)
            pretty_out = args.subunit_trace or user_conf.load.get(
                "subunit-trace", False
            )
            color = args.color or user_conf.load.get("color", False)
            abbreviate = args.abbreviate or user_conf.load.get("abbreviate", False)
            suppress_attachments_conf = user_conf.run.get("suppress-attachments", False)
            all_attachments_conf = user_conf.run.get("all-attachments", False)
            if not args.suppress_attachments and not args.all_attachments:
                suppress_attachments = suppress_attachments_conf
                all_attachments = all_attachments_conf
            elif args.suppress_attachments:
                all_attachments = False
                suppress_attachments = args.suppress_attachments
            elif args.all_attachments:
                suppress_attachments = False
                all_attachments = args.all_attachments
        else:
            force_init = args.force_init
            pretty_out = args.subunit_trace
            color = args.color
            abbreviate = args.abbreviate
            suppress_attachments = args.suppress_attachments
            all_attachments = args.all_attachments
        verbose_level = self.app.options.verbose_level
        stdout = open(os.devnull, "w") if verbose_level == 0 else sys.stdout
        load(
            repo_url=self.app_args.repo_url,
            subunit_out=args.subunit,
            force_init=force_init,
            streams=args.files,
            pretty_out=pretty_out,
            color=color,
            stdout=stdout,
            abbreviate=abbreviate,
            suppress_attachments=suppress_attachments,
            serial=True,
            all_attachments=all_attachments,
            show_binary_attachments=args.show_binary_attachments,
        )


def load(
    force_init=False,
    in_streams=None,
    subunit_out=False,
    repo_url=None,
    run_id=None,
    streams=None,
    pretty_out=False,
    color=False,
    stdout=sys.stdout,
    abbreviate=False,
    suppress_attachments=False,
    serial=False,
    all_attachments=False,
    show_binary_attachments=False,
):
    """Load subunit streams into a repository

    This function will load subunit streams into the repository. It will
    output to STDOUT the results from the input stream. Internally this is
    used by the run command to both output the results as well as store the
    result in the repository.

    :param bool force_init: Initialize the specified repository if it hasn't
        been created.
    :param list in_streams: A list of file objects that will be saved into the
        repository
    :param bool subunit_out: Output the subunit stream to stdout
    :param str repo_url: The url of the repository to use.
    :param run_id: The optional run id to save the subunit stream to.
    :param list streams: A list of file paths to read for the input streams.
    :param bool pretty_out: Use the subunit-trace output filter for the loaded
        stream.
    :param bool color: Enabled colorized subunit-trace output
    :param file stdout: The output file to write all output to. By default
        this is sys.stdout
    :param bool abbreviate: Use abbreviated output if set true
    :param bool suppress_attachments: When set true attachments subunit_trace
        will not print attachments on successful test execution.
    :param bool all_attachments: When set true subunit_trace will print all
        text attachments on successful test execution.
    :param bool show_binary_attachments: When set to true, subunit_trace will
        print binary attachments in addition to text attachments.

    :return return_code: The exit code for the command. 0 for success and > 0
        for failures.
    :rtype: int
    """
    try:
        repo = util.get_repo_open(repo_url=repo_url)
    except repository.RepositoryNotFound:
        if force_init:
            try:
                repo = util.get_repo_initialise(repo_url=repo_url)
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise
                repo_path = repo_url or "./stestr"
                stdout.write(
                    "The specified repository directory %s already "
                    "exists. Please check if the repository already "
                    "exists or select a different path\n" % repo_path
                )
                exit(1)
        else:
            raise
    # Not a full implementation of TestCase, but we only need to iterate
    # back to it. Needs to be a callable - its a head fake for
    # testsuite.add.
    if in_streams:
        streams = utils.iter_streams(in_streams, "subunit")
    elif streams:
        opener = functools.partial(open, mode="rb")
        streams = map(opener, streams)
    else:
        streams = [sys.stdin]

    def mktagger(pos, result):
        return testtools.StreamTagger([result], add=["worker-%d" % pos])

    def make_tests():
        for pos, stream in enumerate(streams):
            # Calls StreamResult API.
            case = subunit.ByteStreamToStreamResult(stream, non_subunit_name="stdout")
            decorate = functools.partial(mktagger, pos)
            case = testtools.DecorateTestCaseResult(case, decorate)
            yield (case, str(pos))

    if not run_id:
        inserter = repo.get_inserter()
    else:
        inserter = repo.get_inserter(run_id=run_id)

    retval = 0
    if serial:
        for stream in streams:
            # Calls StreamResult API.
            case = subunit.ByteStreamToStreamResult(stream, non_subunit_name="stdout")
            result = _load_case(
                inserter,
                repo,
                case,
                subunit_out,
                pretty_out,
                color,
                stdout,
                abbreviate,
                suppress_attachments,
                all_attachments,
                show_binary_attachments,
            )
            if result or retval:
                retval = 1
            else:
                retval = 0
    else:
        case = testtools.ConcurrentStreamTestSuite(make_tests)
        retval = _load_case(
            inserter,
            repo,
            case,
            subunit_out,
            pretty_out,
            color,
            stdout,
            abbreviate,
            suppress_attachments,
            all_attachments,
            show_binary_attachments,
        )

    return retval


def _load_case(
    inserter,
    repo,
    case,
    subunit_out,
    pretty_out,
    color,
    stdout,
    abbreviate,
    suppress_attachments,
    all_attachments,
    show_binary_attachments,
):
    if subunit_out:
        output_result, summary_result = output.make_result(
            inserter.get_id, output=stdout
        )
    elif pretty_out:
        outcomes = testtools.StreamToDict(
            functools.partial(
                subunit_trace.show_outcome,
                stdout,
                enable_color=color,
                abbreviate=abbreviate,
                suppress_attachments=suppress_attachments,
                all_attachments=all_attachments,
                show_binary_attachments=show_binary_attachments,
            )
        )
        summary_result = testtools.StreamSummary()
        output_result = testtools.CopyStreamResult([outcomes, summary_result])
        output_result = testtools.StreamResultRouter(output_result)
        cat = subunit.test_results.CatFiles(stdout)
        output_result.add_rule(cat, "test_id", test_id=None)
    else:
        try:
            previous_run = repo.get_latest_run()
        except KeyError:
            previous_run = None
        output_result = results.CLITestResult(inserter.get_id, stdout, previous_run)
        summary_result = output_result.get_summary()
    result = testtools.CopyStreamResult([inserter, output_result])
    result.startTestRun()
    try:
        case.run(result)
    finally:
        result.stopTestRun()
    if pretty_out and not subunit_out:
        start_times = []
        stop_times = []
        for worker in subunit_trace.RESULTS:
            for test in subunit_trace.RESULTS[worker]:
                if not test["timestamps"][0] or not test["timestamps"][1]:
                    continue
                start_times.append(test["timestamps"][0])
                stop_times.append(test["timestamps"][1])

        # This is not ideal, as it means if you use don't enter this if statement
        # some errors aren't caught
        if subunit_trace.print_full_output(
            stdout, start_times, stop_times, post_fails=True, no_summary=False
        ):
            return 1
    if not results.wasSuccessful(summary_result):
        return 1
    else:
        return 0