blob: 0cf0062ecd5082576cedf2cc7f0e618e9ed04a92 [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):
Chris Sosade91f672010-11-16 10:05:44 -080063 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateUpdateImage')
Chris Sosa0356d3b2010-09-16 15:46:22 -070064 autoupdate.Autoupdate._GetLatestImageDir(self.test_board).AndReturn(
65 '%s/%s/%s' % (self.build_root, self.test_board, self.latest_dir))
Chris Sosade91f672010-11-16 10:05:44 -080066 autoupdate.Autoupdate.GenerateUpdateImage(
Chris Sosa0356d3b2010-09-16 15:46:22 -070067 '%s/%s/%s/chromiumos_image.bin' % (self.build_root, self.test_board,
68 self.latest_dir),
Chris Sosade91f672010-11-16 10:05:44 -080069 move_to_static_dir=True,
70 static_image_dir=self.static_image_dir).AndReturn(True)
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):
Chris Sosade91f672010-11-16 10:05:44 -080080 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateUpdateImage')
Chris Sosa0356d3b2010-09-16 15:46:22 -070081
82 test_data = _TEST_REQUEST % self.test_dict
83
Chris Sosade91f672010-11-16 10:05:44 -080084 autoupdate.Autoupdate.GenerateUpdateImage(
Chris Sosaa387a872010-09-29 11:51:36 -070085 self.forced_image_path,
Chris Sosade91f672010-11-16 10:05:44 -080086 move_to_static_dir=True,
87 static_image_dir=self.static_image_dir).AndReturn(True)
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(
Chris Sosade91f672010-11-16 10:05:44 -0800110 self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn(True)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700111 autoupdate.Autoupdate._GetHash(os.path.join(
112 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Chris Sosaa387a872010-09-29 11:51:36 -0700113 autoupdate.Autoupdate._GetSHA256(os.path.join(
114 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700115 autoupdate.Autoupdate._GetSize(os.path.join(
116 self.static_image_dir, 'update.gz')).AndReturn(self.size)
117 autoupdate.Autoupdate.GetUpdatePayload(
Andrew de los Reyes5679b972010-10-25 17:34:49 -0700118 self.hash, self.sha256, self.size, self.url, False).AndReturn(
119 self.payload)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700120
121 self.mox.ReplayAll()
122 au_mock = self._DummyAutoupdateConstructor()
123 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
124 self.mox.VerifyAll()
125
126 def testHandleUpdatePingForArchivedBuild(self):
127 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateImageFromZip')
128
129 test_data = _TEST_REQUEST % self.test_dict
130
131 autoupdate.Autoupdate.GenerateImageFromZip(
Chris Sosade91f672010-11-16 10:05:44 -0800132 self.static_image_dir).AndReturn(True)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700133 autoupdate.Autoupdate._GetHash(os.path.join(
134 self.static_image_dir, 'update.gz')).AndReturn(self.hash)
Chris Sosaa387a872010-09-29 11:51:36 -0700135 autoupdate.Autoupdate._GetSHA256(os.path.join(
136 self.static_image_dir, 'update.gz')).AndReturn(self.sha256)
Chris Sosa0356d3b2010-09-16 15:46:22 -0700137 autoupdate.Autoupdate._GetSize(os.path.join(
138 self.static_image_dir, 'update.gz')).AndReturn(self.size)
139 autoupdate.Autoupdate.GetUpdatePayload(
Chris Sosaa387a872010-09-29 11:51:36 -0700140 self.hash, self.sha256, self.size,
Andrew de los Reyes5679b972010-10-25 17:34:49 -0700141 'http://%s/static/archive/update.gz' % self.hostname,
142 False).AndReturn(
Chris Sosa0356d3b2010-09-16 15:46:22 -0700143 self.payload)
144
145 self.mox.ReplayAll()
146 au_mock = self._DummyAutoupdateConstructor()
147 au_mock.serve_only = True
Chris Sosaa387a872010-09-29 11:51:36 -0700148 au_mock.static_urlbase = 'http://%s/static/archive' % self.hostname
Chris Sosa0356d3b2010-09-16 15:46:22 -0700149 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload)
150 self.mox.VerifyAll()
151
152
153if __name__ == '__main__':
154 unittest.main()