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
import argparse
import pathlib
import subprocess
import init_cluster
import init_gluster
from config import CLEANUP_TEST_DIR
from inspect import getcallargs
from websocket import WebSocketApp
def setup_args():
parser = argparse.ArgumentParser()
parser.add_argument(
'--initialize-cluster',
action='store_true',
default=False,
help='Setup the cluster for API testing.'
)
parser.add_argument(
'--initialize-gluster',
action='store_true',
default=False,
help='Setup the gluster cluster'
)
parser.add_argument(
'--pytest-path',
default='pytest',
help='Setup the gluster cluster'
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
'--test-dir',
default=False,
help='Relative path to the test directory (i.e. tests/smb)'
)
group.add_argument(
'--test-file',
default=False,
help='Relative path to the test file (i.e. tests/smb/test_blah.py)'
)
return parser.parse_args()
def setup_api_results_dir(resultsfile='results.xml'):
# create the results directory
path = pathlib.Path(pathlib.os.getcwd()).joinpath('results')
path.mkdir(exist_ok=True)
# add the file that will store the api results
path = path.joinpath(resultsfile)
try:
path.unlink()
except FileNotFoundError:
pass
path.touch()
return path.as_posix()
def setup_pytest_command(args, results_path, ignore=True):
cmd = [args.pytest_path, '-v', '-rfesp', '-o', 'junit_family=xunit2', f'--junit-xml={results_path}']
# pytest is clever enough to search the "tests" subdirectory
# and look at the argument that is passed and figure out if
# it's a file or a directory so we don't need to do anything
# fancy other than pass it on
if args.test_file:
cmd.append(args.test_file)
elif args.test_dir:
cmd.append(args.test_dir)
if ignore:
cmd.append(f'--ignore={CLEANUP_TEST_DIR}')
return cmd
def validate_modules():
# check that we have correct websocket client
getcallargs(WebSocketApp, *[None], **{'url': None, 'socket': None})
def main():
args = setup_args()
print('Validating modules')
validate_modules()
print('Modules validated')
if args.initialize_cluster:
print('Initializing cluster')
init_cluster.init()
if args.initialize_gluster:
print('Setting up gluster')
init_gluster.init()
print('Setting up API results directory')
results_path = setup_api_results_dir()
print('Running API tests')
subprocess.call(setup_pytest_command(args, results_path))
print('Setting up cleanup API results file')
cleanup_results_path = setup_api_results_dir(resultsfile='cleanup-results.xml')
print('Running cleanup API tests')
args.test_dir = CLEANUP_TEST_DIR # overwrite test_dir args
subprocess.call(setup_pytest_command(args, cleanup_results_path, ignore=False))
if __name__ == '__main__':
main()