-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopoautostandalonetest.py
executable file
·164 lines (150 loc) · 5.46 KB
/
topoautostandalonetest.py
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
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import sys
import json
import importlib
import subprocess
import configparser
from time import sleep
from pathlib import Path
from topoautotest import main as run_single_test
ALGOS = [
'ospf',
'ecmp',
'ldr',
'minmax-single',
'ldr-single',
]
PROBLEMATIC_SKIPS = [
]
CONTROLLER_VARIABLES = Path('variables.ini')
def run_many_tests(topo, module, results, test_count, returns_result=False):
sucessfulTests = list()
cachedir = Path("resultcache")
cachedir.mkdir(parents=True, exist_ok=True)
cfg = configparser.ConfigParser()
cfg.read_string(CONTROLLER_VARIABLES.read_text())
algo = cfg['GENERAL']['algo']
apacache = Path(f'{module.__name__}.apa.json')
if apacache.exists():
apacache.unlink()
is_problematic = (algo, module.__name__) in PROBLEMATIC_SKIPS
while len(sucessfulTests) < test_count:
cachefile = cachedir.joinpath(
f"{module.__name__}.{algo}.{len(sucessfulTests)}.json")
if cachefile.exists():
result = json.loads(cachefile.read_text())
sucessfulTests.append(result)
continue
result = None
if not is_problematic:
subprocess.run(
['mn', '-c'],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
).check_returncode()
print(
f'*** Running test {len(sucessfulTests)+1} of {test_count} on {module.__name__} with {algo}', file=sys.stderr)
ryuController = subprocess.Popen(
[
'ryu-manager',
'latencycontroller.py',
f'{module.__name__}.json'
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
sleep(0.5)
try:
subprocess.run(
["python3", "topoautotest.py", f"{module.__name__}"]
).check_returncode()
except subprocess.CalledProcessError:
pass
finally:
resultspath = Path(f'{module.__name__}.autotest.json')
statepath = Path(f'{module.__name__}.state')
curswsstatepath = Path(f'~current.sws.state')
if resultspath.exists():
result = json.loads(resultspath.read_text())
resultspath.unlink()
ryuController.kill()
if statepath.exists():
statepath.unlink()
if curswsstatepath.exists():
curswsstatepath.unlink()
else:
result = dict()
if result is not None:
cachefile.write_text(json.dumps(result))
sucessfulTests.append(result)
if returns_result:
return sucessfulTests
else:
results.write_text(json.dumps(
sucessfulTests,
indent=4
))
def run_many_tests_bulk(out_path, call_args, returns_result=False):
tests = dict()
for i, call_arg in enumerate(call_args):
print(
f'** Testing topology {i+1} of {len(call_args)}: {call_arg[1].__name__}', file=sys.stderr)
tests[call_arg[1].__name__] = run_many_tests(*call_arg, True)
if returns_result:
return tests
else:
out_path.write_text(json.dumps(
tests,
indent=4
))
def run_many_tests_bulk_all_algos(out_path, call_args, returns_result=False):
original_config = CONTROLLER_VARIABLES.read_bytes()
try:
tests = dict()
cfg = configparser.ConfigParser()
cfg.read_string(CONTROLLER_VARIABLES.read_text())
for i, algo in enumerate(ALGOS):
cfg['GENERAL']['algo'] = algo
with CONTROLLER_VARIABLES.open('w') as f:
cfg.write(f)
print(
f'* Testing algorithm {i+1} of {len(ALGOS)}: {algo}', file=sys.stderr)
tests[algo] = run_many_tests_bulk(out_path, call_args, True)
if returns_result:
return tests
else:
out_path.write_text(json.dumps(
tests,
indent=4
))
finally:
CONTROLLER_VARIABLES.write_bytes(original_config)
def main(out_path, call_args, returns_result=False):
return run_many_tests_bulk_all_algos(out_path, call_args, returns_result)
if __name__ == "__main__":
if len(sys.argv) < 3:
print('Usage:')
print(
f' {sys.argv[0]} <how_many_tests> <toponame1> [<toponame2> [... [<toponameN>]]]')
print()
print(' Where toponame is will be resolved to')
print(' toponame.json')
else:
prog, tests, *modnames = sys.argv
tests = int(tests)
call_args = list()
for modname in modnames:
modfile = Path(f'{modname}.py')
topopath = Path(f'{modname}.json')
resultspath = Path(f'{modname}.autotests.json')
if not topopath.exists():
print(f'Topology {topopath}.json does not exist.')
if not modfile.exists():
print(f'Topology {topopath}.py does not exist.')
print(f'You might want to use toporender.py to generate required files.')
else:
topo = json.loads(topopath.read_text())
mod = importlib.import_module(modname)
call_args.append((topo, mod, resultspath, tests))
main(Path('__all__.autotests.json'), call_args)