blob: 7b6f5a9f0d6b4357e297c49b45987a9182db9259 [file] [log] [blame]
Girtsdba6ab22010-10-11 15:53:52 -07001#!/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
9import os
10import signal
11import shutil
12import subprocess
13import sys
14import time
15import unittest
16import urllib2
17from xml.dom import minidom
18
19# Paths are relative to this script's base directory.
20STATIC_DIR = 'static'
Zdenek Behan5d21a2a2011-02-12 02:06:01 +010021TEST_IMAGE_PATH = 'testdata/devserver'
22TEST_IMAGE_NAME = 'developer-test.gz'
23TEST_IMAGE = TEST_IMAGE_PATH + '/' + TEST_IMAGE_NAME
Girtsdba6ab22010-10-11 15:53:52 -070024TEST_FACTORY_CONFIG = 'testdata/devserver/miniomaha-test.conf'
Zdenek Behan5d21a2a2011-02-12 02:06:01 +010025TEST_DATA_PATH = '/tmp/devserver-test'
Greg Spencerc8b59b22011-03-15 14:15:23 -070026TEST_CLIENT_PREFIX = 'ChromeOSUpdateEngine'
Girtsdba6ab22010-10-11 15:53:52 -070027
Girtsdba6ab22010-10-11 15:53:52 -070028UPDATE_REQUEST = """<?xml version="1.0" encoding="UTF-8"?>
Greg Spencerc8b59b22011-03-15 14:15:23 -070029<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>
Girtsdba6ab22010-10-11 15:53:52 -070035</o:gupdate>
36"""
37# TODO(girts): use a random available port.
38UPDATE_URL = 'http://127.0.0.1:8080/update'
39
Zdenek Behan1347a312011-02-10 03:59:17 +010040# Run all tests while being in /
41base_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
42os.chdir("/")
Girtsdba6ab22010-10-11 15:53:52 -070043
44class DevserverTest(unittest.TestCase):
45 """Regressions tests for devserver."""
46
47 def setUp(self):
48 """Copies in testing files."""
Girtsdba6ab22010-10-11 15:53:52 -070049
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 Behan5d21a2a2011-02-12 02:06:01 +010053 self.image_src = os.path.join(base_dir, TEST_IMAGE)
54 self.image = os.path.join(base_dir, STATIC_DIR, TEST_IMAGE_NAME)
Girtsdba6ab22010-10-11 15:53:52 -070055 if os.path.exists(self.image):
56 os.unlink(self.image)
Zdenek Behan5d21a2a2011-02-12 02:06:01 +010057 shutil.copy(self.image_src, self.image)
Girtsdba6ab22010-10-11 15:53:52 -070058
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 Behan1347a312011-02-10 03:59:17 +010070 os.path.join(base_dir, 'devserver.py'),
Girtsdba6ab22010-10-11 15:53:52 -070071 '--validate_factory_config',
Greg Spencerc8b59b22011-03-15 14:15:23 -070072 '--client_prefix', TEST_CLIENT_PREFIX,
Girtsdba6ab22010-10-11 15:53:52 -070073 '--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 Behan5d21a2a2011-02-12 02:06:01 +010080 def _StartServer(self, data_dir=''):
Girtsdba6ab22010-10-11 15:53:52 -070081 """Starts devserver, returns process."""
82 cmd = [
83 'python',
Zdenek Behan1347a312011-02-10 03:59:17 +010084 os.path.join(base_dir, 'devserver.py'),
Girtsdba6ab22010-10-11 15:53:52 -070085 'devserver.py',
Greg Spencerc8b59b22011-03-15 14:15:23 -070086 '--client_prefix', TEST_CLIENT_PREFIX,
Girtsdba6ab22010-10-11 15:53:52 -070087 '--factory_config', self.factory_config,
88 ]
Zdenek Behan5d21a2a2011-02-12 02:06:01 +010089 if data_dir:
90 cmd.append('--data_dir')
91 cmd.append(data_dir)
Girtsdba6ab22010-10-11 15:53:52 -070092 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 Behan5d21a2a2011-02-12 02:06:01 +0100104 connection.close()
Girtsdba6ab22010-10-11 15:53:52 -0700105 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 Behan5d21a2a2011-02-12 02:06:01 +0100112 self.assertEqual('http://127.0.0.1:8080/static/' + TEST_IMAGE_NAME,
Girtsdba6ab22010-10-11 15:53:52 -0700113 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 Behan5d21a2a2011-02-12 02:06:01 +0100121 connection.close()
Girtsdba6ab22010-10-11 15:53:52 -0700122 self.assertEqual('Developers, developers, developers!\n', contents)
123 finally:
124 os.kill(pid, signal.SIGKILL)
125
Zdenek Behan5d21a2a2011-02-12 02:06:01 +0100126 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
Girtsdba6ab22010-10-11 15:53:52 -0700169
170if __name__ == '__main__':
171 unittest.main()