Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2021 The ChromiumOS Authors |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Unit tests for the device_imager module.""" |
| 6 | |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 7 | import os |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 8 | import tempfile |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 9 | import time |
Mike Frysinger | 166fea0 | 2021-02-12 05:30:33 -0500 | [diff] [blame] | 10 | from unittest import mock |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 11 | |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 12 | from chromite.cli import device_imager |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 13 | from chromite.lib import constants |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 14 | from chromite.lib import cros_test_lib |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 15 | from chromite.lib import gs |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 16 | from chromite.lib import image_lib |
| 17 | from chromite.lib import image_lib_unittest |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 18 | from chromite.lib import partial_mock |
| 19 | from chromite.lib import remote_access |
| 20 | from chromite.lib import remote_access_unittest |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 21 | from chromite.lib import stateful_updater |
| 22 | from chromite.lib.paygen import paygen_stateful_payload_lib |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 23 | from chromite.lib.xbuddy import xbuddy |
| 24 | |
| 25 | |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 26 | # pylint: disable=protected-access |
| 27 | |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 28 | |
| 29 | def GetFdPath(fd): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 30 | """Returns the fd path for the current process.""" |
| 31 | return f"/proc/self/fd/{fd}" |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 32 | |
| 33 | |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 34 | class DeviceImagerTest(cros_test_lib.MockTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 35 | """Tests DeviceImager class methods.""" |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 36 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 37 | def setUp(self): |
| 38 | """Sets up the class by creating proper mocks.""" |
| 39 | self.rsh_mock = self.StartPatcher(remote_access_unittest.RemoteShMock()) |
| 40 | self.rsh_mock.AddCmdResult(partial_mock.In("${PATH}"), stdout="") |
| 41 | self.path_env = "PATH=%s:" % remote_access.DEV_BIN_PATHS |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 42 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 43 | def test_LocateImageLocalFile(self): |
| 44 | """Tests getting the path to local image.""" |
| 45 | with tempfile.NamedTemporaryFile() as fp: |
| 46 | di = device_imager.DeviceImager(None, fp.name) |
| 47 | di._LocateImage() |
| 48 | self.assertEqual(di._image, fp.name) |
| 49 | self.assertEqual(di._image_type, device_imager.ImageType.FULL) |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 50 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 51 | def test_LocateImageDir(self): |
| 52 | """Tests failing on a given directory as a path.""" |
| 53 | di = device_imager.DeviceImager(None, "/tmp") |
| 54 | with self.assertRaises(ValueError): |
| 55 | di._LocateImage() |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 56 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 57 | @mock.patch.object( |
| 58 | xbuddy.XBuddy, "Translate", return_value=("eve/R90", None) |
| 59 | ) |
| 60 | @mock.patch.object( |
| 61 | remote_access.ChromiumOSDevice, |
| 62 | "board", |
| 63 | return_value="foo", |
| 64 | new_callable=mock.PropertyMock, |
| 65 | ) |
| 66 | # pylint: disable=unused-argument |
| 67 | def test_LocateImageXBuddyRemote(self, _, board_mock): |
| 68 | """Tests getting remote xBuddy image path.""" |
| 69 | with remote_access.ChromiumOSDeviceHandler( |
| 70 | remote_access.TEST_IP |
| 71 | ) as device: |
| 72 | di = device_imager.DeviceImager( |
| 73 | device, "xbuddy://remote/eve/latest" |
| 74 | ) |
| 75 | di._LocateImage() |
| 76 | self.assertEqual(di._image, "gs://chromeos-image-archive/eve/R90") |
| 77 | self.assertEqual( |
| 78 | di._image_type, device_imager.ImageType.REMOTE_DIRECTORY |
| 79 | ) |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 80 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 81 | @mock.patch.object( |
| 82 | xbuddy.XBuddy, "Translate", return_value=("eve/R90", "path/to/file") |
| 83 | ) |
| 84 | @mock.patch.object( |
| 85 | remote_access.ChromiumOSDevice, |
| 86 | "board", |
| 87 | return_value="foo", |
| 88 | new_callable=mock.PropertyMock, |
| 89 | ) |
| 90 | # pylint: disable=unused-argument |
| 91 | def test_LocateImageXBuddyLocal(self, _, board_mock): |
| 92 | """Tests getting local xBuddy image path.""" |
| 93 | with remote_access.ChromiumOSDeviceHandler( |
| 94 | remote_access.TEST_IP |
| 95 | ) as device: |
| 96 | di = device_imager.DeviceImager(device, "xbuddy://local/eve/latest") |
| 97 | di._LocateImage() |
| 98 | self.assertEqual(di._image, "path/to/file") |
| 99 | self.assertEqual(di._image_type, device_imager.ImageType.FULL) |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 100 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 101 | def test_SplitDevPath(self): |
| 102 | """Tests splitting a device path into prefix and partition number.""" |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 103 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 104 | di = device_imager.DeviceImager(None, None) |
| 105 | self.assertEqual(di._SplitDevPath("/dev/foop3"), ("/dev/foop", 3)) |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 106 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 107 | with self.assertRaises(device_imager.Error): |
| 108 | di._SplitDevPath("/foo") |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 109 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 110 | with self.assertRaises(device_imager.Error): |
| 111 | di._SplitDevPath("/foo/p3p") |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 112 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 113 | def test_GetKernelState(self): |
| 114 | """Tests getting the current active and inactive kernel states.""" |
| 115 | di = device_imager.DeviceImager(None, None) |
| 116 | self.assertEqual( |
| 117 | di._GetKernelState(3), |
| 118 | (device_imager.DeviceImager.A, device_imager.DeviceImager.B), |
| 119 | ) |
| 120 | self.assertEqual( |
| 121 | di._GetKernelState(5), |
| 122 | (device_imager.DeviceImager.B, device_imager.DeviceImager.A), |
| 123 | ) |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 124 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | with self.assertRaises(device_imager.Error): |
| 126 | di._GetKernelState(1) |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 127 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 128 | @mock.patch.object( |
| 129 | remote_access.ChromiumOSDevice, |
| 130 | "root_dev", |
| 131 | return_value="/dev/foop3", |
| 132 | new_callable=mock.PropertyMock, |
| 133 | ) |
| 134 | def test_VerifyBootExpectations(self, _): |
| 135 | """Tests verifying the boot expectations after reboot.""" |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 136 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 137 | with remote_access.ChromiumOSDeviceHandler( |
| 138 | remote_access.TEST_IP |
| 139 | ) as device: |
| 140 | di = device_imager.DeviceImager(device, None) |
| 141 | di._inactive_state = device_imager.DeviceImager.A |
| 142 | di._VerifyBootExpectations() |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 143 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 144 | @mock.patch.object( |
| 145 | remote_access.ChromiumOSDevice, |
| 146 | "root_dev", |
| 147 | return_value="/dev/foop3", |
| 148 | new_callable=mock.PropertyMock, |
| 149 | ) |
| 150 | def test_VerifyBootExpectationsFails(self, _): |
| 151 | """Tests failure of boot expectations.""" |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 152 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 153 | with remote_access.ChromiumOSDeviceHandler( |
| 154 | remote_access.TEST_IP |
| 155 | ) as device: |
| 156 | di = device_imager.DeviceImager(device, None) |
| 157 | di._inactive_state = device_imager.DeviceImager.B |
| 158 | with self.assertRaises(device_imager.Error): |
| 159 | di._VerifyBootExpectations() |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 160 | |
| 161 | |
| 162 | class TestReaderBase(cros_test_lib.MockTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 163 | """Test ReaderBase class""" |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 164 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 165 | def testNamedPipe(self): |
| 166 | """Tests initializing the class with named pipe.""" |
| 167 | with device_imager.ReaderBase(use_named_pipes=True) as r: |
| 168 | self.assertIsInstance(r.Target(), str) |
| 169 | self.assertEqual(r._Source(), r.Target()) |
| 170 | self.assertExists(r.Target()) |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 171 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 172 | r._CloseSource() # Should not have any effect. |
| 173 | self.assertExists(r._Source()) |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 174 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 175 | # Closing target should delete the named pipe. |
| 176 | r.CloseTarget() |
| 177 | self.assertNotExists(r.Target()) |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 178 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 179 | def testFdPipe(self): |
| 180 | """Tests initializing the class with normal file descriptor pipes.""" |
| 181 | with device_imager.ReaderBase() as r: |
| 182 | self.assertIsInstance(r.Target(), int) |
| 183 | self.assertIsInstance(r._Source(), int) |
| 184 | self.assertNotEqual(r._Source(), r.Target()) |
| 185 | self.assertExists(GetFdPath(r.Target())) |
| 186 | self.assertExists(GetFdPath(r._Source())) |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 187 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 188 | # Per crbug.com/1196702 it seems like some other process gets the file |
| 189 | # descriptor right after we close it and by the time we check its |
| 190 | # existence, it is still there and this can flake. So it might be better |
| 191 | # to make sure this is checked properly through real paths and not |
| 192 | # symlinks. |
| 193 | path = GetFdPath(r._Source()) |
| 194 | old_path = os.path.realpath(path) |
| 195 | r._CloseSource() |
| 196 | with self.assertRaises(OSError): |
| 197 | new_path = os.path.realpath(path) |
| 198 | self.assertNotEqual(old_path, new_path) |
| 199 | raise OSError("Fake the context manager.") |
Amin Hassani | fa11c69 | 2021-04-07 09:17:31 -0700 | [diff] [blame] | 200 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 201 | self.assertExists(GetFdPath(r.Target())) |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 202 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 203 | path = GetFdPath(r.Target()) |
| 204 | old_path = os.path.realpath(path) |
| 205 | r.CloseTarget() |
| 206 | with self.assertRaises(OSError): |
| 207 | new_path = os.path.realpath(path) |
| 208 | self.assertNotEqual(old_path, new_path) |
| 209 | raise OSError("Fake the context manager.") |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 210 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 211 | def testFdPipeCommunicate(self): |
| 212 | """Tests that file descriptors pipe can actually communicate.""" |
| 213 | with device_imager.ReaderBase() as r: |
| 214 | with os.fdopen(r._Source(), "w") as fp: |
| 215 | fp.write("helloworld") |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 216 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 217 | with os.fdopen(r.Target(), "r") as fp: |
| 218 | self.assertEqual(fp.read(), "helloworld") |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 219 | |
| 220 | |
| 221 | class PartialFileReaderTest(cros_test_lib.RunCommandTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 222 | """Tests PartialFileReader class.""" |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 223 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 224 | def testRun(self): |
| 225 | """Tests the main run() function.""" |
| 226 | with device_imager.PartialFileReader( |
| 227 | "/foo", 512 * 2, 512, ["/usr/bin/pigz"] |
| 228 | ) as pfr: |
| 229 | pass |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 230 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 231 | self.assertCommandCalled( |
| 232 | "dd status=none if=/foo ibs=512 skip=2 count=1 | /usr/bin/pigz", |
| 233 | stdout=pfr._Source(), |
| 234 | shell=True, |
| 235 | ) |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 236 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 237 | # Make sure the source has been close. |
| 238 | self.assertNotExists(GetFdPath(pfr._Source())) |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 239 | |
| 240 | |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 241 | class GsFileCopierTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 242 | """Tests GsFileCopier class.""" |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 243 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 244 | @mock.patch.object(gs.GSContext, "Copy") |
| 245 | def testRun(self, copy_mock): |
| 246 | """Tests the run() function.""" |
| 247 | image = "gs://path/to/image" |
| 248 | with device_imager.GsFileCopier(image) as gfc: |
| 249 | self.assertTrue(gfc._use_named_pipes) |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 250 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 251 | copy_mock.assert_called_with(image, gfc._Source()) |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 252 | |
| 253 | |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 254 | class PartitionUpdaterBaseTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 255 | """Tests PartitionUpdaterBase class""" |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 256 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 257 | def testRunNotImplemented(self): |
| 258 | """Tests running the main Run() function is not implemented.""" |
| 259 | # We just want to make sure the _Run() function is not implemented here. |
| 260 | pub = device_imager.PartitionUpdaterBase(None, None, None, None) |
| 261 | with self.assertRaises(NotImplementedError): |
| 262 | pub.Run() |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 263 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 264 | def testRevertNotImplemented(self): |
| 265 | """Tests running the Revert() function is not implemented.""" |
| 266 | pub = device_imager.PartitionUpdaterBase(None, None, None, None) |
| 267 | with self.assertRaises(NotImplementedError): |
| 268 | pub.Revert() |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 269 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 270 | @mock.patch.object(device_imager.PartitionUpdaterBase, "_Run") |
| 271 | def testIsFinished(self, _): |
| 272 | """Tests IsFinished() function.""" |
| 273 | pub = device_imager.PartitionUpdaterBase(None, None, None, None) |
| 274 | self.assertFalse(pub.IsFinished()) |
| 275 | pub.Run() |
| 276 | self.assertTrue(pub.IsFinished()) |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 277 | |
| 278 | |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 279 | class RawPartitionUpdaterTest(cros_test_lib.MockTempDirTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 280 | """Tests RawPartitionUpdater class.""" |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 281 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 282 | def setUp(self): |
| 283 | """Sets up the class by creating proper mocks.""" |
| 284 | self.rsh_mock = self.StartPatcher(remote_access_unittest.RemoteShMock()) |
| 285 | self.rsh_mock.AddCmdResult(partial_mock.In("${PATH}"), stdout="") |
| 286 | self.path_env = "PATH=%s:" % remote_access.DEV_BIN_PATHS |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 287 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 288 | @mock.patch.object( |
| 289 | device_imager.RawPartitionUpdater, |
| 290 | "_GetPartitionName", |
| 291 | return_value=constants.PART_KERN_A, |
| 292 | ) |
| 293 | @mock.patch.object( |
| 294 | image_lib, |
| 295 | "GetImageDiskPartitionInfo", |
| 296 | return_value=image_lib_unittest.LOOP_PARTITION_INFO, |
| 297 | ) |
| 298 | @mock.patch.object(device_imager.PartialFileReader, "CloseTarget") |
| 299 | @mock.patch.object(device_imager.PartialFileReader, "run") |
| 300 | def test_RunFullImage(self, run_mock, close_mock, _, name_mock): |
| 301 | """Test main Run() function for full image. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 302 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 303 | This function should parts of the source image and write it into the device |
| 304 | using proper compression programs. |
| 305 | """ |
| 306 | with remote_access.ChromiumOSDeviceHandler( |
| 307 | remote_access.TEST_IP |
| 308 | ) as device: |
| 309 | self.rsh_mock.AddCmdResult( |
| 310 | [partial_mock.In("which"), "gzip"], returncode=0 |
| 311 | ) |
| 312 | self.rsh_mock.AddCmdResult( |
| 313 | self.path_env |
| 314 | + " gzip --decompress --stdout | " |
| 315 | + "dd bs=1M oflag=direct of=/dev/mmcblk0p2" |
| 316 | ) |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 317 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 318 | device_imager.RawPartitionUpdater( |
| 319 | device, |
| 320 | "foo-image", |
| 321 | device_imager.ImageType.FULL, |
| 322 | "/dev/mmcblk0p2", |
| 323 | ).Run() |
| 324 | run_mock.assert_called() |
| 325 | close_mock.assert_called() |
| 326 | name_mock.assert_called() |
Amin Hassani | d684e98 | 2021-02-26 11:10:58 -0800 | [diff] [blame] | 327 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 328 | def test_RunRemoteImage(self): |
| 329 | """Test main Run() function for remote images.""" |
| 330 | with remote_access.ChromiumOSDeviceHandler( |
| 331 | remote_access.TEST_IP |
| 332 | ) as device: |
| 333 | self.rsh_mock.AddCmdResult( |
| 334 | [partial_mock.In("which"), "gzip"], returncode=0 |
| 335 | ) |
| 336 | self.rsh_mock.AddCmdResult( |
| 337 | self.path_env |
| 338 | + " gzip --decompress --stdout | " |
| 339 | + "dd bs=1M oflag=direct of=/dev/mmcblk0p2" |
| 340 | ) |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 341 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 342 | path = os.path.join( |
| 343 | self.tempdir, constants.QUICK_PROVISION_PAYLOAD_KERNEL |
| 344 | ) |
| 345 | with open(path, "w") as image: |
| 346 | image.write("helloworld") |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 347 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 348 | device_imager.KernelUpdater( |
| 349 | device, |
| 350 | self.tempdir, |
| 351 | device_imager.ImageType.REMOTE_DIRECTORY, |
| 352 | "/dev/mmcblk0p2", |
| 353 | ).Run() |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 354 | |
Amin Hassani | d684e98 | 2021-02-26 11:10:58 -0800 | [diff] [blame] | 355 | |
| 356 | class KernelUpdaterTest(cros_test_lib.MockTempDirTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 357 | """Tests KernelUpdater class.""" |
Amin Hassani | d684e98 | 2021-02-26 11:10:58 -0800 | [diff] [blame] | 358 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 359 | def test_GetPartitionName(self): |
| 360 | """Tests the name of the partitions.""" |
| 361 | ku = device_imager.KernelUpdater(None, None, None, None) |
| 362 | self.assertEqual(constants.PART_KERN_B, ku._GetPartitionName()) |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 363 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 364 | def test_GetRemotePartitionName(self): |
| 365 | """Tests the name of the partitions.""" |
| 366 | ku = device_imager.KernelUpdater(None, None, None, None) |
| 367 | self.assertEqual( |
| 368 | constants.QUICK_PROVISION_PAYLOAD_KERNEL, |
| 369 | ku._GetRemotePartitionName(), |
| 370 | ) |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 371 | |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 372 | |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 373 | class MiniOSUpdaterTest(cros_test_lib.MockTempDirTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 374 | """Tests MiniOSUpdater class.""" |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 375 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 376 | def test_GetPartitionName(self): |
| 377 | """Tests the name of the partitions.""" |
| 378 | u = device_imager.MiniOSUpdater(*([None] * 4)) |
| 379 | self.assertEqual(constants.PART_MINIOS_A, u._GetPartitionName()) |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 380 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 381 | def test_GetRemotePartitionName(self): |
| 382 | """Tests the name of the partitions.""" |
| 383 | u = device_imager.MiniOSUpdater(*([None] * 4)) |
| 384 | self.assertEqual( |
| 385 | constants.QUICK_PROVISION_PAYLOAD_MINIOS, |
| 386 | u._GetRemotePartitionName(), |
| 387 | ) |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 388 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 389 | @mock.patch.object(device_imager.MiniOSUpdater, "_CopyPartitionFromImage") |
| 390 | @mock.patch.object( |
| 391 | device_imager.MiniOSUpdater, |
| 392 | "_MiniOSPartitionsExistInImage", |
| 393 | return_value=True, |
| 394 | ) |
| 395 | @mock.patch.object(device_imager.MiniOSUpdater, "_RunPostInstall") |
| 396 | def test_Run(self, postinstall_mock, partitions_exist_mock, copy_mock): |
| 397 | """Test main Run() function.""" |
| 398 | with remote_access.ChromiumOSDeviceHandler( |
| 399 | remote_access.TEST_IP |
| 400 | ) as device: |
| 401 | device_imager.MiniOSUpdater( |
| 402 | device, |
| 403 | "foo-image", |
| 404 | device_imager.ImageType.FULL, |
| 405 | "/dev/mmcblk0p10", |
| 406 | ).Run() |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 407 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 408 | copy_mock.assert_called_with(constants.PART_MINIOS_A) |
| 409 | partitions_exist_mock.assert_called_with() |
| 410 | postinstall_mock.assert_called_with() |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 411 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 412 | @mock.patch.object(device_imager.MiniOSUpdater, "_CopyPartitionFromImage") |
| 413 | @mock.patch.object( |
| 414 | device_imager.MiniOSUpdater, |
| 415 | "_MiniOSPartitionsExistInImage", |
| 416 | return_value=False, |
| 417 | ) |
| 418 | def test_RunMissingMiniOS(self, partitions_exist_mock, copy_mock): |
| 419 | """Test main Run() function with missing miniOS partitions on image.""" |
| 420 | with remote_access.ChromiumOSDeviceHandler( |
| 421 | remote_access.TEST_IP |
| 422 | ) as device: |
| 423 | device_imager.MiniOSUpdater( |
| 424 | device, |
| 425 | "foo-image", |
| 426 | device_imager.ImageType.FULL, |
| 427 | "/dev/mmcblk0p10", |
| 428 | ).Run() |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 429 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 430 | copy_mock.assert_not_called() |
| 431 | partitions_exist_mock.assert_called_with() |
Jae Hoon Kim | b88b796 | 2021-10-18 11:08:38 -0700 | [diff] [blame] | 432 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 433 | @mock.patch.object(device_imager.MiniOSUpdater, "_RunPostInstall") |
| 434 | @mock.patch.object(device_imager.MiniOSUpdater, "_RedirectPartition") |
| 435 | @mock.patch.object(device_imager.MiniOSUpdater, "_MiniOSPartitionsExist") |
| 436 | @mock.patch.object(gs.GSContext, "Exists", return_value=False) |
| 437 | def test_RunMissingMiniOSRemotePayload( |
| 438 | self, |
| 439 | gs_context_mock, |
| 440 | partitions_exist_mock, |
| 441 | redirect_mock, |
| 442 | post_install_mock, |
| 443 | ): |
| 444 | """Test main Run() function with missing miniOS remote payloads.""" |
| 445 | with remote_access.ChromiumOSDeviceHandler( |
| 446 | remote_access.TEST_IP |
| 447 | ) as device: |
| 448 | device_imager.MiniOSUpdater( |
| 449 | device, |
| 450 | "foo-image", |
| 451 | device_imager.ImageType.REMOTE_DIRECTORY, |
| 452 | "/dev/mmcblk0p10", |
| 453 | ).Run() |
Jae Hoon Kim | b88b796 | 2021-10-18 11:08:38 -0700 | [diff] [blame] | 454 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 455 | post_install_mock.assert_not_called() |
| 456 | redirect_mock.assert_not_called() |
| 457 | partitions_exist_mock.assert_not_called() |
| 458 | gs_context_mock.assert_called() |
Jae Hoon Kim | b88b796 | 2021-10-18 11:08:38 -0700 | [diff] [blame] | 459 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 460 | @mock.patch.object(device_imager.MiniOSUpdater, "_RunPostInstall") |
| 461 | @mock.patch.object(device_imager.MiniOSUpdater, "_RedirectPartition") |
| 462 | @mock.patch.object( |
| 463 | device_imager.MiniOSUpdater, |
| 464 | "_MiniOSPartitionsExist", |
| 465 | return_value=False, |
| 466 | ) |
| 467 | @mock.patch.object(gs.GSContext, "Exists", return_value=True) |
| 468 | def test_RunMissingMiniOSPartitions( |
| 469 | self, |
| 470 | gs_context_mock, |
| 471 | partitions_exist_mock, |
| 472 | redirect_mock, |
| 473 | post_install_mock, |
| 474 | ): |
| 475 | """Test main Run() function with missing miniOS remote payloads.""" |
| 476 | with remote_access.ChromiumOSDeviceHandler( |
| 477 | remote_access.TEST_IP |
| 478 | ) as device: |
| 479 | device_imager.MiniOSUpdater( |
| 480 | device, |
| 481 | "foo-image", |
| 482 | device_imager.ImageType.REMOTE_DIRECTORY, |
| 483 | "/dev/mmcblk0p10", |
| 484 | ).Run() |
Jae Hoon Kim | b88b796 | 2021-10-18 11:08:38 -0700 | [diff] [blame] | 485 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 486 | post_install_mock.assert_not_called() |
| 487 | redirect_mock.assert_not_called() |
| 488 | partitions_exist_mock.assert_called() |
| 489 | gs_context_mock.assert_called() |
Jae Hoon Kim | b88b796 | 2021-10-18 11:08:38 -0700 | [diff] [blame] | 490 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 491 | @mock.patch.object(device_imager.MiniOSUpdater, "_RunPostInstall") |
| 492 | @mock.patch.object(device_imager.MiniOSUpdater, "_RedirectPartition") |
| 493 | @mock.patch.object( |
| 494 | device_imager.MiniOSUpdater, "_MiniOSPartitionsExist", return_value=True |
| 495 | ) |
| 496 | @mock.patch.object(gs.GSContext, "Exists", return_value=True) |
| 497 | def test_RunMiniOSRemotePayload( |
| 498 | self, |
| 499 | gs_context_mock, |
| 500 | partitions_exist_mock, |
| 501 | redirect_mock, |
| 502 | post_install_mock, |
| 503 | ): |
| 504 | """Test main Run() function with missing miniOS remote payloads.""" |
| 505 | with remote_access.ChromiumOSDeviceHandler( |
| 506 | remote_access.TEST_IP |
| 507 | ) as device: |
| 508 | device_imager.MiniOSUpdater( |
| 509 | device, |
| 510 | "foo-image", |
| 511 | device_imager.ImageType.REMOTE_DIRECTORY, |
| 512 | "/dev/mmcblk0p10", |
| 513 | ).Run() |
Jae Hoon Kim | b88b796 | 2021-10-18 11:08:38 -0700 | [diff] [blame] | 514 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 515 | post_install_mock.assert_called() |
| 516 | redirect_mock.assert_called() |
| 517 | partitions_exist_mock.assert_called() |
| 518 | gs_context_mock.assert_called() |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 519 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 520 | @mock.patch.object(device_imager.MiniOSUpdater, "_FlipMiniOSPriority") |
| 521 | def test_RunPostInstall(self, flip_mock): |
| 522 | """Test _RunPostInstall() function.""" |
| 523 | with remote_access.ChromiumOSDeviceHandler( |
| 524 | remote_access.TEST_IP |
| 525 | ) as device: |
| 526 | device_imager.MiniOSUpdater( |
| 527 | device, |
| 528 | "foo-image", |
| 529 | device_imager.ImageType.FULL, |
| 530 | "/dev/mmcblk0p10", |
| 531 | )._RunPostInstall() |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 532 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 533 | flip_mock.assert_called_with() |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 534 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 535 | @mock.patch.object(device_imager.MiniOSUpdater, "_FlipMiniOSPriority") |
| 536 | def test_Revert(self, flip_mock): |
| 537 | """Test Revert() function.""" |
| 538 | u = device_imager.MiniOSUpdater( |
| 539 | None, "foo-image", device_imager.ImageType.FULL, "/dev/mmcblk0p10" |
| 540 | ) |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 541 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 542 | # Before PostInstall runs. |
| 543 | u.Revert() |
| 544 | flip_mock.assert_not_called() |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 545 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 546 | u._ran_postinst = True |
| 547 | u.Revert() |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 548 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 549 | flip_mock.assert_called_with() |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 550 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 551 | @mock.patch.object( |
| 552 | device_imager.MiniOSUpdater, "_GetMiniOSPriority", return_value="A" |
| 553 | ) |
| 554 | @mock.patch.object(device_imager.MiniOSUpdater, "_SetMiniOSPriority") |
| 555 | def test_FlipMiniOSPriority(self, set_mock, get_mock): |
| 556 | """Test _FlipMiniOSPriority() function.""" |
| 557 | device_imager.MiniOSUpdater( |
| 558 | None, "foo-image", device_imager.ImageType.FULL, "/dev/mmcblk0p10" |
| 559 | )._FlipMiniOSPriority() |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 560 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 561 | get_mock.assert_called_with() |
| 562 | set_mock.assert_called_with("B") |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 563 | |
| 564 | |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 565 | class RootfsUpdaterTest(cros_test_lib.MockTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 566 | """Tests RootfsUpdater class.""" |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 567 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 568 | def setUp(self): |
| 569 | """Sets up the class by creating proper mocks.""" |
| 570 | self.rsh_mock = self.StartPatcher(remote_access_unittest.RemoteShMock()) |
| 571 | self.rsh_mock.AddCmdResult(partial_mock.In("${PATH}"), stdout="") |
| 572 | self.path_env = "PATH=%s:" % remote_access.DEV_BIN_PATHS |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 573 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 574 | def test_GetPartitionName(self): |
| 575 | """Tests the name of the partitions.""" |
| 576 | ru = device_imager.RootfsUpdater(None, None, None, None, None) |
| 577 | self.assertEqual(constants.PART_ROOT_A, ru._GetPartitionName()) |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 578 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 579 | def test_GetRemotePartitionName(self): |
| 580 | """Tests the name of the partitions.""" |
| 581 | ru = device_imager.RootfsUpdater(None, None, None, None, None) |
| 582 | self.assertEqual( |
| 583 | constants.QUICK_PROVISION_PAYLOAD_ROOTFS, |
| 584 | ru._GetRemotePartitionName(), |
| 585 | ) |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 586 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 587 | @mock.patch.object(device_imager.ProgressWatcher, "run") |
| 588 | @mock.patch.object(device_imager.RootfsUpdater, "_RunPostInst") |
| 589 | @mock.patch.object(device_imager.RootfsUpdater, "_CopyPartitionFromImage") |
| 590 | def test_Run(self, copy_mock, postinst_mock, pw_mock): |
| 591 | """Test main Run() function. |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 592 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 593 | This function should parts of the source image and write it into the device |
| 594 | using proper compression programs. |
| 595 | """ |
| 596 | with remote_access.ChromiumOSDeviceHandler( |
| 597 | remote_access.TEST_IP |
| 598 | ) as device: |
| 599 | device_imager.RootfsUpdater( |
| 600 | "/dev/mmcblk0p5", |
| 601 | device, |
| 602 | "foo-image", |
| 603 | device_imager.ImageType.FULL, |
| 604 | "/dev/mmcblk0p3", |
| 605 | ).Run() |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 606 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 607 | copy_mock.assert_called_with(constants.PART_ROOT_A) |
| 608 | postinst_mock.assert_called_with() |
| 609 | pw_mock.assert_called() |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 610 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 611 | def test_RunPostInstOnTarget(self): |
| 612 | """Test _RunPostInst() function.""" |
| 613 | target = "/dev/mmcblk0p3" |
| 614 | with remote_access.ChromiumOSDeviceHandler( |
| 615 | remote_access.TEST_IP |
| 616 | ) as device: |
| 617 | device._work_dir = "/tmp/work_dir" |
| 618 | temp_dir = os.path.join(device.work_dir, "dir") |
| 619 | self.rsh_mock.AddCmdResult( |
| 620 | [self.path_env, "mktemp", "-d", "-p", device.work_dir], |
| 621 | stdout=temp_dir, |
| 622 | ) |
| 623 | self.rsh_mock.AddCmdResult( |
| 624 | [self.path_env, "mount", "-o", "ro", target, temp_dir] |
| 625 | ) |
| 626 | self.rsh_mock.AddCmdResult( |
| 627 | [self.path_env, os.path.join(temp_dir, "postinst"), target] |
| 628 | ) |
| 629 | self.rsh_mock.AddCmdResult([self.path_env, "umount", temp_dir]) |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 630 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 631 | device_imager.RootfsUpdater( |
| 632 | "/dev/mmcblk0p5", |
| 633 | device, |
| 634 | "foo-image", |
| 635 | device_imager.ImageType.FULL, |
| 636 | target, |
| 637 | )._RunPostInst() |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 638 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 639 | def test_RunPostInstOnCurrentRoot(self): |
| 640 | """Test _RunPostInst() on current root (used for reverting an update).""" |
| 641 | root_dev = "/dev/mmcblk0p5" |
| 642 | self.rsh_mock.AddCmdResult([self.path_env, "/postinst", root_dev]) |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 643 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 644 | with remote_access.ChromiumOSDeviceHandler( |
| 645 | remote_access.TEST_IP |
| 646 | ) as device: |
| 647 | device_imager.RootfsUpdater( |
| 648 | root_dev, |
| 649 | device, |
| 650 | "foo-image", |
| 651 | device_imager.ImageType.FULL, |
| 652 | "/dev/mmcblk0p3", |
| 653 | )._RunPostInst(on_target=False) |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 654 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 655 | @mock.patch.object(device_imager.RootfsUpdater, "_RunPostInst") |
| 656 | def testRevert(self, postinst_mock): |
| 657 | """Tests Revert() function.""" |
| 658 | ru = device_imager.RootfsUpdater(None, None, None, None, None) |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 659 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 660 | ru.Revert() |
| 661 | postinst_mock.assert_not_called() |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 662 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 663 | ru._ran_postinst = True |
| 664 | ru.Revert() |
| 665 | postinst_mock.assert_called_with(on_target=False) |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 666 | |
| 667 | |
| 668 | class StatefulPayloadGeneratorTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 669 | """Tests stateful payload generator.""" |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 670 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 671 | @mock.patch.object(paygen_stateful_payload_lib, "GenerateStatefulPayload") |
| 672 | def testRun(self, paygen_mock): |
| 673 | """Tests run() function.""" |
| 674 | image = "/foo/image" |
| 675 | with device_imager.StatefulPayloadGenerator(image) as spg: |
| 676 | pass |
| 677 | |
| 678 | paygen_mock.assert_called_with(image, spg._Source()) |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 679 | |
| 680 | |
| 681 | class StatefulUpdaterTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 682 | """Tests StatefulUpdater.""" |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 683 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 684 | @mock.patch.object(paygen_stateful_payload_lib, "GenerateStatefulPayload") |
| 685 | @mock.patch.object(stateful_updater.StatefulUpdater, "Update") |
| 686 | def test_RunFullImage(self, update_mock, paygen_mock): |
| 687 | """Test main Run() function for full image.""" |
| 688 | with remote_access.ChromiumOSDeviceHandler( |
| 689 | remote_access.TEST_IP |
| 690 | ) as device: |
| 691 | device_imager.StatefulUpdater( |
| 692 | False, device, "foo-image", device_imager.ImageType.FULL, None |
| 693 | ).Run() |
| 694 | update_mock.assert_called_with( |
| 695 | mock.ANY, is_payload_on_device=False, update_type=None |
| 696 | ) |
| 697 | paygen_mock.assert_called() |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 698 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 699 | @mock.patch.object(gs.GSContext, "Copy") |
| 700 | @mock.patch.object(stateful_updater.StatefulUpdater, "Update") |
| 701 | def test_RunRemoteImage(self, update_mock, copy_mock): |
| 702 | """Test main Run() function for remote images.""" |
| 703 | with remote_access.ChromiumOSDeviceHandler( |
| 704 | remote_access.TEST_IP |
| 705 | ) as device: |
| 706 | device_imager.StatefulUpdater( |
| 707 | False, |
| 708 | device, |
| 709 | "gs://foo-image", |
| 710 | device_imager.ImageType.REMOTE_DIRECTORY, |
| 711 | None, |
| 712 | ).Run() |
| 713 | copy_mock.assert_called_with( |
| 714 | "gs://foo-image/stateful.tgz", mock.ANY |
| 715 | ) |
| 716 | update_mock.assert_called_with( |
| 717 | mock.ANY, is_payload_on_device=False, update_type=None |
| 718 | ) |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 719 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 720 | @mock.patch.object(stateful_updater.StatefulUpdater, "Reset") |
| 721 | def testRevert(self, reset_mock): |
| 722 | """Tests Revert() function.""" |
| 723 | su = device_imager.StatefulUpdater(False, None, None, None, None) |
| 724 | |
| 725 | su.Revert() |
| 726 | reset_mock.assert_called() |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 727 | |
| 728 | |
| 729 | class ProgressWatcherTest(cros_test_lib.MockTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 730 | """Tests ProgressWatcher class""" |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 731 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 732 | def setUp(self): |
| 733 | """Sets up the class by creating proper mocks.""" |
| 734 | self.rsh_mock = self.StartPatcher(remote_access_unittest.RemoteShMock()) |
| 735 | self.rsh_mock.AddCmdResult(partial_mock.In("${PATH}"), stdout="") |
| 736 | self.path_env = "PATH=%s:" % remote_access.DEV_BIN_PATHS |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 737 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 738 | @mock.patch.object(time, "sleep") |
| 739 | @mock.patch.object( |
| 740 | device_imager.ProgressWatcher, |
| 741 | "_ShouldExit", |
| 742 | side_effect=[False, False, True], |
| 743 | ) |
| 744 | # pylint: disable=unused-argument |
| 745 | def testRun(self, exit_mock, _): |
| 746 | """Tests the run() function.""" |
| 747 | with remote_access.ChromiumOSDeviceHandler( |
| 748 | remote_access.TEST_IP |
| 749 | ) as device: |
| 750 | target_root = "/foo/root" |
| 751 | self.rsh_mock.AddCmdResult( |
| 752 | [self.path_env, "blockdev", "--getsize64", target_root], |
| 753 | stdout="100", |
| 754 | ) |
| 755 | self.rsh_mock.AddCmdResult( |
| 756 | [self.path_env, "lsof", "-t", target_root], stdout="999" |
| 757 | ) |
| 758 | self.rsh_mock.AddCmdResult( |
| 759 | [self.path_env, "cat", "/proc/999/fdinfo/1"], |
| 760 | stdout="pos: 10\nflags: foo", |
| 761 | ) |
| 762 | pw = device_imager.ProgressWatcher(device, target_root) |
| 763 | pw.run() |