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 | |
| 9 | import mox |
| 10 | import os |
| 11 | import unittest |
| 12 | import web |
| 13 | |
| 14 | import autoupdate |
| 15 | |
| 16 | _TEST_REQUEST = """ |
| 17 | <client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(client)s" > |
| 18 | <o:app version="%(version)s" track="%(track)s" board="%(board)s" /> |
| 19 | </client_test>""" |
| 20 | |
| 21 | |
| 22 | class AutoupdateTest(mox.MoxTestBase): |
| 23 | def setUp(self): |
| 24 | mox.MoxTestBase.setUp(self) |
| 25 | self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSize') |
| 26 | self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetHash') |
| 27 | self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GetUpdatePayload') |
| 28 | self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetLatestImageDir') |
| 29 | self.test_board = 'test-board' |
| 30 | self.build_root = '/src_path/build/images' |
| 31 | self.latest_dir = '12345_af_12-a1' |
| 32 | self.latest_verision = '12345_af_12' |
| 33 | self.static_image_dir = '/tmp/static-dir/' |
| 34 | self.hostname = 'fake-host' |
| 35 | self.test_dict = { 'client': 'ChromeOSUpdateEngine-1.0', |
| 36 | 'version': 'ForcedUpdate', |
| 37 | 'track': 'unused_var', |
| 38 | 'board': self.test_board |
| 39 | } |
| 40 | self.test_data = _TEST_REQUEST % self.test_dict |
| 41 | self.forced_image_path = '/path_to_force/chromiumos_image.bin' |
| 42 | self.hash = 12345 |
| 43 | self.size = 54321 |
| 44 | self.url = 'http://%s/static/update.gz' % self.hostname |
| 45 | self.payload = 'My payload' |
| 46 | |
| 47 | def _DummyAutoupdateConstructor(self): |
| 48 | """Creates a dummy autoupdater. Used to avoid using constructor.""" |
| 49 | dummy = autoupdate.Autoupdate(root_dir=None, |
| 50 | static_dir=self.static_image_dir) |
| 51 | dummy.client_prefix = 'ChromeOSUpdateEngine' |
| 52 | |
| 53 | # Set to fool the web. |
| 54 | web.ctx.host = self.hostname |
| 55 | return dummy |
| 56 | |
| 57 | def testGenerateLatestUpdateImageWithForced(self): |
| 58 | self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateUpdateImage') |
| 59 | autoupdate.Autoupdate._GetLatestImageDir(self.test_board).AndReturn( |
| 60 | '%s/%s/%s' % (self.build_root, self.test_board, self.latest_dir)) |
| 61 | autoupdate.Autoupdate.GenerateUpdateImage( |
| 62 | '%s/%s/%s/chromiumos_image.bin' % (self.build_root, self.test_board, |
| 63 | self.latest_dir), |
| 64 | move_to_static_dir=True, |
| 65 | static_image_dir=self.static_image_dir).AndReturn(True) |
| 66 | |
| 67 | self.mox.ReplayAll() |
| 68 | au_mock = self._DummyAutoupdateConstructor() |
| 69 | self.assertTrue(au_mock.GenerateLatestUpdateImage(self.test_board, |
| 70 | 'ForcedUpdate', |
| 71 | self.static_image_dir)) |
| 72 | self.mox.VerifyAll() |
| 73 | |
| 74 | def testHandleUpdatePingForForcedImage(self): |
| 75 | self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateUpdateImage') |
| 76 | |
| 77 | test_data = _TEST_REQUEST % self.test_dict |
| 78 | |
| 79 | autoupdate.Autoupdate.GenerateUpdateImage( |
| 80 | self.forced_image_path, self.static_image_dir).AndReturn(True) |
| 81 | autoupdate.Autoupdate._GetHash(os.path.join( |
| 82 | self.static_image_dir, 'update.gz')).AndReturn(self.hash) |
| 83 | autoupdate.Autoupdate._GetSize(os.path.join( |
| 84 | self.static_image_dir, 'update.gz')).AndReturn(self.size) |
| 85 | autoupdate.Autoupdate.GetUpdatePayload( |
| 86 | self.hash, self.size, self.url).AndReturn(self.payload) |
| 87 | |
| 88 | self.mox.ReplayAll() |
| 89 | au_mock = self._DummyAutoupdateConstructor() |
| 90 | au_mock.forced_image = self.forced_image_path |
| 91 | self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) |
| 92 | self.mox.VerifyAll() |
| 93 | |
| 94 | def testHandleUpdatePingForLatestImage(self): |
| 95 | self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage') |
| 96 | |
| 97 | test_data = _TEST_REQUEST % self.test_dict |
| 98 | |
| 99 | autoupdate.Autoupdate.GenerateLatestUpdateImage( |
| 100 | self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn(True) |
| 101 | autoupdate.Autoupdate._GetHash(os.path.join( |
| 102 | self.static_image_dir, 'update.gz')).AndReturn(self.hash) |
| 103 | autoupdate.Autoupdate._GetSize(os.path.join( |
| 104 | self.static_image_dir, 'update.gz')).AndReturn(self.size) |
| 105 | autoupdate.Autoupdate.GetUpdatePayload( |
| 106 | self.hash, self.size, self.url).AndReturn(self.payload) |
| 107 | |
| 108 | self.mox.ReplayAll() |
| 109 | au_mock = self._DummyAutoupdateConstructor() |
| 110 | self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) |
| 111 | self.mox.VerifyAll() |
| 112 | |
| 113 | def testHandleUpdatePingForArchivedBuild(self): |
| 114 | self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateImageFromZip') |
| 115 | |
| 116 | test_data = _TEST_REQUEST % self.test_dict |
| 117 | |
| 118 | autoupdate.Autoupdate.GenerateImageFromZip( |
| 119 | self.static_image_dir).AndReturn(True) |
| 120 | autoupdate.Autoupdate._GetHash(os.path.join( |
| 121 | self.static_image_dir, 'update.gz')).AndReturn(self.hash) |
| 122 | autoupdate.Autoupdate._GetSize(os.path.join( |
| 123 | self.static_image_dir, 'update.gz')).AndReturn(self.size) |
| 124 | autoupdate.Autoupdate.GetUpdatePayload( |
| 125 | self.hash, self.size, |
| 126 | 'http://%s/static/archive/update.gz' % self.hostname).AndReturn( |
| 127 | self.payload) |
| 128 | |
| 129 | self.mox.ReplayAll() |
| 130 | au_mock = self._DummyAutoupdateConstructor() |
| 131 | au_mock.serve_only = True |
| 132 | self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) |
| 133 | self.mox.VerifyAll() |
| 134 | |
| 135 | |
| 136 | if __name__ == '__main__': |
| 137 | unittest.main() |