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 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 9 | import json |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 10 | import os |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 11 | import shutil |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 12 | import socket |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 13 | import tempfile |
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 | |
Gilad Arnold | abb352e | 2012-09-23 01:24:27 -0700 | [diff] [blame] | 16 | import cherrypy |
| 17 | import mox |
| 18 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 19 | import autoupdate |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 20 | import autoupdate_lib |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 21 | import common_util |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 22 | import devserver_constants as constants |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 23 | import xbuddy |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 24 | |
Gilad Arnold | abb352e | 2012-09-23 01:24:27 -0700 | [diff] [blame] | 25 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 26 | _TEST_REQUEST = """ |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 27 | <client_test xmlns:o="http://www.google.com/update2/request" updaterversion="%(client)s" protocol="3.0"> |
| 28 | <app version="%(version)s" track="%(track)s" board="%(board)s" /> |
| 29 | <updatecheck /> |
| 30 | <event eventresult="%(event_result)d" eventtype="%(event_type)d" /> |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 31 | </client_test>""" |
| 32 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 33 | #pylint: disable=W0212 |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 34 | class AutoupdateTest(mox.MoxTestBase): |
| 35 | def setUp(self): |
| 36 | mox.MoxTestBase.setUp(self) |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 37 | self.mox.StubOutWithMock(common_util, 'GetFileSize') |
| 38 | self.mox.StubOutWithMock(common_util, 'GetFileSha1') |
| 39 | self.mox.StubOutWithMock(common_util, 'GetFileSha256') |
Chris Sosa | c4e8784 | 2013-08-16 18:04:14 -0700 | [diff] [blame] | 40 | self.mox.StubOutWithMock(common_util, 'IsInsideChroot') |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 41 | self.mox.StubOutWithMock(autoupdate_lib, 'GetUpdateResponse') |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 42 | self.mox.StubOutWithMock(autoupdate.Autoupdate, '_GetRemotePayloadAttrs') |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 43 | self.port = 8080 |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 44 | self.test_board = 'test-board' |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 45 | self.build_root = tempfile.mkdtemp('autoupdate_build_root') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 46 | self.latest_dir = '12345_af_12-a1' |
| 47 | self.latest_verision = '12345_af_12' |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 48 | self.static_image_dir = tempfile.mkdtemp('autoupdate_static_dir') |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 49 | self.hostname = '%s:%s' % (socket.gethostname(), self.port) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 50 | self.test_dict = { |
| 51 | 'client': 'ChromeOSUpdateEngine-1.0', |
| 52 | 'version': 'ForcedUpdate', |
| 53 | 'track': 'unused_var', |
| 54 | 'board': self.test_board, |
| 55 | 'event_result': 2, |
| 56 | 'event_type': 3 |
| 57 | } |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 58 | self.test_data = _TEST_REQUEST % self.test_dict |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 59 | self.sha1 = 12345 |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 60 | self.size = 54321 |
| 61 | self.url = 'http://%s/static/update.gz' % self.hostname |
| 62 | self.payload = 'My payload' |
Chris Sosa | a387a87 | 2010-09-29 11:51:36 -0700 | [diff] [blame] | 63 | self.sha256 = 'SHA LA LA' |
Chris Sosa | 5455586 | 2010-10-25 17:26:17 -0700 | [diff] [blame] | 64 | cherrypy.request.base = 'http://%s' % self.hostname |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 65 | common_util.MkDirP(self.static_image_dir) |
| 66 | self._xbuddy = xbuddy.XBuddy(False, |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 67 | static_dir=self.static_image_dir) |
| 68 | self.mox.StubOutWithMock(xbuddy.XBuddy, '_GetArtifact') |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 69 | |
| 70 | def tearDown(self): |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 71 | shutil.rmtree(self.build_root) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 72 | shutil.rmtree(self.static_image_dir) |
Chris Sosa | 5455586 | 2010-10-25 17:26:17 -0700 | [diff] [blame] | 73 | |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 74 | def _DummyAutoupdateConstructor(self, **kwargs): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 75 | """Creates a dummy autoupdater. Used to avoid using constructor.""" |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 76 | dummy = autoupdate.Autoupdate(self._xbuddy, |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 77 | static_dir=self.static_image_dir, |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 78 | **kwargs) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 79 | return dummy |
| 80 | |
Chris Sosa | 744e147 | 2011-09-07 19:32:50 -0700 | [diff] [blame] | 81 | def testGetRightSignedDeltaPayloadDir(self): |
| 82 | """Test that our directory is what we expect it to be for signed updates.""" |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 83 | self.mox.StubOutWithMock(common_util, 'GetFileMd5') |
Chris Sosa | 744e147 | 2011-09-07 19:32:50 -0700 | [diff] [blame] | 84 | key_path = 'test_key_path' |
| 85 | src_image = 'test_src_image' |
| 86 | target_image = 'test_target_image' |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 87 | src_hash = '12345' |
| 88 | target_hash = '67890' |
| 89 | key_hash = 'abcde' |
Chris Sosa | 744e147 | 2011-09-07 19:32:50 -0700 | [diff] [blame] | 90 | |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 91 | common_util.GetFileMd5(src_image).AndReturn(src_hash) |
| 92 | common_util.GetFileMd5(target_image).AndReturn(target_hash) |
| 93 | common_util.GetFileMd5(key_path).AndReturn(key_hash) |
Chris Sosa | 744e147 | 2011-09-07 19:32:50 -0700 | [diff] [blame] | 94 | |
| 95 | self.mox.ReplayAll() |
| 96 | au_mock = self._DummyAutoupdateConstructor() |
| 97 | au_mock.private_key = key_path |
| 98 | update_dir = au_mock.FindCachedUpdateImageSubDir(src_image, target_image) |
Scott Zawalski | 1695453 | 2012-03-20 15:31:36 -0400 | [diff] [blame] | 99 | self.assertEqual(os.path.basename(update_dir), |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 100 | '%s_%s+%s+patched_kernel' % |
| 101 | (src_hash, target_hash, key_hash)) |
Chris Sosa | 744e147 | 2011-09-07 19:32:50 -0700 | [diff] [blame] | 102 | self.mox.VerifyAll() |
| 103 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 104 | def testGenerateLatestUpdateImage(self): |
| 105 | """Test default behavior in response to plain update call.""" |
| 106 | latest_label = os.path.join(self.test_board, self.latest_dir) |
| 107 | # Generate a fake latest image |
| 108 | latest_image_dir = os.path.join(self.static_image_dir, latest_label) |
| 109 | common_util.MkDirP(latest_image_dir) |
| 110 | image = os.path.join(latest_image_dir, constants.TEST_IMAGE_FILE) |
| 111 | with open(image, 'w') as fh: |
| 112 | fh.write('') |
| 113 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 114 | self.mox.StubOutWithMock(autoupdate.Autoupdate, |
| 115 | 'GenerateUpdateImageWithCache') |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 116 | au_mock = self._DummyAutoupdateConstructor() |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 117 | |
Chris Sosa | c4e8784 | 2013-08-16 18:04:14 -0700 | [diff] [blame] | 118 | common_util.IsInsideChroot().AndReturn(True) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 119 | self._xbuddy._GetArtifact( |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 120 | [''], board=self.test_board, lookup_only=True).AndReturn( |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 121 | (latest_label, constants.TEST_IMAGE_FILE)) |
| 122 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 123 | au_mock.GenerateUpdateImageWithCache( |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 124 | os.path.join(self.static_image_dir, self.test_board, self.latest_dir, |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 125 | constants.TEST_IMAGE_FILE)).AndReturn('update.gz') |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 126 | |
| 127 | self.mox.ReplayAll() |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 128 | test_data = _TEST_REQUEST % self.test_dict |
| 129 | self.assertTrue(au_mock.HandleUpdatePing(test_data)) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 130 | self.mox.VerifyAll() |
| 131 | |
| 132 | def testHandleUpdatePingForForcedImage(self): |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 133 | """Test update response to having a forced image.""" |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 134 | self.mox.StubOutWithMock(autoupdate.Autoupdate, |
| 135 | 'GenerateUpdateImageWithCache') |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 136 | self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile') |
| 137 | au_mock = self._DummyAutoupdateConstructor() |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 138 | test_data = _TEST_REQUEST % self.test_dict |
| 139 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 140 | # Generate a fake image |
| 141 | forced_image_dir = '/tmp/path_to_force/' |
| 142 | forced_image = forced_image_dir + constants.IMAGE_FILE |
| 143 | common_util.MkDirP(forced_image_dir) |
| 144 | with open(forced_image, 'w') as fh: |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 145 | fh.write('') |
| 146 | |
joychen | 18737f3 | 2013-08-16 17:18:12 -0700 | [diff] [blame] | 147 | cache_image_dir = os.path.join(self.static_image_dir, 'cache') |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 148 | |
| 149 | # Mock out GenerateUpdateImageWithCache to make an update file in cache |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 150 | def mock_fn(_image): |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 151 | print 'mock_fn' |
| 152 | # No good way to introduce an update file during execution. |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 153 | cache_dir = os.path.join(self.static_image_dir, 'cache') |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 154 | common_util.MkDirP(cache_dir) |
| 155 | update_image = os.path.join(cache_dir, constants.UPDATE_FILE) |
| 156 | with open(update_image, 'w') as fh: |
| 157 | fh.write('') |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 158 | metadata_hash = os.path.join(cache_dir, constants.METADATA_HASH_FILE) |
| 159 | with open(metadata_hash, 'w') as fh: |
| 160 | fh.write('') |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 161 | |
Chris Sosa | c4e8784 | 2013-08-16 18:04:14 -0700 | [diff] [blame] | 162 | common_util.IsInsideChroot().AndReturn(True) |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 163 | au_mock.GenerateUpdateImageWithCache(forced_image).WithSideEffects( |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 164 | mock_fn).AndReturn('cache') |
| 165 | |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 166 | common_util.GetFileSha1(os.path.join( |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 167 | cache_image_dir, 'update.gz')).AndReturn(self.sha1) |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 168 | common_util.GetFileSha256(os.path.join( |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 169 | cache_image_dir, 'update.gz')).AndReturn(self.sha256) |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 170 | common_util.GetFileSize(os.path.join( |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 171 | cache_image_dir, 'update.gz')).AndReturn(self.size) |
| 172 | au_mock._StoreMetadataToFile(cache_image_dir, |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 173 | mox.IsA(autoupdate.UpdateMetadata)) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 174 | forced_url = 'http://%s/static/%s/update.gz' % (self.hostname, |
joychen | 18737f3 | 2013-08-16 17:18:12 -0700 | [diff] [blame] | 175 | 'cache') |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 176 | autoupdate_lib.GetUpdateResponse( |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 177 | self.sha1, self.sha256, self.size, forced_url, False, 0, None, None, |
| 178 | '3.0', False).AndReturn(self.payload) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 179 | |
| 180 | self.mox.ReplayAll() |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 181 | au_mock.forced_image = forced_image |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 182 | self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) |
| 183 | self.mox.VerifyAll() |
| 184 | |
joychen | dbfe6c9 | 2013-08-16 20:03:49 -0700 | [diff] [blame] | 185 | def testHandleForcePregenerateXBuddy(self): |
| 186 | """Check pregenerating an xbuddy path. |
| 187 | |
| 188 | A forced image that starts with 'xbuddy:' uses the following path to |
| 189 | obtain an update. |
| 190 | """ |
| 191 | self.mox.StubOutWithMock(autoupdate.Autoupdate, |
| 192 | 'GetUpdateForLabel') |
| 193 | au_mock = self._DummyAutoupdateConstructor() |
| 194 | au_mock.forced_image = "xbuddy:b/v/a" |
| 195 | |
joychen | 365a574 | 2013-08-21 10:41:18 -0700 | [diff] [blame] | 196 | self._xbuddy._GetArtifact( |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 197 | ['b', 'v', 'a']).AndReturn(('label', constants.TEST_IMAGE_FILE)) |
joychen | 365a574 | 2013-08-21 10:41:18 -0700 | [diff] [blame] | 198 | |
joychen | dbfe6c9 | 2013-08-16 20:03:49 -0700 | [diff] [blame] | 199 | au_mock.GetUpdateForLabel( |
| 200 | autoupdate.FORCED_UPDATE, 'b/v/a').AndReturn('p') |
| 201 | self.mox.ReplayAll() |
| 202 | |
| 203 | au_mock.PreGenerateUpdate() |
| 204 | self.mox.VerifyAll() |
| 205 | |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 206 | def testChangeUrlPort(self): |
| 207 | r = autoupdate._ChangeUrlPort('http://fuzzy:8080/static', 8085) |
| 208 | self.assertEqual(r, 'http://fuzzy:8085/static') |
| 209 | |
| 210 | r = autoupdate._ChangeUrlPort('http://fuzzy/static', 8085) |
| 211 | self.assertEqual(r, 'http://fuzzy:8085/static') |
| 212 | |
| 213 | r = autoupdate._ChangeUrlPort('ftp://fuzzy/static', 8085) |
| 214 | self.assertEqual(r, 'ftp://fuzzy:8085/static') |
| 215 | |
| 216 | r = autoupdate._ChangeUrlPort('ftp://fuzzy', 8085) |
| 217 | self.assertEqual(r, 'ftp://fuzzy:8085') |
| 218 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 219 | def testHandleHostInfoPing(self): |
| 220 | au_mock = self._DummyAutoupdateConstructor() |
| 221 | self.assertRaises(AssertionError, au_mock.HandleHostInfoPing, None) |
| 222 | |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 223 | # Setup fake host_infos entry and ensure it comes back to us in one piece. |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 224 | test_ip = '1.2.3.4' |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 225 | au_mock.host_infos.GetInitHostInfo(test_ip).attrs = self.test_dict |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 226 | self.assertEqual( |
| 227 | json.loads(au_mock.HandleHostInfoPing(test_ip)), self.test_dict) |
| 228 | |
| 229 | def testHandleSetUpdatePing(self): |
| 230 | au_mock = self._DummyAutoupdateConstructor() |
| 231 | test_ip = '1.2.3.4' |
| 232 | test_label = 'test/old-update' |
| 233 | self.assertRaises( |
| 234 | AssertionError, au_mock.HandleSetUpdatePing, test_ip, None) |
| 235 | self.assertRaises( |
| 236 | AssertionError, au_mock.HandleSetUpdatePing, None, test_label) |
| 237 | self.assertRaises( |
| 238 | AssertionError, au_mock.HandleSetUpdatePing, None, None) |
| 239 | |
| 240 | au_mock.HandleSetUpdatePing(test_ip, test_label) |
| 241 | self.assertEqual( |
Chris Sosa | 1885d03 | 2012-11-29 17:07:27 -0800 | [diff] [blame] | 242 | au_mock.host_infos.GetHostInfo(test_ip).attrs['forced_update_label'], |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 243 | test_label) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 244 | |
| 245 | def testHandleUpdatePingWithSetUpdate(self): |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 246 | """If update is set, it should use the update found in that directory.""" |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 247 | self.mox.StubOutWithMock(autoupdate.Autoupdate, '_StoreMetadataToFile') |
| 248 | au_mock = self._DummyAutoupdateConstructor() |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 249 | |
| 250 | test_data = _TEST_REQUEST % self.test_dict |
| 251 | test_label = 'new_update-test/the-new-update' |
| 252 | new_image_dir = os.path.join(self.static_image_dir, test_label) |
| 253 | new_url = self.url.replace('update.gz', test_label + '/update.gz') |
| 254 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 255 | # Generate a fake payload. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 256 | common_util.MkDirP(new_image_dir) |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 257 | update_gz = os.path.join(new_image_dir, constants.UPDATE_FILE) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 258 | with open(update_gz, 'w') as fh: |
| 259 | fh.write('') |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 260 | metadata_hash = os.path.join(new_image_dir, constants.METADATA_HASH_FILE) |
| 261 | with open(metadata_hash, 'w') as fh: |
| 262 | fh.write('') |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 263 | |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 264 | common_util.GetFileSha1(os.path.join( |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 265 | new_image_dir, 'update.gz')).AndReturn(self.sha1) |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 266 | common_util.GetFileSha256(os.path.join( |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 267 | new_image_dir, 'update.gz')).AndReturn(self.sha256) |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 268 | common_util.GetFileSize(os.path.join( |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 269 | new_image_dir, 'update.gz')).AndReturn(self.size) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 270 | au_mock._StoreMetadataToFile(new_image_dir, |
| 271 | mox.IsA(autoupdate.UpdateMetadata)) |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 272 | autoupdate_lib.GetUpdateResponse( |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 273 | self.sha1, self.sha256, self.size, new_url, False, 0, None, None, |
| 274 | '3.0', False).AndReturn(self.payload) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 275 | |
| 276 | self.mox.ReplayAll() |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 277 | au_mock.HandleSetUpdatePing('127.0.0.1', test_label) |
| 278 | self.assertEqual( |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 279 | au_mock.host_infos.GetHostInfo('127.0.0.1'). |
Chris Sosa | 1885d03 | 2012-11-29 17:07:27 -0800 | [diff] [blame] | 280 | attrs['forced_update_label'], |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 281 | test_label) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 282 | self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 283 | self.assertFalse('forced_update_label' in |
| 284 | au_mock.host_infos.GetHostInfo('127.0.0.1').attrs) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 285 | |
Daniel Erat | 8a0bc4a | 2011-09-30 08:52:52 -0700 | [diff] [blame] | 286 | def testGetVersionFromDir(self): |
| 287 | au = self._DummyAutoupdateConstructor() |
| 288 | |
| 289 | # New-style version number. |
| 290 | self.assertEqual( |
| 291 | au._GetVersionFromDir('/foo/x86-alex/R16-1102.0.2011_09_30_0806-a1'), |
| 292 | '1102.0.2011_09_30_0806') |
| 293 | |
Daniel Erat | 8a0bc4a | 2011-09-30 08:52:52 -0700 | [diff] [blame] | 294 | def testCanUpdate(self): |
| 295 | au = self._DummyAutoupdateConstructor() |
| 296 | |
| 297 | # When both the client and the server have new-style versions, we should |
| 298 | # just compare the tokens directly. |
| 299 | self.assertTrue( |
| 300 | au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_30_0806')) |
| 301 | self.assertTrue( |
| 302 | au._CanUpdate('1098.0.2011_09_28_1635', '1100.0.2011_09_26_0000')) |
| 303 | self.assertFalse( |
| 304 | au._CanUpdate('1098.0.2011_09_28_1635', '1098.0.2011_09_26_0000')) |
| 305 | self.assertFalse( |
| 306 | au._CanUpdate('1098.0.2011_09_28_1635', '1096.0.2011_09_30_0000')) |
| 307 | |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 308 | def testHandleUpdatePingRemotePayload(self): |
| 309 | remote_urlbase = 'http://remotehost:6666' |
| 310 | remote_payload_path = 'static/path/to/update.gz' |
| 311 | remote_url = '/'.join([remote_urlbase, remote_payload_path, 'update.gz']) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 312 | au_mock = self._DummyAutoupdateConstructor(urlbase=remote_urlbase, |
| 313 | payload_path=remote_payload_path, |
| 314 | remote_payload=True) |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 315 | |
| 316 | test_data = _TEST_REQUEST % self.test_dict |
| 317 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 318 | au_mock._GetRemotePayloadAttrs(remote_url).AndReturn( |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 319 | autoupdate.UpdateMetadata(self.sha1, self.sha256, self.size, False, |
| 320 | 0, '')) |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 321 | autoupdate_lib.GetUpdateResponse( |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 322 | self.sha1, self.sha256, self.size, remote_url, False, 0, None, None, |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 323 | '3.0', False).AndReturn(self.payload) |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 324 | |
| 325 | self.mox.ReplayAll() |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 326 | self.assertEqual(au_mock.HandleUpdatePing(test_data), self.payload) |
| 327 | self.mox.VerifyAll() |
| 328 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 329 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 330 | if __name__ == '__main__': |
| 331 | unittest.main() |