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)):
58 def run_once (self, pos_cont, neg_cont):
62 if ((res == False) and (neg_cont != None)):
64 if ((res == True) and (pos_cont != None)):
67 def evaluate (self, failed_only):
70 for c in self.conditions:
71 if (False == c.evaluate (failed_only)):
75 print (str(pos) +' out of '+ str (pos+neg) + ' conditions fulfilled')
78 self.fulfilled = False
79 for c in self.conditions:
84 self.fulfilled = False
86 def __init__(self, type):
87 self.fulfilled = False
91 def evaluate (self, failed_only):
92 if ((self.fulfilled == False) and (failed_only == True)):
93 print str(self.type) + 'condition for was ' + str(self.fulfilled)
94 elif (failed_only == False):
95 print str(self.type) + 'condition for was ' + str(self.fulfilled)
98 class FileExistCondition (Condition):
99 def __init__(self, file):
100 self.fulfilled = False
104 if (self.fulfilled == False):
105 res = os.path.isfile(self.file)
107 self.fulfilled = True
113 def evaluate (self, failed_only):
114 if ((self.fulfilled == False) and (failed_only == True)):
115 print str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled)
116 elif (failed_only == False):
117 print str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled)
118 return self.fulfilled
120 class StatisticsCondition (Condition):
121 def __init__(self, peer, subsystem, name, value):
122 self.fulfilled = False
123 self.type = 'statistics'
125 self.subsystem = subsystem;
130 if (self.fulfilled == False):
131 self.result = self.peer.get_statistics_value (self.subsystem, self.name)
132 if (str(self.result) == str(self.value)):
133 self.fulfilled = True
139 def evaluate (self, failed_only):
140 if (self.result == -1):
143 res = str(self.result)
144 if (self.fulfilled == False):
150 if (((self.fulfilled == False) and (failed_only == True)) or (failed_only == False)):
151 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
152 return self.fulfilled
154 # Specify two statistic values and check if they are equal
155 class EqualStatisticsCondition (Condition):
156 def __init__(self, peer, subsystem, name, peer2, subsystem2, name2):
157 self.fulfilled = False
158 self.type = 'equalstatistics'
160 self.subsystem = subsystem;
164 self.subsystem2 = subsystem2;
168 if (self.fulfilled == False):
169 self.result = self.peer.get_statistics_value (self.subsystem, self.name);
170 self.result2 = self.peer2.get_statistics_value (self.subsystem2, self.name2);
171 if (str(self.result) == str(self.result2)):
172 self.fulfilled = True
178 def evaluate (self, failed_only):
179 if (self.result == -1):
182 res = str(self.result)
183 if (self.result2 == -1):
186 res2 = str(self.result2)
187 if (self.fulfilled == False):
193 if (((self.fulfilled == False) and (failed_only == True)) or (failed_only == False)):
194 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)
195 return self.fulfilled
198 def __init__(self, testname, verbose):
200 self.verbose = verbose;
201 self.name = testname;
203 gnunet_pyexpect_dir = os.path.join (srcdir, "contrib")
204 if gnunet_pyexpect_dir not in sys.path:
205 sys.path.append (gnunet_pyexpect_dir)
207 self.gnunetstatistics = ''
208 if os.name == 'posix':
209 self.gnunetarm = 'gnunet-arm'
210 self.gnunetstatistics = 'gnunet-statistics'
211 self.gnunetpeerinfo = 'gnunet-peerinfo'
212 elif os.name == 'nt':
213 self.gnunetarm = 'gnunet-arm.exe'
214 self.gnunetstatistics = 'gnunet-statistics.exe'
215 self.gnunetpeerinfo = 'gnunet-peerinfo.exe'
217 shutil.rmtree (os.path.join (os.getenv ("TEMP"), testname), True)
219 shutil.rmtree ("/tmp/" + testname, True)
220 def add_peer (self, peer):
221 self.peers.append(peer)
223 if (self.verbose == True):
227 def __init__(self, test, cfg_file):
228 if (False == os.path.isfile(cfg_file)):
229 print ("Peer cfg " + cfg_file + ": FILE NOT FOUND")
235 if (self.started == True):
236 print 'ERROR! Peer using cfg ' + self.cfg + ' was not stopped'
239 print 'ERROR! Peer using cfg ' + self.cfg + ' could not be stopped'
245 self.test.p ("Starting peer using cfg " + self.cfg)
247 server = subprocess.Popen ([self.test.gnunetarm, '-sq', '-c', self.cfg])
248 server.communicate ()
250 print "Can not start peer"
257 server.spawn (None, [self.test.gnunetpeerinfo, '-c', self.cfg ,'-s'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
258 test = server.read("stdout", 1024)
260 print "Can not get peer identity"
261 test = (test.split('`')[1])
262 self.id = test.split('\'')[0]
265 if (self.started == False):
267 self.test.p ("Stopping peer using cfg " + self.cfg)
269 server = subprocess.Popen ([self.test.gnunetarm, '-eq', '-c', self.cfg])
270 server.communicate ()
272 print "Can not stop peer"
276 def get_statistics_value (self, subsystem, name):
278 server.spawn (None, [self.test.gnunetstatistics, '-c', self.cfg ,'-q','-n', name, '-s', subsystem ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
279 #server.expect ("stdout", re.compile (r""))
280 test = server.read("stdout", 10240)
281 tests = test.partition('\n')
282 # On W32 GNUnet outputs with \r\n, rather than \n
283 if os.name == 'nt' and tests[1] == '\n' and tests[0][-1] == '\r':
284 tests = (tests[0][:-1], tests[1], tests[2])
286 if (tests.isdigit() == True):