Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame^] | 1 | # Copyright 2021 The Chromium OS Authors. All rights reserved. |
| 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 | |
| 7 | import sys |
| 8 | import tempfile |
| 9 | |
| 10 | import mock |
| 11 | |
| 12 | from chromite.cli import device_imager |
| 13 | from chromite.lib import cros_test_lib |
| 14 | from chromite.lib import partial_mock |
| 15 | from chromite.lib import remote_access |
| 16 | from chromite.lib import remote_access_unittest |
| 17 | from chromite.lib.xbuddy import xbuddy |
| 18 | |
| 19 | |
| 20 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 21 | |
| 22 | |
| 23 | # pylint: disable=protected-access |
| 24 | |
| 25 | class DeviceImagerTest(cros_test_lib.MockTestCase): |
| 26 | """Tests DeviceImager class methods.""" |
| 27 | |
| 28 | def setUp(self): |
| 29 | """Sets up the class by creating proper mocks.""" |
| 30 | self.rsh_mock = self.StartPatcher(remote_access_unittest.RemoteShMock()) |
| 31 | self.rsh_mock.AddCmdResult(partial_mock.In('${PATH}'), stdout='') |
| 32 | self.path_env = 'PATH=%s:' % remote_access.DEV_BIN_PATHS |
| 33 | |
| 34 | def test_GetImageLocalFile(self): |
| 35 | """Tests getting the path to local image.""" |
| 36 | with tempfile.NamedTemporaryFile() as fp: |
| 37 | di = device_imager.DeviceImager(None, fp.name) |
| 38 | self.assertEqual(di._GetImage(), (fp.name, device_imager.ImageType.FULL)) |
| 39 | |
| 40 | def test_GetImageDir(self): |
| 41 | """Tests failing on a given directory as a path.""" |
| 42 | di = device_imager.DeviceImager(None, '/tmp') |
| 43 | with self.assertRaises(ValueError): |
| 44 | di._GetImage() |
| 45 | |
| 46 | @mock.patch.object(xbuddy.XBuddy, 'Translate', return_value=('eve/R90', None)) |
| 47 | def test_GetImageXBuddyRemote(self, _): |
| 48 | """Tests getting remote xBuddy image path.""" |
| 49 | di = device_imager.DeviceImager(None, 'xbuddy://remote/eve/latest') |
| 50 | self.assertEqual(di._GetImage(), |
| 51 | ('gs://chromeos-image-archive/eve/R90', |
| 52 | device_imager.ImageType.REMOTE_DIRECTORY)) |
| 53 | |
| 54 | @mock.patch.object(xbuddy.XBuddy, 'Translate', |
| 55 | return_value=('eve/R90', 'path/to/file')) |
| 56 | def test_GetImageXBuddyLocal(self, _): |
| 57 | """Tests getting local xBuddy image path.""" |
| 58 | di = device_imager.DeviceImager(None, 'xbuddy://local/eve/latest') |
| 59 | self.assertEqual(di._GetImage(), |
| 60 | ('path/to/file', device_imager.ImageType.FULL)) |
| 61 | |
| 62 | def test_SplitDevPath(self): |
| 63 | """Tests splitting a device path into prefix and partition number.""" |
| 64 | |
| 65 | di = device_imager.DeviceImager(None, None) |
| 66 | self.assertEqual(di._SplitDevPath('/dev/foop3'), ('/dev/foop', 3)) |
| 67 | |
| 68 | with self.assertRaises(device_imager.Error): |
| 69 | di._SplitDevPath('/foo') |
| 70 | |
| 71 | with self.assertRaises(device_imager.Error): |
| 72 | di._SplitDevPath('/foo/p3p') |
| 73 | |
| 74 | def test_GetKernelState(self): |
| 75 | """Tests getting the current active and inactive kernel states.""" |
| 76 | di = device_imager.DeviceImager(None, None) |
| 77 | self.assertEqual(di._GetKernelState(3), (device_imager.DeviceImager.A, |
| 78 | device_imager.DeviceImager.B)) |
| 79 | self.assertEqual(di._GetKernelState(5), (device_imager.DeviceImager.B, |
| 80 | device_imager.DeviceImager.A)) |
| 81 | |
| 82 | with self.assertRaises(device_imager.Error): |
| 83 | di._GetKernelState(1) |
| 84 | |
| 85 | @mock.patch.object(remote_access.ChromiumOSDevice, 'root_dev', |
| 86 | return_value='/dev/foop3', new_callable=mock.PropertyMock) |
| 87 | def test_VerifyBootExpectations(self, _): |
| 88 | """Tests verifying the boot expectations after reboot.""" |
| 89 | |
| 90 | with remote_access.ChromiumOSDeviceHandler(remote_access.TEST_IP) as device: |
| 91 | di = device_imager.DeviceImager(device, None) |
| 92 | di._inactive_state = device_imager.DeviceImager.A |
| 93 | di._VerifyBootExpectations() |
| 94 | |
| 95 | @mock.patch.object(remote_access.ChromiumOSDevice, 'root_dev', |
| 96 | return_value='/dev/foop3', new_callable=mock.PropertyMock) |
| 97 | def test_VerifyBootExpectationsFails(self, _): |
| 98 | """Tests failure of boot expectations.""" |
| 99 | |
| 100 | with remote_access.ChromiumOSDeviceHandler(remote_access.TEST_IP) as device: |
| 101 | di = device_imager.DeviceImager(device, None) |
| 102 | di._inactive_state = device_imager.DeviceImager.B |
| 103 | with self.assertRaises(device_imager.Error): |
| 104 | di._VerifyBootExpectations() |