adding nat clique test
[oweals/gnunet.git] / src / integration-tests / gnunet_testing.py.in
1 #!@PYTHON@
2 #    This file is part of GNUnet.
3 #    (C) 2010 Christian Grothoff (and other contributing authors)
4 #
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.
9 #
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.
14 #
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.
19 #
20 # Functions for integration testing
21 import os
22 import re
23 import subprocess
24 import sys
25 import shutil
26 import time
27
28
29 class Check:
30     def __init__(self, test):
31         self.fulfilled = False
32         self.conditions = list()
33         self.test = test
34     def add (self, condition):
35         self.conditions.append(condition)
36     def run (self):
37         fulfilled = True
38         pos = 0
39         neg = 0
40         for c in self.conditions:
41             if (False == c.check ()):
42                 fulfilled = False
43                 neg += 1
44             else:
45                 pos += 1
46         self.test.p (str(pos) +' out of '+ str (pos+neg) + ' conditions fulfilled')
47         return fulfilled
48     def run_blocking (self, timeout, pos_cont, neg_cont):
49         execs = 0;
50         res = False
51         while ((False == res) and (execs < timeout)):
52             res = self.run()
53             time.sleep(1)
54             execs += 1
55         if (res == False):
56             neg_cont (self)
57         else:
58             pos_cont (self)
59     def eval(self, failed_only):
60         pos = 0
61         neg = 0
62         for c in self.conditions:
63             if (False == c.eval (failed_only)):
64                 neg += 1
65             else:
66                 pos += 1
67         print (str(pos) +' out of '+ str (pos+neg) + ' conditions fulfilled')
68         return self.fulfilled
69         
70 class Condition:
71     def __init__(self):
72         self.fulfilled = False
73         self.type = 'generic'
74     def __init__(self, type):
75         self.fulfilled = False
76         self.type = type
77     def check(self):
78         return 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)
84         return self.fulfilled            
85
86 class FileExistCondition (Condition):
87     def __init__(self, file):
88         self.fulfilled = False
89         self.type = 'file'
90         self.file = file
91     def check(self):
92         if (self.fulfilled == False):
93             res = os.path.isfile(self.file)
94             if (res == True):
95                 self.fulfilled = True
96                 return True
97             else:
98                 return False
99         else:
100             return True
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
107
108 class StatisticsCondition (Condition):
109     def __init__(self, peer, subsystem, name, value):
110         self.fulfilled = False
111         self.type = 'statistics'
112         self.peer = peer;
113         self.subsystem = subsystem;
114         self.name = name;
115         self.value = value;
116         self.result = -1;
117     def check(self):
118         if (self.fulfilled == False):
119             self.result = self.peer.check (self.subsystem, self.name, self.value);
120             if (self.result == self.value):
121                 self.fulfilled = True
122                 return True
123             else:
124                 return False
125         else:
126             return True
127     def eval(self, failed_only):
128         if ((self.fulfilled == False) and (failed_only == True)):
129             if (self.fulfilled == False):
130                 fail = " FAIL!" 
131                 op = " != "
132             else: 
133                 fail = ""
134                 op = " == "
135             print self.peer.cfg + " " + str(self.type) + ' condition in subsystem "' + self.subsystem.ljust(12) +'" : "' + self.name.ljust(30) +'" : ' + str(self.value) + op + str(self.result) + fail
136         elif (failed_only == False): 
137             if (self.fulfilled == False):
138                 fail = " FAIL!" 
139                 op = " != "
140             else: 
141                 fail = ""
142                 op = " == "
143             print self.peer.cfg + " " + str(self.type) + ' condition in subsystem "' + self.subsystem.ljust(12) +'" : "' + self.name.ljust(30) +'" : ' + str(self.value) + op + str(self.result) + fail 
144         return self.fulfilled    
145         
146 class Test:
147     def __init__(self, testname, verbose):
148         self.peers = list()
149         self.verbose = verbose;
150         self.name = testname;
151         srcdir = "../.."
152         gnunet_pyexpect_dir = os.path.join (srcdir, "contrib")
153         if gnunet_pyexpect_dir not in sys.path:
154                 sys.path.append (gnunet_pyexpect_dir)
155                 from gnunet_pyexpect import pexpect
156         self.gnunetarm = ''        
157         self.gnunetstatistics = ''
158         if os.name == 'posix':
159            self.gnunetarm = 'gnunet-arm'
160            self.gnunetstatistics = 'gnunet-statistics'
161         elif os.name == 'nt':
162            self.gnunetarm = 'gnunet-arm.exe'
163            self.gnunetstatistics = 'gnunet-statistics.exe'    
164         if os.name == "nt":
165             shutil.rmtree (os.path.join (os.getenv ("TEMP"), testname), True)
166         else:
167             shutil.rmtree ("/tmp/" + testname, True)
168     def add_peer (peer):
169         self.conditions.append(condition)
170     def p (self, msg):
171         if (self.verbose == True):
172            print msg    
173
174 class Peer:
175     def __init__(self, test, cfg_file):
176         if (False == os.path.isfile(cfg_file)):
177             print ("Peer cfg " + cfg_file + ": FILE NOT FOUND")
178         self.test = test
179         self.started = False
180         self.cfg = cfg_file 
181     def __del__(self):
182         if (self.started == True):
183                 print 'ERROR! Peer using cfg ' + self.cfg + ' was not stopped'
184                 if (ret == self.stop ()):
185                         print 'ERROR! Peer using cfg ' + self.cfg + ' could not be stopped'
186                 self.started == False
187                 return ret
188         else:
189                 return False
190     def start (self):
191         self.test.p ("Starting peer using cfg " + self.cfg)
192         try:
193             server = subprocess.Popen ([self.test.gnunetarm, '-sq', '-c', self.cfg])
194             server.communicate ()    
195         except OSError:
196             print "Can not start peer"
197             self.started = False
198             return False
199         self.started = True
200         return True 
201     def stop (self):
202         if (self.started == False):
203                 return False
204         self.test.p ("Stopping peer using cfg " + self.cfg)
205         try:
206             server = subprocess.Popen ([self.test.gnunetarm, '-eq', '-c', self.cfg])
207             server.communicate ()    
208         except OSError:
209             print "Can not stop peer"
210             return False
211         self.started = False
212         return True;
213     def check (self, subsystem, name, value):
214         from gnunet_pyexpect import pexpect
215         server = pexpect ()
216         server.spawn (None, [self.test.gnunetstatistics, '-c', self.cfg ,'-q','-n', name, '-s', subsystem ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
217         #server.expect ("stdout", re.compile (r""))
218         test = server.read("stdout", 10240)
219         if (test.find(str(value)) == -1): 
220             return value
221         else:
222             return value  
223
224