blob: 0aaa5d373ceed95afa00606976d304c7c354ed12 [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
Chris Sosa0356d3b2010-09-16 15:46:22 -070010import mox
11import os
Chris Sosa7c931362010-10-11 19:49:01 -070012import socket
Chris Sosa0356d3b2010-09-16 15:46:22 -070013import unittest
Chris Sosa0356d3b2010-09-16 15:46:22 -070014
15import autoupdate
16
17_TEST_REQUEST = """
18<client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(client)s" >
19 <o:app version="%(version)s" track="%(track)s" board="%(board)s" />
Chris Sosaa387a872010-09-29 11:51:36 -070020 <o:updatecheck />
Chris Sosa0356d3b2010-09-16 15:46:22 -070021</client_test>"""
22
23
24class AutoupdateTest(mox.MoxTestBase):
25 def setUp(self):
26 mox.MoxTestBase.setUp(self)
27 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSize')
28 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetHash')
Chris Sosaa387a872010-09-29 11:51:36 -070029 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSHA256')
Chris Sosa0356d3b2010-09-16 15:46:22 -070030 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GetUpdatePayload')
31 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetLatestImageDir')
Chris Sosa7c931362010-10-11 19:49:01 -070032 self.port = 8080
Chris Sosa0356d3b2010-09-16 15:46:22 -070033 self.test_board = 'test-board'
34 self.build_root = '/src_path/build/images'
35 self.latest_dir = '12345_af_12-a1'
36 self.latest_verision = '12345_af_12'
37 self.static_image_dir = '/tmp/static-dir/'
Chris Sosa7c931362010-10-11 19:49:01 -070038 self.hostname = '%s:%s' % (socket.gethostname(), self.port)
Chris Sosa0356d3b2010-09-16 15:46:22 -070039 self.test_dict = { 'client': 'ChromeOSUpdateEngine-1.0',
40 'version': 'ForcedUpdate',
41 'track': 'unused_var',
42 'board': self.test_board
43 }
44 self.test_data = _TEST_REQUEST % self.test_dict
45 self.forced_image_path = '/path_to_force/chromiumos_image.bin'
46 self.hash = 12345
47 self.size = 54321
48 self.url = 'http://%s/static/update.gz' % self.hostname
49 self.payload = 'My payload'
Chris Sosaa387a872010-09-29 11:51:36 -070050 self.sha256 = 'SHA LA LA'
Chris Sosa54555862010-10-25 17:26:17 -070051 cherrypy.request.base = 'http://%s' % self.hostname
52
Chris Sosa0356d3b2010-09-16 15:46:22 -070053
54 def _DummyAutoupdateConstructor(self):
55 """Creates a dummy autoupdater. Used to avoid using constructor."""
56 dummy = autoupdate.Autoupdate(root_dir=None,
Chris Sosa7c931362010-10-11 19:49:01 -070057 static_dir=self.static_image_dir,
58 port=self.port)
Chris Sosa0356d3b2010-09-16 15:46:22 -070059 dummy.client_prefix = 'ChromeOSUpdateEngine'
Chris Sosa0356d3b2010-09-16 15:46:22 -070060 return dummy
61
62 def testGenerateLatestUpdateImageWithForced(self):
Don Garrett710470d2010-11-15 17:43:44 -080063 self.mox.StubOutWithMock(autoupdate.Autoupdate,
64 'GenerateUpdateImageWithCache')
Chris Sosa0356d3b2010-09-16 15:46:22 -070065 autoupdate.Autoupdate._GetLatestImageDir(self.test_board).AndReturn(
66 '%s/%s/%s' % (self.build_root, self.test_board, self.latest_dir))
Don Garrett710470d2010-11-15 17:43:44 -080067 autoupdate.Autoupdate.GenerateUpdateImageWithCache(
Chris Sosa0356d3b2010-09-16 15:46:22 -070068 '%s/%s/%s/chromiumos_image.bin' % (self.build_root, self.test_board,
69 self.latest_dir),
Don Garrett710470d2010-11-15 17:43:44 -080070 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -070071
72 self.mox.ReplayAll()
73 au_mock = self._DummyAutoupdateConstructor()
74 self.assertTrue(au_mock.GenerateLatestUpdateImage(self.test_board,
75 'ForcedUpdate',
76 self.static_image_dir))
77 self.mox.VerifyAll()
78
79 def testHandleUpdatePingForForcedImage(self):
Don Garrett710470d2010-11-15 17:43:44 -080080 self.mox.StubOutWithMock(autoupdate.Autoupdate,
81 'GenerateUpdateImageWithCache')
Chris Sosa0356d3b2010-09-16 15:46:22 -070082
83 test_data = _TEST_REQUEST % self.test_dict
84
Don Garrett710470d2010-11-15 17:43:44 -080085 autoupdate.Autoupdate.GenerateUpdateImageWithCache(
Chris Sosaa387a872010-09-29 11:51:36 -070086 self.forced_image_path,
Don Garrett710470d2010-11-15 17:43:44 -080087 static_image_dir=self.static_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -070088 autoupdate.Autoupdate._GetHash(os.path.join(
89 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Chris Sosaa387a872010-09-29 11:51:36 -070090 autoupdate.Autoupdate._GetSHA256(os.path.join(
91 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Chris Sosa0356d3b2010-09-16 15:46:22 -070092 autoupdate.Autoupdate._GetSize(os.path.join(
93 self.static_image_dir, 'update.gz')).AndReturn(self.size)
94 autoupdate.Autoupdate.GetUpdatePayload(
Andrew de los Reyes5679b972010-10-25 17:34:49 -070095 self.hash, self.sha256, self.size, self.url, False).AndReturn(
96 self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -070097
98 self.mox.ReplayAll()
99 au_mock = self._DummyAutoupdateConstructor()
100 au_mock.forced_image = self.forced_image_path
101 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
102 self.mox.VerifyAll()
103
104 def testHandleUpdatePingForLatestImage(self):
105 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage')
106
107 test_data = _TEST_REQUEST % self.test_dict
108
109 autoupdate.Autoupdate.GenerateLatestUpdateImage(
Don Garrett710470d2010-11-15 17:43:44 -0800110 self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn(
111 'update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700112 autoupdate.Autoupdate._GetHash(os.path.join(
113 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Chris Sosaa387a872010-09-29 11:51:36 -0700114 autoupdate.Autoupdate._GetSHA256(os.path.join(
115 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700116 autoupdate.Autoupdate._GetSize(os.path.join(
117 self.static_image_dir, 'update.gz')).AndReturn(self.size)
118 autoupdate.Autoupdate.GetUpdatePayload(
Andrew de los Reyes5679b972010-10-25 17:34:49 -0700119 self.hash, self.sha256, self.size, self.url, False).AndReturn(
120 self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700121
122 self.mox.ReplayAll()
123 au_mock = self._DummyAutoupdateConstructor()
124 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
125 self.mox.VerifyAll()
126
127 def testHandleUpdatePingForArchivedBuild(self):
128 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateImageFromZip')
129
130 test_data = _TEST_REQUEST % self.test_dict
131
132 autoupdate.Autoupdate.GenerateImageFromZip(
Don Garrett710470d2010-11-15 17:43:44 -0800133 self.static_image_dir).AndReturn('update.gz')
Chris Sosa0356d3b2010-09-16 15:46:22 -0700134 autoupdate.Autoupdate._GetHash(os.path.join(
135 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Chris Sosaa387a872010-09-29 11:51:36 -0700136 autoupdate.Autoupdate._GetSHA256(os.path.join(
137 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700138 autoupdate.Autoupdate._GetSize(os.path.join(
139 self.static_image_dir, 'update.gz')).AndReturn(self.size)
140 autoupdate.Autoupdate.GetUpdatePayload(
Chris Sosaa387a872010-09-29 11:51:36 -0700141 self.hash, self.sha256, self.size,
Andrew de los Reyes5679b972010-10-25 17:34:49 -0700142 'http://%s/static/archive/update.gz' % self.hostname,
143 False).AndReturn(
Chris Sosa0356d3b2010-09-16 15:46:22 -0700144 self.payload)
145
146 self.mox.ReplayAll()
147 au_mock = self._DummyAutoupdateConstructor()
148 au_mock.serve_only = True
Chris Sosaa387a872010-09-29 11:51:36 -0700149 au_mock.static_urlbase = 'http://%s/static/archive' % self.hostname
Chris Sosa0356d3b2010-09-16 15:46:22 -0700150 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
151 self.mox.VerifyAll()
152
153
154if __name__ == '__main__':
155 unittest.main()