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
6 # it under the terms of the GNU General Public License as published
7 # by the Free Software Foundation; either version 2, or (at your
8 # 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 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with GNUnet; see the file COPYING. If not, write to the
17 # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 # Boston, MA 02110-1301, USA.
20 # Functions for integration testing
26 from gnunet_pyexpect import pexpect
30 def __init__(self, test):
31 self.fulfilled = False
32 self.conditions = list()
35 def add(self, condition):
36 self.conditions.append(condition)
42 for c in self.conditions:
43 if (False == c.check()):
50 def run_blocking(self, timeout, pos_cont, neg_cont):
53 while ((False == res) and (execs < timeout)):
57 if ((False == res) and (execs >= timeout)):
58 print(('Check had timeout after ' + str(timeout) + ' seconds'))
60 elif ((False == res) and (execs < timeout)):
61 if (None != neg_cont):
64 if (None != pos_cont):
68 def run_once(self, pos_cont, neg_cont):
72 if ((res == False) and (neg_cont != None)):
74 if ((res == True) and (pos_cont != None)):
78 def evaluate(self, failed_only):
81 for c in self.conditions:
82 if (False == c.evaluate(failed_only)):
86 print((str(pos) + ' out of ' + str(pos+neg) + ' conditions fulfilled'))
90 self.fulfilled = False
91 for c in self.conditions:
97 self.fulfilled = False
100 def __init__(self, type):
101 self.fulfilled = False
107 def evaluate(self, failed_only):
108 if ((self.fulfilled == False) and (failed_only == True)):
109 print(str(self.type) + 'condition for was ' + str(self.fulfilled))
110 elif (failed_only == False):
111 print(str(self.type) + 'condition for was ' + str(self.fulfilled))
112 return self.fulfilled
115 class FileExistCondition(Condition):
116 def __init__(self, file):
117 self.fulfilled = False
122 if (self.fulfilled == False):
123 res = os.path.isfile(self.file)
125 self.fulfilled = True
132 def evaluate(self, failed_only):
133 if ((self.fulfilled == False) and (failed_only == True)):
134 print(str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled))
135 elif (failed_only == False):
136 print(str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled))
137 return self.fulfilled
140 class StatisticsCondition (Condition):
141 def __init__(self, peer, subsystem, name, value):
142 self.fulfilled = False
143 self.type = 'statistics'
145 self.subsystem = subsystem
151 if (self.fulfilled == False):
152 self.result = self.peer.get_statistics_value(self.subsystem, self.name)
153 if (str(self.result) == str(self.value)):
154 self.fulfilled = True
161 def evaluate(self, failed_only):
162 if (self.result == -1):
165 res = str(self.result)
166 if (self.fulfilled == False):
172 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)
174 return self.fulfilled
177 # Specify two statistic values and check if they are equal
178 class EqualStatisticsCondition (Condition):
179 def __init__(self, peer, subsystem, name, peer2, subsystem2, name2):
180 self.fulfilled = False
181 self.type = 'equalstatistics'
183 self.subsystem = subsystem
187 self.subsystem2 = subsystem2
192 if (self.fulfilled == False):
193 self.result = self.peer.get_statistics_value(self.subsystem, self.name)
194 self.result2 = self.peer2.get_statistics_value(self.subsystem2, self.name2)
195 if (str(self.result) == str(self.result2)):
196 self.fulfilled = True
203 def evaluate(self, failed_only):
204 if (self.result == -1):
207 res = str(self.result)
208 if (self.result2 == -1):
211 res2 = str(self.result2)
212 if (self.fulfilled == False):
218 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))
220 return self.fulfilled
224 def __init__(self, testname, verbose):
226 self.verbose = verbose
229 gnunet_pyexpect_dir = os.path.join(srcdir, "contrib/scripts")
230 if gnunet_pyexpect_dir not in sys.path:
231 sys.path.append(gnunet_pyexpect_dir)
233 self.gnunetstatistics = ''
234 if os.name == 'posix':
235 self.gnunetarm = 'gnunet-arm'
236 self.gnunetstatistics = 'gnunet-statistics'
237 self.gnunetpeerinfo = 'gnunet-peerinfo'
238 elif os.name == 'nt':
239 self.gnunetarm = 'gnunet-arm.exe'
240 self.gnunetstatistics = 'gnunet-statistics.exe'
241 self.gnunetpeerinfo = 'gnunet-peerinfo.exe'
243 shutil.rmtree(os.path.join(os.getenv("TEMP"), testname), True)
245 shutil.rmtree("/tmp/" + testname, True)
247 def add_peer(self, peer):
248 self.peers.append(peer)
251 if (self.verbose == True):
256 def __init__(self, test, cfg_file):
257 if (False == os.path.isfile(cfg_file)):
258 print(("Peer cfg " + cfg_file + ": FILE NOT FOUND"))
265 if (self.started == True):
266 print('ERROR! Peer using cfg ' + self.cfg + ' was not stopped')
269 print('ERROR! Peer using cfg ' + self.cfg + ' could not be stopped')
276 self.test.p("Starting peer using cfg " + self.cfg)
278 server = subprocess.Popen([self.test.gnunetarm, '-sq', '-c', self.cfg])
281 print("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 test = (test.split('`')[1])
293 self.id = test.split('\'')[0]
297 if (self.started == False):
299 self.test.p("Stopping peer using cfg " + self.cfg)
301 server = subprocess.Popen([self.test.gnunetarm, '-eq', '-c', self.cfg])
304 print("Can not stop peer")
309 def get_statistics_value(self, subsystem, name):
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""))
313 test = server.read("stdout", 10240)
314 tests = test.partition('\n')
315 # On W32 GNUnet outputs with \r\n, rather than \n
316 if os.name == 'nt' and tests[1] == '\n' and tests[0][-1] == '\r':
317 tests = (tests[0][:-1], tests[1], tests[2])
319 if (tests.isdigit() == True):