diff options
Diffstat (limited to 'src/integration-tests/gnunet_testing.py.in')
-rw-r--r-- | src/integration-tests/gnunet_testing.py.in | 104 |
1 files changed, 56 insertions, 48 deletions
diff --git a/src/integration-tests/gnunet_testing.py.in b/src/integration-tests/gnunet_testing.py.in index 0d02a792f..c9342ecf0 100644 --- a/src/integration-tests/gnunet_testing.py.in +++ b/src/integration-tests/gnunet_testing.py.in | |||
@@ -11,22 +11,34 @@ | |||
11 | # WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | # Affero General Public License for more details. | 13 | # Affero General Public License for more details. |
14 | # | 14 | # |
15 | # You should have received a copy of the GNU Affero General Public License | 15 | # You should have received a copy of the GNU Affero General Public License |
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | # | 17 | # |
18 | # SPDX-License-Identifier: AGPL3.0-or-later | 18 | # SPDX-License-Identifier: AGPL3.0-or-later |
19 | # | 19 | # |
20 | # Functions for integration testing | 20 | # Functions for integration testing |
21 | from __future__ import unicode_literals | ||
22 | from __future__ import print_function | ||
23 | from builtins import object | ||
24 | from builtins import str | ||
21 | import os | 25 | import os |
22 | import subprocess | 26 | import subprocess |
23 | import sys | 27 | import sys |
24 | import shutil | 28 | import shutil |
25 | import time | 29 | import time |
26 | from gnunet_pyexpect import pexpect | 30 | from gnunet_pyexpect import pexpect |
31 | import logging | ||
27 | 32 | ||
33 | logger = logging.getLogger() | ||
34 | handler = logging.StreamHandler() | ||
35 | formatter = logging.Formatter( | ||
36 | '%(asctime)s %(name)-12s %(levelname)-8s %(message)s') | ||
37 | handler.setFormatter(formatter) | ||
38 | logger.addHandler(handler) | ||
39 | logger.setLevel(logging.DEBUG) | ||
28 | 40 | ||
29 | class Check: | 41 | class Check(object): |
30 | def __init__(self, test): | 42 | def __init__(self, test): |
31 | self.fulfilled = False | 43 | self.fulfilled = False |
32 | self.conditions = list() | 44 | self.conditions = list() |
@@ -55,7 +67,7 @@ class Check: | |||
55 | time.sleep(1) | 67 | time.sleep(1) |
56 | execs += 1 | 68 | execs += 1 |
57 | if ((False == res) and (execs >= timeout)): | 69 | if ((False == res) and (execs >= timeout)): |
58 | print(('Check had timeout after ' + str(timeout) + ' seconds')) | 70 | logger.debug('Check had timeout after %s seconds', str(timeout)) |
59 | neg_cont(self) | 71 | neg_cont(self) |
60 | elif ((False == res) and (execs < timeout)): | 72 | elif ((False == res) and (execs < timeout)): |
61 | if (None != neg_cont): | 73 | if (None != neg_cont): |
@@ -83,7 +95,7 @@ class Check: | |||
83 | neg += 1 | 95 | neg += 1 |
84 | else: | 96 | else: |
85 | pos += 1 | 97 | pos += 1 |
86 | print((str(pos) + ' out of ' + str(pos+neg) + ' conditions fulfilled')) | 98 | logger.debug('%s out of %s conditions fulfilled', str(pos), str(pos+neg)) |
87 | return self.fulfilled | 99 | return self.fulfilled |
88 | 100 | ||
89 | def reset(self): | 101 | def reset(self): |
@@ -92,7 +104,7 @@ class Check: | |||
92 | c.fulfilled = False | 104 | c.fulfilled = False |
93 | 105 | ||
94 | 106 | ||
95 | class Condition: | 107 | class Condition(object): |
96 | def __init__(self): | 108 | def __init__(self): |
97 | self.fulfilled = False | 109 | self.fulfilled = False |
98 | self.type = 'generic' | 110 | self.type = 'generic' |
@@ -106,9 +118,9 @@ class Condition: | |||
106 | 118 | ||
107 | def evaluate(self, failed_only): | 119 | def evaluate(self, failed_only): |
108 | if ((self.fulfilled == False) and (failed_only == True)): | 120 | if ((self.fulfilled == False) and (failed_only == True)): |
109 | print(str(self.type) + 'condition for was ' + str(self.fulfilled)) | 121 | logger.debug('%s condition for was %s', str(self.type), str(self.fulfilled)) |
110 | elif (failed_only == False): | 122 | elif (failed_only == False): |
111 | print(str(self.type) + 'condition for was ' + str(self.fulfilled)) | 123 | logger.debug('%s condition for was %s', str(self.type), str(self.fulfilled)) |
112 | return self.fulfilled | 124 | return self.fulfilled |
113 | 125 | ||
114 | 126 | ||
@@ -131,26 +143,26 @@ class FileExistCondition(Condition): | |||
131 | 143 | ||
132 | def evaluate(self, failed_only): | 144 | def evaluate(self, failed_only): |
133 | if ((self.fulfilled == False) and (failed_only == True)): | 145 | if ((self.fulfilled == False) and (failed_only == True)): |
134 | print(str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled)) | 146 | logger.debug('%s confition for file %s was %s', str(self.type), self.file, str(self.fulfilled)) |
135 | elif (failed_only == False): | 147 | elif (failed_only == False): |
136 | print(str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled)) | 148 | logger.debug('%s confition for file %s was %s', str(self.type), self.file, str(self.fulfilled)) |
137 | return self.fulfilled | 149 | return self.fulfilled |
138 | 150 | ||
139 | 151 | ||
140 | class StatisticsCondition (Condition): | 152 | class StatisticsCondition(Condition): |
141 | def __init__(self, peer, subsystem, name, value): | 153 | def __init__(self, peer, subsystem, name, value): |
142 | self.fulfilled = False | 154 | self.fulfilled = False |
143 | self.type = 'statistics' | 155 | self.type = 'statistics' |
144 | self.peer = peer | 156 | self.peer = peer |
145 | self.subsystem = subsystem | 157 | self.subsystem = subsystem |
146 | self.name = name | 158 | self.name = name |
147 | self.value = value | 159 | self.value = str(value) |
148 | self.result = -1 | 160 | self.result = -1 |
149 | 161 | ||
150 | def check(self): | 162 | def check(self): |
151 | if (self.fulfilled == False): | 163 | if (self.fulfilled == False): |
152 | self.result = self.peer.get_statistics_value(self.subsystem, self.name) | 164 | self.result = self.peer.get_statistics_value(self.subsystem, self.name) |
153 | if (str(self.result) == str(self.value)): | 165 | if (self.result == self.value): |
154 | self.fulfilled = True | 166 | self.fulfilled = True |
155 | return True | 167 | return True |
156 | else: | 168 | else: |
@@ -159,10 +171,6 @@ class StatisticsCondition (Condition): | |||
159 | return True | 171 | return True |
160 | 172 | ||
161 | def evaluate(self, failed_only): | 173 | def evaluate(self, failed_only): |
162 | if (self.result == -1): | ||
163 | res = 'NaN' | ||
164 | else: | ||
165 | res = str(self.result) | ||
166 | if (self.fulfilled == False): | 174 | if (self.fulfilled == False): |
167 | fail = " FAIL!" | 175 | fail = " FAIL!" |
168 | op = " != " | 176 | op = " != " |
@@ -170,12 +178,12 @@ class StatisticsCondition (Condition): | |||
170 | fail = "" | 178 | fail = "" |
171 | op = " == " | 179 | op = " == " |
172 | if (((self.fulfilled == False) and (failed_only == True)) or (failed_only == False)): | 180 | if (((self.fulfilled == False) and (failed_only == True)) or (failed_only == False)): |
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) | 181 | logger.debug('%s %s condition in subsystem %s: %s: (expected/real value) %s %s %s %s', self.peer.id[:4].decode("utf-8"), self.peer.cfg, self.subsystem.ljust(12), self.name.ljust(30), self.value, op, self.result, fail) |
174 | return self.fulfilled | 182 | return self.fulfilled |
175 | 183 | ||
176 | 184 | ||
177 | # Specify two statistic values and check if they are equal | 185 | # Specify two statistic values and check if they are equal |
178 | class EqualStatisticsCondition (Condition): | 186 | class EqualStatisticsCondition(Condition): |
179 | def __init__(self, peer, subsystem, name, peer2, subsystem2, name2): | 187 | def __init__(self, peer, subsystem, name, peer2, subsystem2, name2): |
180 | self.fulfilled = False | 188 | self.fulfilled = False |
181 | self.type = 'equalstatistics' | 189 | self.type = 'equalstatistics' |
@@ -192,7 +200,7 @@ class EqualStatisticsCondition (Condition): | |||
192 | if (self.fulfilled == False): | 200 | if (self.fulfilled == False): |
193 | self.result = self.peer.get_statistics_value(self.subsystem, self.name) | 201 | self.result = self.peer.get_statistics_value(self.subsystem, self.name) |
194 | self.result2 = self.peer2.get_statistics_value(self.subsystem2, self.name2) | 202 | self.result2 = self.peer2.get_statistics_value(self.subsystem2, self.name2) |
195 | if (str(self.result) == str(self.result2)): | 203 | if (self.result == self.result2): |
196 | self.fulfilled = True | 204 | self.fulfilled = True |
197 | return True | 205 | return True |
198 | else: | 206 | else: |
@@ -201,26 +209,12 @@ class EqualStatisticsCondition (Condition): | |||
201 | return True | 209 | return True |
202 | 210 | ||
203 | def evaluate(self, failed_only): | 211 | def evaluate(self, failed_only): |
204 | if (self.result == -1): | ||
205 | res = 'NaN' | ||
206 | else: | ||
207 | res = str(self.result) | ||
208 | if (self.result2 == -1): | ||
209 | res2 = 'NaN' | ||
210 | else: | ||
211 | res2 = str(self.result2) | ||
212 | if (self.fulfilled == False): | ||
213 | fail = " FAIL!" | ||
214 | op = " != " | ||
215 | else: | ||
216 | fail = "" | ||
217 | op = " == " | ||
218 | if (((self.fulfilled == False) and (failed_only == True)) or (failed_only == False)): | 212 | if (((self.fulfilled == False) and (failed_only == True)) or (failed_only == False)): |
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)) | 213 | logger.debug('%s %s %s == %s %s %s %s %s', self.peer.id[:4], self.subsystem.ljust(12), self.name.ljust(30), self.result, self.peer2.id[:4], self.subsystem2.ljust(12), self.name2.ljust(30), self.result2) |
220 | return self.fulfilled | 214 | return self.fulfilled |
221 | 215 | ||
222 | 216 | ||
223 | class Test: | 217 | class Test(object): |
224 | def __init__(self, testname, verbose): | 218 | def __init__(self, testname, verbose): |
225 | self.peers = list() | 219 | self.peers = list() |
226 | self.verbose = verbose | 220 | self.verbose = verbose |
@@ -252,10 +246,11 @@ class Test: | |||
252 | print(msg) | 246 | print(msg) |
253 | 247 | ||
254 | 248 | ||
255 | class Peer: | 249 | class Peer(object): |
256 | def __init__(self, test, cfg_file): | 250 | def __init__(self, test, cfg_file): |
257 | if (False == os.path.isfile(cfg_file)): | 251 | if (False == os.path.isfile(cfg_file)): |
258 | print(("Peer cfg " + cfg_file + ": FILE NOT FOUND")) | 252 | # print(("Peer cfg " + cfg_file + ": FILE NOT FOUND")) |
253 | logger.debug('Peer cfg %s : FILE NOT FOUND', cfg_file) | ||
259 | self.id = "<NaN>" | 254 | self.id = "<NaN>" |
260 | self.test = test | 255 | self.test = test |
261 | self.started = False | 256 | self.started = False |
@@ -263,22 +258,30 @@ class Peer: | |||
263 | 258 | ||
264 | def __del__(self): | 259 | def __del__(self): |
265 | if (self.started == True): | 260 | if (self.started == True): |
266 | print('ERROR! Peer using cfg ' + self.cfg + ' was not stopped') | 261 | # print('ERROR! Peer using cfg ' + self.cfg + ' was not stopped') |
262 | logger.debug('ERROR! Peer using cfg %s was not stopped', self.cfg) | ||
267 | ret = self.stop() | 263 | ret = self.stop() |
268 | if (False == ret): | 264 | if (False == ret): |
269 | print('ERROR! Peer using cfg ' + self.cfg + ' could not be stopped') | 265 | # print('ERROR! Peer using cfg ' + |
266 | # self.cfg + | ||
267 | # ' could not be stopped') | ||
268 | logger.debug('ERROR! Peer using cfg %s could not be stopped', self.cfg) | ||
270 | self.started = False | 269 | self.started = False |
271 | return ret | 270 | return ret |
272 | else: | 271 | else: |
273 | return False | 272 | return False |
274 | 273 | ||
275 | def start(self): | 274 | def start(self): |
275 | os.unsetenv ("XDG_CONFIG_HOME") | ||
276 | os.unsetenv ("XDG_DATA_HOME") | ||
277 | os.unsetenv ("XDG_CACHE_HOME") | ||
276 | self.test.p("Starting peer using cfg " + self.cfg) | 278 | self.test.p("Starting peer using cfg " + self.cfg) |
277 | try: | 279 | try: |
278 | server = subprocess.Popen([self.test.gnunetarm, '-sq', '-c', self.cfg]) | 280 | server = subprocess.Popen([self.test.gnunetarm, '-sq', '-c', self.cfg]) |
279 | server.communicate() | 281 | server.communicate() |
280 | except OSError: | 282 | except OSError: |
281 | print("Can not start peer") | 283 | # print("Can not start peer") |
284 | logger.debug('Can not start peer') | ||
282 | self.started = False | 285 | self.started = False |
283 | return False | 286 | return False |
284 | self.started = True | 287 | self.started = True |
@@ -288,9 +291,10 @@ class Peer: | |||
288 | server.spawn(None, [self.test.gnunetpeerinfo, '-c', self.cfg, '-s'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 291 | server.spawn(None, [self.test.gnunetpeerinfo, '-c', self.cfg, '-s'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
289 | test = server.read("stdout", 1024) | 292 | test = server.read("stdout", 1024) |
290 | except OSError: | 293 | except OSError: |
291 | print("Can not get peer identity") | 294 | # print("Can not get peer identity") |
292 | test = (test.split('`')[1]) | 295 | logger.debug('Can not get peer identity') |
293 | self.id = test.split('\'')[0] | 296 | test = (test.split(b'`')[1]) |
297 | self.id = test.split(b'\'')[0] | ||
294 | return True | 298 | return True |
295 | 299 | ||
296 | def stop(self): | 300 | def stop(self): |
@@ -301,7 +305,8 @@ class Peer: | |||
301 | server = subprocess.Popen([self.test.gnunetarm, '-eq', '-c', self.cfg]) | 305 | server = subprocess.Popen([self.test.gnunetarm, '-eq', '-c', self.cfg]) |
302 | server.communicate() | 306 | server.communicate() |
303 | except OSError: | 307 | except OSError: |
304 | print("Can not stop peer") | 308 | # print("Can not stop peer") |
309 | logger.debug('Can not stop peer') | ||
305 | return False | 310 | return False |
306 | self.started = False | 311 | self.started = False |
307 | return True | 312 | return True |
@@ -311,12 +316,15 @@ class Peer: | |||
311 | server.spawn(None, [self.test.gnunetstatistics, '-c', self.cfg, '-q', '-n', name, '-s', subsystem], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 316 | 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"")) | 317 | # server.expect ("stdout", re.compile (r"")) |
313 | test = server.read("stdout", 10240) | 318 | test = server.read("stdout", 10240) |
314 | tests = test.partition('\n') | 319 | tests = test.partition(b'\n') |
315 | # On W32 GNUnet outputs with \r\n, rather than \n | 320 | # On W32 GNUnet outputs with \r\n, rather than \n |
316 | if os.name == 'nt' and tests[1] == '\n' and tests[0][-1] == '\r': | 321 | if os.name == 'nt' and tests[1] == b'\n' and tests[0][-1] == b'\r': |
317 | tests = (tests[0][:-1], tests[1], tests[2]) | 322 | tests = (tests[0][:-1], tests[1], tests[2]) |
318 | tests = tests[0] | 323 | tests = tests[0] |
319 | if (tests.isdigit() == True): | 324 | result = tests.decode("utf-8").strip() |
320 | return tests | 325 | logger.debug('running gnunet-statistics %s for %s "/" %s yields %s', self.cfg, name, subsystem, result) |
326 | if (result.isdigit() == True): | ||
327 | return result | ||
321 | else: | 328 | else: |
329 | logger.debug('Invalid statistics value: %s is not a number!', result) | ||
322 | return -1 | 330 | return -1 |