OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Regression tests for devserver.""" | 7 """Regression tests for devserver.""" |
8 | 8 |
9 import os | 9 import os |
10 import signal | 10 import signal |
11 import shutil | 11 import shutil |
12 import subprocess | 12 import subprocess |
13 import sys | 13 import sys |
14 import time | 14 import time |
15 import unittest | 15 import unittest |
16 import urllib2 | 16 import urllib2 |
17 from xml.dom import minidom | 17 from xml.dom import minidom |
18 | 18 |
19 # Paths are relative to this script's base directory. | 19 # Paths are relative to this script's base directory. |
20 STATIC_DIR = 'static' | 20 STATIC_DIR = 'static' |
21 TEST_IMAGE = 'testdata/devserver/developer-test.gz' | 21 TEST_IMAGE_PATH = 'testdata/devserver' |
| 22 TEST_IMAGE_NAME = 'developer-test.gz' |
| 23 TEST_IMAGE = TEST_IMAGE_PATH + '/' + TEST_IMAGE_NAME |
22 TEST_FACTORY_CONFIG = 'testdata/devserver/miniomaha-test.conf' | 24 TEST_FACTORY_CONFIG = 'testdata/devserver/miniomaha-test.conf' |
| 25 TEST_DATA_PATH = '/tmp/devserver-test' |
23 | 26 |
24 # TODO(girts): Get a copy of a recent request. For now, I copied this from | 27 # TODO(girts): Get a copy of a recent request. For now, I copied this from |
25 # update_test. | 28 # update_test. |
26 UPDATE_REQUEST = """<?xml version="1.0" encoding="UTF-8"?> | 29 UPDATE_REQUEST = """<?xml version="1.0" encoding="UTF-8"?> |
27 <o:gupdate | 30 <o:gupdate |
28 xmlns:o="http://www.google.com/update2/request" | 31 xmlns:o="http://www.google.com/update2/request" |
29 version="MementoSoftwareUpdate-0.1.0.0" | 32 version="MementoSoftwareUpdate-0.1.0.0" |
30 protocol="2.0" | 33 protocol="2.0" |
31 machineid="{1B0A13AC-7004-638C-3CA6-EC082E8F5DE9}" | 34 machineid="{1B0A13AC-7004-638C-3CA6-EC082E8F5DE9}" |
32 ismachine="0" | 35 ismachine="0" |
(...skipping 22 matching lines...) Expand all Loading... |
55 | 58 |
56 class DevserverTest(unittest.TestCase): | 59 class DevserverTest(unittest.TestCase): |
57 """Regressions tests for devserver.""" | 60 """Regressions tests for devserver.""" |
58 | 61 |
59 def setUp(self): | 62 def setUp(self): |
60 """Copies in testing files.""" | 63 """Copies in testing files.""" |
61 | 64 |
62 # Copy in developer-test.gz, as "static/" directory is hardcoded, and it | 65 # Copy in developer-test.gz, as "static/" directory is hardcoded, and it |
63 # would be very hard to change it (static file serving is handled deep | 66 # would be very hard to change it (static file serving is handled deep |
64 # inside webpy). | 67 # inside webpy). |
65 image_src = os.path.join(base_dir, TEST_IMAGE) | 68 self.image_src = os.path.join(base_dir, TEST_IMAGE) |
66 self.image = os.path.join(base_dir, STATIC_DIR, 'developer-test.gz') | 69 self.image = os.path.join(base_dir, STATIC_DIR, TEST_IMAGE_NAME) |
67 if os.path.exists(self.image): | 70 if os.path.exists(self.image): |
68 os.unlink(self.image) | 71 os.unlink(self.image) |
69 shutil.copy(image_src, self.image) | 72 shutil.copy(self.image_src, self.image) |
70 | 73 |
71 self.factory_config = os.path.join(base_dir, TEST_FACTORY_CONFIG) | 74 self.factory_config = os.path.join(base_dir, TEST_FACTORY_CONFIG) |
72 | 75 |
73 def tearDown(self): | 76 def tearDown(self): |
74 """Removes testing files.""" | 77 """Removes testing files.""" |
75 if os.path.exists(self.image): | 78 if os.path.exists(self.image): |
76 os.unlink(self.image) | 79 os.unlink(self.image) |
77 | 80 |
78 def testValidateFactoryConfig(self): | 81 def testValidateFactoryConfig(self): |
79 """Tests --validate_factory_config.""" | 82 """Tests --validate_factory_config.""" |
80 cmd = [ | 83 cmd = [ |
81 'python', | 84 'python', |
82 os.path.join(base_dir, 'devserver.py'), | 85 os.path.join(base_dir, 'devserver.py'), |
83 '--validate_factory_config', | 86 '--validate_factory_config', |
84 '--factory_config', self.factory_config, | 87 '--factory_config', self.factory_config, |
85 ] | 88 ] |
86 process = subprocess.Popen(cmd, stdout=subprocess.PIPE) | 89 process = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
87 stdout, _ = process.communicate() | 90 stdout, _ = process.communicate() |
88 self.assertEqual(0, process.returncode) | 91 self.assertEqual(0, process.returncode) |
89 self.assertTrue('Config file looks good.' in stdout) | 92 self.assertTrue('Config file looks good.' in stdout) |
90 | 93 |
91 def _StartServer(self): | 94 def _StartServer(self, data_dir=''): |
92 """Starts devserver, returns process.""" | 95 """Starts devserver, returns process.""" |
93 cmd = [ | 96 cmd = [ |
94 'python', | 97 'python', |
95 os.path.join(base_dir, 'devserver.py'), | 98 os.path.join(base_dir, 'devserver.py'), |
96 'devserver.py', | 99 'devserver.py', |
97 '--factory_config', self.factory_config, | 100 '--factory_config', self.factory_config, |
98 ] | 101 ] |
| 102 if data_dir: |
| 103 cmd.append('--data_dir') |
| 104 cmd.append(data_dir) |
99 process = subprocess.Popen(cmd) | 105 process = subprocess.Popen(cmd) |
100 return process.pid | 106 return process.pid |
101 | 107 |
102 def testHandleUpdate(self): | 108 def testHandleUpdate(self): |
103 """Tests running the server and getting an update.""" | 109 """Tests running the server and getting an update.""" |
104 pid = self._StartServer() | 110 pid = self._StartServer() |
105 try: | 111 try: |
106 # Wait for the server to start up. | 112 # Wait for the server to start up. |
107 time.sleep(1) | 113 time.sleep(1) |
108 request = urllib2.Request(UPDATE_URL, UPDATE_REQUEST) | 114 request = urllib2.Request(UPDATE_URL, UPDATE_REQUEST) |
109 connection = urllib2.urlopen(request) | 115 connection = urllib2.urlopen(request) |
110 response = connection.read() | 116 response = connection.read() |
| 117 connection.close() |
111 self.assertNotEqual('', response) | 118 self.assertNotEqual('', response) |
112 | 119 |
113 # Parse the response and check if it contains the right result. | 120 # Parse the response and check if it contains the right result. |
114 dom = minidom.parseString(response) | 121 dom = minidom.parseString(response) |
115 update = dom.getElementsByTagName('updatecheck')[0] | 122 update = dom.getElementsByTagName('updatecheck')[0] |
116 | 123 |
117 codebase = update.getAttribute('codebase') | 124 codebase = update.getAttribute('codebase') |
118 self.assertEqual('http://127.0.0.1:8080/static/developer-test.gz', | 125 self.assertEqual('http://127.0.0.1:8080/static/' + TEST_IMAGE_NAME, |
119 codebase) | 126 codebase) |
120 | 127 |
121 hash_value = update.getAttribute('hash') | 128 hash_value = update.getAttribute('hash') |
122 self.assertEqual('kGcOinJ0vA8vdYX53FN0F5BdwfY=', hash_value) | 129 self.assertEqual('kGcOinJ0vA8vdYX53FN0F5BdwfY=', hash_value) |
123 | 130 |
124 # Try to fetch the image. | 131 # Try to fetch the image. |
125 connection = urllib2.urlopen(codebase) | 132 connection = urllib2.urlopen(codebase) |
126 contents = connection.read() | 133 contents = connection.read() |
| 134 connection.close() |
127 self.assertEqual('Developers, developers, developers!\n', contents) | 135 self.assertEqual('Developers, developers, developers!\n', contents) |
128 finally: | 136 finally: |
129 os.kill(pid, signal.SIGKILL) | 137 os.kill(pid, signal.SIGKILL) |
130 | 138 |
| 139 def testHandleDatadirUpdate(self): |
| 140 """Tests getting an update from a specified datadir""" |
| 141 # Push the image to the expected path where devserver picks it up. |
| 142 image_path = os.path.join(TEST_DATA_PATH, STATIC_DIR) |
| 143 if not os.path.exists(image_path): |
| 144 os.makedirs(image_path) |
| 145 |
| 146 foreign_image = os.path.join(image_path, TEST_IMAGE_NAME) |
| 147 if os.path.exists(foreign_image): |
| 148 os.unlink(foreign_image) |
| 149 shutil.copy(self.image_src, foreign_image) |
| 150 |
| 151 pid = self._StartServer(data_dir=TEST_DATA_PATH) |
| 152 try: |
| 153 # Wait for the server to start up. |
| 154 time.sleep(1) |
| 155 |
| 156 request = urllib2.Request(UPDATE_URL, UPDATE_REQUEST) |
| 157 connection = urllib2.urlopen(request) |
| 158 response = connection.read() |
| 159 connection.close() |
| 160 self.assertNotEqual('', response) |
| 161 |
| 162 # Parse the response and check if it contains the right result. |
| 163 dom = minidom.parseString(response) |
| 164 update = dom.getElementsByTagName('updatecheck')[0] |
| 165 |
| 166 codebase = update.getAttribute('codebase') |
| 167 self.assertEqual('http://127.0.0.1:8080/static/' + TEST_IMAGE_NAME, |
| 168 codebase) |
| 169 |
| 170 hash_value = update.getAttribute('hash') |
| 171 self.assertEqual('kGcOinJ0vA8vdYX53FN0F5BdwfY=', hash_value) |
| 172 |
| 173 # Try to fetch the image. |
| 174 connection = urllib2.urlopen(codebase) |
| 175 contents = connection.read() |
| 176 connection.close() |
| 177 self.assertEqual('Developers, developers, developers!\n', contents) |
| 178 os.unlink(foreign_image) |
| 179 finally: |
| 180 os.kill(pid, signal.SIGKILL) |
| 181 |
131 | 182 |
132 if __name__ == '__main__': | 183 if __name__ == '__main__': |
133 unittest.main() | 184 unittest.main() |
OLD | NEW |