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 | |
| 9 | import os |
| 10 | import signal |
| 11 | import shutil |
| 12 | import subprocess |
| 13 | import sys |
| 14 | import time |
| 15 | import unittest |
| 16 | import urllib2 |
| 17 | from xml.dom import minidom |
| 18 | |
| 19 | # Paths are relative to this script's base directory. |
| 20 | STATIC_DIR = 'static' |
Zdenek Behan | 5d21a2a | 2011-02-12 02:06:01 +0100 | [diff] [blame] | 21 | TEST_IMAGE_PATH = 'testdata/devserver' |
| 22 | TEST_IMAGE_NAME = 'developer-test.gz' |
| 23 | TEST_IMAGE = TEST_IMAGE_PATH + '/' + TEST_IMAGE_NAME |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 24 | TEST_FACTORY_CONFIG = 'testdata/devserver/miniomaha-test.conf' |
Zdenek Behan | 5d21a2a | 2011-02-12 02:06:01 +0100 | [diff] [blame] | 25 | TEST_DATA_PATH = '/tmp/devserver-test' |
Greg Spencer | c8b59b2 | 2011-03-15 14:15:23 -0700 | [diff] [blame] | 26 | TEST_CLIENT_PREFIX = 'ChromeOSUpdateEngine' |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 27 | |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 28 | UPDATE_REQUEST = """<?xml version="1.0" encoding="UTF-8"?> |
Greg Spencer | c8b59b2 | 2011-03-15 14:15:23 -0700 | [diff] [blame] | 29 | <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"> |
| 30 | <o:os version="Indy" platform="Chrome OS" sp="0.11.254.2011_03_09_1814_i686"></o:os> |
| 31 | <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"> |
| 32 | <o:updatecheck></o:updatecheck> |
| 33 | <o:event eventtype="3" eventresult="2" previousversion="0.11.216.2011_03_02_1358"></o:event> |
| 34 | </o:app> |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 35 | </o:gupdate> |
| 36 | """ |
| 37 | # TODO(girts): use a random available port. |
| 38 | UPDATE_URL = 'http://127.0.0.1:8080/update' |
| 39 | |
Zdenek Behan | 1347a31 | 2011-02-10 03:59:17 +0100 | [diff] [blame] | 40 | # Run all tests while being in / |
| 41 | base_dir = os.path.dirname(os.path.abspath(sys.argv[0])) |
| 42 | os.chdir("/") |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 43 | |
| 44 | class DevserverTest(unittest.TestCase): |
| 45 | """Regressions tests for devserver.""" |
| 46 | |
| 47 | def setUp(self): |
| 48 | """Copies in testing files.""" |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 49 | |
| 50 | # Copy in developer-test.gz, as "static/" directory is hardcoded, and it |
| 51 | # would be very hard to change it (static file serving is handled deep |
| 52 | # inside webpy). |
Zdenek Behan | 5d21a2a | 2011-02-12 02:06:01 +0100 | [diff] [blame] | 53 | self.image_src = os.path.join(base_dir, TEST_IMAGE) |
| 54 | self.image = os.path.join(base_dir, STATIC_DIR, TEST_IMAGE_NAME) |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 55 | if os.path.exists(self.image): |
| 56 | os.unlink(self.image) |
Zdenek Behan | 5d21a2a | 2011-02-12 02:06:01 +0100 | [diff] [blame] | 57 | shutil.copy(self.image_src, self.image) |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 58 | |
| 59 | self.factory_config = os.path.join(base_dir, TEST_FACTORY_CONFIG) |
| 60 | |
| 61 | def tearDown(self): |
| 62 | """Removes testing files.""" |
| 63 | if os.path.exists(self.image): |
| 64 | os.unlink(self.image) |
| 65 | |
| 66 | def testValidateFactoryConfig(self): |
| 67 | """Tests --validate_factory_config.""" |
| 68 | cmd = [ |
| 69 | 'python', |
Zdenek Behan | 1347a31 | 2011-02-10 03:59:17 +0100 | [diff] [blame] | 70 | os.path.join(base_dir, 'devserver.py'), |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 71 | '--validate_factory_config', |
Greg Spencer | c8b59b2 | 2011-03-15 14:15:23 -0700 | [diff] [blame] | 72 | '--client_prefix', TEST_CLIENT_PREFIX, |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 73 | '--factory_config', self.factory_config, |
| 74 | ] |
| 75 | process = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| 76 | stdout, _ = process.communicate() |
| 77 | self.assertEqual(0, process.returncode) |
| 78 | self.assertTrue('Config file looks good.' in stdout) |
| 79 | |
Zdenek Behan | 5d21a2a | 2011-02-12 02:06:01 +0100 | [diff] [blame] | 80 | def _StartServer(self, data_dir=''): |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 81 | """Starts devserver, returns process.""" |
| 82 | cmd = [ |
| 83 | 'python', |
Zdenek Behan | 1347a31 | 2011-02-10 03:59:17 +0100 | [diff] [blame] | 84 | os.path.join(base_dir, 'devserver.py'), |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 85 | 'devserver.py', |
Greg Spencer | c8b59b2 | 2011-03-15 14:15:23 -0700 | [diff] [blame] | 86 | '--client_prefix', TEST_CLIENT_PREFIX, |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 87 | '--factory_config', self.factory_config, |
| 88 | ] |
Zdenek Behan | 5d21a2a | 2011-02-12 02:06:01 +0100 | [diff] [blame] | 89 | if data_dir: |
| 90 | cmd.append('--data_dir') |
| 91 | cmd.append(data_dir) |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 92 | process = subprocess.Popen(cmd) |
| 93 | return process.pid |
| 94 | |
| 95 | def testHandleUpdate(self): |
| 96 | """Tests running the server and getting an update.""" |
| 97 | pid = self._StartServer() |
| 98 | try: |
| 99 | # Wait for the server to start up. |
| 100 | time.sleep(1) |
| 101 | request = urllib2.Request(UPDATE_URL, UPDATE_REQUEST) |
| 102 | connection = urllib2.urlopen(request) |
| 103 | response = connection.read() |
Zdenek Behan | 5d21a2a | 2011-02-12 02:06:01 +0100 | [diff] [blame] | 104 | connection.close() |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 105 | self.assertNotEqual('', response) |
| 106 | |
| 107 | # Parse the response and check if it contains the right result. |
| 108 | dom = minidom.parseString(response) |
| 109 | update = dom.getElementsByTagName('updatecheck')[0] |
| 110 | |
| 111 | codebase = update.getAttribute('codebase') |
Zdenek Behan | 5d21a2a | 2011-02-12 02:06:01 +0100 | [diff] [blame] | 112 | self.assertEqual('http://127.0.0.1:8080/static/' + TEST_IMAGE_NAME, |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 113 | codebase) |
| 114 | |
| 115 | hash_value = update.getAttribute('hash') |
| 116 | self.assertEqual('kGcOinJ0vA8vdYX53FN0F5BdwfY=', hash_value) |
| 117 | |
| 118 | # Try to fetch the image. |
| 119 | connection = urllib2.urlopen(codebase) |
| 120 | contents = connection.read() |
Zdenek Behan | 5d21a2a | 2011-02-12 02:06:01 +0100 | [diff] [blame] | 121 | connection.close() |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 122 | self.assertEqual('Developers, developers, developers!\n', contents) |
| 123 | finally: |
| 124 | os.kill(pid, signal.SIGKILL) |
| 125 | |
Zdenek Behan | 5d21a2a | 2011-02-12 02:06:01 +0100 | [diff] [blame] | 126 | def testHandleDatadirUpdate(self): |
| 127 | """Tests getting an update from a specified datadir""" |
| 128 | # Push the image to the expected path where devserver picks it up. |
| 129 | image_path = os.path.join(TEST_DATA_PATH, STATIC_DIR) |
| 130 | if not os.path.exists(image_path): |
| 131 | os.makedirs(image_path) |
| 132 | |
| 133 | foreign_image = os.path.join(image_path, TEST_IMAGE_NAME) |
| 134 | if os.path.exists(foreign_image): |
| 135 | os.unlink(foreign_image) |
| 136 | shutil.copy(self.image_src, foreign_image) |
| 137 | |
| 138 | pid = self._StartServer(data_dir=TEST_DATA_PATH) |
| 139 | try: |
| 140 | # Wait for the server to start up. |
| 141 | time.sleep(1) |
| 142 | |
| 143 | request = urllib2.Request(UPDATE_URL, UPDATE_REQUEST) |
| 144 | connection = urllib2.urlopen(request) |
| 145 | response = connection.read() |
| 146 | connection.close() |
| 147 | self.assertNotEqual('', response) |
| 148 | |
| 149 | # Parse the response and check if it contains the right result. |
| 150 | dom = minidom.parseString(response) |
| 151 | update = dom.getElementsByTagName('updatecheck')[0] |
| 152 | |
| 153 | codebase = update.getAttribute('codebase') |
| 154 | self.assertEqual('http://127.0.0.1:8080/static/' + TEST_IMAGE_NAME, |
| 155 | codebase) |
| 156 | |
| 157 | hash_value = update.getAttribute('hash') |
| 158 | self.assertEqual('kGcOinJ0vA8vdYX53FN0F5BdwfY=', hash_value) |
| 159 | |
| 160 | # Try to fetch the image. |
| 161 | connection = urllib2.urlopen(codebase) |
| 162 | contents = connection.read() |
| 163 | connection.close() |
| 164 | self.assertEqual('Developers, developers, developers!\n', contents) |
| 165 | os.unlink(foreign_image) |
| 166 | finally: |
| 167 | os.kill(pid, signal.SIGKILL) |
| 168 | |
Girts | dba6ab2 | 2010-10-11 15:53:52 -0700 | [diff] [blame] | 169 | |
| 170 | if __name__ == '__main__': |
| 171 | unittest.main() |