glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / integration-tests / test_integration_clique.py.in
1 #!@PYTHON@
2 #    This file is part of GNUnet.
3 #    (C) 2010, 2018 Christian Grothoff (and other contributing authors)
4 #
5 #    GNUnet is free software: you can redistribute it and/or modify it
6 #    under the terms of the GNU Affero General Public License as published
7 #    by the Free Software Foundation, either version 3 of the License,
8 #    or (at your 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 #    Affero General Public License for more details.
14 #
15 #
16 #
17 # This test starts 3 peers (nated, server, no nat)and expects bootstrap
18 # and a connected clique
19 #
20 # Conditions for successful exit:
21 # Both peers have 2 connected peers in transport, core, topology, fs and dht
22
23 import sys
24 import signal
25 import os
26 import subprocess
27 import re
28 import shutil
29 import time
30 from gnunet_testing import Peer
31 from gnunet_testing import Test
32 from gnunet_testing import Check
33 from gnunet_testing import Condition
34 from gnunet_testing import *
35
36 if os.name == "nt":
37     tmp = os.getenv("TEMP")
38 else:
39     tmp = "/tmp"
40
41 # definitions
42
43 testname = "test_integration_clique"
44 verbose = True
45 check_timeout = 180
46
47
48 def cleanup_onerror(function, path, excinfo):
49     import stat
50     if not os.path.exists(path):
51         pass
52     elif not os.access(path, os.W_OK):
53         # Is the error an access error ?
54         os.chmod(path, stat.S_IWUSR)
55         function(path)
56     else:
57         raise
58
59
60 def cleanup():
61     retries = 10
62     path = os.path.join(tmp, "c_bootstrap_server")
63     test.p("Removing " + path)
64     while ((os.path.exists(path)) and (retries > 0)):
65         shutil.rmtree((path), False, cleanup_onerror)
66         time.sleep(1)
67         retries -= 1
68     if (os.path.exists(path)):
69         test.p("Failed to remove " + path)
70     retries = 10
71     path = os.path.join(tmp, "c_no_nat_client")
72     test.p("Removing " + path)
73     while ((os.path.exists(path)) and (retries > 0)):
74         shutil.rmtree((path), False, cleanup_onerror)
75         time.sleep(1)
76         retries -= 1
77     if (os.path.exists(path)):
78         test.p("Failed to remove " + path)
79         retries = 10
80     path = os.path.join(tmp, "c_nat_client")
81     test.p("Removing " + path)
82     while ((os.path.exists(path)) and (retries > 0)):
83         shutil.rmtree((path), False, cleanup_onerror)
84         time.sleep(1)
85         retries -= 1
86     if (os.path.exists(path)):
87         test.p("Failed to remove " + path)
88
89
90 def success_cont(check):
91     global success
92     success = True
93     print('Connected clique successfully')
94
95
96 def fail_cont(check):
97     global success
98     success = False
99     check.evaluate(True)
100     print('Failed to connect clique')
101
102
103 def check_connect():
104     check = Check(test)
105     check.add(StatisticsCondition(client, 'transport', '# peers connected', 2))
106     check.add(StatisticsCondition(client, 'core', '# peers connected', 2))
107     check.add(StatisticsCondition(client, 'topology', '# peers connected', 2))
108     check.add(StatisticsCondition(client, 'dht', '# peers connected', 2))
109     check.add(StatisticsCondition(client, 'fs', '# peers connected', 2))
110
111     check.add(StatisticsCondition(client_nat, 'transport', '# peers connected', 2))
112     check.add(StatisticsCondition(client_nat, 'core', '# peers connected', 2))
113     check.add(StatisticsCondition(client_nat, 'topology', '# peers connected', 2))
114     check.add(StatisticsCondition(client_nat, 'dht', '# peers connected', 2))
115     check.add(StatisticsCondition(client_nat, 'fs', '# peers connected', 2))
116
117     check.add(StatisticsCondition(server, 'transport', '# peers connected', 2))
118     check.add(StatisticsCondition(server, 'core', '# peers connected', 2))
119     check.add(StatisticsCondition(server, 'topology', '# peers connected', 2))
120     check.add(StatisticsCondition(server, 'dht', '# peers connected', 2))
121     check.add(StatisticsCondition(server, 'fs', '# peers connected', 2))
122
123     check.run_blocking(check_timeout, success_cont, fail_cont)
124
125 #
126 # Test execution
127 #
128
129
130 def SigHandler(signum=None, frame=None):
131     global success
132     global server
133     global client
134     global client_nat
135
136     print('Test was aborted!')
137     if (None != server):
138         server.stop()
139     if (None != client):
140         client.stop()
141     if (None != client_nat):
142         client_nat.stop()
143     cleanup()
144     sys.exit(success)
145
146
147 def run():
148     global success
149     global test
150     global server
151     global client
152     global client_nat
153
154     success = False
155     server = None
156     client = None
157     client_nat = None
158     test = Test('test_integration_clique', verbose)
159     cleanup()
160
161     server = Peer(test, './confs/c_bootstrap_server.conf')
162     if (True != server.start()):
163         print('Failed to start server')
164         if (None != server):
165             server.stop()
166         cleanup()
167         sys.exit(success)
168
169     # Server has to settle down
170     time.sleep(5)
171
172     client = Peer(test, './confs/c_no_nat_client.conf')
173     if (True != client.start()):
174         print('Failed to start client')
175         if (None != server):
176             server.stop()
177         if (None != client):
178             client.stop()
179         cleanup()
180         sys.exit(success)
181
182     # Server has to settle down
183     time.sleep(5)
184
185     client_nat = Peer(test, './confs/c_nat_client.conf')
186     if (True != client_nat.start()):
187         print('Failed to start client_nat')
188         if (None != server):
189             server.stop()
190         if (None != client):
191             client.stop()
192         if (None != client_nat):
193             client_nat.stop()
194         cleanup()
195         sys.exit(success)
196
197     if ((client.started == True) and (client_nat.started == True) and (server.started == True)):
198         test.p('Peers started, running check')
199         check_connect()
200
201     server.stop()
202     client.stop()
203     client_nat.stop()
204
205     cleanup()
206
207     if (success == False):
208         print('Test failed')
209         return False
210     else:
211         return True
212
213
214 try:
215     run()
216 except (KeyboardInterrupt, SystemExit):
217     print('Test interrupted')
218     server.stop()
219     client.stop()
220     client_nat.stop()
221     cleanup()
222 if (success == False):
223     sys.exit(1)
224 else:
225     sys.exit(0)