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