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 ()):
45 self.test.p (str(pos) +' out of '+ str (pos+neg) + ' conditions fulfilled')
47 def run_blocking (self, timeout, pos_cont, neg_cont):
50 while ((False == res) and (execs < timeout)):
58 def evaluate (self, failed_only):
61 for c in self.conditions:
62 if (False == c.evaluate (failed_only)):
66 print (str(pos) +' out of '+ str (pos+neg) + ' conditions fulfilled')
71 self.fulfilled = False
73 def __init__(self, type):
74 self.fulfilled = False
78 def evaluate (self, failed_only):
79 if ((self.fulfilled == False) and (failed_only == True)):
80 print str(self.type) + 'condition for was ' + str(self.fulfilled)
81 elif (failed_only == False):
82 print str(self.type) + 'condition for was ' + str(self.fulfilled)
85 class FileExistCondition (Condition):
86 def __init__(self, file):
87 self.fulfilled = False
91 if (self.fulfilled == False):
92 res = os.path.isfile(self.file)
100 def evaluate (self, failed_only):
101 if ((self.fulfilled == False) and (failed_only == True)):
102 print str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled)
103 elif (failed_only == False):
104 print str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled)
105 return self.fulfilled
107 class StatisticsCondition (Condition):
108 def __init__(self, peer, subsystem, name, value):
109 self.fulfilled = False
110 self.type = 'statistics'
112 self.subsystem = subsystem;
117 if (self.fulfilled == False):
118 self.result = self.peer.get_statistics_value (self.subsystem, self.name);
119 if (str(self.result) == str(self.value)):
120 self.fulfilled = True
126 def evaluate (self, failed_only):
127 if (self.result == -1):
130 res = str(self.result)
131 if (self.fulfilled == False):
137 if ((self.fulfilled == False) and (failed_only == True)):
138 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
139 elif (failed_only == False):
140 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
141 return self.fulfilled
144 def __init__(self, testname, verbose):
146 self.verbose = verbose;
147 self.name = testname;
149 gnunet_pyexpect_dir = os.path.join (srcdir, "contrib")
150 if gnunet_pyexpect_dir not in sys.path:
151 sys.path.append (gnunet_pyexpect_dir)
153 self.gnunetstatistics = ''
154 if os.name == 'posix':
155 self.gnunetarm = 'gnunet-arm'
156 self.gnunetstatistics = 'gnunet-statistics'
157 self.gnunetpeerinfo = 'gnunet-peerinfo'
158 elif os.name == 'nt':
159 self.gnunetarm = 'gnunet-arm.exe'
160 self.gnunetstatistics = 'gnunet-statistics.exe'
161 self.gnunetpeerinfo = 'gnunet-peerinfo.exe'
163 shutil.rmtree (os.path.join (os.getenv ("TEMP"), testname), True)
165 shutil.rmtree ("/tmp/" + testname, True)
166 def add_peer (self, peer):
167 self.peers.append(peer)
169 if (self.verbose == True):
173 def __init__(self, test, cfg_file):
174 if (False == os.path.isfile(cfg_file)):
175 print ("Peer cfg " + cfg_file + ": FILE NOT FOUND")
181 if (self.started == True):
182 print 'ERROR! Peer using cfg ' + self.cfg + ' was not stopped'
185 print 'ERROR! Peer using cfg ' + self.cfg + ' could not be stopped'
191 self.test.p ("Starting peer using cfg " + self.cfg)
193 server = subprocess.Popen ([self.test.gnunetarm, '-sq', '-c', self.cfg])
194 server.communicate ()
196 print "Can not start peer"
203 server.spawn (None, [self.test.gnunetpeerinfo, '-c', self.cfg ,'-s'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
204 test = server.read("stdout", 1024)
206 print "Can not get peer identity"
207 test = (test.split('`')[1])
208 self.id = test.split('\'')[0]
211 if (self.started == False):
213 self.test.p ("Stopping peer using cfg " + self.cfg)
215 server = subprocess.Popen ([self.test.gnunetarm, '-eq', '-c', self.cfg])
216 server.communicate ()
218 print "Can not stop peer"
222 def get_statistics_value (self, subsystem, name):
224 server.spawn (None, [self.test.gnunetstatistics, '-c', self.cfg ,'-q','-n', name, '-s', subsystem ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
225 #server.expect ("stdout", re.compile (r""))
226 test = server.read("stdout", 10240)
227 tests = test.partition('\n')
228 # On W32 GNUnet outputs with \r\n, rather than \n
229 if os.name == 'nt' and tests[1] == '\n' and tests[0][-1] == '\r':
230 tests = (tests[0][:-1], tests[1], tests[2])
232 if (tests.isdigit() == True):