Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 1 | #!/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 Sosa | 5455586 | 2010-10-25 17:26:17 -0700 | [diff] [blame] | 9 | import cherrypy |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 10 | import mox |
| 11 | import os |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 12 | import socket |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 13 | import unittest |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 14 | |
| 15 | import 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 Sosa | a387a87 | 2010-09-29 11:51:36 -0700 | [diff] [blame] | 20 | <o:updatecheck /> |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 21 | </client_test>""" |
| 22 | |
| 23 | |
| 24 | class 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 Sosa | a387a87 | 2010-09-29 11:51:36 -0700 | [diff] [blame] | 29 | self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSHA256') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 30 | self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GetUpdatePayload') |
| 31 | self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetLatestImageDir') |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 32 | self.port = 8080 |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 33 | 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 Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 38 | self.hostname = '%s:%s' % (socket.gethostname(), self.port) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 39 | 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 Sosa | a387a87 | 2010-09-29 11:51:36 -0700 | [diff] [blame] | 50 | self.sha256 = 'SHA LA LA' |
Chris Sosa | 5455586 | 2010-10-25 17:26:17 -0700 | [diff] [blame] | 51 | cherrypy.request.base = 'http://%s' % self.hostname |
| 52 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 53 | |
| 54 | def _DummyAutoupdateConstructor(self): |
| 55 | """Creates a dummy autoupdater. Used to avoid using constructor.""" |
| 56 | dummy = autoupdate.Autoupdate(root_dir=None, |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 57 | static_dir=self.static_image_dir, |
| 58 | port=self.port) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 59 | dummy.client_prefix = 'ChromeOSUpdateEngine' |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 60 | return dummy |
| 61 | |
| 62 | def testGenerateLatestUpdateImageWithForced(self): |
Chris Sosa | de91f67 | 2010-11-16 10:05:44 -0800 | [diff] [blame^] | 63 | self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateUpdateImage') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 64 | autoupdate.Autoupdate._GetLatestImageDir(self.test_board).AndReturn( |
| 65 | '%s/%s/%s' % (self.build_root, self.test_board, self.latest_dir)) |
Chris Sosa | de91f67 | 2010-11-16 10:05:44 -0800 | [diff] [blame^] | 66 | autoupdate.Autoupdate.GenerateUpdateImage( |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 67 | '%s/%s/%s/chromiumos_image.bin' % (self.build_root, self.test_board, |
| 68 | self.latest_dir), |
Chris Sosa | de91f67 | 2010-11-16 10:05:44 -0800 | [diff] [blame^] | 69 | move_to_static_dir=True, |
| 70 | static_image_dir=self.static_image_dir).AndReturn(True) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 71 | |
| 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 Sosa | de91f67 | 2010-11-16 10:05:44 -0800 | [diff] [blame^] | 80 | self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateUpdateImage') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 81 | |
| 82 | test_data = _TEST_REQUEST % self.test_dict |
| 83 | |
Chris Sosa | de91f67 | 2010-11-16 10:05:44 -0800 | [diff] [blame^] | 84 | autoupdate.Autoupdate.GenerateUpdateImage( |
Chris Sosa | a387a87 | 2010-09-29 11:51:36 -0700 | [diff] [blame] | 85 | self.forced_image_path, |
Chris Sosa | de91f67 | 2010-11-16 10:05:44 -0800 | [diff] [blame^] | 86 | move_to_static_dir=True, |
| 87 | static_image_dir=self.static_image_dir).AndReturn(True) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 88 | autoupdate.Autoupdate._GetHash(os.path.join( |
| 89 | self.static_image_dir, 'update.gz')).AndReturn(self.hash) |
Chris Sosa | a387a87 | 2010-09-29 11:51:36 -0700 | [diff] [blame] | 90 | autoupdate.Autoupdate._GetSHA256(os.path.join( |
| 91 | self.static_image_dir, 'update.gz')).AndReturn(self.sha256) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 92 | autoupdate.Autoupdate._GetSize(os.path.join( |
| 93 | self.static_image_dir, 'update.gz')).AndReturn(self.size) |
| 94 | autoupdate.Autoupdate.GetUpdatePayload( |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 95 | self.hash, self.sha256, self.size, self.url, False).AndReturn( |
| 96 | self.payload) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 97 | |
| 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 Sosa | de91f67 | 2010-11-16 10:05:44 -0800 | [diff] [blame^] | 110 | self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn(True) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 111 | autoupdate.Autoupdate._GetHash(os.path.join( |
| 112 | self.static_image_dir, 'update.gz')).AndReturn(self.hash) |
Chris Sosa | a387a87 | 2010-09-29 11:51:36 -0700 | [diff] [blame] | 113 | autoupdate.Autoupdate._GetSHA256(os.path.join( |
| 114 | self.static_image_dir, 'update.gz')).AndReturn(self.sha256) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 115 | autoupdate.Autoupdate._GetSize(os.path.join( |
| 116 | self.static_image_dir, 'update.gz')).AndReturn(self.size) |
| 117 | autoupdate.Autoupdate.GetUpdatePayload( |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 118 | self.hash, self.sha256, self.size, self.url, False).AndReturn( |
| 119 | self.payload) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 120 | |
| 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 Sosa | de91f67 | 2010-11-16 10:05:44 -0800 | [diff] [blame^] | 132 | self.static_image_dir).AndReturn(True) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 133 | autoupdate.Autoupdate._GetHash(os.path.join( |
| 134 | self.static_image_dir, 'update.gz')).AndReturn(self.hash) |
Chris Sosa | a387a87 | 2010-09-29 11:51:36 -0700 | [diff] [blame] | 135 | autoupdate.Autoupdate._GetSHA256(os.path.join( |
| 136 | self.static_image_dir, 'update.gz')).AndReturn(self.sha256) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 137 | autoupdate.Autoupdate._GetSize(os.path.join( |
| 138 | self.static_image_dir, 'update.gz')).AndReturn(self.size) |
| 139 | autoupdate.Autoupdate.GetUpdatePayload( |
Chris Sosa | a387a87 | 2010-09-29 11:51:36 -0700 | [diff] [blame] | 140 | self.hash, self.sha256, self.size, |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 141 | 'http://%s/static/archive/update.gz' % self.hostname, |
| 142 | False).AndReturn( |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 143 | self.payload) |
| 144 | |
| 145 | self.mox.ReplayAll() |
| 146 | au_mock = self._DummyAutoupdateConstructor() |
| 147 | au_mock.serve_only = True |
Chris Sosa | a387a87 | 2010-09-29 11:51:36 -0700 | [diff] [blame] | 148 | au_mock.static_urlbase = 'http://%s/static/archive' % self.hostname |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 149 | self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) |
| 150 | self.mox.VerifyAll() |
| 151 | |
| 152 | |
| 153 | if __name__ == '__main__': |
| 154 | unittest.main() |