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
30 def __init__(self, test):
31 self.fulfilled = False
32 self.conditions = list()
34 def add (self, condition):
35 self.conditions.append(condition)
40 for c in self.conditions:
41 if (False == c.check ()):
46 self.test.p (str(pos) +' out of '+ str (pos+neg) + ' conditions fulfilled')
48 def run_blocking (self, timeout, pos_cont, neg_cont):
51 while ((False == res) and (execs < timeout)):
59 def eval(self, failed_only):
62 for c in self.conditions:
63 if (False == c.eval (failed_only)):
67 print (str(pos) +' out of '+ str (pos+neg) + ' conditions fulfilled')
72 self.fulfilled = False
74 def __init__(self, type):
75 self.fulfilled = False
79 def eval(self, failed_only):
80 if ((self.fulfilled == False) and (failed_only == True)):
81 print str(self.type) + 'condition for was ' + str(self.fulfilled)
82 elif (failed_only == False):
83 print str(self.type) + 'condition for was ' + str(self.fulfilled)
86 class FileExistCondition (Condition):
87 def __init__(self, file):
88 self.fulfilled = False
92 if (self.fulfilled == False):
93 res = os.path.isfile(self.file)
101 def eval(self, failed_only):
102 if ((self.fulfilled == False) and (failed_only == True)):
103 print str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled)
104 elif (failed_only == False):
105 print str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled)
106 return self.fulfilled
108 class StatisticsCondition (Condition):
109 def __init__(self, peer, subsystem, name, value):
110 self.fulfilled = False
111 self.type = 'statistics'
113 self.subsystem = subsystem;
118 if (self.fulfilled == False):
119 self.result = self.peer.get_statistics_value (self.subsystem, self.name);
120 if (str(self.result) == str(self.value)):
121 self.fulfilled = True
127 def eval(self, failed_only):
128 if (self.result == -1):
131 res = str(self.result)
132 if (self.fulfilled == False):
138 if ((self.fulfilled == False) and (failed_only == True)):
139 print 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
140 elif (failed_only == False):
141 print 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
142 return self.fulfilled
145 def __init__(self, testname, verbose):
147 self.verbose = verbose;
148 self.name = testname;
150 gnunet_pyexpect_dir = os.path.join (srcdir, "contrib")
151 if gnunet_pyexpect_dir not in sys.path:
152 sys.path.append (gnunet_pyexpect_dir)
153 from gnunet_pyexpect import pexpect
155 self.gnunetstatistics = ''
156 if os.name == 'posix':
157 self.gnunetarm = 'gnunet-arm'
158 self.gnunetstatistics = 'gnunet-statistics'
159 elif os.name == 'nt':
160 self.gnunetarm = 'gnunet-arm.exe'
161 self.gnunetstatistics = 'gnunet-statistics.exe'
163 shutil.rmtree (os.path.join (os.getenv ("TEMP"), testname), True)
165 shutil.rmtree ("/tmp/" + testname, True)
167 self.conditions.append(condition)
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")
180 if (self.started == True):
181 print 'ERROR! Peer using cfg ' + self.cfg + ' was not stopped'
182 if (ret == self.stop ()):
183 print 'ERROR! Peer using cfg ' + self.cfg + ' could not be stopped'
184 self.started == False
189 self.test.p ("Starting peer using cfg " + self.cfg)
191 server = subprocess.Popen ([self.test.gnunetarm, '-sq', '-c', self.cfg])
192 server.communicate ()
194 print "Can not start peer"
200 if (self.started == False):
202 self.test.p ("Stopping peer using cfg " + self.cfg)
204 server = subprocess.Popen ([self.test.gnunetarm, '-eq', '-c', self.cfg])
205 server.communicate ()
207 print "Can not stop peer"
211 def get_statistics_value (self, subsystem, name):
212 from gnunet_pyexpect import pexpect
214 server.spawn (None, [self.test.gnunetstatistics, '-c', self.cfg ,'-q','-n', name, '-s', subsystem ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
215 #server.expect ("stdout", re.compile (r""))
216 test = server.read("stdout", 10240)
217 tests = test.partition('\n')[0]
218 if (tests.isdigit() == True):