blob: 69c754fcd6ed34417ee141fe3373cee9c6922d10 [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'
21TEST_IMAGE = 'testdata/devserver/developer-test.gz'
22TEST_FACTORY_CONFIG = 'testdata/devserver/miniomaha-test.conf'
23
24# TODO(girts): Get a copy of a recent request. For now, I copied this from
25# update_test.
26UPDATE_REQUEST = """<?xml version="1.0" encoding="UTF-8"?>
27<o:gupdate
28 xmlns:o="http://www.google.com/update2/request"
29 version="MementoSoftwareUpdate-0.1.0.0"
30 protocol="2.0"
31 machineid="{1B0A13AC-7004-638C-3CA6-EC082E8F5DE9}"
32 ismachine="0"
33 userid="{bogus}">
34<o:os version="Memento"
35 platform="memento"
36 sp="ForcedUpdate_i686">
37</o:os>
38<o:app appid="{87efface-864d-49a5-9bb3-4b050a7c227a}"
39 version="ForcedUpdate"
40 lang="en-us"
41 brand="GGLG"
42 track="developer-build"
43 board="x86-generic">
44<o:ping active="0"></o:ping>
45<o:updatecheck></o:updatecheck>
46</o:app>
47</o:gupdate>
48"""
49# TODO(girts): use a random available port.
50UPDATE_URL = 'http://127.0.0.1:8080/update'
51
Zdenek Behan1347a312011-02-10 03:59:17 +010052# Run all tests while being in /
53base_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
54os.chdir("/")
Girtsdba6ab22010-10-11 15:53:52 -070055
56class DevserverTest(unittest.TestCase):
57 """Regressions tests for devserver."""
58
59 def setUp(self):
60 """Copies in testing files."""
Girtsdba6ab22010-10-11 15:53:52 -070061
62 # 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
64 # inside webpy).
65 image_src = os.path.join(base_dir, TEST_IMAGE)
66 self.image = os.path.join(base_dir, STATIC_DIR, 'developer-test.gz')
67 if os.path.exists(self.image):
68 os.unlink(self.image)
69 shutil.copy(image_src, self.image)
70
71 self.factory_config = os.path.join(base_dir, TEST_FACTORY_CONFIG)
72
73 def tearDown(self):
74 """Removes testing files."""
75 if os.path.exists(self.image):
76 os.unlink(self.image)
77
78 def testValidateFactoryConfig(self):
79 """Tests --validate_factory_config."""
80 cmd = [
81 'python',
Zdenek Behan1347a312011-02-10 03:59:17 +010082 os.path.join(base_dir, 'devserver.py'),
Girtsdba6ab22010-10-11 15:53:52 -070083 '--validate_factory_config',
84 '--factory_config', self.factory_config,
85 ]
86 process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
87 stdout, _ = process.communicate()
88 self.assertEqual(0, process.returncode)
89 self.assertTrue('Config file looks good.' in stdout)
90
91 def _StartServer(self):
92 """Starts devserver, returns process."""
93 cmd = [
94 'python',
Zdenek Behan1347a312011-02-10 03:59:17 +010095 os.path.join(base_dir, 'devserver.py'),
Girtsdba6ab22010-10-11 15:53:52 -070096 'devserver.py',
97 '--factory_config', self.factory_config,
98 ]
99 process = subprocess.Popen(cmd)
100 return process.pid
101
102 def testHandleUpdate(self):
103 """Tests running the server and getting an update."""
104 pid = self._StartServer()
105 try:
106 # Wait for the server to start up.
107 time.sleep(1)
108 request = urllib2.Request(UPDATE_URL, UPDATE_REQUEST)
109 connection = urllib2.urlopen(request)
110 response = connection.read()
111 self.assertNotEqual('', response)
112
113 # Parse the response and check if it contains the right result.
114 dom = minidom.parseString(response)
115 update = dom.getElementsByTagName('updatecheck')[0]
116
117 codebase = update.getAttribute('codebase')
118 self.assertEqual('http://127.0.0.1:8080/static/developer-test.gz',
119 codebase)
120
121 hash_value = update.getAttribute('hash')
122 self.assertEqual('kGcOinJ0vA8vdYX53FN0F5BdwfY=', hash_value)
123
124 # Try to fetch the image.
125 connection = urllib2.urlopen(codebase)
126 contents = connection.read()
127 self.assertEqual('Developers, developers, developers!\n', contents)
128 finally:
129 os.kill(pid, signal.SIGKILL)
130
131
132if __name__ == '__main__':
133 unittest.main()