aboutsummaryrefslogtreecommitdiff
path: root/src/integration-tests/gnunet_testing.py.in
blob: be288ff449b37b54e8d69d63e96df4f86463347d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!@PYTHON@
#    This file is part of GNUnet.
#    (C) 2010 Christian Grothoff (and other contributing authors)
#
#    GNUnet is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published
#    by the Free Software Foundation; either version 2, or (at your
#    option) any later version.
#
#    GNUnet is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with GNUnet; see the file COPYING.  If not, write to the
#    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#    Boston, MA 02111-1307, USA.
#
# Functions for integration testing
import os
import re
import subprocess
import sys
import shutil
import time


class Check:
    def __init__(self):
        self.fulfilled = False
        self.conditions = list()
    def add (self, condition):
        self.conditions.append(condition)
    def run (self):
        fulfilled = True
        pos = 0
        neg = 0
        for c in self.conditions:
            if (False == c.check ()):
                fulfilled = False
                neg += 1
            else:
                pos += 1
        print (str(pos) +' out of '+ str (pos+neg) + ' conditions fulfilled')
        return fulfilled
    def run_blocking (self, timeout, pos_cont, neg_cont):
        execs = 0;
        res = False
        while ((False == res) and (execs < timeout)):
            res = self.run()
            time.sleep(1)
            execs += 1
        if (res == False):
            neg_cont ()
        else:
            pos_cont ()
        

class Condition:
    def __init__(self, type):
        self.fulfilled = False
        self.type = type
    def check(self):
        return False;

class FileExistCondition (Condition):
    def __init__(self, file):
        self.fulfilled = False
        self.type = 'file'
        self.file = file
    def check(self):
        if (self.fulfilled == False):
            res = os.path.isfile(self.file)
            if (res == True):
                self.fulfilled = True
                return True
            else:
                return False
        else:
            return True

class StatisticsCondition (Condition):
    def __init__(self, peer, subsystem, name, value):
        self.fulfilled = False
        self.type = 'statistics'
        self.peer = peer;
        self.subsystem = subsystem;
        self.name = name;
        self.value = value;
    def check(self):
        if (self.fulfilled == False):
            res = self.peer.check (self.subsystem, self.name, self.value);
            if (res == True):
                self.fulfilled = True
                return True
            else:
                return False
        else:
            return True
class Test:
    def __init__(self, testname, verbose):
    	self.verbose = verbose;
    	self.name = testname;
    	srcdir = "../.."
        gnunet_pyexpect_dir = os.path.join (srcdir, "contrib")
        if gnunet_pyexpect_dir not in sys.path:
        	sys.path.append (gnunet_pyexpect_dir)
        	from gnunet_pyexpect import pexpect
        self.gnunetarm = ''        
        self.gnunetstatistics = ''
        if os.name == 'posix':
           self.gnunetarm = 'gnunet-arm'
           self.gnunetstatistics = 'gnunet-statistics'
        elif os.name == 'nt':
           self.gnunetarm = 'gnunet-arm.exe'
           self.gnunetstatistics = 'gnunet-statistics.exe'    
        if os.name == "nt":
            shutil.rmtree (os.path.join (os.getenv ("TEMP"), testname), True)
        else:
            shutil.rmtree ("/tmp/" + testname, True)
    def p (self, msg):
    	print msg    

class Peer:
    def __init__(self, test, cfg_file):
        if (False == os.path.isfile(cfg_file)):
            print ("Peer cfg " + cfg_file + ": FILE NOT FOUND")
        self.test = test
        self.started = False
        self.cfg = cfg_file 
    def __del__(self):
    	if (self.started == True):
    		print 'ERROR! Peer using cfg ' + self.cfg + ' was not stopped'
    		self.started == False
    		if (False == self.stop ()):
    			print 'ERROR! Peer using cfg ' + self.cfg + ' could not be stopped'
    def start (self):
        self.test.p ("Starting peer using cfg " + self.cfg)
        try:
            server = subprocess.Popen ([self.test.gnunetarm, '-sq', '-c', self.cfg])
            server.communicate ()    
        except OSError:
            print "Can not start peer"
            self.started = False
            return False
        self.started = True
        return True 
    def stop (self):
        self.test.p ("Stopping peer using cfg " + self.cfg)
        try:
            server = subprocess.Popen ([self.test.gnunetarm, '-eq', '-c', self.cfg])
            server.communicate ()    
        except OSError:
            print "Can not stop peer"
            return False
        self.started = False
        return True;
    def check (self, subsystem, name, value):
        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
        else:
            return True