Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 1 | # Copyright 2014 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 functions in test_image.""" |
| 6 | |
| 7 | import os |
| 8 | import tempfile |
| 9 | import unittest |
| 10 | |
Aviv Keshet | b7519e1 | 2016-10-04 00:50:00 -0700 | [diff] [blame] | 11 | from chromite.lib import constants |
Mike Frysinger | 417a85c | 2021-04-23 00:58:17 -0400 | [diff] [blame] | 12 | from chromite.lib import cros_build_lib |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 13 | from chromite.lib import cros_test_lib |
Mike Nichols | a8e8f24 | 2019-09-09 14:10:29 -0600 | [diff] [blame] | 14 | from chromite.lib import image_lib |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame] | 15 | from chromite.lib import image_test_lib |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 16 | from chromite.lib import osutils |
| 17 | from chromite.scripts import test_image |
| 18 | |
| 19 | |
| 20 | class TestImageTest(cros_test_lib.MockTempDirTestCase): |
| 21 | """Common class for tests ImageTest. |
| 22 | |
| 23 | This sets up proper directory with test image. The image file is zero-byte. |
| 24 | """ |
| 25 | |
| 26 | def setUp(self): |
Mike Frysinger | 417a85c | 2021-04-23 00:58:17 -0400 | [diff] [blame] | 27 | # Create empty image file. |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 28 | self.image_file = os.path.join(self.tempdir, |
| 29 | constants.BASE_IMAGE_NAME + '.bin') |
Mike Frysinger | 417a85c | 2021-04-23 00:58:17 -0400 | [diff] [blame] | 30 | osutils.AllocateFile(self.image_file, 1024 * 1024) |
| 31 | # In case sfdisk is in /sbin, search that too. |
| 32 | cros_build_lib.run( |
| 33 | ['sfdisk', self.image_file], |
| 34 | extra_env={'PATH': '/sbin:/usr/sbin:%s' % os.environ['PATH']}, |
| 35 | input='label: dos') |
LaMont Jones | ea6dda6 | 2018-12-04 16:11:45 -0700 | [diff] [blame] | 36 | fake_partitions = ( |
Mike Frysinger | a2aeb37 | 2019-09-20 16:15:43 -0400 | [diff] [blame] | 37 | image_lib.PartitionInfo(1, 0, 0, 0, 'fs', 'STATE', 'flag'), |
| 38 | image_lib.PartitionInfo(2, 0, 0, 0, 'fs', 'KERN-A', 'flag'), |
| 39 | image_lib.PartitionInfo(3, 0, 0, 0, 'fs', 'ROOT-A', 'flag'), |
LaMont Jones | ea6dda6 | 2018-12-04 16:11:45 -0700 | [diff] [blame] | 40 | ) |
Mike Frysinger | a2aeb37 | 2019-09-20 16:15:43 -0400 | [diff] [blame] | 41 | self.PatchObject(image_lib, 'GetImageDiskPartitionInfo', |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 42 | autospec=True, return_value=fake_partitions) |
Mike Nichols | a8e8f24 | 2019-09-09 14:10:29 -0600 | [diff] [blame] | 43 | self.PatchObject(image_lib.LoopbackPartitions, '_Mount', autospec=True) |
| 44 | self.PatchObject(image_lib.LoopbackPartitions, '_Unmount', autospec=True) |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 45 | |
| 46 | |
| 47 | class FindImageTest(TestImageTest): |
| 48 | """Test FindImage() function.""" |
| 49 | |
| 50 | def _testFindOkay(self, image_path): |
| 51 | res = test_image.FindImage(image_path) |
| 52 | self.assertEqual( |
| 53 | res, |
| 54 | os.path.join(self.tempdir, constants.BASE_IMAGE_NAME + '.bin') |
| 55 | ) |
| 56 | |
| 57 | def testFindWithDirectory(self): |
| 58 | self._testFindOkay(self.tempdir) |
| 59 | |
| 60 | def testFindWithFile(self): |
| 61 | self._testFindOkay(self.image_file) |
| 62 | |
| 63 | def testFindWithInvalid(self): |
| 64 | self.assertRaises(ValueError, test_image.FindImage, |
| 65 | os.path.join(self.tempdir, '404')) |
| 66 | |
| 67 | def testFindWithInvalidDirectory(self): |
| 68 | os.unlink(self.image_file) |
| 69 | self.assertRaises(ValueError, test_image.FindImage, |
| 70 | os.path.join(self.tempdir)) |
| 71 | |
| 72 | |
| 73 | class MainTest(TestImageTest): |
| 74 | """Test the main invocation of the script.""" |
| 75 | |
| 76 | def testChdir(self): |
| 77 | """Verify the CWD is in a temp directory.""" |
| 78 | |
Mike Frysinger | 3e04661 | 2017-10-18 17:19:23 -0400 | [diff] [blame] | 79 | class CwdTest(image_test_lib.ImageTestCase): |
Mike Frysinger | a5c6e79 | 2022-03-15 23:42:12 -0400 | [diff] [blame] | 80 | """A stub test class to verify current working directory.""" |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 81 | |
| 82 | _expected_dir = None |
| 83 | |
| 84 | def SetCwd(self, cwd): |
| 85 | self._expected_dir = cwd |
| 86 | |
| 87 | def testExpectedCwd(self): |
| 88 | self.assertEqual(self._expected_dir, os.getcwd()) |
| 89 | |
| 90 | self.assertNotEqual('/tmp', os.getcwd()) |
| 91 | os.chdir('/tmp') |
| 92 | |
| 93 | test = CwdTest('testExpectedCwd') |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame] | 94 | suite = image_test_lib.ImageTestSuite() |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 95 | suite.addTest(test) |
| 96 | self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True, |
Mike Frysinger | 3e04661 | 2017-10-18 17:19:23 -0400 | [diff] [blame] | 97 | return_value=suite) |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 98 | |
| 99 | # Set up the expected directory. |
| 100 | expected_dir = os.path.join(self.tempdir, 'my-subdir') |
| 101 | os.mkdir(expected_dir) |
| 102 | test.SetCwd(expected_dir) |
| 103 | self.PatchObject(tempfile, 'mkdtemp', autospec=True, |
| 104 | return_value=expected_dir) |
| 105 | |
Mike Frysinger | 3bb61cb | 2022-04-14 16:07:44 -0400 | [diff] [blame^] | 106 | argv = [str(self.tempdir)] |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 107 | self.assertEqual(0, test_image.main(argv)) |
| 108 | self.assertEqual('/tmp', os.getcwd()) |
| 109 | |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 110 | def testBoardAndDirectory(self): |
| 111 | """Verify that "--board", "--test_results_root" are passed to the tests.""" |
| 112 | |
Mike Frysinger | 3e04661 | 2017-10-18 17:19:23 -0400 | [diff] [blame] | 113 | class AttributeTest(image_test_lib.ImageTestCase): |
Mike Frysinger | a5c6e79 | 2022-03-15 23:42:12 -0400 | [diff] [blame] | 114 | """Stub test class to hold board and directory.""" |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 115 | |
| 116 | def testOkay(self): |
| 117 | pass |
| 118 | |
| 119 | test = AttributeTest('testOkay') |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame] | 120 | suite = image_test_lib.ImageTestSuite() |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 121 | suite.addTest(test) |
| 122 | self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True, |
Mike Frysinger | 3e04661 | 2017-10-18 17:19:23 -0400 | [diff] [blame] | 123 | return_value=suite) |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 124 | argv = [ |
| 125 | '--board', |
| 126 | 'my-board', |
| 127 | '--test_results_root', |
| 128 | 'your-root', |
Mike Frysinger | 3bb61cb | 2022-04-14 16:07:44 -0400 | [diff] [blame^] | 129 | str(self.tempdir), |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 130 | ] |
| 131 | test_image.main(argv) |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 132 | # pylint: disable=protected-access |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 133 | self.assertEqual('my-board', test._board) |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 134 | # pylint: disable=protected-access |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 135 | self.assertEqual('your-root', os.path.basename(test._result_dir)) |