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 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 # Functions for integration testing
21 from gnunet_pyexpect import pexpect
25 def __init__(self, test):
26 self.fulfilled = False
27 self.conditions = list()
30 def add(self, condition):
31 self.conditions.append(condition)
37 for c in self.conditions:
38 if (False == c.check()):
45 def run_blocking(self, timeout, pos_cont, neg_cont):
48 while ((False == res) and (execs < timeout)):
52 if ((False == res) and (execs >= timeout)):
53 print(('Check had timeout after ' + str(timeout) + ' seconds'))
55 elif ((False == res) and (execs < timeout)):
56 if (None != neg_cont):
59 if (None != pos_cont):
63 def run_once(self, pos_cont, neg_cont):
67 if ((res == False) and (neg_cont != None)):
69 if ((res == True) and (pos_cont != None)):
73 def evaluate(self, failed_only):
76 for c in self.conditions:
77 if (False == c.evaluate(failed_only)):
81 print((str(pos) + ' out of ' + str(pos+neg) + ' conditions fulfilled'))
85 self.fulfilled = False
86 for c in self.conditions:
92 self.fulfilled = False
95 def __init__(self, type):
96 self.fulfilled = False
102 def evaluate(self, failed_only):
103 if ((self.fulfilled == False) and (failed_only == True)):
104 print(str(self.type) + 'condition for was ' + str(self.fulfilled))
105 elif (failed_only == False):
106 print(str(self.type) + 'condition for was ' + str(self.fulfilled))
107 return self.fulfilled
110 class FileExistCondition(Condition):
111 def __init__(self, file):
112 self.fulfilled = False
117 if (self.fulfilled == False):
118 res = os.path.isfile(self.file)
120 self.fulfilled = True
127 def evaluate(self, failed_only):
128 if ((self.fulfilled == False) and (failed_only == True)):
129 print(str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled))
130 elif (failed_only == False):
131 print(str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled))
132 return self.fulfilled
135 class StatisticsCondition (Condition):
136 def __init__(self, peer, subsystem, name, value):
137 self.fulfilled = False
138 self.type = 'statistics'
140 self.subsystem = subsystem
146 if (self.fulfilled == False):
147 self.result = self.peer.get_statistics_value(self.subsystem, self.name)
148 if (str(self.result) == str(self.value)):
149 self.fulfilled = True
156 def evaluate(self, failed_only):
157 if (self.result == -1):
160 res = str(self.result)
161 if (self.fulfilled == False):
167 if (((self.fulfilled == False) and (failed_only == True)) or (failed_only == False)):
168 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)
169 return self.fulfilled
172 # Specify two statistic values and check if they are equal
173 class EqualStatisticsCondition (Condition):
174 def __init__(self, peer, subsystem, name, peer2, subsystem2, name2):
175 self.fulfilled = False
176 self.type = 'equalstatistics'
178 self.subsystem = subsystem
182 self.subsystem2 = subsystem2
187 if (self.fulfilled == False):
188 self.result = self.peer.get_statistics_value(self.subsystem, self.name)
189 self.result2 = self.peer2.get_statistics_value(self.subsystem2, self.name2)
190 if (str(self.result) == str(self.result2)):
191 self.fulfilled = True
198 def evaluate(self, failed_only):
199 if (self.result == -1):
202 res = str(self.result)
203 if (self.result2 == -1):
206 res2 = str(self.result2)
207 if (self.fulfilled == False):
213 if (((self.fulfilled == False) and (failed_only == True)) or (failed_only == False)):
214 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))
215 return self.fulfilled
219 def __init__(self, testname, verbose):
221 self.verbose = verbose
224 gnunet_pyexpect_dir = os.path.join(srcdir, "contrib/scripts")
225 if gnunet_pyexpect_dir not in sys.path:
226 sys.path.append(gnunet_pyexpect_dir)
228 self.gnunetstatistics = ''
229 if os.name == 'posix':
230 self.gnunetarm = 'gnunet-arm'
231 self.gnunetstatistics = 'gnunet-statistics'
232 self.gnunetpeerinfo = 'gnunet-peerinfo'
233 elif os.name == 'nt':
234 self.gnunetarm = 'gnunet-arm.exe'
235 self.gnunetstatistics = 'gnunet-statistics.exe'
236 self.gnunetpeerinfo = 'gnunet-peerinfo.exe'
238 shutil.rmtree(os.path.join(os.getenv("TEMP"), testname), True)
240 shutil.rmtree("/tmp/" + testname, True)
242 def add_peer(self, peer):
243 self.peers.append(peer)
246 if (self.verbose == True):
251 def __init__(self, test, cfg_file):
252 if (False == os.path.isfile(cfg_file)):
253 print(("Peer cfg " + cfg_file + ": FILE NOT FOUND"))
260 if (self.started == True):
261 print('ERROR! Peer using cfg ' + self.cfg + ' was not stopped')
264 print('ERROR! Peer using cfg ' + self.cfg + ' could not be stopped')
271 self.test.p("Starting peer using cfg " + self.cfg)
273 server = subprocess.Popen([self.test.gnunetarm, '-sq', '-c', self.cfg])
276 print("Can not start peer")
283 server.spawn(None, [self.test.gnunetpeerinfo, '-c', self.cfg, '-s'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
284 test = server.read("stdout", 1024)
286 print("Can not get peer identity")
287 test = (test.split('`')[1])
288 self.id = test.split('\'')[0]
292 if (self.started == False):
294 self.test.p("Stopping peer using cfg " + self.cfg)
296 server = subprocess.Popen([self.test.gnunetarm, '-eq', '-c', self.cfg])
299 print("Can not stop peer")
304 def get_statistics_value(self, subsystem, name):
306 server.spawn(None, [self.test.gnunetstatistics, '-c', self.cfg, '-q', '-n', name, '-s', subsystem], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
307 # server.expect ("stdout", re.compile (r""))
308 test = server.read("stdout", 10240)
309 tests = test.partition('\n')
310 # On W32 GNUnet outputs with \r\n, rather than \n
311 if os.name == 'nt' and tests[1] == '\n' and tests[0][-1] == '\r':
312 tests = (tests[0][:-1], tests[1], tests[2])
314 if (tests.isdigit() == True):