2 # This file is part of GNUnet.
3 # (C) 2010, 2017, 2018 Christian Grothoff (and other contributing authors)
5 # GNUnet is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Affero General Public License as published
7 # by the Free Software Foundation, either version 3 of the License,
8 # or (at your option) any later version.
10 # GNUnet is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Affero General Public License for more details.
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/>.
18 # SPDX-License-Identifier: AGPL3.0-or-later
20 # Functions for integration testing
27 from gnunet_pyexpect import pexpect
30 logger = logging.getLogger()
31 handler = logging.StreamHandler()
32 formatter = logging.Formatter(
33 '%(asctime)s %(name)-12s %(levelname)-8s %(message)s'
35 handler.setFormatter(formatter)
36 logger.addHandler(handler)
37 logger.setLevel(logging.DEBUG)
41 def __init__(self, test):
42 self.fulfilled = False
43 self.conditions = list()
46 def add(self, condition):
47 self.conditions.append(condition)
53 for c in self.conditions:
54 if (False == c.check()):
61 def run_blocking(self, timeout, pos_cont, neg_cont):
64 while ((False == res) and (execs < timeout)):
68 if ((False == res) and (execs >= timeout)):
69 logger.debug('Check had timeout after %s seconds', str(timeout))
71 elif ((False == res) and (execs < timeout)):
72 if (None != neg_cont):
75 if (None != pos_cont):
79 def run_once(self, pos_cont, neg_cont):
83 if ((res == False) and (neg_cont != None)):
85 if ((res == True) and (pos_cont != None)):
89 def evaluate(self, failed_only):
92 for c in self.conditions:
93 if (False == c.evaluate(failed_only)):
98 '%s out of %s conditions fulfilled', str(pos), str(pos + neg)
100 return self.fulfilled
103 self.fulfilled = False
104 for c in self.conditions:
108 class Condition(object):
110 self.fulfilled = False
111 self.type = 'generic'
113 def __init__(self, type):
114 self.fulfilled = False
120 def evaluate(self, failed_only):
121 if ((self.fulfilled == False) and (failed_only == True)):
123 '%s condition for was %s', str(self.type), str(self.fulfilled)
125 elif (failed_only == False):
127 '%s condition for was %s', str(self.type), str(self.fulfilled)
129 return self.fulfilled
132 class FileExistCondition(Condition):
133 def __init__(self, file):
134 self.fulfilled = False
139 if (self.fulfilled == False):
140 res = os.path.isfile(self.file)
142 self.fulfilled = True
149 def evaluate(self, failed_only):
150 if ((self.fulfilled == False) and (failed_only == True)):
152 '%s confition for file %s was %s', str(self.type), self.file,
155 elif (failed_only == False):
157 '%s confition for file %s was %s', str(self.type), self.file,
160 return self.fulfilled
163 class StatisticsCondition(Condition):
164 def __init__(self, peer, subsystem, name, value):
165 self.fulfilled = False
166 self.type = 'statistics'
168 self.subsystem = subsystem
170 self.value = str(value)
174 if (self.fulfilled == False):
175 self.result = self.peer.get_statistics_value(
176 self.subsystem, self.name
178 if (self.result == self.value):
179 self.fulfilled = True
186 def evaluate(self, failed_only):
187 if (self.fulfilled == False):
193 if (((self.fulfilled == False) and (failed_only == True))
194 or (failed_only == False)):
196 '%s %s condition in subsystem %s: %s: (expected/real value) %s %s %s %s',
197 self.peer.id[:4].decode("utf-8"), self.peer.cfg,
198 self.subsystem.ljust(12), self.name.ljust(30), self.value, op,
201 return self.fulfilled
204 # Specify two statistic values and check if they are equal
205 class EqualStatisticsCondition(Condition):
206 def __init__(self, peer, subsystem, name, peer2, subsystem2, name2):
207 self.fulfilled = False
208 self.type = 'equalstatistics'
210 self.subsystem = subsystem
214 self.subsystem2 = subsystem2
219 if (self.fulfilled == False):
220 self.result = self.peer.get_statistics_value(
221 self.subsystem, self.name
223 self.result2 = self.peer2.get_statistics_value(
224 self.subsystem2, self.name2
226 if (self.result == self.result2):
227 self.fulfilled = True
234 def evaluate(self, failed_only):
235 if (((self.fulfilled == False) and (failed_only == True))
236 or (failed_only == False)):
238 '%s %s %s == %s %s %s %s %s', self.peer.id[:4],
239 self.subsystem.ljust(12), self.name.ljust(30), self.result,
240 self.peer2.id[:4], self.subsystem2.ljust(12),
241 self.name2.ljust(30), self.result2
243 return self.fulfilled
247 def __init__(self, testname, verbose):
249 self.verbose = verbose
252 gnunet_pyexpect_dir = os.path.join(srcdir, "contrib/scripts")
253 if gnunet_pyexpect_dir not in sys.path:
254 sys.path.append(gnunet_pyexpect_dir)
256 self.gnunetstatistics = ''
257 if os.name == 'posix':
258 self.gnunetarm = 'gnunet-arm'
259 self.gnunetstatistics = 'gnunet-statistics'
260 self.gnunetpeerinfo = 'gnunet-peerinfo'
261 elif os.name == 'nt':
262 self.gnunetarm = 'gnunet-arm.exe'
263 self.gnunetstatistics = 'gnunet-statistics.exe'
264 self.gnunetpeerinfo = 'gnunet-peerinfo.exe'
266 shutil.rmtree(os.path.join(os.getenv("TEMP"), testname), True)
268 shutil.rmtree("/tmp/" + testname, True)
270 def add_peer(self, peer):
271 self.peers.append(peer)
274 if (self.verbose == True):
279 def __init__(self, test, cfg_file):
280 if (False == os.path.isfile(cfg_file)):
281 # print(("Peer cfg " + cfg_file + ": FILE NOT FOUND"))
282 logger.debug('Peer cfg %s : FILE NOT FOUND', cfg_file)
289 if (self.started == True):
290 # print('ERROR! Peer using cfg ' + self.cfg + ' was not stopped')
291 logger.debug('ERROR! Peer using cfg %s was not stopped', self.cfg)
294 # print('ERROR! Peer using cfg ' +
296 # ' could not be stopped')
298 'ERROR! Peer using cfg %s could not be stopped', self.cfg
306 os.unsetenv("XDG_CONFIG_HOME")
307 os.unsetenv("XDG_DATA_HOME")
308 os.unsetenv("XDG_CACHE_HOME")
309 self.test.p("Starting peer using cfg " + self.cfg)
311 server = subprocess.Popen([
312 self.test.gnunetarm, '-sq', '-c', self.cfg
316 # print("Can not start peer")
317 logger.debug('Can not start peer')
325 None, [self.test.gnunetpeerinfo, '-c', self.cfg, '-s'],
326 stdout=subprocess.PIPE,
327 stderr=subprocess.STDOUT
329 test = server.read("stdout", 1024)
331 # print("Can not get peer identity")
332 logger.debug('Can not get peer identity')
333 test = (test.split(b'`')[1])
334 self.id = test.split(b'\'')[0]
338 if (self.started == False):
340 self.test.p("Stopping peer using cfg " + self.cfg)
342 server = subprocess.Popen([
343 self.test.gnunetarm, '-eq', '-c', self.cfg
347 # print("Can not stop peer")
348 logger.debug('Can not stop peer')
353 def get_statistics_value(self, subsystem, name):
357 self.test.gnunetstatistics, '-c', self.cfg, '-q', '-n', name,
360 stdout=subprocess.PIPE,
361 stderr=subprocess.STDOUT
363 # server.expect ("stdout", re.compile (r""))
364 test = server.read("stdout", 10240)
365 tests = test.partition(b'\n')
366 # On W32 GNUnet outputs with \r\n, rather than \n
367 if os.name == 'nt' and tests[1] == b'\n' and tests[0][-1] == b'\r':
368 tests = (tests[0][:-1], tests[1], tests[2])
370 result = tests.decode("utf-8").strip()
372 'running gnunet-statistics %s for %s "/" %s yields %s', self.cfg,
373 name, subsystem, result
375 if (result.isdigit() == True):
379 'Invalid statistics value: %s is not a number!', result