• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2
3__author__ = """Ashwin Ganti (aganti@google.com)"""
4
5import os, sys, socket, errno, unittest, threading
6from time import time, sleep
7import common
8from autotest_lib.client.common_lib import error, base_barrier, utils
9barrier = base_barrier
10from autotest_lib.client.common_lib.test_utils import mock
11
12
13class listen_server_test(unittest.TestCase):
14
15    def setUp(self):
16        self.god = mock.mock_god()
17        self.god.stub_function(utils, 'run')
18        utils.run.expect_any_call()
19
20
21    def test_init(self):
22        server = barrier.listen_server()
23        server.close()
24
25
26    def test_close(self):
27        server = barrier.listen_server()
28        # cannot bind on the same port again
29        utils.run.expect_any_call()
30        self.assertRaises(socket.error, barrier.listen_server)
31        server.close()
32        # now we can
33        utils.run.expect_any_call()
34        server = barrier.listen_server()
35        server.close()
36
37
38class barrier_test(unittest.TestCase):
39
40    def setUp(self):
41        self.god = mock.mock_god()
42        self.god.mock_io()
43        self.god.stub_function(utils, 'run')
44        utils.run.expect_any_call()
45
46
47    def tearDown(self):
48        self.god.unmock_io()
49
50
51    def test_initialize(self):
52        b = barrier.barrier('127.0.0.1#', 'testtag', 100, 11921)
53        self.assertEqual(b._hostid, '127.0.0.1#')
54        self.assertEqual(b._tag, 'testtag')
55        self.assertEqual(b._timeout_secs, 100)
56        self.assertEqual(b._port, 11921)
57
58
59    def test_get_host_from_id(self):
60        hostname = barrier.get_host_from_id('my_host')
61        self.assertEqual(hostname, 'my_host')
62
63        hostname = barrier.get_host_from_id('my_host#')
64        self.assertEqual(hostname, 'my_host')
65
66        self.assertRaises(error.BarrierError, barrier.get_host_from_id, '#my_host')
67
68
69    def test_update_timeout(self):
70        b = barrier.barrier('127.0.0.1#', 'update', 100)
71        b._update_timeout(120)
72        self.assertEqual(b._timeout_secs, 120)
73
74
75    def test_remaining(self):
76        b = barrier.barrier('127.0.0.1#', 'remain', 100)
77        remain = b._remaining()
78        self.assertEqual(remain, 100)
79
80
81    def test_master_welcome_garbage(self):
82        b = barrier.barrier('127.0.0.1#', 'garbage', 100)
83        waiting_before = dict(b._waiting)
84        seen_before = b._seen
85
86        sender, receiver = socket.socketpair()
87        try:
88            sender.send('GET /foobar?p=-1 HTTP/1.0\r\n\r\n')
89            # This should not raise an exception.
90            b._master_welcome((receiver, 'fakeaddr'))
91
92            self.assertEqual(waiting_before, b._waiting)
93            self.assertEqual(seen_before, b._seen)
94
95            sender, receiver = socket.socketpair()
96            sender.send('abcdefg\x00\x01\x02\n'*5)
97            # This should not raise an exception.
98            b._master_welcome((receiver, 'fakeaddr'))
99
100            self.assertEqual(waiting_before, b._waiting)
101            self.assertEqual(seen_before, b._seen)
102        finally:
103            sender.close()
104            receiver.close()
105
106
107    def test_rendezvous_basic(self):
108        # Basic rendezvous testing
109        self.rendezvous_test(60, port=11920)
110
111
112    def test_rendezvous_timeout(self):
113        # The rendezvous should time out here and throw a
114        # BarrierError since we are specifying a timeout of 0
115        self.assertRaises(error.BarrierError,
116                          self.rendezvous_test, 0, port=11921)
117
118
119    def test_rendezvous_abort_ok(self):
120        # Test with abort flag set to not abort.
121        self.rendezvous_test(60, port=11920,
122                             test_abort=True, abort=False)
123
124
125    def test_rendezvous_abort(self):
126        # The rendezvous should abort here and throw a
127        # BarrierError since we are asking to abort
128        self.assertRaises(error.BarrierError,
129                          self.rendezvous_test, 0, port=11921,
130                          test_abort=True, abort=True)
131
132
133    def test_rendezvous_servers_basic(self):
134        # The rendezvous should time out here and throw a
135        # BarrierError since we are specifying a timeout of 0
136        self.rendezvous_test(60, port=11921,
137                             rendezvous_servers=True)
138
139
140    def test_rendezvous_servers_timeout(self):
141        # The rendezvous should time out here and throw a
142        # BarrierError since we are specifying a timeout of 0
143        self.assertRaises(error.BarrierError,
144                          self.rendezvous_test, 0, port=11922,
145                          rendezvous_servers=True)
146
147
148    def test_rendezvous_servers_abort_ok(self):
149        # Test with abort flag set to not abort.
150        self.rendezvous_test(60, port=11920, rendezvous_servers=True,
151                             test_abort=True, abort=False)
152
153
154    def test_rendezvous_servers_abort(self):
155        # The rendezvous should abort here and throw a
156        # BarrierError since we are asking to abort
157        self.assertRaises(error.BarrierError,
158                          self.rendezvous_test, 0, port=11922,
159                          rendezvous_servers=True,
160                          test_abort=True, abort=True)
161
162
163    # Internal utility function (not a unit test)
164    def rendezvous_test(self, timeout, port=11922,
165                        rendezvous_servers=False, test_abort=False,
166                        abort=False, listen_server=None):
167        if listen_server:
168            port = None
169
170        def _rdv(addr):
171            b1 = barrier.barrier(addr, "test_meeting", timeout, port,
172                                 listen_server=listen_server)
173            if not rendezvous_servers:
174                if test_abort:
175                    b1.rendezvous('127.0.0.1#0', '127.0.0.1#1', abort=abort)
176                else:
177                    b1.rendezvous('127.0.0.1#0', '127.0.0.1#1')
178            else:
179                if test_abort:
180                    b1.rendezvous_servers('127.0.0.1#0', '127.0.0.1#1',
181                                          abort=abort)
182                else:
183                    b1.rendezvous_servers('127.0.0.1#0', '127.0.0.1#1')
184
185
186        def _thread_rdv(addr):
187            # We need to ignore the exception on one side.
188            try:
189                _rdv(addr)
190            except error.BarrierError:
191                pass
192
193        client = threading.Thread(target=_thread_rdv,
194                                  args=('127.0.0.1#0',))
195        client.start()
196        _rdv('127.0.0.1#1')
197        client.join()
198
199
200    def test_reusing_listen_server(self):
201        """
202        Test that reusing the same listen server object works.
203        """
204        server = barrier.listen_server()
205        self.rendezvous_test(10, listen_server=server)
206        self.rendezvous_test(10, listen_server=server)
207        self.rendezvous_test(10, listen_server=server)
208
209
210if __name__ == "__main__":
211    unittest.main()
212