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 |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame^] | 10 | import json |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 11 | import mox |
| 12 | import os |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 13 | import socket |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 14 | import unittest |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 15 | |
| 16 | import autoupdate |
| 17 | |
| 18 | _TEST_REQUEST = """ |
| 19 | <client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(client)s" > |
| 20 | <o:app version="%(version)s" track="%(track)s" board="%(board)s" /> |
Chris Sosa | a387a87 | 2010-09-29 11:51:36 -0700 | [diff] [blame] | 21 | <o:updatecheck /> |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame^] | 22 | <o:event eventresult="%(event_result)d" eventtype="%(event_type)d" /> |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 23 | </client_test>""" |
| 24 | |
| 25 | |
| 26 | class AutoupdateTest(mox.MoxTestBase): |
| 27 | def setUp(self): |
| 28 | mox.MoxTestBase.setUp(self) |
| 29 | self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSize') |
| 30 | self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetHash') |
Chris Sosa | a387a87 | 2010-09-29 11:51:36 -0700 | [diff] [blame] | 31 | self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetSHA256') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 32 | self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GetUpdatePayload') |
| 33 | self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetLatestImageDir') |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 34 | self.port = 8080 |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 35 | self.test_board = 'test-board' |
| 36 | self.build_root = '/src_path/build/images' |
| 37 | self.latest_dir = '12345_af_12-a1' |
| 38 | self.latest_verision = '12345_af_12' |
| 39 | self.static_image_dir = '/tmp/static-dir/' |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 40 | self.hostname = '%s:%s' % (socket.gethostname(), self.port) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame^] | 41 | self.test_dict = { |
| 42 | 'client': 'ChromeOSUpdateEngine-1.0', |
| 43 | 'version': 'ForcedUpdate', |
| 44 | 'track': 'unused_var', |
| 45 | 'board': self.test_board, |
| 46 | 'event_result': 2, |
| 47 | 'event_type': 3 |
| 48 | } |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 49 | self.test_data = _TEST_REQUEST % self.test_dict |
| 50 | self.forced_image_path = '/path_to_force/chromiumos_image.bin' |
| 51 | self.hash = 12345 |
| 52 | self.size = 54321 |
| 53 | self.url = 'http://%s/static/update.gz' % self.hostname |
| 54 | self.payload = 'My payload' |
Chris Sosa | a387a87 | 2010-09-29 11:51:36 -0700 | [diff] [blame] | 55 | self.sha256 = 'SHA LA LA' |
Chris Sosa | 5455586 | 2010-10-25 17:26:17 -0700 | [diff] [blame] | 56 | cherrypy.request.base = 'http://%s' % self.hostname |
| 57 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 58 | def _DummyAutoupdateConstructor(self): |
| 59 | """Creates a dummy autoupdater. Used to avoid using constructor.""" |
| 60 | dummy = autoupdate.Autoupdate(root_dir=None, |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 61 | static_dir=self.static_image_dir, |
| 62 | port=self.port) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 63 | dummy.client_prefix = 'ChromeOSUpdateEngine' |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 64 | return dummy |
| 65 | |
| 66 | def testGenerateLatestUpdateImageWithForced(self): |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 67 | self.mox.StubOutWithMock(autoupdate.Autoupdate, |
| 68 | 'GenerateUpdateImageWithCache') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 69 | autoupdate.Autoupdate._GetLatestImageDir(self.test_board).AndReturn( |
| 70 | '%s/%s/%s' % (self.build_root, self.test_board, self.latest_dir)) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 71 | autoupdate.Autoupdate.GenerateUpdateImageWithCache( |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 72 | '%s/%s/%s/chromiumos_image.bin' % (self.build_root, self.test_board, |
| 73 | self.latest_dir), |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 74 | static_image_dir=self.static_image_dir).AndReturn('update.gz') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 75 | |
| 76 | self.mox.ReplayAll() |
| 77 | au_mock = self._DummyAutoupdateConstructor() |
| 78 | self.assertTrue(au_mock.GenerateLatestUpdateImage(self.test_board, |
| 79 | 'ForcedUpdate', |
| 80 | self.static_image_dir)) |
| 81 | self.mox.VerifyAll() |
| 82 | |
| 83 | def testHandleUpdatePingForForcedImage(self): |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 84 | self.mox.StubOutWithMock(autoupdate.Autoupdate, |
| 85 | 'GenerateUpdateImageWithCache') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 86 | |
| 87 | test_data = _TEST_REQUEST % self.test_dict |
| 88 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 89 | autoupdate.Autoupdate.GenerateUpdateImageWithCache( |
Chris Sosa | a387a87 | 2010-09-29 11:51:36 -0700 | [diff] [blame] | 90 | self.forced_image_path, |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 91 | static_image_dir=self.static_image_dir).AndReturn('update.gz') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 92 | autoupdate.Autoupdate._GetHash(os.path.join( |
| 93 | self.static_image_dir, 'update.gz')).AndReturn(self.hash) |
Chris Sosa | a387a87 | 2010-09-29 11:51:36 -0700 | [diff] [blame] | 94 | autoupdate.Autoupdate._GetSHA256(os.path.join( |
| 95 | self.static_image_dir, 'update.gz')).AndReturn(self.sha256) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 96 | autoupdate.Autoupdate._GetSize(os.path.join( |
| 97 | self.static_image_dir, 'update.gz')).AndReturn(self.size) |
| 98 | autoupdate.Autoupdate.GetUpdatePayload( |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 99 | self.hash, self.sha256, self.size, self.url, False).AndReturn( |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame^] | 100 | self.payload) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 101 | |
| 102 | self.mox.ReplayAll() |
| 103 | au_mock = self._DummyAutoupdateConstructor() |
| 104 | au_mock.forced_image = self.forced_image_path |
| 105 | self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) |
| 106 | self.mox.VerifyAll() |
| 107 | |
| 108 | def testHandleUpdatePingForLatestImage(self): |
| 109 | self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage') |
| 110 | |
| 111 | test_data = _TEST_REQUEST % self.test_dict |
| 112 | |
| 113 | autoupdate.Autoupdate.GenerateLatestUpdateImage( |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 114 | self.test_board, 'ForcedUpdate', self.static_image_dir).AndReturn( |
| 115 | 'update.gz') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 116 | autoupdate.Autoupdate._GetHash(os.path.join( |
| 117 | self.static_image_dir, 'update.gz')).AndReturn(self.hash) |
Chris Sosa | a387a87 | 2010-09-29 11:51:36 -0700 | [diff] [blame] | 118 | autoupdate.Autoupdate._GetSHA256(os.path.join( |
| 119 | self.static_image_dir, 'update.gz')).AndReturn(self.sha256) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 120 | autoupdate.Autoupdate._GetSize(os.path.join( |
| 121 | self.static_image_dir, 'update.gz')).AndReturn(self.size) |
| 122 | autoupdate.Autoupdate.GetUpdatePayload( |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 123 | self.hash, self.sha256, self.size, self.url, False).AndReturn( |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame^] | 124 | self.payload) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 125 | |
| 126 | self.mox.ReplayAll() |
| 127 | au_mock = self._DummyAutoupdateConstructor() |
| 128 | self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame^] | 129 | self.assertEqual( |
| 130 | au_mock.host_info['127.0.0.1']['last_known_version'], 'ForcedUpdate') |
| 131 | self.assertEqual( |
| 132 | au_mock.host_info['127.0.0.1']['last_event_type'], |
| 133 | self.test_dict['event_type']) |
| 134 | self.assertEqual( |
| 135 | au_mock.host_info['127.0.0.1']['last_event_status'], |
| 136 | self.test_dict['event_result']) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 137 | self.mox.VerifyAll() |
| 138 | |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 139 | def testChangeUrlPort(self): |
| 140 | r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085) |
| 141 | self.assertEqual(r, 'http://fuzzy:8085/static') |
| 142 | |
| 143 | r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085) |
| 144 | self.assertEqual(r, 'http://fuzzy:8085/static') |
| 145 | |
| 146 | r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085) |
| 147 | self.assertEqual(r, 'ftp://fuzzy:8085/static') |
| 148 | |
| 149 | r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085) |
| 150 | self.assertEqual(r, 'ftp://fuzzy:8085') |
| 151 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame^] | 152 | def testHandleHostInfoPing(self): |
| 153 | au_mock = self._DummyAutoupdateConstructor() |
| 154 | self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None) |
| 155 | |
| 156 | # Setup fake host_info entry and ensure it comes back to us in one piece. |
| 157 | test_ip = '1.2.3.4' |
| 158 | au_mock.host_info[test_ip] = self.test_dict |
| 159 | self.assertEqual( |
| 160 | json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict) |
| 161 | |
| 162 | def testHandleSetUpdatePing(self): |
| 163 | au_mock = self._DummyAutoupdateConstructor() |
| 164 | test_ip = '1.2.3.4' |
| 165 | test_label = 'test/old-update' |
| 166 | self.assertRaises( |
| 167 | AssertionError, au_mock.HandleSetUpdatePing, test_ip, None) |
| 168 | self.assertRaises( |
| 169 | AssertionError, au_mock.HandleSetUpdatePing, None, test_label) |
| 170 | self.assertRaises( |
| 171 | AssertionError, au_mock.HandleSetUpdatePing, None, None) |
| 172 | |
| 173 | au_mock.HandleSetUpdatePing(test_ip, test_label) |
| 174 | self.assertEqual( |
| 175 | au_mock.host_info[test_ip]['forced_update_label'], test_label) |
| 176 | |
| 177 | def testHandleUpdatePingWithSetUpdate(self): |
| 178 | self.mox.StubOutWithMock(autoupdate.Autoupdate, 'GenerateLatestUpdateImage') |
| 179 | |
| 180 | test_data = _TEST_REQUEST % self.test_dict |
| 181 | test_label = 'new_update-test/the-new-update' |
| 182 | new_image_dir = os.path.join(self.static_image_dir, test_label) |
| 183 | new_url = self.url.replace('update.gz', test_label + '/update.gz') |
| 184 | |
| 185 | autoupdate.Autoupdate.GenerateLatestUpdateImage( |
| 186 | self.test_board, 'ForcedUpdate', new_image_dir).AndReturn( |
| 187 | 'update.gz') |
| 188 | autoupdate.Autoupdate._GetHash(os.path.join( |
| 189 | new_image_dir, 'update.gz')).AndReturn(self.hash) |
| 190 | autoupdate.Autoupdate._GetSHA256(os.path.join( |
| 191 | new_image_dir, 'update.gz')).AndReturn(self.sha256) |
| 192 | autoupdate.Autoupdate._GetSize(os.path.join( |
| 193 | new_image_dir, 'update.gz')).AndReturn(self.size) |
| 194 | autoupdate.Autoupdate.GetUpdatePayload( |
| 195 | self.hash, self.sha256, self.size, new_url, False).AndReturn( |
| 196 | self.payload) |
| 197 | |
| 198 | self.mox.ReplayAll() |
| 199 | au_mock = self._DummyAutoupdateConstructor() |
| 200 | au_mock.HandleSetUpdatePing('127.0.0.1', test_label) |
| 201 | self.assertEqual( |
| 202 | au_mock.host_info['127.0.0.1']['forced_update_label'], test_label) |
| 203 | self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) |
| 204 | self.assertFalse('forced_update_label' in au_mock.host_info['127.0.0.1']) |
| 205 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 206 | |
| 207 | if __name__ == '__main__': |
| 208 | unittest.main() |