-fixing source port randomization for DNS service
[oweals/gnunet.git] / src / integration-tests / gnunet_testing.py.in
index dccaf43eb73d2ee28a33554b10d70c26b5fe9c74..9d4a328770cd2498cae46b5237b486c1791b2a98 100644 (file)
@@ -27,9 +27,10 @@ import time
 
 
 class Check:
-    def __init__(self):
+    def __init__(self, test):
         self.fulfilled = False
         self.conditions = list()
+        self.test = test
     def add (self, condition):
         self.conditions.append(condition)
     def run (self):
@@ -42,7 +43,7 @@ class Check:
                 neg += 1
             else:
                 pos += 1
-        print (str(pos) +' out of '+ str (pos+neg) + ' conditions fulfilled')
+        self.test.p (str(pos) +' out of '+ str (pos+neg) + ' conditions fulfilled')
         return fulfilled
     def run_blocking (self, timeout, pos_cont, neg_cont):
         execs = 0;
@@ -52,17 +53,35 @@ class Check:
             time.sleep(1)
             execs += 1
         if (res == False):
-            neg_cont ()
+            neg_cont (self)
         else:
-            pos_cont ()
+            pos_cont (self)
+    def eval(self, failed_only):
+        pos = 0
+        neg = 0
+        for c in self.conditions:
+            if (False == c.eval (failed_only)):
+                neg += 1
+            else:
+                pos += 1
+        print (str(pos) +' out of '+ str (pos+neg) + ' conditions fulfilled')
+        return self.fulfilled
         
-
 class Condition:
+    def __init__(self):
+        self.fulfilled = False
+        self.type = 'generic'
     def __init__(self, type):
         self.fulfilled = False
         self.type = type
     def check(self):
         return False;
+    def eval(self, failed_only):
+        if ((self.fulfilled == False) and (failed_only == True)):
+            print str(self.type) + 'condition for was ' + str(self.fulfilled)
+        elif (failed_only == False): 
+            print str(self.type) + 'condition for was ' + str(self.fulfilled)
+        return self.fulfilled            
 
 class FileExistCondition (Condition):
     def __init__(self, file):
@@ -79,6 +98,12 @@ class FileExistCondition (Condition):
                 return False
         else:
             return True
+    def eval(self, failed_only):
+        if ((self.fulfilled == False) and (failed_only == True)):
+            print str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled)
+        elif (failed_only == False): 
+            print str(self.type) + 'condition for file '+self.file+' was ' + str(self.fulfilled)
+        return self.fulfilled
 
 class StatisticsCondition (Condition):
     def __init__(self, peer, subsystem, name, value):
@@ -86,19 +111,39 @@ class StatisticsCondition (Condition):
         self.type = 'statistics'
         self.peer = peer;
         self.subsystem = subsystem;
-        self.name = value;
+        self.name = name;
+        self.value = value;
+        self.result = -1;
     def check(self):
         if (self.fulfilled == False):
-            res = self.peer.check (subsystem, name, value);
-            if (res == True):
+            self.result = self.peer.get_statistics_value (self.subsystem, self.name);
+            if (str(self.result) == str(self.value)):
                 self.fulfilled = True
                 return True
             else:
                 return False
         else:
             return True
+    def eval(self, failed_only):
+       if (self.result == -1):
+               res = 'NaN'
+       else:
+               res = str(self.result)
+        if (self.fulfilled == False):
+            fail = " FAIL!" 
+            op = " != "
+        else: 
+            fail = ""
+            op = " == "
+        if ((self.fulfilled == False) and (failed_only == True)):
+            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
+        elif (failed_only == False): 
+            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 
+        return self.fulfilled    
+        
 class Test:
     def __init__(self, testname, verbose):
+        self.peers = list()
        self.verbose = verbose;
        self.name = testname;
        srcdir = "../.."
@@ -118,8 +163,11 @@ class Test:
             shutil.rmtree (os.path.join (os.getenv ("TEMP"), testname), True)
         else:
             shutil.rmtree ("/tmp/" + testname, True)
+    def add_peer (peer):
+       self.conditions.append(condition)
     def p (self, msg):
-       print msg    
+        if (self.verbose == True):
+          print msg    
 
 class Peer:
     def __init__(self, test, cfg_file):
@@ -131,9 +179,12 @@ class Peer:
     def __del__(self):
        if (self.started == True):
                print 'ERROR! Peer using cfg ' + self.cfg + ' was not stopped'
-               self.started == False
-               if (False == self.stop ()):
+               if (ret == self.stop ()):
                        print 'ERROR! Peer using cfg ' + self.cfg + ' could not be stopped'
+               self.started == False
+               return ret
+       else:
+               return False
     def start (self):
         self.test.p ("Starting peer using cfg " + self.cfg)
         try:
@@ -146,6 +197,8 @@ class Peer:
         self.started = True
         return True 
     def stop (self):
+       if (self.started == False):
+               return False
         self.test.p ("Stopping peer using cfg " + self.cfg)
         try:
             server = subprocess.Popen ([self.test.gnunetarm, '-eq', '-c', self.cfg])
@@ -155,14 +208,15 @@ class Peer:
             return False
         self.started = False
         return True;
-    def check (self, subsystem, name, value):
+    def get_statistics_value (self, subsystem, name):
         from gnunet_pyexpect import pexpect
         server = pexpect ()
         server.spawn (None, [self.test.gnunetstatistics, '-c', self.cfg ,'-q','-n', name, '-s', subsystem ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
         #server.expect ("stdout", re.compile (r""))
         test = server.read("stdout", 10240)
-        if (test.find(str(value)) == -1): 
-            return False
+        tests = test.partition('\n')[0]
+        if (tests.isdigit() == True):
+            return tests
         else:
-            return True  
+            return -1
         
\ No newline at end of file