aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNils Gillmann <ng0@n0.is>2018-05-23 09:55:22 +0000
committerNils Gillmann <ng0@n0.is>2018-05-23 09:55:22 +0000
commit9b6800526297919de64abb2ebb214b2c73faf2e7 (patch)
tree68b30dc919fe5e9abd44c7e07daf706f91daef2e /src
parent1690977efe4268f9c3be0866d7a48e6b4fe80a59 (diff)
downloadgnunet-9b6800526297919de64abb2ebb214b2c73faf2e7.tar.gz
gnunet-9b6800526297919de64abb2ebb214b2c73faf2e7.zip
integration-tests: gnunet-testing: flake8
Signed-off-by: Nils Gillmann <ng0@n0.is>
Diffstat (limited to 'src')
-rw-r--r--src/integration-tests/gnunet_testing.py.in207
1 files changed, 117 insertions, 90 deletions
diff --git a/src/integration-tests/gnunet_testing.py.in b/src/integration-tests/gnunet_testing.py.in
index 9ca514df4..d18f2b9f8 100644
--- a/src/integration-tests/gnunet_testing.py.in
+++ b/src/integration-tests/gnunet_testing.py.in
@@ -1,6 +1,6 @@
1#!@PYTHON@ 1#!@PYTHON@
2# This file is part of GNUnet. 2# This file is part of GNUnet.
3# (C) 2010, 2017 Christian Grothoff (and other contributing authors) 3# (C) 2010, 2017, 2018 Christian Grothoff (and other contributing authors)
4# 4#
5# GNUnet is free software; you can redistribute it and/or modify 5# GNUnet is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published 6# it under the terms of the GNU General Public License as published
@@ -25,86 +25,99 @@ import shutil
25import time 25import time
26from gnunet_pyexpect import pexpect 26from gnunet_pyexpect import pexpect
27 27
28
28class Check: 29class Check:
29 def __init__(self, test): 30 def __init__(self, test):
30 self.fulfilled = False 31 self.fulfilled = False
31 self.conditions = list() 32 self.conditions = list()
32 self.test = test 33 self.test = test
33 def add (self, condition): 34
35 def add(self, condition):
34 self.conditions.append(condition) 36 self.conditions.append(condition)
35 def run (self): 37
38 def run(self):
36 fulfilled = True 39 fulfilled = True
37 pos = 0 40 pos = 0
38 neg = 0 41 neg = 0
39 for c in self.conditions: 42 for c in self.conditions:
40 if (False == c.check ()): 43 if (False == c.check()):
41 fulfilled = False 44 fulfilled = False
42 neg += 1 45 neg += 1
43 else: 46 else:
44 pos += 1 47 pos += 1
45 return fulfilled 48 return fulfilled
46 def run_blocking (self, timeout, pos_cont, neg_cont): 49
47 execs = 0; 50 def run_blocking(self, timeout, pos_cont, neg_cont):
51 execs = 0
48 res = False 52 res = False
49 while ((False == res) and (execs < timeout)): 53 while ((False == res) and (execs < timeout)):
50 res = self.run() 54 res = self.run()
51 time.sleep(1) 55 time.sleep(1)
52 execs += 1 56 execs += 1
53 if ((False == res) and (execs >= timeout)): 57 if ((False == res) and (execs >= timeout)):
54 print(('Check had timeout after ' +str(timeout)+ ' seconds')) 58 print(('Check had timeout after ' + str(timeout) + ' seconds'))
55 neg_cont (self) 59 neg_cont(self)
56 elif ((False == res) and (execs < timeout)): 60 elif ((False == res) and (execs < timeout)):
57 if (None != neg_cont): 61 if (None != neg_cont):
58 neg_cont (self) 62 neg_cont(self)
59 else: 63 else:
60 if (None != pos_cont): 64 if (None != pos_cont):
61 pos_cont (self) 65 pos_cont(self)
62 return res 66 return res
63 def run_once (self, pos_cont, neg_cont): 67
64 execs = 0; 68 def run_once(self, pos_cont, neg_cont):
69 execs = 0
65 res = False 70 res = False
66 res = self.run() 71 res = self.run()
67 if ((res == False) and (neg_cont != None)): 72 if ((res == False) and (neg_cont != None)):
68 neg_cont (self) 73 neg_cont(self)
69 if ((res == True) and (pos_cont != None)): 74 if ((res == True) and (pos_cont != None)):
70 pos_cont (self) 75 pos_cont(self)
71 return res 76 return res
72 def evaluate (self, failed_only): 77
78 def evaluate(self, failed_only):
73 pos = 0 79 pos = 0
74 neg = 0 80 neg = 0
75 for c in self.conditions: 81 for c in self.conditions:
76 if (False == c.evaluate (failed_only)): 82 if (False == c.evaluate(failed_only)):
77 neg += 1 83 neg += 1
78 else: 84 else:
79 pos += 1 85 pos += 1
80 print((str(pos) +' out of '+ str (pos+neg) + ' conditions fulfilled')) 86 print((str(pos) + ' out of ' + str(pos+neg) + ' conditions fulfilled'))
81 return self.fulfilled 87 return self.fulfilled
82 def reset (self): 88
83 self.fulfilled = False 89 def reset(self):
84 for c in self.conditions: 90 self.fulfilled = False
85 c.fulfilled = False 91 for c in self.conditions:
86 92 c.fulfilled = False
93
94
87class Condition: 95class Condition:
88 def __init__(self): 96 def __init__(self):
89 self.fulfilled = False 97 self.fulfilled = False
90 self.type = 'generic' 98 self.type = 'generic'
99
91 def __init__(self, type): 100 def __init__(self, type):
92 self.fulfilled = False 101 self.fulfilled = False
93 self.type = type 102 self.type = type
103
94 def check(self): 104 def check(self):
95 return False; 105 return False
96 def evaluate (self, failed_only): 106
107 def evaluate(self, failed_only):
97 if ((self.fulfilled == False) and (failed_only == True)): 108 if ((self.fulfilled == False) and (failed_only == True)):
98 print(str(self.type) + 'condition for was ' + str(self.fulfilled)) 109 print(str(self.type) + 'condition for was ' + str(self.fulfilled))
99 elif (failed_only == False): 110 elif (failed_only == False):
100 print(str(self.type) + 'condition for was ' + str(self.fulfilled)) 111 print(str(self.type) + 'condition for was ' + str(self.fulfilled))
101 return self.fulfilled 112 return self.fulfilled
113
102 114
103class FileExistCondition (Condition): 115class FileExistCondition(Condition):
104 def __init__(self, file): 116 def __init__(self, file):
105 self.fulfilled = False 117 self.fulfilled = False
106 self.type = 'file' 118 self.type = 'file'
107 self.file = file 119 self.file = file
120
108 def check(self): 121 def check(self):
109 if (self.fulfilled == False): 122 if (self.fulfilled == False):
110 res = os.path.isfile(self.file) 123 res = os.path.isfile(self.file)
@@ -115,25 +128,28 @@ class FileExistCondition (Condition):
115 return False 128 return False
116 else: 129 else:
117 return True 130 return True
118 def evaluate (self, failed_only): 131
132 def evaluate(self, failed_only):
119 if ((self.fulfilled == False) and (failed_only == True)): 133 if ((self.fulfilled == False) and (failed_only == True)):
120 print(str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled)) 134 print(str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled))
121 elif (failed_only == False): 135 elif (failed_only == False):
122 print(str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled)) 136 print(str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled))
123 return self.fulfilled 137 return self.fulfilled
124 138
139
125class StatisticsCondition (Condition): 140class StatisticsCondition (Condition):
126 def __init__(self, peer, subsystem, name, value): 141 def __init__(self, peer, subsystem, name, value):
127 self.fulfilled = False 142 self.fulfilled = False
128 self.type = 'statistics' 143 self.type = 'statistics'
129 self.peer = peer; 144 self.peer = peer
130 self.subsystem = subsystem; 145 self.subsystem = subsystem
131 self.name = name; 146 self.name = name
132 self.value = value; 147 self.value = value
133 self.result = -1; 148 self.result = -1
149
134 def check(self): 150 def check(self):
135 if (self.fulfilled == False): 151 if (self.fulfilled == False):
136 self.result = self.peer.get_statistics_value (self.subsystem, self.name) 152 self.result = self.peer.get_statistics_value(self.subsystem, self.name)
137 if (str(self.result) == str(self.value)): 153 if (str(self.result) == str(self.value)):
138 self.fulfilled = True 154 self.fulfilled = True
139 return True 155 return True
@@ -141,38 +157,41 @@ class StatisticsCondition (Condition):
141 return False 157 return False
142 else: 158 else:
143 return True 159 return True
144 def evaluate (self, failed_only): 160
161 def evaluate(self, failed_only):
145 if (self.result == -1): 162 if (self.result == -1):
146 res = 'NaN' 163 res = 'NaN'
147 else: 164 else:
148 res = str(self.result) 165 res = str(self.result)
149 if (self.fulfilled == False): 166 if (self.fulfilled == False):
150 fail = " FAIL!" 167 fail = " FAIL!"
151 op = " != " 168 op = " != "
152 else: 169 else:
153 fail = "" 170 fail = ""
154 op = " == " 171 op = " == "
155 if (((self.fulfilled == False) and (failed_only == True)) or (failed_only == False)): 172 if (((self.fulfilled == False) and (failed_only == True)) or (failed_only == False)):
156 print(self.peer.id[:4] + " " +self.peer.cfg + " " + str(self.type) + ' condition in subsystem "' + self.subsystem.ljust(12) +'" : "' + self.name.ljust(30) +'" : (expected/real value) ' + str(self.value) + op + res + fail) 173 print(self.peer.id[:4] + " " + self.peer.cfg + " " + str(self.type) + ' condition in subsystem "' + self.subsystem.ljust(12) + '" : "' + self.name.ljust(30) + '" : (expected/real value) ' + str(self.value) + op + res + fail)
157 return self.fulfilled 174 return self.fulfilled
158 175
159# Specify two statistic values and check if they are equal 176
177# Specify two statistic values and check if they are equal
160class EqualStatisticsCondition (Condition): 178class EqualStatisticsCondition (Condition):
161 def __init__(self, peer, subsystem, name, peer2, subsystem2, name2): 179 def __init__(self, peer, subsystem, name, peer2, subsystem2, name2):
162 self.fulfilled = False 180 self.fulfilled = False
163 self.type = 'equalstatistics' 181 self.type = 'equalstatistics'
164 self.peer = peer; 182 self.peer = peer
165 self.subsystem = subsystem; 183 self.subsystem = subsystem
166 self.name = name; 184 self.name = name
167 self.result = -1; 185 self.result = -1
168 self.peer2 = peer2; 186 self.peer2 = peer2
169 self.subsystem2 = subsystem2; 187 self.subsystem2 = subsystem2
170 self.name2 = name2; 188 self.name2 = name2
171 self.result2 = -1; 189 self.result2 = -1
190
172 def check(self): 191 def check(self):
173 if (self.fulfilled == False): 192 if (self.fulfilled == False):
174 self.result = self.peer.get_statistics_value (self.subsystem, self.name); 193 self.result = self.peer.get_statistics_value(self.subsystem, self.name)
175 self.result2 = self.peer2.get_statistics_value (self.subsystem2, self.name2); 194 self.result2 = self.peer2.get_statistics_value(self.subsystem2, self.name2)
176 if (str(self.result) == str(self.result2)): 195 if (str(self.result) == str(self.result2)):
177 self.fulfilled = True 196 self.fulfilled = True
178 return True 197 return True
@@ -180,7 +199,8 @@ class EqualStatisticsCondition (Condition):
180 return False 199 return False
181 else: 200 else:
182 return True 201 return True
183 def evaluate (self, failed_only): 202
203 def evaluate(self, failed_only):
184 if (self.result == -1): 204 if (self.result == -1):
185 res = 'NaN' 205 res = 'NaN'
186 else: 206 else:
@@ -188,27 +208,28 @@ class EqualStatisticsCondition (Condition):
188 if (self.result2 == -1): 208 if (self.result2 == -1):
189 res2 = 'NaN' 209 res2 = 'NaN'
190 else: 210 else:
191 res2 = str(self.result2) 211 res2 = str(self.result2)
192 if (self.fulfilled == False): 212 if (self.fulfilled == False):
193 fail = " FAIL!" 213 fail = " FAIL!"
194 op = " != " 214 op = " != "
195 else: 215 else:
196 fail = "" 216 fail = ""
197 op = " == " 217 op = " == "
198 if (((self.fulfilled == False) and (failed_only == True)) or (failed_only == False)): 218 if (((self.fulfilled == False) and (failed_only == True)) or (failed_only == False)):
199 print(self.peer.id[:4] + ' "' + self.subsystem.ljust(12) + '" "' + self.name.ljust(30) + '" == ' + str(self.result) +" " + self.peer2.id[:4] + ' "' + self.subsystem2.ljust(12) + '" '+ self.name2.ljust(30) + '" ' + str(self.result2)) 219 print(self.peer.id[:4] + ' "' + self.subsystem.ljust(12) + '" "' + self.name.ljust(30) + '" == ' + str(self.result) + " " + self.peer2.id[:4] + ' "' + self.subsystem2.ljust(12) + '" ' + self.name2.ljust(30) + '" ' + str(self.result2))
200 return self.fulfilled 220 return self.fulfilled
201 221
222
202class Test: 223class Test:
203 def __init__(self, testname, verbose): 224 def __init__(self, testname, verbose):
204 self.peers = list() 225 self.peers = list()
205 self.verbose = verbose; 226 self.verbose = verbose
206 self.name = testname; 227 self.name = testname
207 srcdir = "../.." 228 srcdir = "../.."
208 gnunet_pyexpect_dir = os.path.join (srcdir, "contrib/scripts") 229 gnunet_pyexpect_dir = os.path.join(srcdir, "contrib/scripts")
209 if gnunet_pyexpect_dir not in sys.path: 230 if gnunet_pyexpect_dir not in sys.path:
210 sys.path.append (gnunet_pyexpect_dir) 231 sys.path.append(gnunet_pyexpect_dir)
211 self.gnunetarm = '' 232 self.gnunetarm = ''
212 self.gnunetstatistics = '' 233 self.gnunetstatistics = ''
213 if os.name == 'posix': 234 if os.name == 'posix':
214 self.gnunetarm = 'gnunet-arm' 235 self.gnunetarm = 'gnunet-arm'
@@ -217,17 +238,20 @@ class Test:
217 elif os.name == 'nt': 238 elif os.name == 'nt':
218 self.gnunetarm = 'gnunet-arm.exe' 239 self.gnunetarm = 'gnunet-arm.exe'
219 self.gnunetstatistics = 'gnunet-statistics.exe' 240 self.gnunetstatistics = 'gnunet-statistics.exe'
220 self.gnunetpeerinfo = 'gnunet-peerinfo.exe' 241 self.gnunetpeerinfo = 'gnunet-peerinfo.exe'
221 if os.name == "nt": 242 if os.name == "nt":
222 shutil.rmtree (os.path.join (os.getenv ("TEMP"), testname), True) 243 shutil.rmtree(os.path.join(os.getenv("TEMP"), testname), True)
223 else: 244 else:
224 shutil.rmtree ("/tmp/" + testname, True) 245 shutil.rmtree("/tmp/" + testname, True)
225 def add_peer (self, peer): 246
247 def add_peer(self, peer):
226 self.peers.append(peer) 248 self.peers.append(peer)
227 def p (self, msg): 249
250 def p(self, msg):
228 if (self.verbose == True): 251 if (self.verbose == True):
229 print(msg) 252 print(msg)
230 253
254
231class Peer: 255class Peer:
232 def __init__(self, test, cfg_file): 256 def __init__(self, test, cfg_file):
233 if (False == os.path.isfile(cfg_file)): 257 if (False == os.path.isfile(cfg_file)):
@@ -235,53 +259,57 @@ class Peer:
235 self.id = "<NaN>" 259 self.id = "<NaN>"
236 self.test = test 260 self.test = test
237 self.started = False 261 self.started = False
238 self.cfg = cfg_file 262 self.cfg = cfg_file
263
239 def __del__(self): 264 def __del__(self):
240 if (self.started == True): 265 if (self.started == True):
241 print('ERROR! Peer using cfg ' + self.cfg + ' was not stopped') 266 print('ERROR! Peer using cfg ' + self.cfg + ' was not stopped')
242 ret = self.stop () 267 ret = self.stop()
243 if (False == ret): 268 if (False == ret):
244 print('ERROR! Peer using cfg ' + self.cfg + ' could not be stopped') 269 print('ERROR! Peer using cfg ' + self.cfg + ' could not be stopped')
245 self.started = False 270 self.started = False
246 return ret 271 return ret
247 else: 272 else:
248 return False 273 return False
249 def start (self): 274
250 self.test.p ("Starting peer using cfg " + self.cfg) 275 def start(self):
276 self.test.p("Starting peer using cfg " + self.cfg)
251 try: 277 try:
252 server = subprocess.Popen ([self.test.gnunetarm, '-sq', '-c', self.cfg]) 278 server = subprocess.Popen([self.test.gnunetarm, '-sq', '-c', self.cfg])
253 server.communicate () 279 server.communicate()
254 except OSError: 280 except OSError:
255 print("Can not start peer") 281 print("Can not start peer")
256 self.started = False 282 self.started = False
257 return False 283 return False
258 self.started = True; 284 self.started = True
259 test = '' 285 test = ''
260 try: 286 try:
261 server = pexpect () 287 server = pexpect()
262 server.spawn (None, [self.test.gnunetpeerinfo, '-c', self.cfg ,'-s'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 288 server.spawn(None, [self.test.gnunetpeerinfo, '-c', self.cfg, '-s'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
263 test = server.read("stdout", 1024) 289 test = server.read("stdout", 1024)
264 except OSError: 290 except OSError:
265 print("Can not get peer identity") 291 print("Can not get peer identity")
266 test = (test.split('`')[1]) 292 test = (test.split('`')[1])
267 self.id = test.split('\'')[0] 293 self.id = test.split('\'')[0]
268 return True 294 return True
269 def stop (self): 295
296 def stop(self):
270 if (self.started == False): 297 if (self.started == False):
271 return False 298 return False
272 self.test.p ("Stopping peer using cfg " + self.cfg) 299 self.test.p("Stopping peer using cfg " + self.cfg)
273 try: 300 try:
274 server = subprocess.Popen ([self.test.gnunetarm, '-eq', '-c', self.cfg]) 301 server = subprocess.Popen([self.test.gnunetarm, '-eq', '-c', self.cfg])
275 server.communicate () 302 server.communicate()
276 except OSError: 303 except OSError:
277 print("Can not stop peer") 304 print("Can not stop peer")
278 return False 305 return False
279 self.started = False 306 self.started = False
280 return True; 307 return True
281 def get_statistics_value (self, subsystem, name): 308
282 server = pexpect () 309 def get_statistics_value(self, subsystem, name):
283 server.spawn (None, [self.test.gnunetstatistics, '-c', self.cfg ,'-q','-n', name, '-s', subsystem ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 310 server = pexpect()
284 #server.expect ("stdout", re.compile (r"")) 311 server.spawn(None, [self.test.gnunetstatistics, '-c', self.cfg, '-q', '-n', name, '-s', subsystem], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
312 # server.expect ("stdout", re.compile (r""))
285 test = server.read("stdout", 10240) 313 test = server.read("stdout", 10240)
286 tests = test.partition('\n') 314 tests = test.partition('\n')
287 # On W32 GNUnet outputs with \r\n, rather than \n 315 # On W32 GNUnet outputs with \r\n, rather than \n
@@ -292,4 +320,3 @@ class Peer:
292 return tests 320 return tests
293 else: 321 else:
294 return -1 322 return -1
295