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