| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 | 2 |
| 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Unit tests for autoupdate.py.""" | 7 """Unit tests for autoupdate.py.""" |
| 8 | 8 |
| 9 import mox | 9 import mox |
| 10 import os | 10 import os |
| 11 import unittest | 11 import unittest |
| 12 import web | 12 import web |
| 13 | 13 |
| 14 import autoupdate | 14 import autoupdate |
| 15 | 15 |
| 16 _TEST_REQUEST = """ | 16 _TEST_REQUEST = """ |
| 17 <client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(c
lient)s" > | 17 <client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(c
lient)s" > |
| 18 <o:app version="%(version)s" track="%(track)s" board="%(board)s" /> | 18 <o:app version="%(version)s" track="%(track)s" board="%(board)s" /> |
| 19 <o:updatecheck /> |
| 19 </client_test>""" | 20 </client_test>""" |
| 20 | 21 |
| 21 | 22 |
| 22 class AutoupdateTest(mox.MoxTestBase): | 23 class AutoupdateTest(mox.MoxTestBase): |
| 23 def setUp(self): | 24 def setUp(self): |
| 24 mox.MoxTestBase.setUp(self) | 25 mox.MoxTestBase.setUp(self) |
| 25 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSize') | 26 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSize') |
| 26 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetHash') | 27 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetHash') |
| 28 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSHA256') |
| 27 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GetUpdatePayload') | 29 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GetUpdatePayload') |
| 28 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetLatestImageDir') | 30 self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetLatestImageDir') |
| 29 self.test_board = 'test-board' | 31 self.test_board = 'test-board' |
| 30 self.build_root = '/src_path/build/images' | 32 self.build_root = '/src_path/build/images' |
| 31 self.latest_dir = '12345_af_12-a1' | 33 self.latest_dir = '12345_af_12-a1' |
| 32 self.latest_verision = '12345_af_12' | 34 self.latest_verision = '12345_af_12' |
| 33 self.static_image_dir = '/tmp/static-dir/' | 35 self.static_image_dir = '/tmp/static-dir/' |
| 34 self.hostname = 'fake-host' | 36 self.hostname = 'fake-host' |
| 35 self.test_dict = { 'client': 'ChromeOSUpdateEngine-1.0', | 37 self.test_dict = { 'client': 'ChromeOSUpdateEngine-1.0', |
| 36 'version': 'ForcedUpdate', | 38 'version': 'ForcedUpdate', |
| 37 'track': 'unused_var', | 39 'track': 'unused_var', |
| 38 'board': self.test_board | 40 'board': self.test_board |
| 39 } | 41 } |
| 40 self.test_data = _TEST_REQUEST % self.test_dict | 42 self.test_data = _TEST_REQUEST % self.test_dict |
| 41 self.forced_image_path = '/path_to_force/chromiumos_image.bin' | 43 self.forced_image_path = '/path_to_force/chromiumos_image.bin' |
| 42 self.hash = 12345 | 44 self.hash = 12345 |
| 43 self.size = 54321 | 45 self.size = 54321 |
| 44 self.url = 'http://%s/static/update.gz' % self.hostname | 46 self.url = 'http://%s/static/update.gz' % self.hostname |
| 45 self.payload = 'My payload' | 47 self.payload = 'My payload' |
| 48 self.sha256 = 'SHA LA LA' |
| 46 | 49 |
| 47 def _DummyAutoupdateConstructor(self): | 50 def _DummyAutoupdateConstructor(self): |
| 48 """Creates a dummy autoupdater. Used to avoid using constructor.""" | 51 """Creates a dummy autoupdater. Used to avoid using constructor.""" |
| 49 dummy = autoupdate.Autoupdate(root_dir=None, | 52 dummy = autoupdate.Autoupdate(root_dir=None, |
| 50 static_dir=self.static_image_dir) | 53 static_dir=self.static_image_dir) |
| 51 dummy.client_prefix = 'ChromeOSUpdateEngine' | 54 dummy.client_prefix = 'ChromeOSUpdateEngine' |
| 52 | 55 |
| 53 # Set to fool the web. | 56 # Set to fool the web. |
| 54 web.ctx.host = self.hostname | 57 web.ctx.host = self.hostname |
| 55 return dummy | 58 return dummy |
| (...skipping 14 matching lines...) Expand all Loading... |
| 70 'ForcedUpdate', | 73 'ForcedUpdate', |
| 71 self.static_image_dir)) | 74 self.static_image_dir)) |
| 72 self.mox.VerifyAll() | 75 self.mox.VerifyAll() |
| 73 | 76 |
| 74 def testHandleUpdatePingForForcedImage(self): | 77 def testHandleUpdatePingForForcedImage(self): |
| 75 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateUpdateImage') | 78 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateUpdateImage') |
| 76 | 79 |
| 77 test_data = _TEST_REQUEST % self.test_dict | 80 test_data = _TEST_REQUEST % self.test_dict |
| 78 | 81 |
| 79 autoupdate.Autoupdate.GenerateUpdateImage( | 82 autoupdate.Autoupdate.GenerateUpdateImage( |
| 80 self.forced_image_path, self.static_image_dir).AndReturn(True) | 83 self.forced_image_path, |
| 84 move_to_static_dir=True, |
| 85 static_image_dir=self.static_image_dir).AndReturn(True) |
| 81 autoupdate.Autoupdate._GetHash(os.path.join( | 86 autoupdate.Autoupdate._GetHash(os.path.join( |
| 82 self.static_image_dir, 'update.gz')).AndReturn(self.hash) | 87 self.static_image_dir, 'update.gz')).AndReturn(self.hash) |
| 88 autoupdate.Autoupdate._GetSHA256(os.path.join( |
| 89 self.static_image_dir, 'update.gz')).AndReturn(self.sha256) |
| 83 autoupdate.Autoupdate._GetSize(os.path.join( | 90 autoupdate.Autoupdate._GetSize(os.path.join( |
| 84 self.static_image_dir, 'update.gz')).AndReturn(self.size) | 91 self.static_image_dir, 'update.gz')).AndReturn(self.size) |
| 85 autoupdate.Autoupdate.GetUpdatePayload( | 92 autoupdate.Autoupdate.GetUpdatePayload( |
| 86 self.hash, self.size, self.url).AndReturn(self.payload) | 93 self.hash, self.sha256, self.size, self.url).AndReturn(self.payload) |
| 87 | 94 |
| 88 self.mox.ReplayAll() | 95 self.mox.ReplayAll() |
| 89 au_mock = self._DummyAutoupdateConstructor() | 96 au_mock = self._DummyAutoupdateConstructor() |
| 90 au_mock.forced_image = self.forced_image_path | 97 au_mock.forced_image = self.forced_image_path |
| 91 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) | 98 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) |
| 92 self.mox.VerifyAll() | 99 self.mox.VerifyAll() |
| 93 | 100 |
| 94 def testHandleUpdatePingForLatestImage(self): | 101 def testHandleUpdatePingForLatestImage(self): |
| 95 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage') | 102 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage') |
| 96 | 103 |
| 97 test_data = _TEST_REQUEST % self.test_dict | 104 test_data = _TEST_REQUEST % self.test_dict |
| 98 | 105 |
| 99 autoupdate.Autoupdate.GenerateLatestUpdateImage( | 106 autoupdate.Autoupdate.GenerateLatestUpdateImage( |
| 100 self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn(True) | 107 self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn(True) |
| 101 autoupdate.Autoupdate._GetHash(os.path.join( | 108 autoupdate.Autoupdate._GetHash(os.path.join( |
| 102 self.static_image_dir, 'update.gz')).AndReturn(self.hash) | 109 self.static_image_dir, 'update.gz')).AndReturn(self.hash) |
| 110 autoupdate.Autoupdate._GetSHA256(os.path.join( |
| 111 self.static_image_dir, 'update.gz')).AndReturn(self.sha256) |
| 103 autoupdate.Autoupdate._GetSize(os.path.join( | 112 autoupdate.Autoupdate._GetSize(os.path.join( |
| 104 self.static_image_dir, 'update.gz')).AndReturn(self.size) | 113 self.static_image_dir, 'update.gz')).AndReturn(self.size) |
| 105 autoupdate.Autoupdate.GetUpdatePayload( | 114 autoupdate.Autoupdate.GetUpdatePayload( |
| 106 self.hash, self.size, self.url).AndReturn(self.payload) | 115 self.hash, self.sha256, self.size, self.url).AndReturn(self.payload) |
| 107 | 116 |
| 108 self.mox.ReplayAll() | 117 self.mox.ReplayAll() |
| 109 au_mock = self._DummyAutoupdateConstructor() | 118 au_mock = self._DummyAutoupdateConstructor() |
| 110 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) | 119 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) |
| 111 self.mox.VerifyAll() | 120 self.mox.VerifyAll() |
| 112 | 121 |
| 113 def testHandleUpdatePingForArchivedBuild(self): | 122 def testHandleUpdatePingForArchivedBuild(self): |
| 114 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateImageFromZip') | 123 self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateImageFromZip') |
| 115 | 124 |
| 116 test_data = _TEST_REQUEST % self.test_dict | 125 test_data = _TEST_REQUEST % self.test_dict |
| 117 | 126 |
| 118 autoupdate.Autoupdate.GenerateImageFromZip( | 127 autoupdate.Autoupdate.GenerateImageFromZip( |
| 119 self.static_image_dir).AndReturn(True) | 128 self.static_image_dir).AndReturn(True) |
| 120 autoupdate.Autoupdate._GetHash(os.path.join( | 129 autoupdate.Autoupdate._GetHash(os.path.join( |
| 121 self.static_image_dir, 'update.gz')).AndReturn(self.hash) | 130 self.static_image_dir, 'update.gz')).AndReturn(self.hash) |
| 131 autoupdate.Autoupdate._GetSHA256(os.path.join( |
| 132 self.static_image_dir, 'update.gz')).AndReturn(self.sha256) |
| 122 autoupdate.Autoupdate._GetSize(os.path.join( | 133 autoupdate.Autoupdate._GetSize(os.path.join( |
| 123 self.static_image_dir, 'update.gz')).AndReturn(self.size) | 134 self.static_image_dir, 'update.gz')).AndReturn(self.size) |
| 124 autoupdate.Autoupdate.GetUpdatePayload( | 135 autoupdate.Autoupdate.GetUpdatePayload( |
| 125 self.hash, self.size, | 136 self.hash, self.sha256, self.size, |
| 126 'http://%s/static/archive/update.gz' % self.hostname).AndReturn( | 137 'http://%s/static/archive/update.gz' % self.hostname).AndReturn( |
| 127 self.payload) | 138 self.payload) |
| 128 | 139 |
| 129 self.mox.ReplayAll() | 140 self.mox.ReplayAll() |
| 130 au_mock = self._DummyAutoupdateConstructor() | 141 au_mock = self._DummyAutoupdateConstructor() |
| 131 au_mock.serve_only = True | 142 au_mock.serve_only = True |
| 143 au_mock.static_urlbase = 'http://%s/static/archive' % self.hostname |
| 132 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) | 144 self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) |
| 133 self.mox.VerifyAll() | 145 self.mox.VerifyAll() |
| 134 | 146 |
| 135 | 147 |
| 136 if __name__ == '__main__': | 148 if __name__ == '__main__': |
| 137 unittest.main() | 149 unittest.main() |
| OLD | NEW |