Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 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 |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Regression tests for devserver.""" |
| 8 | |
Chris Sosa | 65d339b | 2013-01-21 18:59:21 -0800 | [diff] [blame] | 9 | import json |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 10 | from xml.dom import minidom |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 11 | import os |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 12 | import shutil |
Gilad Arnold | abb352e | 2012-09-23 01:24:27 -0700 | [diff] [blame] | 13 | import signal |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 14 | import subprocess |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 15 | import tempfile |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 16 | import time |
| 17 | import unittest |
| 18 | import urllib2 |
Gilad Arnold | abb352e | 2012-09-23 01:24:27 -0700 | [diff] [blame] | 19 | |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 20 | |
| 21 | # Paths are relative to this script's base directory. |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 22 | LABEL = 'devserver' |
Zdenek Behan | 5d21a2a | 2011-02-12 02:06:01 +0100 | [diff] [blame] | 23 | TEST_IMAGE_PATH = 'testdata/devserver' |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 24 | TEST_IMAGE_NAME = 'update.gz' |
Zdenek Behan | 5d21a2a | 2011-02-12 02:06:01 +0100 | [diff] [blame] | 25 | TEST_IMAGE = TEST_IMAGE_PATH + '/' + TEST_IMAGE_NAME |
Jay Srinivasan | ac69d26 | 2012-10-30 19:05:53 -0700 | [diff] [blame] | 26 | EXPECTED_HASH = 'kGcOinJ0vA8vdYX53FN0F5BdwfY=' |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 27 | |
Jay Srinivasan | ac69d26 | 2012-10-30 19:05:53 -0700 | [diff] [blame] | 28 | # Update request based on Omaha v2 protocol format. |
| 29 | UPDATE_REQUEST = {} |
| 30 | UPDATE_REQUEST['2.0'] = """<?xml version="1.0" encoding="UTF-8"?> |
Greg Spencer | c8b59b2 | 2011-03-15 14:15:23 -0700 | [diff] [blame] | 31 | <o:gupdate xmlns:o="http://www.google.com/update2/request" version="ChromeOSUpdateEngine-0.1.0.0" updaterversion="ChromeOSUpdateEngine-0.1.0.0" protocol="2.0" ismachine="1"> |
| 32 | <o:os version="Indy" platform="Chrome OS" sp="0.11.254.2011_03_09_1814_i686"></o:os> |
| 33 | <o:app appid="{DEV-BUILD}" version="0.11.254.2011_03_09_1814" lang="en-US" track="developer-build" board="x86-generic" hardware_class="BETA DVT" delta_okay="true"> |
| 34 | <o:updatecheck></o:updatecheck> |
| 35 | <o:event eventtype="3" eventresult="2" previousversion="0.11.216.2011_03_02_1358"></o:event> |
| 36 | </o:app> |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 37 | </o:gupdate> |
| 38 | """ |
Jay Srinivasan | ac69d26 | 2012-10-30 19:05:53 -0700 | [diff] [blame] | 39 | |
| 40 | # Update request based on Omaha v3 protocol format. |
| 41 | UPDATE_REQUEST['3.0'] = """<?xml version="1.0" encoding="UTF-8"?> |
| 42 | <request version="ChromeOSUpdateEngine-0.1.0.0" updaterversion="ChromeOSUpdateEngine-0.1.0.0" protocol="3.0" ismachine="1"> |
| 43 | <os version="Indy" platform="Chrome OS" sp="0.11.254.2011_03_09_1814_i686"></os> |
| 44 | <app appid="{DEV-BUILD}" version="0.11.254.2011_03_09_1814" lang="en-US" track="developer-build" board="x86-generic" hardware_class="BETA DVT" delta_okay="true"> |
| 45 | <updatecheck></updatecheck> |
| 46 | <event eventtype="3" eventresult="2" previousversion="0.11.216.2011_03_02_1358"></event> |
| 47 | </app> |
| 48 | </request> |
| 49 | """ |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 50 | |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 51 | # TODO(girts): use a random available port. |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 52 | UPDATE_URL = 'http://127.0.0.1:8080/update/devserver' |
joychen | 3b6bac6 | 2013-07-12 11:42:49 -0700 | [diff] [blame] | 53 | CHECK_HEALTH_URL = 'http://127.0.0.1:8080/check_health' |
joychen | ed64b22 | 2013-06-21 16:39:34 -0700 | [diff] [blame] | 54 | STATIC_URL = 'http://127.0.0.1:8080/static/' |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 55 | SERVE_URL = STATIC_URL + LABEL + '/' |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 56 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 57 | API_HOST_INFO_BAD_URL = 'http://127.0.0.1:8080/api/hostinfo/' |
| 58 | API_HOST_INFO_URL = API_HOST_INFO_BAD_URL + '127.0.0.1' |
| 59 | |
| 60 | API_SET_UPDATE_BAD_URL = 'http://127.0.0.1:8080/api/setnextupdate/' |
| 61 | API_SET_UPDATE_URL = API_SET_UPDATE_BAD_URL + '127.0.0.1' |
| 62 | |
| 63 | API_SET_UPDATE_REQUEST = 'new_update-test/the-new-update' |
| 64 | |
joychen | 3b6bac6 | 2013-07-12 11:42:49 -0700 | [diff] [blame] | 65 | DEVSERVER_START_TIMEOUT = 15 |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 66 | |
| 67 | class DevserverTest(unittest.TestCase): |
| 68 | """Regressions tests for devserver.""" |
| 69 | |
| 70 | def setUp(self): |
| 71 | """Copies in testing files.""" |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 72 | |
| 73 | # Copy in developer-test.gz, as "static/" directory is hardcoded, and it |
| 74 | # would be very hard to change it (static file serving is handled deep |
| 75 | # inside webpy). |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 76 | self.test_data_path = tempfile.mkdtemp() |
| 77 | self.src_dir = os.path.dirname(__file__) |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 78 | |
| 79 | # Copy the payload to the root of static_dir. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 80 | self.image_src = os.path.join(self.src_dir, TEST_IMAGE) |
| 81 | self.image = os.path.join(self.test_data_path, TEST_IMAGE_NAME) |
Zdenek Behan | 5d21a2a | 2011-02-12 02:06:01 +0100 | [diff] [blame] | 82 | shutil.copy(self.image_src, self.image) |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 83 | |
| 84 | # Copy the payload to the location of the update label "devserver." |
| 85 | os.makedirs(os.path.join(self.test_data_path, LABEL)) |
| 86 | shutil.copy(self.image_src, os.path.join(self.test_data_path, LABEL, |
| 87 | TEST_IMAGE_NAME)) |
| 88 | |
| 89 | # Copy the payload to the location of forced label. |
| 90 | os.makedirs(os.path.join(self.test_data_path, API_SET_UPDATE_REQUEST)) |
| 91 | shutil.copy(self.image_src, os.path.join(self.test_data_path, |
| 92 | API_SET_UPDATE_REQUEST, |
| 93 | TEST_IMAGE_NAME)) |
| 94 | |
joychen | 3b6bac6 | 2013-07-12 11:42:49 -0700 | [diff] [blame] | 95 | self.devserver_process = self._StartServer() |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 96 | |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 97 | def tearDown(self): |
| 98 | """Removes testing files.""" |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 99 | shutil.rmtree(self.test_data_path) |
joychen | 3b6bac6 | 2013-07-12 11:42:49 -0700 | [diff] [blame] | 100 | os.kill(self.devserver_process.pid, signal.SIGKILL) |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 101 | |
Jay Srinivasan | ac69d26 | 2012-10-30 19:05:53 -0700 | [diff] [blame] | 102 | # Helper methods begin here. |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 103 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 104 | def _StartServer(self): |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 105 | """Starts devserver, returns process.""" |
| 106 | cmd = [ |
| 107 | 'python', |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 108 | os.path.join(self.src_dir, 'devserver.py'), |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 109 | 'devserver.py', |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 110 | '--static_dir', |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 111 | self.test_data_path, |
| 112 | ] |
| 113 | |
joychen | 3b6bac6 | 2013-07-12 11:42:49 -0700 | [diff] [blame] | 114 | process = subprocess.Popen(cmd, |
| 115 | stderr=subprocess.PIPE) |
| 116 | |
| 117 | # wait for devserver to start |
| 118 | current_time = time.time() |
| 119 | deadline = current_time + DEVSERVER_START_TIMEOUT |
| 120 | while current_time < deadline: |
| 121 | current_time = time.time() |
| 122 | try: |
| 123 | urllib2.urlopen(CHECK_HEALTH_URL, timeout=0.05) |
| 124 | break |
| 125 | except Exception: |
| 126 | continue |
| 127 | else: |
| 128 | self.fail('Devserver failed to start within timeout.') |
| 129 | |
| 130 | return process |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 131 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 132 | def VerifyHandleUpdate(self, protocol): |
| 133 | """Tests running the server and getting an update for the given protocol.""" |
joychen | 3b6bac6 | 2013-07-12 11:42:49 -0700 | [diff] [blame] | 134 | request = urllib2.Request(UPDATE_URL, UPDATE_REQUEST[protocol]) |
| 135 | connection = urllib2.urlopen(request) |
| 136 | response = connection.read() |
| 137 | connection.close() |
| 138 | self.assertNotEqual('', response) |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 139 | |
joychen | 3b6bac6 | 2013-07-12 11:42:49 -0700 | [diff] [blame] | 140 | # Parse the response and check if it contains the right result. |
| 141 | dom = minidom.parseString(response) |
| 142 | update = dom.getElementsByTagName('updatecheck')[0] |
| 143 | if protocol == '2.0': |
| 144 | url = self.VerifyV2Response(update) |
| 145 | else: |
| 146 | url = self.VerifyV3Response(update) |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 147 | |
joychen | 3b6bac6 | 2013-07-12 11:42:49 -0700 | [diff] [blame] | 148 | # Try to fetch the image. |
| 149 | connection = urllib2.urlopen(url) |
| 150 | contents = connection.read() |
| 151 | connection.close() |
| 152 | self.assertEqual('Developers, developers, developers!\n', contents) |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 153 | |
Jay Srinivasan | ac69d26 | 2012-10-30 19:05:53 -0700 | [diff] [blame] | 154 | def VerifyV2Response(self, update): |
| 155 | """Verifies the update DOM from a v2 response and returns the url.""" |
| 156 | codebase = update.getAttribute('codebase') |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 157 | self.assertEqual(SERVE_URL + TEST_IMAGE_NAME, codebase) |
Jay Srinivasan | ac69d26 | 2012-10-30 19:05:53 -0700 | [diff] [blame] | 158 | |
| 159 | hash_value = update.getAttribute('hash') |
| 160 | self.assertEqual(EXPECTED_HASH, hash_value) |
Jay Srinivasan | ac69d26 | 2012-10-30 19:05:53 -0700 | [diff] [blame] | 161 | return codebase |
| 162 | |
| 163 | def VerifyV3Response(self, update): |
| 164 | """Verifies the update DOM from a v3 response and returns the url.""" |
| 165 | # Parse the response and check if it contains the right result. |
| 166 | urls = update.getElementsByTagName('urls')[0] |
| 167 | url = urls.getElementsByTagName('url')[0] |
| 168 | |
| 169 | codebase = url.getAttribute('codebase') |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 170 | self.assertEqual(SERVE_URL, codebase) |
Jay Srinivasan | ac69d26 | 2012-10-30 19:05:53 -0700 | [diff] [blame] | 171 | |
| 172 | manifest = update.getElementsByTagName('manifest')[0] |
| 173 | packages = manifest.getElementsByTagName('packages')[0] |
| 174 | package = packages.getElementsByTagName('package')[0] |
| 175 | filename = package.getAttribute('name') |
| 176 | self.assertEqual(TEST_IMAGE_NAME, filename) |
| 177 | |
| 178 | hash_value = package.getAttribute('hash') |
| 179 | self.assertEqual(EXPECTED_HASH, hash_value) |
| 180 | |
| 181 | url = os.path.join(codebase, filename) |
| 182 | return url |
| 183 | |
Jay Srinivasan | ac69d26 | 2012-10-30 19:05:53 -0700 | [diff] [blame] | 184 | # Tests begin here. |
Jay Srinivasan | ac69d26 | 2012-10-30 19:05:53 -0700 | [diff] [blame] | 185 | def testHandleUpdateV2(self): |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 186 | self.VerifyHandleUpdate('2.0') |
Zdenek Behan | 5d21a2a | 2011-02-12 02:06:01 +0100 | [diff] [blame] | 187 | |
Jay Srinivasan | ac69d26 | 2012-10-30 19:05:53 -0700 | [diff] [blame] | 188 | def testHandleUpdateV3(self): |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 189 | self.VerifyHandleUpdate('3.0') |
Zdenek Behan | 5d21a2a | 2011-02-12 02:06:01 +0100 | [diff] [blame] | 190 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 191 | def testApiBadSetNextUpdateRequest(self): |
| 192 | """Tests sending a bad setnextupdate request.""" |
joychen | 3b6bac6 | 2013-07-12 11:42:49 -0700 | [diff] [blame] | 193 | # Send bad request and ensure it fails... |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 194 | try: |
joychen | 3b6bac6 | 2013-07-12 11:42:49 -0700 | [diff] [blame] | 195 | request = urllib2.Request(API_SET_UPDATE_URL, '') |
| 196 | connection = urllib2.urlopen(request) |
| 197 | connection.read() |
| 198 | connection.close() |
| 199 | self.fail('Invalid setnextupdate request did not fail!') |
| 200 | except urllib2.URLError: |
| 201 | pass |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 202 | |
| 203 | def testApiBadSetNextUpdateURL(self): |
| 204 | """Tests contacting a bad setnextupdate url.""" |
joychen | 3b6bac6 | 2013-07-12 11:42:49 -0700 | [diff] [blame] | 205 | # Send bad request and ensure it fails... |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 206 | try: |
joychen | 3b6bac6 | 2013-07-12 11:42:49 -0700 | [diff] [blame] | 207 | connection = urllib2.urlopen(API_SET_UPDATE_BAD_URL) |
| 208 | connection.read() |
| 209 | connection.close() |
| 210 | self.fail('Invalid setnextupdate url did not fail!') |
| 211 | except urllib2.URLError: |
| 212 | pass |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 213 | |
| 214 | def testApiBadHostInfoURL(self): |
| 215 | """Tests contacting a bad hostinfo url.""" |
joychen | 3b6bac6 | 2013-07-12 11:42:49 -0700 | [diff] [blame] | 216 | # Send bad request and ensure it fails... |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 217 | try: |
joychen | 3b6bac6 | 2013-07-12 11:42:49 -0700 | [diff] [blame] | 218 | connection = urllib2.urlopen(API_HOST_INFO_BAD_URL) |
| 219 | connection.read() |
| 220 | connection.close() |
| 221 | self.fail('Invalid hostinfo url did not fail!') |
| 222 | except urllib2.URLError: |
| 223 | pass |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 224 | |
| 225 | def testApiHostInfoAndSetNextUpdate(self): |
| 226 | """Tests using the setnextupdate and hostinfo api commands.""" |
joychen | 3b6bac6 | 2013-07-12 11:42:49 -0700 | [diff] [blame] | 227 | # Send setnextupdate command. |
| 228 | request = urllib2.Request(API_SET_UPDATE_URL, API_SET_UPDATE_REQUEST) |
| 229 | connection = urllib2.urlopen(request) |
| 230 | response = connection.read() |
| 231 | connection.close() |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 232 | |
joychen | 3b6bac6 | 2013-07-12 11:42:49 -0700 | [diff] [blame] | 233 | # Send hostinfo command and verify the setnextupdate worked. |
| 234 | connection = urllib2.urlopen(API_HOST_INFO_URL) |
| 235 | response = connection.read() |
| 236 | connection.close() |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 237 | |
joychen | 3b6bac6 | 2013-07-12 11:42:49 -0700 | [diff] [blame] | 238 | self.assertEqual( |
| 239 | json.loads(response)['forced_update_label'], API_SET_UPDATE_REQUEST) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 240 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 241 | |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 242 | if __name__ == '__main__': |
| 243 | unittest.main() |