OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 |
| 3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 |
| 7 """Tests for Constrained Network Server.""" |
| 8 import os |
| 9 import signal |
| 10 import subprocess |
| 11 import tempfile |
| 12 import time |
| 13 import unittest |
| 14 import urllib2 |
| 15 |
| 16 import cns |
| 17 |
| 18 |
| 19 class PortAllocatorTest(unittest.TestCase): |
| 20 """Unit tests for the Port Allocator class.""" |
| 21 |
| 22 # Expiration time for ports. In mock time. |
| 23 _EXPIRY_TIME = 6 |
| 24 |
| 25 def setUp(self): |
| 26 # Mock out time.time() to accelerate port expiration testing. |
| 27 self._old_time = time.time |
| 28 self._current_time = 0 |
| 29 time.time = lambda: self._current_time |
| 30 |
| 31 # TODO(dalecurtis): Mock out actual calls to shadi's port setup. |
| 32 self._pa = cns.PortAllocator(cns._DEFAULT_CNS_PORT_RANGE, self._EXPIRY_TIME) |
| 33 |
| 34 def tearDown(self): |
| 35 self._pa.Cleanup(all_ports=True) |
| 36 # Ensure ports are cleaned properly. |
| 37 self.assertEquals(self._pa._ports, {}) |
| 38 time.time = self._old_time |
| 39 |
| 40 def testPortAllocator(self): |
| 41 # Ensure Get() succeeds and returns the correct port. |
| 42 self.assertEquals(self._pa.Get('test'), cns._DEFAULT_CNS_PORT_RANGE[0]) |
| 43 |
| 44 # Call again with the same key and make sure we get the same port. |
| 45 self.assertEquals(self._pa.Get('test'), cns._DEFAULT_CNS_PORT_RANGE[0]) |
| 46 |
| 47 # Call with a different key and make sure we get a different port. |
| 48 self.assertEquals(self._pa.Get('test2'), cns._DEFAULT_CNS_PORT_RANGE[0] + 1) |
| 49 |
| 50 # Update fake time so that ports should expire. |
| 51 self._current_time += self._EXPIRY_TIME + 1 |
| 52 |
| 53 # Test to make sure cache is checked before expiring ports. |
| 54 self.assertEquals(self._pa.Get('test2'), cns._DEFAULT_CNS_PORT_RANGE[0] + 1) |
| 55 |
| 56 # Update fake time so that ports should expire. |
| 57 self._current_time += self._EXPIRY_TIME + 1 |
| 58 |
| 59 # Request a new port, old ports should be expired, so we should get the |
| 60 # first port in the range. Make sure this is the only allocated port. |
| 61 self.assertEquals(self._pa.Get('test3'), cns._DEFAULT_CNS_PORT_RANGE[0]) |
| 62 self.assertEquals(self._pa._ports.keys(), [cns._DEFAULT_CNS_PORT_RANGE[0]]) |
| 63 |
| 64 def testPortAllocatorExpiresOnlyCorrectPorts(self): |
| 65 # Ensure Get() succeeds and returns the correct port. |
| 66 self.assertEquals(self._pa.Get('test'), cns._DEFAULT_CNS_PORT_RANGE[0]) |
| 67 |
| 68 # Stagger port allocation and so we can ensure only ports older than the |
| 69 # expiry time are actually expired. |
| 70 self._current_time += self._EXPIRY_TIME / 2 + 1 |
| 71 |
| 72 # Call with a different key and make sure we get a different port. |
| 73 self.assertEquals(self._pa.Get('test2'), cns._DEFAULT_CNS_PORT_RANGE[0] + 1) |
| 74 |
| 75 # After this sleep the port with key 'test' should expire on the next Get(). |
| 76 self._current_time += self._EXPIRY_TIME / 2 + 1 |
| 77 |
| 78 # Call with a different key and make sure we get the first port. |
| 79 self.assertEquals(self._pa.Get('test3'), cns._DEFAULT_CNS_PORT_RANGE[0]) |
| 80 self.assertEquals(set(self._pa._ports.keys()), set([ |
| 81 cns._DEFAULT_CNS_PORT_RANGE[0], cns._DEFAULT_CNS_PORT_RANGE[0] + 1])) |
| 82 |
| 83 |
| 84 class ConstrainedNetworkServerTest(unittest.TestCase): |
| 85 """End to end tests for ConstrainedNetworkServer system.""" |
| 86 |
| 87 # Amount of time to wait for the CNS to start up. |
| 88 _SERVER_START_SLEEP_SECS = 1 |
| 89 |
| 90 # Sample data used to verify file serving. |
| 91 _TEST_DATA = 'The quick brown fox jumps over the lazy dog' |
| 92 |
| 93 # Server information. |
| 94 _SERVER_URL = ('http://127.0.0.1:%d/ServeConstrained?' % |
| 95 cns._DEFAULT_SERVING_PORT) |
| 96 |
| 97 # Setting for latency testing. |
| 98 _LATENCY_TEST_SECS = 5 |
| 99 |
| 100 def _StartServer(self): |
| 101 """Starts the CNS, returns pid.""" |
| 102 cmd = ['python', 'cns.py'] |
| 103 process = subprocess.Popen(cmd, stderr=subprocess.PIPE) |
| 104 |
| 105 # Wait for server to startup. |
| 106 line = True |
| 107 while line: |
| 108 line = process.stderr.readline() |
| 109 if 'STARTED' in line: |
| 110 return process.pid |
| 111 |
| 112 self.fail('Failed to start CNS.') |
| 113 |
| 114 def setUp(self): |
| 115 # Start the CNS. |
| 116 self._server_pid = self._StartServer() |
| 117 |
| 118 # Create temp file for serving. Run after server start so if a failure |
| 119 # during setUp() occurs we don't leave junk files around. |
| 120 f, self._file = tempfile.mkstemp(dir=os.getcwd()) |
| 121 os.write(f, self._TEST_DATA) |
| 122 os.close(f) |
| 123 |
| 124 # Strip cwd off so we have a proper relative path. |
| 125 self._relative_fn = self._file[len(os.getcwd()) + 1:] |
| 126 |
| 127 def tearDown(self): |
| 128 os.unlink(self._file) |
| 129 os.kill(self._server_pid, signal.SIGKILL) |
| 130 |
| 131 def testServerServesFiles(self): |
| 132 now = time.time() |
| 133 |
| 134 f = urllib2.urlopen('%sf=%s' % (self._SERVER_URL, self._relative_fn)) |
| 135 |
| 136 # Verify file data is served correctly. |
| 137 self.assertEqual(self._TEST_DATA, f.read()) |
| 138 |
| 139 # For completeness ensure an unconstrained call takes less time than our |
| 140 # artificial constraints checked in the tests below. |
| 141 self.assertTrue(time.time() - now < self._LATENCY_TEST_SECS) |
| 142 |
| 143 def testServerLatencyConstraint(self): |
| 144 now = time.time() |
| 145 |
| 146 base_url = '%sf=%s' % (self._SERVER_URL, self._relative_fn) |
| 147 url = '%s&latency=%d' % (base_url, self._LATENCY_TEST_SECS * 1000) |
| 148 f = urllib2.urlopen(url) |
| 149 |
| 150 # Verify file data is served correctly. |
| 151 self.assertEqual(self._TEST_DATA, f.read()) |
| 152 |
| 153 # Verify the request took longer than the requested latency. |
| 154 self.assertTrue(time.time() - now > self._LATENCY_TEST_SECS) |
| 155 |
| 156 # Verify the server properly redirected the URL. |
| 157 self.assertEquals(f.geturl(), base_url.replace( |
| 158 str(cns._DEFAULT_SERVING_PORT), str(cns._DEFAULT_CNS_PORT_RANGE[0]))) |
| 159 |
| 160 |
| 161 if __name__ == '__main__': |
| 162 unittest.main() |
OLD | NEW |