summaryrefslogtreecommitdiffstats
path: root/test/run-unit-tests.py
blob: 9a75cd421ec1562bdd10e5235a7cace34add5064 (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
#!/usr/bin/env python3

import argparse
import dataclasses
import glob
import os
import subprocess
import sys
try:
    import colorama as c
    GREEN = c.Fore.GREEN
    YELLOW = c.Fore.YELLOW
    RED = c.Fore.RED
    RESET_ALL = c.Style.RESET_ALL
    BRIGHT = c.Style.BRIGHT
except ImportError:
    GREEN = YELLOW = RED = RESET_ALL = BRIGHT = ''

@dataclasses.dataclass
class Total:
    total:int
    good:int = 0
    skip:int = 0
    fail:int = 0

def argument_parser():
    p = argparse.ArgumentParser()
    p.add_argument('-u', '--unsafe', action='store_true',
                   help='run "unsafe" tests too')
    return p

opts = argument_parser().parse_args()

tests = glob.glob('/usr/lib/systemd/tests/test-*')
if opts.unsafe:
    tests += glob.glob('/usr/lib/systemd/tests/unsafe/test-*')

total = Total(total=len(tests))
for test in tests:
    name = os.path.basename(test)

    ex = subprocess.run(test, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if ex.returncode == 0:
        print(f'{GREEN}PASS: {name}{RESET_ALL}')
        total.good += 1
    elif ex.returncode == 77:
        print(f'{YELLOW}SKIP: {name}{RESET_ALL}')
        total.skip += 1
    else:
        print(f'{RED}FAIL: {name}{RESET_ALL}')
        total.fail += 1

        # stdout/stderr might not be valid unicode, let's just dump it to the terminal.
        # Also let's reset the style afterwards, in case our output sets something.
        sys.stdout.buffer.write(ex.stdout)
        print(f'{RESET_ALL}{BRIGHT}')
        sys.stdout.buffer.write(ex.stderr)
        print(f'{RESET_ALL}')

print(f'{BRIGHT}OK: {total.good} SKIP: {total.skip} FAIL: {total.fail}{RESET_ALL}')
sys.exit(total.fail > 0)