blob: ace323bcdd4c100366667a8553cc737880a1f876 [file] [log] [blame]
Chris Sosa0356d3b2010-09-16 15:46:22 -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"""Unit tests for autoupdate.py."""
8
Chris Sosa54555862010-10-25 17:26:17 -07009import cherrypy
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070010import json
Chris Sosa0356d3b2010-09-16 15:46:22 -070011import mox
12import os
Chris Sosa7c931362010-10-11 19:49:01 -070013import socket
Chris Sosa0356d3b2010-09-16 15:46:22 -070014import unittest
Chris Sosa0356d3b2010-09-16 15:46:22 -070015
16import autoupdate
17
18_TEST_REQUEST = """
19<client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(client)s" >
20 <o:app version="%(version)s" track="%(track)s" board="%(board)s" />
Chris Sosaa387a872010-09-29 11:51:36 -070021 <o:updatecheck />
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070022 <o:event eventresult="%(event_result)d" eventtype="%(event_type)d" />
Chris Sosa0356d3b2010-09-16 15:46:22 -070023</client_test>"""
24
25
26class AutoupdateTest(mox.MoxTestBase):
27 def setUp(self):
28 mox.MoxTestBase.setUp(self)
29 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSize')
30 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetHash')
Chris Sosaa387a872010-09-29 11:51:36 -070031 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSHA256')
Chris Sosa0356d3b2010-09-16 15:46:22 -070032 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GetUpdatePayload')
33 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetLatestImageDir')
Chris Sosa7c931362010-10-11 19:49:01 -070034 self.port = 8080
Chris Sosa0356d3b2010-09-16 15:46:22 -070035 self.test_board = 'test-board'
36 self.build_root = '/src_path/build/images'
37 self.latest_dir = '12345_af_12-a1'
38 self.latest_verision = '12345_af_12'
39 self.static_image_dir = '/tmp/static-dir/'
Chris Sosa7c931362010-10-11 19:49:01 -070040 self.hostname = '%s:%s' % (socket.gethostname(), self.port)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -070041 self.test_dict = {
42 'client': 'ChromeOSUpdateEngine-1.0',
43 'version': 'ForcedUpdate',
44 'track': 'unused_var',
45 'board': self.test_board,
46 'event_result': 2,
47 'event_type': 3
48 }
Chris Sosa0356d3b2010-09-16 15:46:22 -070049 self.test_data = _TEST_REQUEST % self.test_dict
50 self.forced_image_path = '/path_to_force/chromiumos_image.bin'
51 self.hash = 12345
52 self.size = 54321
53 self.url = 'http://%s/static/update.gz' % self.hostname
54 self.payload = 'My payload'
Chris Sosaa387a872010-09-29 11:51:36 -070055 self.sha256 = 'SHA LA LA'
Chris Sosa54555862010-10-25 17:26:17 -070056 cherrypy.request.base = 'http://%s' % self.hostname
57
Chris Sosa0356d3b2010-09-16 15:46:22 -070058 def _DummyAutoupdateConstructor(self):
59 """Creates a dummy autoupdater. Used to avoid using constructor."""
60 dummy = autoupdate.Autoupdate(root_dir=None,
Chris Sosa7c931362010-10-11 19:49:01 -070061 static_dir=self.static_image_dir,
62 port=self.port)
Chris Sosa0356d3b2010-09-16 15:46:22 -070063 dummy.client_prefix = 'ChromeOSUpdateEngine'
Chris Sosa0356d3b2010-09-16 15:46:22 -070064 return dummy
65
66 def testGenerateLatestUpdateImageWithForced(self):
Don Garrettf90edf02010-11-16 17:36:14 -080067 self.mox.StubOutWithMock(autoupdate.Autoupdate,
68 'GenerateUpdateImageWithCache')
Chris Sosa0356d3b2010-09-16 15:46:22 -070069 autoupdate.Autoupdate._GetLatestImageDir(self.test_board).AndReturn(
70 '%s/%s/%s' % (self.build_root, self.test_board, self.latest_dir))
Don Garrettf90edf02010-11-16 17:36:14 -080071 autoupdate.Autoupdate.GenerateUpdateImageWithCache(
Chris Sosa0356d3b2010-09-16 15:46:22 -070072 '%s/%s/%s/chromiumos_image.bin' % (self.build_root, self.test_board,
73 self.latest_dir),
Don Garrettf90edf02010-11-16 17:36:14 -080074 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -070075
76 self.mox.ReplayAll()
77 au_mock = self._DummyAutoupdateConstructor()
78 self.assertTrue(au_mock.GenerateLatestUpdateImage(self.test_board,
79 'ForcedUpdate',
80 self.static_image_dir))
81 self.mox.VerifyAll()
82
83 def testHandleUpdatePingForForcedImage(self):
Don Garrettf90edf02010-11-16 17:36:14 -080084 self.mox.StubOutWithMock(autoupdate.Autoupdate,
85 'GenerateUpdateImageWithCache')
Chris Sosa0356d3b2010-09-16 15:46:22 -070086
87 test_data = _TEST_REQUEST % self.test_dict
88
Don Garrettf90edf02010-11-16 17:36:14 -080089 autoupdate.Autoupdate.GenerateUpdateImageWithCache(
Chris Sosaa387a872010-09-29 11:51:36 -070090 self.forced_image_path,
Don Garrettf90edf02010-11-16 17:36:14 -080091 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -070092 autoupdate.Autoupdate._GetHash(os.path.join(
93 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Chris Sosaa387a872010-09-29 11:51:36 -070094 autoupdate.Autoupdate._GetSHA256(os.path.join(
95 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Chris Sosa0356d3b2010-09-16 15:46:22 -070096 autoupdate.Autoupdate._GetSize(os.path.join(
97 self.static_image_dir, 'update.gz')).AndReturn(self.size)
98 autoupdate.Autoupdate.GetUpdatePayload(
Andrew de los Reyes5679b972010-10-25 17:34:49 -070099 self.hash, self.sha256, self.size, self.url, False).AndReturn(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700100 self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700101
102 self.mox.ReplayAll()
103 au_mock = self._DummyAutoupdateConstructor()
104 au_mock.forced_image = self.forced_image_path
105 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
106 self.mox.VerifyAll()
107
108 def testHandleUpdatePingForLatestImage(self):
109 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
110
111 test_data = _TEST_REQUEST % self.test_dict
112
113 autoupdate.Autoupdate.GenerateLatestUpdateImage(
Don Garrettf90edf02010-11-16 17:36:14 -0800114 self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn(
115 'update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700116 autoupdate.Autoupdate._GetHash(os.path.join(
117 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Chris Sosaa387a872010-09-29 11:51:36 -0700118 autoupdate.Autoupdate._GetSHA256(os.path.join(
119 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700120 autoupdate.Autoupdate._GetSize(os.path.join(
121 self.static_image_dir, 'update.gz')).AndReturn(self.size)
122 autoupdate.Autoupdate.GetUpdatePayload(
Andrew de los Reyes5679b972010-10-25 17:34:49 -0700123 self.hash, self.sha256, self.size, self.url, False).AndReturn(
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700124 self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700125
126 self.mox.ReplayAll()
127 au_mock = self._DummyAutoupdateConstructor()
128 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700129 self.assertEqual(
130 au_mock.host_info['127.0.0.1']['last_known_version'], 'ForcedUpdate')
131 self.assertEqual(
132 au_mock.host_info['127.0.0.1']['last_event_type'],
133 self.test_dict['event_type'])
134 self.assertEqual(
135 au_mock.host_info['127.0.0.1']['last_event_status'],
136 self.test_dict['event_result'])
Chris Sosa0356d3b2010-09-16 15:46:22 -0700137 self.mox.VerifyAll()
138
Don Garrett0ad09372010-12-06 16:20:30 -0800139 def testChangeUrlPort(self):
140 r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085)
141 self.assertEqual(r, 'http://fuzzy:8085/static')
142
143 r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085)
144 self.assertEqual(r, 'http://fuzzy:8085/static')
145
146 r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085)
147 self.assertEqual(r, 'ftp://fuzzy:8085/static')
148
149 r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085)
150 self.assertEqual(r, 'ftp://fuzzy:8085')
151
Dale Curtisc9aaf3a2011-08-09 15:47:40 -0700152 def testHandleHostInfoPing(self):
153 au_mock = self._DummyAutoupdateConstructor()
154 self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None)
155
156 # Setup fake host_info entry and ensure it comes back to us in one piece.
157 test_ip = '1.2.3.4'
158 au_mock.host_info[test_ip] = self.test_dict
159 self.assertEqual(
160 json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict)
161
162 def testHandleSetUpdatePing(self):
163 au_mock = self._DummyAutoupdateConstructor()
164 test_ip = '1.2.3.4'
165 test_label = 'test/old-update'
166 self.assertRaises(
167 AssertionError, au_mock.HandleSetUpdatePing, test_ip, None)
168 self.assertRaises(
169 AssertionError, au_mock.HandleSetUpdatePing, None, test_label)
170 self.assertRaises(
171 AssertionError, au_mock.HandleSetUpdatePing, None, None)
172
173 au_mock.HandleSetUpdatePing(test_ip, test_label)
174 self.assertEqual(
175 au_mock.host_info[test_ip]['forced_update_label'], test_label)
176
177 def testHandleUpdatePingWithSetUpdate(self):
178 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
179
180 test_data = _TEST_REQUEST % self.test_dict
181 test_label = 'new_update-test/the-new-update'
182 new_image_dir = os.path.join(self.static_image_dir, test_label)
183 new_url = self.url.replace('update.gz', test_label + '/update.gz')
184
185 autoupdate.Autoupdate.GenerateLatestUpdateImage(
186 self.test_board, 'ForcedUpdate', new_image_dir).AndReturn(
187 'update.gz')
188 autoupdate.Autoupdate._GetHash(os.path.join(
189 new_image_dir, 'update.gz')).AndReturn(self.hash)
190 autoupdate.Autoupdate._GetSHA256(os.path.join(
191 new_image_dir, 'update.gz')).AndReturn(self.sha256)
192 autoupdate.Autoupdate._GetSize(os.path.join(
193 new_image_dir, 'update.gz')).AndReturn(self.size)
194 autoupdate.Autoupdate.GetUpdatePayload(
195 self.hash, self.sha256, self.size, new_url, False).AndReturn(
196 self.payload)
197
198 self.mox.ReplayAll()
199 au_mock = self._DummyAutoupdateConstructor()
200 au_mock.HandleSetUpdatePing('127.0.0.1', test_label)
201 self.assertEqual(
202 au_mock.host_info['127.0.0.1']['forced_update_label'], test_label)
203 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
204 self.assertFalse('forced_update_label' in au_mock.host_info['127.0.0.1'])
205
Chris Sosa0356d3b2010-09-16 15:46:22 -0700206
207if __name__ == '__main__':
208 unittest.main()