Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: media/tools/constrained_network_server/cns_test.py

Issue 10824173: Enable CNS to serve files from different port. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 self._current_time += self._EXPIRY_TIME 110 self._current_time += self._EXPIRY_TIME
111 111
112 # Send second Get() which would normally cause ports to expire. Ensure that 112 # Send second Get() which would normally cause ports to expire. Ensure that
113 # the ports did not expire. 113 # the ports did not expire.
114 self.assertEquals(self._pa.Get('test2'), cns._DEFAULT_CNS_PORT_RANGE[0] + 1) 114 self.assertEquals(self._pa.Get('test2'), cns._DEFAULT_CNS_PORT_RANGE[0] + 1)
115 self.assertEquals(set(self._pa._ports.keys()), set([ 115 self.assertEquals(set(self._pa._ports.keys()), set([
116 cns._DEFAULT_CNS_PORT_RANGE[0], cns._DEFAULT_CNS_PORT_RANGE[0] + 1])) 116 cns._DEFAULT_CNS_PORT_RANGE[0], cns._DEFAULT_CNS_PORT_RANGE[0] + 1]))
117 117
118 118
119 class ConstrainedNetworkServerTest(unittest.TestCase): 119 class ConstrainedNetworkServerTest(unittest.TestCase):
120 """End to end tests for ConstrainedNetworkServer system.""" 120 """End to end tests for ConstrainedNetworkServer system.
121
122 These tests require root access and run the cherrypy server along with
123 tc/iptables commands.
124 """
121 125
122 # Amount of time to wait for the CNS to start up. 126 # Amount of time to wait for the CNS to start up.
123 _SERVER_START_SLEEP_SECS = 1 127 _SERVER_START_SLEEP_SECS = 1
124 128
125 # Sample data used to verify file serving. 129 # Sample data used to verify file serving.
126 _TEST_DATA = 'The quick brown fox jumps over the lazy dog' 130 _TEST_DATA = 'The quick brown fox jumps over the lazy dog'
127 131
128 # Server information. 132 # Server information.
129 _SERVER_URL = ('http://127.0.0.1:%d/ServeConstrained?' % 133 _SERVER_URL = ('http://127.0.0.1:%d/ServeConstrained?' %
130 cns._DEFAULT_SERVING_PORT) 134 cns._DEFAULT_SERVING_PORT)
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 url = '%s&latency=%d' % (base_url, self._LATENCY_TEST_SECS * 1000) 189 url = '%s&latency=%d' % (base_url, self._LATENCY_TEST_SECS * 1000)
186 f = urllib2.urlopen(url) 190 f = urllib2.urlopen(url)
187 191
188 # Verify file data is served correctly. 192 # Verify file data is served correctly.
189 self.assertEqual(self._TEST_DATA, f.read()) 193 self.assertEqual(self._TEST_DATA, f.read())
190 194
191 # Verify the request took longer than the requested latency. 195 # Verify the request took longer than the requested latency.
192 self.assertTrue(time.time() - now > self._LATENCY_TEST_SECS) 196 self.assertTrue(time.time() - now > self._LATENCY_TEST_SECS)
193 197
194 # Verify the server properly redirected the URL. 198 # Verify the server properly redirected the URL.
195 self.assertEquals(f.geturl(), base_url.replace( 199 self.assertTrue(f.geturl().startswith(base_url.replace(
196 str(cns._DEFAULT_SERVING_PORT), str(cns._DEFAULT_CNS_PORT_RANGE[0]))) 200 str(cns._DEFAULT_SERVING_PORT), str(cns._DEFAULT_CNS_PORT_RANGE[0]))))
201
202
203 class ConstrainedNetworkServerUnitTests(unittest.TestCase):
204 """ConstrainedNetworkServer class unit tests."""
205
206 def testGetServerURL(self):
207 """Test server URL is correct when using Cherrypy port."""
208 cns_obj = cns.ConstrainedNetworkServer(self.DummyOptions(), None)
209
210 self.assertEqual(cns_obj._GetServerURL('ab/xz.webm', port=1234, t=1),
211 'http://127.0.0.1:1234/ServeConstrained?f=ab/xz.webm&t=1')
212
213 def testGetServerURLWithLocalServer(self):
214 """Test server URL is correct when using --local-server-port port."""
215 cns_obj = cns.ConstrainedNetworkServer(self.DummyOptionsWithServer(), None)
216
217 self.assertEqual(cns_obj._GetServerURL('ab/xz.webm', port=1234, t=1),
218 'http://127.0.0.1:1234/media/ab/xz.webm?t=1')
219
220 class DummyOptions(object):
221 www_root = 'media'
222 port = 9000
223 cherrypy.url = lambda: 'http://127.0.0.1:9000/ServeConstrained'
224 local_server_port = None
225
226 class DummyOptionsWithServer(object):
227 www_root = 'media'
228 port = 9000
229 cherrypy.url = lambda: 'http://127.0.0.1:9000/ServeConstrained'
230 local_server_port = 8080
197 231
198 232
199 if __name__ == '__main__': 233 if __name__ == '__main__':
200 unittest.main() 234 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698