blob: b4ccb70dfa8e845b23d539b2a2d1503f447fb2f8 [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
52
53class DevserverTest(unittest.TestCase):
54 """Regressions tests for devserver."""
55
56 def setUp(self):
57 """Copies in testing files."""
58 base_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
59
60 # Copy in developer-test.gz, as "static/" directory is hardcoded, and it
61 # would be very hard to change it (static file serving is handled deep
62 # inside webpy).
63 image_src = os.path.join(base_dir, TEST_IMAGE)
64 self.image = os.path.join(base_dir, STATIC_DIR, 'developer-test.gz')
65 if os.path.exists(self.image):
66 os.unlink(self.image)
67 shutil.copy(image_src, self.image)
68
69 self.factory_config = os.path.join(base_dir, TEST_FACTORY_CONFIG)
70
71 def tearDown(self):
72 """Removes testing files."""
73 if os.path.exists(self.image):
74 os.unlink(self.image)
75
76 def testValidateFactoryConfig(self):
77 """Tests --validate_factory_config."""
78 cmd = [
79 'python',
80 'devserver.py',
81 '--validate_factory_config',
82 '--factory_config', self.factory_config,
83 ]
84 process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
85 stdout, _ = process.communicate()
86 self.assertEqual(0, process.returncode)
87 self.assertTrue('Config file looks good.' in stdout)
88
89 def _StartServer(self):
90 """Starts devserver, returns process."""
91 cmd = [
92 'python',
93 'devserver.py',
94 '--factory_config', self.factory_config,
95 ]
96 process = subprocess.Popen(cmd)
97 return process.pid
98
99 def testHandleUpdate(self):
100 """Tests running the server and getting an update."""
101 pid = self._StartServer()
102 try:
103 # Wait for the server to start up.
104 time.sleep(1)
105 request = urllib2.Request(UPDATE_URL, UPDATE_REQUEST)
106 connection = urllib2.urlopen(request)
107 response = connection.read()
108 self.assertNotEqual('', response)
109
110 # Parse the response and check if it contains the right result.
111 dom = minidom.parseString(response)
112 update = dom.getElementsByTagName('updatecheck')[0]
113
114 codebase = update.getAttribute('codebase')
115 self.assertEqual('http://127.0.0.1:8080/static/developer-test.gz',
116 codebase)
117
118 hash_value = update.getAttribute('hash')
119 self.assertEqual('kGcOinJ0vA8vdYX53FN0F5BdwfY=', hash_value)
120
121 # Try to fetch the image.
122 connection = urllib2.urlopen(codebase)
123 contents = connection.read()
124 self.assertEqual('Developers, developers, developers!\n', contents)
125 finally:
126 os.kill(pid, signal.SIGKILL)
127
128
129if __name__ == '__main__':
130 unittest.main()