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')
34 handler.setFormatter(formatter)
35 logger.addHandler(handler)
36 logger.setLevel(logging.DEBUG)
39 def __init__(self, test):
40 self.fulfilled = False
41 self.conditions = list()
44 def add(self, condition):
45 self.conditions.append(condition)
51 for c in self.conditions:
52 if (False == c.check()):
59 def run_blocking(self, timeout, pos_cont, neg_cont):
62 while ((False == res) and (execs < timeout)):
66 if ((False == res) and (execs >= timeout)):
67 logger.debug('Check had timeout after %s seconds', str(timeout))
69 elif ((False == res) and (execs < timeout)):
70 if (None != neg_cont):
73 if (None != pos_cont):
77 def run_once(self, pos_cont, neg_cont):
81 if ((res == False) and (neg_cont != None)):
83 if ((res == True) and (pos_cont != None)):
87 def evaluate(self, failed_only):
90 for c in self.conditions:
91 if (False == c.evaluate(failed_only)):
95 logger.debug('%s out of %s conditions fulfilled', str(pos), str(pos+neg))
99 self.fulfilled = False
100 for c in self.conditions:
104 class Condition(object):
106 self.fulfilled = False
107 self.type = 'generic'
109 def __init__(self, type):
110 self.fulfilled = False
116 def evaluate(self, failed_only):
117 if ((self.fulfilled == False) and (failed_only == True)):
118 logger.debug('%s condition for was %s', str(self.type), str(self.fulfilled))
119 elif (failed_only == False):
120 logger.debug('%s condition for was %s', str(self.type), str(self.fulfilled))
121 return self.fulfilled
124 class FileExistCondition(Condition):
125 def __init__(self, file):
126 self.fulfilled = False
131 if (self.fulfilled == False):
132 res = os.path.isfile(self.file)
134 self.fulfilled = True
141 def evaluate(self, failed_only):
142 if ((self.fulfilled == False) and (failed_only == True)):
143 logger.debug('%s confition for file %s was %s', str(self.type), self.file, str(self.fulfilled))
144 elif (failed_only == False):
145 logger.debug('%s confition for file %s was %s', str(self.type), self.file, str(self.fulfilled))
146 return self.fulfilled
149 class StatisticsCondition(Condition):
150 def __init__(self, peer, subsystem, name, value):
151 self.fulfilled = False
152 self.type = 'statistics'
154 self.subsystem = subsystem
156 self.value = str(value)
160 if (self.fulfilled == False):
161 self.result = self.peer.get_statistics_value(self.subsystem, self.name)
162 if (self.result == self.value):
163 self.fulfilled = True
170 def evaluate(self, failed_only):
171 if (self.fulfilled == False):
177 if (((self.fulfilled == False) and (failed_only == True)) or (failed_only == False)):
178 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)
179 return self.fulfilled
182 # Specify two statistic values and check if they are equal
183 class EqualStatisticsCondition(Condition):
184 def __init__(self, peer, subsystem, name, peer2, subsystem2, name2):
185 self.fulfilled = False
186 self.type = 'equalstatistics'
188 self.subsystem = subsystem
192 self.subsystem2 = subsystem2
197 if (self.fulfilled == False):
198 self.result = self.peer.get_statistics_value(self.subsystem, self.name)
199 self.result2 = self.peer2.get_statistics_value(self.subsystem2, self.name2)
200 if (self.result == self.result2):
201 self.fulfilled = True
208 def evaluate(self, failed_only):
209 if (((self.fulfilled == False) and (failed_only == True)) or (failed_only == False)):
210 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)
211 return self.fulfilled
215 def __init__(self, testname, verbose):
217 self.verbose = verbose
220 gnunet_pyexpect_dir = os.path.join(srcdir, "contrib/scripts")
221 if gnunet_pyexpect_dir not in sys.path:
222 sys.path.append(gnunet_pyexpect_dir)
224 self.gnunetstatistics = ''
225 if os.name == 'posix':
226 self.gnunetarm = 'gnunet-arm'
227 self.gnunetstatistics = 'gnunet-statistics'
228 self.gnunetpeerinfo = 'gnunet-peerinfo'
229 elif os.name == 'nt':
230 self.gnunetarm = 'gnunet-arm.exe'
231 self.gnunetstatistics = 'gnunet-statistics.exe'
232 self.gnunetpeerinfo = 'gnunet-peerinfo.exe'
234 shutil.rmtree(os.path.join(os.getenv("TEMP"), testname), True)
236 shutil.rmtree("/tmp/" + testname, True)
238 def add_peer(self, peer):
239 self.peers.append(peer)
242 if (self.verbose == True):
247 def __init__(self, test, cfg_file):
248 if (False == os.path.isfile(cfg_file)):
249 # print(("Peer cfg " + cfg_file + ": FILE NOT FOUND"))
250 logger.debug('Peer cfg %s : FILE NOT FOUND', cfg_file)
257 if (self.started == True):
258 # print('ERROR! Peer using cfg ' + self.cfg + ' was not stopped')
259 logger.debug('ERROR! Peer using cfg %s was not stopped', self.cfg)
262 # print('ERROR! Peer using cfg ' +
264 # ' could not be stopped')
265 logger.debug('ERROR! Peer using cfg %s could not be stopped', self.cfg)
272 os.unsetenv ("XDG_CONFIG_HOME")
273 os.unsetenv ("XDG_DATA_HOME")
274 os.unsetenv ("XDG_CACHE_HOME")
275 self.test.p("Starting peer using cfg " + self.cfg)
277 server = subprocess.Popen([self.test.gnunetarm, '-sq', '-c', self.cfg])
280 # print("Can not start peer")
281 logger.debug('Can not start peer')
288 server.spawn(None, [self.test.gnunetpeerinfo, '-c', self.cfg, '-s'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
289 test = server.read("stdout", 1024)
291 # print("Can not get peer identity")
292 logger.debug('Can not get peer identity')
293 test = (test.split(b'`')[1])
294 self.id = test.split(b'\'')[0]
298 if (self.started == False):
300 self.test.p("Stopping peer using cfg " + self.cfg)
302 server = subprocess.Popen([self.test.gnunetarm, '-eq', '-c', self.cfg])
305 # print("Can not stop peer")
306 logger.debug('Can not stop peer')
311 def get_statistics_value(self, subsystem, name):
313 server.spawn(None, [self.test.gnunetstatistics, '-c', self.cfg, '-q', '-n', name, '-s', subsystem], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
314 # server.expect ("stdout", re.compile (r""))
315 test = server.read("stdout", 10240)
316 tests = test.partition(b'\n')
317 # On W32 GNUnet outputs with \r\n, rather than \n
318 if os.name == 'nt' and tests[1] == b'\n' and tests[0][-1] == b'\r':
319 tests = (tests[0][:-1], tests[1], tests[2])
321 result = tests.decode("utf-8").strip()
322 logger.debug('running gnunet-statistics %s for %s "/" %s yields %s', self.cfg, name, subsystem, result)
323 if (result.isdigit() == True):
326 logger.debug('Invalid statistics value: %s is not a number!', result)