2 # This file is part of GNUnet.
3 # (C) 2010 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., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA.
20 # Functions for integration testing
26 from gnunet_pyexpect import pexpect
29 def __init__(self, test):
30 self.fulfilled = False
31 self.conditions = list()
33 def add (self, condition):
34 self.conditions.append(condition)
39 for c in self.conditions:
40 if (False == c.check ()):
46 def run_blocking (self, timeout, pos_cont, neg_cont):
49 while ((False == res) and (execs < timeout)):
53 if ((False == res) and (execs >= timeout)):
54 print ('Check had timeout after ' +str(timeout)+ ' seconds')
56 elif ((False == res) and (execs < timeout)):
57 if (None != neg_cont):
60 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)):
72 def evaluate (self, failed_only):
75 for c in self.conditions:
76 if (False == c.evaluate (failed_only)):
80 print (str(pos) +' out of '+ str (pos+neg) + ' conditions fulfilled')
83 self.fulfilled = False
84 for c in self.conditions:
89 self.fulfilled = False
91 def __init__(self, type):
92 self.fulfilled = False
96 def evaluate (self, failed_only):
97 if ((self.fulfilled == False) and (failed_only == True)):
98 print str(self.type) + 'condition for was ' + str(self.fulfilled)
99 elif (failed_only == False):
100 print str(self.type) + 'condition for was ' + str(self.fulfilled)
101 return self.fulfilled
103 class FileExistCondition (Condition):
104 def __init__(self, file):
105 self.fulfilled = False
109 if (self.fulfilled == False):
110 res = os.path.isfile(self.file)
112 self.fulfilled = True
118 def evaluate (self, failed_only):
119 if ((self.fulfilled == False) and (failed_only == True)):
120 print str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled)
121 elif (failed_only == False):
122 print str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled)
123 return self.fulfilled
125 class StatisticsCondition (Condition):
126 def __init__(self, peer, subsystem, name, value):
127 self.fulfilled = False
128 self.type = 'statistics'
130 self.subsystem = subsystem;
135 if (self.fulfilled == False):
136 self.result = self.peer.get_statistics_value (self.subsystem, self.name)
137 if (str(self.result) == str(self.value)):
138 self.fulfilled = True
144 def evaluate (self, failed_only):
145 if (self.result == -1):
148 res = str(self.result)
149 if (self.fulfilled == False):
155 if (((self.fulfilled == False) and (failed_only == True)) or (failed_only == False)):
156 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
157 return self.fulfilled
159 # Specify two statistic values and check if they are equal
160 class EqualStatisticsCondition (Condition):
161 def __init__(self, peer, subsystem, name, peer2, subsystem2, name2):
162 self.fulfilled = False
163 self.type = 'equalstatistics'
165 self.subsystem = subsystem;
169 self.subsystem2 = subsystem2;
173 if (self.fulfilled == False):
174 self.result = self.peer.get_statistics_value (self.subsystem, self.name);
175 self.result2 = self.peer2.get_statistics_value (self.subsystem2, self.name2);
176 if (str(self.result) == str(self.result2)):
177 self.fulfilled = True
183 def evaluate (self, failed_only):
184 if (self.result == -1):
187 res = str(self.result)
188 if (self.result2 == -1):
191 res2 = str(self.result2)
192 if (self.fulfilled == False):
198 if (((self.fulfilled == False) and (failed_only == True)) or (failed_only == False)):
199 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)
200 return self.fulfilled
203 def __init__(self, testname, verbose):
205 self.verbose = verbose;
206 self.name = testname;
208 gnunet_pyexpect_dir = os.path.join (srcdir, "contrib")
209 if gnunet_pyexpect_dir not in sys.path:
210 sys.path.append (gnunet_pyexpect_dir)
212 self.gnunetstatistics = ''
213 if os.name == 'posix':
214 self.gnunetarm = 'gnunet-arm'
215 self.gnunetstatistics = 'gnunet-statistics'
216 self.gnunetpeerinfo = 'gnunet-peerinfo'
217 elif os.name == 'nt':
218 self.gnunetarm = 'gnunet-arm.exe'
219 self.gnunetstatistics = 'gnunet-statistics.exe'
220 self.gnunetpeerinfo = 'gnunet-peerinfo.exe'
222 shutil.rmtree (os.path.join (os.getenv ("TEMP"), testname), True)
224 shutil.rmtree ("/tmp/" + testname, True)
225 def add_peer (self, peer):
226 self.peers.append(peer)
228 if (self.verbose == True):
232 def __init__(self, test, cfg_file):
233 if (False == os.path.isfile(cfg_file)):
234 print ("Peer cfg " + cfg_file + ": FILE NOT FOUND")
240 if (self.started == True):
241 print 'ERROR! Peer using cfg ' + self.cfg + ' was not stopped'
244 print 'ERROR! Peer using cfg ' + self.cfg + ' could not be stopped'
250 self.test.p ("Starting peer using cfg " + self.cfg)
252 server = subprocess.Popen ([self.test.gnunetarm, '-sq', '-c', self.cfg])
253 server.communicate ()
255 print "Can not start peer"
262 server.spawn (None, [self.test.gnunetpeerinfo, '-c', self.cfg ,'-s'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
263 test = server.read("stdout", 1024)
265 print "Can not get peer identity"
266 test = (test.split('`')[1])
267 self.id = test.split('\'')[0]
270 if (self.started == False):
272 self.test.p ("Stopping peer using cfg " + self.cfg)
274 server = subprocess.Popen ([self.test.gnunetarm, '-eq', '-c', self.cfg])
275 server.communicate ()
277 print "Can not stop peer"
281 def get_statistics_value (self, subsystem, name):
283 server.spawn (None, [self.test.gnunetstatistics, '-c', self.cfg ,'-q','-n', name, '-s', subsystem ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
284 #server.expect ("stdout", re.compile (r""))
285 test = server.read("stdout", 10240)
286 tests = test.partition('\n')
287 # On W32 GNUnet outputs with \r\n, rather than \n
288 if os.name == 'nt' and tests[1] == '\n' and tests[0][-1] == '\r':
289 tests = (tests[0][:-1], tests[1], tests[2])
291 if (tests.isdigit() == True):