OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Tests for Constrained Network Server.""" | 6 """Tests for Constrained Network Server.""" |
7 import os | 7 import os |
8 import signal | 8 import signal |
9 import subprocess | 9 import subprocess |
10 import tempfile | 10 import tempfile |
11 import time | 11 import time |
12 import unittest | 12 import unittest |
13 import urllib2 | 13 import urllib2 |
14 | 14 import cherrypy |
15 import cns | 15 import cns |
16 import traffic_control | 16 import traffic_control |
17 | 17 |
18 # The local interface to test on. | 18 # The local interface to test on. |
19 _INTERFACE = 'lo' | 19 _INTERFACE = 'lo' |
20 | 20 |
21 | 21 |
22 class PortAllocatorTest(unittest.TestCase): | 22 class PortAllocatorTest(unittest.TestCase): |
23 """Unit tests for the Port Allocator class.""" | 23 """Unit tests for the Port Allocator class.""" |
24 | 24 |
25 # Expiration time for ports. In mock time. | 25 # Expiration time for ports. In mock time. |
26 _EXPIRY_TIME = 6 | 26 _EXPIRY_TIME = 6 |
27 | 27 |
28 def setUp(self): | 28 def setUp(self): |
29 # Mock out time.time() to accelerate port expiration testing. | 29 # Mock out time.time() to accelerate port expiration testing. |
30 self._old_time = time.time | 30 self._old_time = time.time |
31 self._current_time = 0 | 31 self._current_time = 0 |
32 time.time = lambda: self._current_time | 32 time.time = lambda: self._current_time |
33 | 33 |
34 # TODO(dalecurtis): Mock out actual calls to shadi's port setup. | 34 # TODO(dalecurtis): Mock out actual calls to shadi's port setup. |
35 self._pa = cns.PortAllocator(cns._DEFAULT_CNS_PORT_RANGE, self._EXPIRY_TIME) | 35 self._pa = cns.PortAllocator(cns._DEFAULT_CNS_PORT_RANGE, self._EXPIRY_TIME) |
36 self._MockTrafficControl() | 36 self._MockTrafficControl() |
37 | 37 |
38 def tearDown(self): | 38 def tearDown(self): |
39 self._pa.Cleanup(_INTERFACE, all_ports=True) | 39 self._pa.Cleanup(all_ports=True) |
40 # Ensure ports are cleaned properly. | 40 # Ensure ports are cleaned properly. |
41 self.assertEquals(self._pa._ports, {}) | 41 self.assertEquals(self._pa._ports, {}) |
42 time.time = self._old_time | 42 time.time = self._old_time |
43 self._RestoreTrafficControl() | 43 self._RestoreTrafficControl() |
44 | 44 |
45 def _MockTrafficControl(self): | 45 def _MockTrafficControl(self): |
46 self.old_CreateConstrainedPort = traffic_control.CreateConstrainedPort | 46 self.old_CreateConstrainedPort = traffic_control.CreateConstrainedPort |
47 self.old_DeleteConstrainedPort = traffic_control.DeleteConstrainedPort | 47 self.old_DeleteConstrainedPort = traffic_control.DeleteConstrainedPort |
48 self.old_TearDown = traffic_control.TearDown | 48 self.old_TearDown = traffic_control.TearDown |
49 | 49 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 | 141 |
142 self._pa.Cleanup(all_ports=False, request_ip='abc') | 142 self._pa.Cleanup(all_ports=False, request_ip='abc') |
143 self.assertEquals(self._pa._ports.keys(), | 143 self.assertEquals(self._pa._ports.keys(), |
144 [cns._DEFAULT_CNS_PORT_RANGE[0] + 1]) | 144 [cns._DEFAULT_CNS_PORT_RANGE[0] + 1]) |
145 | 145 |
146 self._pa.Cleanup(all_ports=False, request_ip='ip1') | 146 self._pa.Cleanup(all_ports=False, request_ip='ip1') |
147 self.assertEquals(self._pa._ports.keys(), []) | 147 self.assertEquals(self._pa._ports.keys(), []) |
148 | 148 |
149 | 149 |
150 class ConstrainedNetworkServerTest(unittest.TestCase): | 150 class ConstrainedNetworkServerTest(unittest.TestCase): |
151 """End to end tests for ConstrainedNetworkServer system.""" | 151 """End to end tests for ConstrainedNetworkServer system. |
| 152 |
| 153 These tests require root access and run the cherrypy server along with |
| 154 tc/iptables commands. |
| 155 """ |
152 | 156 |
153 # Amount of time to wait for the CNS to start up. | 157 # Amount of time to wait for the CNS to start up. |
154 _SERVER_START_SLEEP_SECS = 1 | 158 _SERVER_START_SLEEP_SECS = 1 |
155 | 159 |
156 # Sample data used to verify file serving. | 160 # Sample data used to verify file serving. |
157 _TEST_DATA = 'The quick brown fox jumps over the lazy dog' | 161 _TEST_DATA = 'The quick brown fox jumps over the lazy dog' |
158 | 162 |
159 # Server information. | 163 # Server information. |
160 _SERVER_URL = ('http://127.0.0.1:%d/ServeConstrained?' % | 164 _SERVER_URL = ('http://127.0.0.1:%d/ServeConstrained?' % |
161 cns._DEFAULT_SERVING_PORT) | 165 cns._DEFAULT_SERVING_PORT) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 url = '%s&latency=%d' % (base_url, self._LATENCY_TEST_SECS * 1000) | 220 url = '%s&latency=%d' % (base_url, self._LATENCY_TEST_SECS * 1000) |
217 f = urllib2.urlopen(url) | 221 f = urllib2.urlopen(url) |
218 | 222 |
219 # Verify file data is served correctly. | 223 # Verify file data is served correctly. |
220 self.assertEqual(self._TEST_DATA, f.read()) | 224 self.assertEqual(self._TEST_DATA, f.read()) |
221 | 225 |
222 # Verify the request took longer than the requested latency. | 226 # Verify the request took longer than the requested latency. |
223 self.assertTrue(time.time() - now > self._LATENCY_TEST_SECS) | 227 self.assertTrue(time.time() - now > self._LATENCY_TEST_SECS) |
224 | 228 |
225 # Verify the server properly redirected the URL. | 229 # Verify the server properly redirected the URL. |
226 self.assertEquals(f.geturl(), base_url.replace( | 230 self.assertTrue(f.geturl().startswith(base_url.replace( |
227 str(cns._DEFAULT_SERVING_PORT), str(cns._DEFAULT_CNS_PORT_RANGE[0]))) | 231 str(cns._DEFAULT_SERVING_PORT), str(cns._DEFAULT_CNS_PORT_RANGE[0])))) |
| 232 |
| 233 |
| 234 class ConstrainedNetworkServerUnitTests(unittest.TestCase): |
| 235 """ConstrainedNetworkServer class unit tests.""" |
| 236 |
| 237 def testGetServerURL(self): |
| 238 """Test server URL is correct when using Cherrypy port.""" |
| 239 cns_obj = cns.ConstrainedNetworkServer(self.DummyOptions(), None) |
| 240 |
| 241 self.assertEqual(cns_obj._GetServerURL('ab/xz.webm', port=1234, t=1), |
| 242 'http://127.0.0.1:1234/ServeConstrained?f=ab/xz.webm&t=1') |
| 243 |
| 244 def testGetServerURLWithLocalServer(self): |
| 245 """Test server URL is correct when using --local-server-port port.""" |
| 246 cns_obj = cns.ConstrainedNetworkServer(self.DummyOptionsWithServer(), None) |
| 247 |
| 248 self.assertEqual(cns_obj._GetServerURL('ab/xz.webm', port=1234, t=1), |
| 249 'http://127.0.0.1:1234/media/ab/xz.webm?t=1') |
| 250 |
| 251 class DummyOptions(object): |
| 252 www_root = 'media' |
| 253 port = 9000 |
| 254 cherrypy.url = lambda: 'http://127.0.0.1:9000/ServeConstrained' |
| 255 local_server_port = None |
| 256 |
| 257 class DummyOptionsWithServer(object): |
| 258 www_root = 'media' |
| 259 port = 9000 |
| 260 cherrypy.url = lambda: 'http://127.0.0.1:9000/ServeConstrained' |
| 261 local_server_port = 8080 |
228 | 262 |
229 | 263 |
230 if __name__ == '__main__': | 264 if __name__ == '__main__': |
231 unittest.main() | 265 unittest.main() |
OLD | NEW |