forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcns_test.py
executable file
·300 lines (232 loc) · 10.4 KB
/
cns_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env vpython
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# [VPYTHON:BEGIN]
# wheel: <
# name: "infra/python/wheels/pytz-py2_py3"
# version: "version:2018.4"
# >
# wheel: <
# name: "infra/python/wheels/tempora-py2_py3"
# version: "version:1.11"
# >
# wheel: <
# name: "infra/python/wheels/more-itertools-py2_py3"
# version: "version:4.1.0"
# >
# wheel: <
# name: "infra/python/wheels/backports_functools_lru_cache-py2_py3"
# version: "version:1.5"
# >
# wheel: <
# name: "infra/python/wheels/six-py2_py3"
# version: "version:1.12.0"
# >
# wheel: <
# name: "infra/python/wheels/portend-py2_py3"
# version: "version:2.2"
# >
# wheel: <
# name: "infra/python/wheels/cheroot-py2_py3"
# version: "version:6.2.4"
# >
# wheel: <
# name: "infra/python/wheels/cherrypy-py2_py3"
# version: "version:14.2.0"
# >
# [VPYTHON:END]
"""Tests for Constrained Network Server."""
import os
import signal
import subprocess
import tempfile
import time
import unittest
import urllib2
import cherrypy
import cns
import traffic_control
# The local interface to test on.
_INTERFACE = 'lo'
class PortAllocatorTest(unittest.TestCase):
"""Unit tests for the Port Allocator class."""
# Expiration time for ports. In mock time.
_EXPIRY_TIME = 6
def setUp(self):
# Mock out time.time() to accelerate port expiration testing.
self._old_time = time.time
self._current_time = 0
time.time = lambda: self._current_time
# TODO(dalecurtis): Mock out actual calls to shadi's port setup.
self._pa = cns.PortAllocator(cns._DEFAULT_CNS_PORT_RANGE, self._EXPIRY_TIME)
self._MockTrafficControl()
def tearDown(self):
self._pa.Cleanup(all_ports=True)
# Ensure ports are cleaned properly.
self.assertEquals(self._pa._ports, {})
time.time = self._old_time
self._RestoreTrafficControl()
def _MockTrafficControl(self):
self.old_CreateConstrainedPort = traffic_control.CreateConstrainedPort
self.old_DeleteConstrainedPort = traffic_control.DeleteConstrainedPort
self.old_TearDown = traffic_control.TearDown
traffic_control.CreateConstrainedPort = lambda config: True
traffic_control.DeleteConstrainedPort = lambda config: True
traffic_control.TearDown = lambda config: True
def _RestoreTrafficControl(self):
traffic_control.CreateConstrainedPort = self.old_CreateConstrainedPort
traffic_control.DeleteConstrainedPort = self.old_DeleteConstrainedPort
traffic_control.TearDown = self.old_TearDown
def testPortAllocator(self):
# Ensure Get() succeeds and returns the correct port.
self.assertEquals(self._pa.Get('test'), cns._DEFAULT_CNS_PORT_RANGE[0])
# Call again with the same key and make sure we get the same port.
self.assertEquals(self._pa.Get('test'), cns._DEFAULT_CNS_PORT_RANGE[0])
# Call with a different key and make sure we get a different port.
self.assertEquals(self._pa.Get('test2'), cns._DEFAULT_CNS_PORT_RANGE[0] + 1)
# Update fake time so that ports should expire.
self._current_time += self._EXPIRY_TIME + 1
# Test to make sure cache is checked before expiring ports.
self.assertEquals(self._pa.Get('test2'), cns._DEFAULT_CNS_PORT_RANGE[0] + 1)
# Update fake time so that ports should expire.
self._current_time += self._EXPIRY_TIME + 1
# Request a new port, old ports should be expired, so we should get the
# first port in the range. Make sure this is the only allocated port.
self.assertEquals(self._pa.Get('test3'), cns._DEFAULT_CNS_PORT_RANGE[0])
self.assertEquals(self._pa._ports.keys(), [cns._DEFAULT_CNS_PORT_RANGE[0]])
def testPortAllocatorExpiresOnlyCorrectPorts(self):
# Ensure Get() succeeds and returns the correct port.
self.assertEquals(self._pa.Get('test'), cns._DEFAULT_CNS_PORT_RANGE[0])
# Stagger port allocation and so we can ensure only ports older than the
# expiry time are actually expired.
self._current_time += self._EXPIRY_TIME / 2 + 1
# Call with a different key and make sure we get a different port.
self.assertEquals(self._pa.Get('test2'), cns._DEFAULT_CNS_PORT_RANGE[0] + 1)
# After this sleep the port with key 'test' should expire on the next Get().
self._current_time += self._EXPIRY_TIME / 2 + 1
# Call with a different key and make sure we get the first port.
self.assertEquals(self._pa.Get('test3'), cns._DEFAULT_CNS_PORT_RANGE[0])
self.assertEquals(set(self._pa._ports.keys()), set([
cns._DEFAULT_CNS_PORT_RANGE[0], cns._DEFAULT_CNS_PORT_RANGE[0] + 1]))
def testPortAllocatorNoExpiration(self):
# Setup PortAllocator w/o port expiration.
self._pa = cns.PortAllocator(cns._DEFAULT_CNS_PORT_RANGE, 0)
# Ensure Get() succeeds and returns the correct port.
self.assertEquals(self._pa.Get('test'), cns._DEFAULT_CNS_PORT_RANGE[0])
# Update fake time to see if ports expire.
self._current_time += self._EXPIRY_TIME
# Send second Get() which would normally cause ports to expire. Ensure that
# the ports did not expire.
self.assertEquals(self._pa.Get('test2'), cns._DEFAULT_CNS_PORT_RANGE[0] + 1)
self.assertEquals(set(self._pa._ports.keys()), set([
cns._DEFAULT_CNS_PORT_RANGE[0], cns._DEFAULT_CNS_PORT_RANGE[0] + 1]))
def testPortAllocatorCleanMatchingIP(self):
# Setup PortAllocator w/o port expiration.
self._pa = cns.PortAllocator(cns._DEFAULT_CNS_PORT_RANGE, 0)
# Ensure Get() succeeds and returns the correct port.
self.assertEquals(self._pa.Get('ip1', t=1), cns._DEFAULT_CNS_PORT_RANGE[0])
self.assertEquals(self._pa.Get('ip1', t=2),
cns._DEFAULT_CNS_PORT_RANGE[0] + 1)
self.assertEquals(self._pa.Get('ip1', t=3),
cns._DEFAULT_CNS_PORT_RANGE[0] + 2)
self.assertEquals(self._pa.Get('ip2', t=1),
cns._DEFAULT_CNS_PORT_RANGE[0] + 3)
self._pa.Cleanup(all_ports=False, request_ip='ip1')
self.assertEquals(self._pa._ports.keys(),
[cns._DEFAULT_CNS_PORT_RANGE[0] + 3])
self.assertEquals(self._pa.Get('ip2'), cns._DEFAULT_CNS_PORT_RANGE[0])
self.assertEquals(self._pa.Get('ip1'), cns._DEFAULT_CNS_PORT_RANGE[0] + 1)
self._pa.Cleanup(all_ports=False, request_ip='ip2')
self.assertEquals(self._pa._ports.keys(),
[cns._DEFAULT_CNS_PORT_RANGE[0] + 1])
self._pa.Cleanup(all_ports=False, request_ip='abc')
self.assertEquals(self._pa._ports.keys(),
[cns._DEFAULT_CNS_PORT_RANGE[0] + 1])
self._pa.Cleanup(all_ports=False, request_ip='ip1')
self.assertEquals(self._pa._ports.keys(), [])
class ConstrainedNetworkServerTest(unittest.TestCase):
"""End to end tests for ConstrainedNetworkServer system.
These tests require root access and run the cherrypy server along with
tc/iptables commands.
"""
# Amount of time to wait for the CNS to start up.
_SERVER_START_SLEEP_SECS = 1
# Sample data used to verify file serving.
_TEST_DATA = 'The quick brown fox jumps over the lazy dog'
# Server information.
_SERVER_URL = ('http://127.0.0.1:%d/ServeConstrained?' %
cns._DEFAULT_SERVING_PORT)
# Setting for latency testing.
_LATENCY_TEST_SECS = 1
def _StartServer(self):
"""Starts the CNS, returns pid."""
cmd = ['python', 'cns.py', '--interface=%s' % _INTERFACE]
process = subprocess.Popen(cmd, stderr=subprocess.PIPE)
# Wait for server to startup.
line = True
while line:
line = process.stderr.readline()
if 'STARTED' in line:
return process.pid
self.fail('Failed to start CNS.')
def setUp(self):
# Start the CNS.
self._server_pid = self._StartServer()
# Create temp file for serving. Run after server start so if a failure
# during setUp() occurs we don't leave junk files around.
f, self._file = tempfile.mkstemp(dir=os.getcwd())
os.write(f, self._TEST_DATA)
os.close(f)
# Strip cwd off so we have a proper relative path.
self._relative_fn = self._file[len(os.getcwd()) + 1:]
def tearDown(self):
os.unlink(self._file)
os.kill(self._server_pid, signal.SIGTERM)
def testServerServesFiles(self):
now = time.time()
f = urllib2.urlopen('%sf=%s' % (self._SERVER_URL, self._relative_fn))
# Verify file data is served correctly.
self.assertEqual(self._TEST_DATA, f.read())
# For completeness ensure an unconstrained call takes less time than our
# artificial constraints checked in the tests below.
self.assertTrue(time.time() - now < self._LATENCY_TEST_SECS)
def testServerLatencyConstraint(self):
"""Tests serving a file with a latency network constraint."""
# Abort if does not have root access.
self.assertEqual(os.geteuid(), 0, 'You need root access to run this test.')
now = time.time()
base_url = '%sf=%s' % (self._SERVER_URL, self._relative_fn)
url = '%s&latency=%d' % (base_url, self._LATENCY_TEST_SECS * 1000)
f = urllib2.urlopen(url)
# Verify file data is served correctly.
self.assertEqual(self._TEST_DATA, f.read())
# Verify the request took longer than the requested latency.
self.assertTrue(time.time() - now > self._LATENCY_TEST_SECS)
# Verify the server properly redirected the URL.
self.assertTrue(f.geturl().startswith(base_url.replace(
str(cns._DEFAULT_SERVING_PORT), str(cns._DEFAULT_CNS_PORT_RANGE[0]))))
class ConstrainedNetworkServerUnitTests(unittest.TestCase):
"""ConstrainedNetworkServer class unit tests."""
def testGetServerURL(self):
"""Test server URL is correct when using Cherrypy port."""
cns_obj = cns.ConstrainedNetworkServer(self.DummyOptions(), None)
self.assertEqual(cns_obj._GetServerURL('ab/xz.webm', port=1234, t=1),
'http://127.0.0.1:1234/ServeConstrained?f=ab/xz.webm&t=1')
def testGetServerURLWithLocalServer(self):
"""Test server URL is correct when using --local-server-port port."""
cns_obj = cns.ConstrainedNetworkServer(self.DummyOptionsWithServer(), None)
self.assertEqual(cns_obj._GetServerURL('ab/xz.webm', port=1234, t=1),
'http://127.0.0.1:1234/media/ab/xz.webm?t=1')
class DummyOptions(object):
www_root = 'media'
port = 9000
cherrypy.url = lambda: 'http://127.0.0.1:9000/ServeConstrained'
local_server_port = None
class DummyOptionsWithServer(object):
www_root = 'media'
port = 9000
cherrypy.url = lambda: 'http://127.0.0.1:9000/ServeConstrained'
local_server_port = 8080
if __name__ == '__main__':
unittest.main()