-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-setup.py
202 lines (170 loc) · 6.44 KB
/
test-setup.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
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
#!/usr/bin/python3
#pylint: disable=invalid-name
'''Bandwidth tests for tunnel experiments'''
import os
import json
import subprocess
import itertools
from argparse import ArgumentParser
IPSEC="/usr/sbin/ipsec"
CONFIG="tests.json"
class Test():
'''Simple Test Class'''
def __init__(self, config):
conf = open(config, "r")
parsed = json.load(conf)
self.tests = parsed["tests"]
self.ipsec = "\n".join(parsed["ipsec"])
self.secrets = "\n".join(parsed["secrets"])
self.common = parsed["common"]
self.ipsec_id = os.getpid()
conf.close()
@staticmethod
def run_command(command, params, dummy):
'''Command runner with dry run capability'''
if dummy:
print(command.format(**params))
else:
subprocess.run(command.format(**params), shell=True, check=False)
def test_up(self, params, ip_addr=1, dummy=True):
'''Bring up all tunnels using supplied params'''
for test in self.tests:
params["link"] = "{}{}".format(test["name"],self.ipsec_id)
params["inner"] = params["subnet"] + "." + str(ip_addr)
if ip_addr % 2 == 0:
params["inner-remote"] = params["subnet"] + "." + str(ip_addr - 1)
else:
params["inner-remote"] = params["subnet"] + "." + str(ip_addr + 1)
ip_addr = ip_addr + 4
if dummy:
print("Test {}".format(test["name"]))
for command in itertools.chain(test["up"], self.common["up"]):
self.run_command(command, params, dummy)
def test_down(self, params, ip_addr=1, dummy=True):
'''Bring down all tunnels using supplied params'''
for test in self.tests:
params["link"] = "{}{}".format(test["name"],self.ipsec_id)
params["inner"] = params["subnet"] + ".{}".format(ip_addr)
ip_addr = ip_addr + 4
for command in itertools.chain(self.common["down"], test["down"]):
self.run_command(command, params, dummy)
def ipsec_on(self, params):
'''Create a temporary ipsec config and enable it'''
ipsec = open("/etc/ipsec.d/{}.conf".format(self.ipsec_id), "w+")
params["id"] = self.ipsec_id
ipsec.write(self.ipsec.format(**params))
ipsec.close()
secrets = open("/etc/ipsec.d/{}.secrets".format(self.ipsec_id), "w+")
secrets.write(self.secrets.format(**params))
secrets.close()
subprocess.run("ipsec restart", shell=True, check=False)
def ipsec_off(self):
'''Disable the temporary ipsec config'''
os.unlink("/etc/ipsec.d/{}.conf".format(self.ipsec_id))
os.unlink("/etc/ipsec.d/{}.secrets".format(self.ipsec_id))
subprocess.run("ipsec restart", shell=True, check=False)
def run_server(self):
'''Run iperf on the server side and record the results'''
try:
print("Press Ctrl-C to terminate test")
#pylint: disable=too-many-format-args
subprocess.run(
"iperf -s".format(self.ipsec_id),
shell=True,
capture_output=True,
check=False)
except KeyboardInterrupt:
pass
def run_client(self, params):
'''Run iperf on the server side and record the results'''
ip_addr = 1
#pylint: disable=unused-variable
print ("Report will be saved in result.{}.txt".format(self.ipsec_id))
report = open("result.{}.txt".format(self.ipsec_id), "w+")
for test in self.tests:
print("Test {}".format(test["name"]))
source = params["subnet"] + ".{}".format(ip_addr + 1)
report.write("Test {}\n".format(test["name"]))
target = params["subnet"] + ".{}".format(ip_addr)
report.write(subprocess.run(
"iperf -c {} -B {}".format(target, source),
shell=True,
capture_output=True,
check=False).stdout.decode("utf-8"))
ip_addr = ip_addr + 4
report.close()
def main():
'''Run the test suite'''
aparser = ArgumentParser(description=main.__doc__)
aparser.add_argument(
'--config',
help='Alternative test control config',
type=str)
aparser.add_argument(
'--local',
help='Local IP address (must exist on this host)',
required=True,
type=str)
aparser.add_argument(
'--remote',
help='Remote IP address',
required=True,
type=str)
aparser.add_argument(
'--subnet',
help='Subnet for tunnel allocation',
required=True,
type=str)
aparser.add_argument(
'--server',
action='store_true',
help='Run tests as server side')
aparser.add_argument(
'--sport',
default=1702,
help='EoL2TPv3 default source port')
aparser.add_argument(
'--dport',
default=1702,
help='EoL2TPv3 default dest port')
aparser.add_argument(
'--client',
action='store_true',
help='Run tests as client side')
aparser.add_argument(
'--ipsec',
action='store_true',
help='Run the tests with transport mode IPSEC between local and remote')
aparser.add_argument(
'--dryrun',
action='store_true',
help='Dry run, do not make any config changes')
args = vars(aparser.parse_args())
if (not args.get('server')) and (not args.get('client')):
raise Exception("Invalid Arguments, no role specified")
if args.get('server') and args.get('client'):
raise Exception("Invalid Arguments, only one role at a time")
Tester = None
if args.get('config') is not None:
Tester = Test(args.get('config'))
else:
Tester = Test(CONFIG)
if args.get('ipsec'):
if args["client"]:
args["inner"] = args["subnet"] + ".2"
args["inner-remote"] = args["subnet"] + ".1"
else:
args["inner"] = args["subnet"] + ".1"
args["inner-remote"] = args["subnet"] + ".2"
Tester.ipsec_on(args)
if args.get('server'):
Tester.test_up(args, dummy=args.get('dryrun'))
Tester.run_server()
else:
Tester.test_up(args, ip_addr=2, dummy=args.get('dryrun'))
Tester.run_client(args)
Tester.test_down(args, dummy=args.get('dryrun'))
if args.get('ipsec'):
Tester.ipsec_off()
if __name__ == '__main__':
main()