first batch of license fixes (boring)
[oweals/gnunet.git] / src / integration-tests / test_integration_bootstrap_and_connect.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 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 import signal
17 import sys
18 import os
19 import subprocess
20 import re
21 import shutil
22 import time
23 from gnunet_testing import Peer
24 from gnunet_testing import Test
25 from gnunet_testing import Check
26 from gnunet_testing import Condition
27 from gnunet_testing import *
28
29
30 #
31 # This test tests if a fresh peer bootstraps from a hostlist server and then
32 # successfully connects to the server
33 #
34 # Conditions for successful exit:
35 # Both peers have 1 connected peer in transport, core, topology, fs
36
37 #
38 # This test tests if a fresh peer bootstraps from a hostlist server and then
39 # successfully connects to the server
40 #
41 # Conditions for successful exit:
42 # Both peers have 1 connected peer in transport, core, topology, fs
43
44 # definitions
45
46 testname = "test_integration_bootstrap_and_connect"
47 verbose = False
48 check_timeout = 180
49
50 if os.name == "nt":
51     tmp = os.getenv("TEMP")
52     signals = [signal.SIGTERM, signal.SIGINT]
53 else:
54     tmp = "/tmp"
55     signals = [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]
56
57
58 def cleanup_onerror(function, path, excinfo):
59     import stat
60     if not os.path.exists(path):
61         pass
62     elif not os.access(path, os.W_OK):
63         # Is the error an access error ?
64         os.chmod(path, stat.S_IWUSR)
65         function(path)
66     else:
67         raise
68
69
70 def cleanup():
71     retries = 10
72     path = os.path.join(tmp, "c_bootstrap_server")
73     test.p("Removing " + path)
74     while ((os.path.exists(path)) and (retries > 0)):
75         shutil.rmtree((path), False, cleanup_onerror)
76         time.sleep(1)
77         retries -= 1
78     if (os.path.exists(path)):
79         test.p("Failed to remove " + path)
80
81     retries = 10
82     path = os.path.join(tmp, "c_no_nat_client")
83     test.p("Removing " + path)
84     while ((os.path.exists(path)) and (retries > 0)):
85         shutil.rmtree((path), False, cleanup_onerror)
86         time.sleep(1)
87         retries -= 1
88     if (os.path.exists(path)):
89         test.p("Failed to remove " + path)
90
91
92 def success_cont(check):
93     global success
94     success = True
95     print('Peers connected successfully')
96
97
98 def fail_cont(check):
99     global success
100     success = False
101     print('Peers did not connect')
102     check.evaluate(True)
103
104
105 def check():
106     check = Check(test)
107     check.add(StatisticsCondition(client, 'transport', '# peers connected', 1))
108     check.add(StatisticsCondition(client, 'core', '# peers connected', 1))
109     check.add(StatisticsCondition(client, 'topology', '# peers connected', 1))
110     check.add(StatisticsCondition(client, 'dht', '# peers connected', 1))
111     check.add(StatisticsCondition(client, 'fs', '# peers connected', 1))
112
113     check.add(StatisticsCondition(server, 'transport', '# peers connected', 1))
114     check.add(StatisticsCondition(server, 'core', '# peers connected', 1))
115     check.add(StatisticsCondition(server, 'topology', '# peers connected', 1))
116     check.add(StatisticsCondition(server, 'dht', '# peers connected', 1))
117     check.add(StatisticsCondition(server, 'fs', '# peers connected', 1))
118
119     check.run_blocking(check_timeout, success_cont, fail_cont)
120
121 #
122 # Test execution
123 #
124
125
126 def SigHandler(signum=None, frame=None):
127     global success
128     global server
129     global client
130
131     print('Test was aborted!')
132     if (None != server):
133         server.stop()
134     if (None != client):
135         client.stop()
136     cleanup()
137     sys.exit(success)
138
139
140 def run():
141     global success
142     global test
143     global server
144     global client
145
146     server = None
147     client = None
148     success = False
149
150     for sig in signals:
151         signal.signal(sig, SigHandler)
152
153     test = Test('test_integration_bootstrap_and_connect.py', verbose)
154     cleanup()
155
156     server = Peer(test, './confs/c_bootstrap_server.conf')
157     client = Peer(test, './confs/c_no_nat_client.conf')
158
159     if (True != server.start()):
160         print('Failed to start server')
161         if (None != server):
162             server.stop()
163         if (None != server):
164             client.stop()
165         cleanup()
166         sys.exit(success)
167
168     # Give the server time to start
169     time.sleep(5)
170
171     if (True != client.start()):
172         print('Failed to start client')
173         if (None != server):
174             server.stop()
175         if (None != server):
176             client.stop()
177         cleanup()
178         sys.exit(success)
179
180     if ((client.started == True) and (server.started == True)):
181         test.p('Peers started, running check')
182         time.sleep(5)
183         check()
184     server.stop()
185     client.stop()
186
187     cleanup()
188
189     if (success == False):
190         print('Test failed')
191         return False
192     else:
193         return True
194
195
196 try:
197     run()
198 except (KeyboardInterrupt, SystemExit):
199     print('Test interrupted')
200     server.stop()
201     client.stop()
202     cleanup()
203 if (success == False):
204     sys.exit(1)
205 else:
206     sys.exit(0)