Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 2 | # Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Unit tests for the functions in test_image.""" |
| 7 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 10 | import os |
Mike Frysinger | 6165cdc | 2020-02-21 02:38:07 -0500 | [diff] [blame] | 11 | import sys |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 12 | import tempfile |
| 13 | import unittest |
| 14 | |
Aviv Keshet | b7519e1 | 2016-10-04 00:50:00 -0700 | [diff] [blame] | 15 | from chromite.lib import constants |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 16 | from chromite.lib import cros_test_lib |
Mike Nichols | a8e8f24 | 2019-09-09 14:10:29 -0600 | [diff] [blame] | 17 | from chromite.lib import image_lib |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame] | 18 | from chromite.lib import image_test_lib |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 19 | from chromite.lib import osutils |
| 20 | from chromite.scripts import test_image |
| 21 | |
Greg Edelston | a4c9b3b | 2020-01-07 17:51:13 -0700 | [diff] [blame] | 22 | pytestmark = cros_test_lib.pytestmark_inside_only |
| 23 | |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 24 | |
Mike Frysinger | 6165cdc | 2020-02-21 02:38:07 -0500 | [diff] [blame] | 25 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 26 | |
| 27 | |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 28 | class TestImageTest(cros_test_lib.MockTempDirTestCase): |
| 29 | """Common class for tests ImageTest. |
| 30 | |
| 31 | This sets up proper directory with test image. The image file is zero-byte. |
| 32 | """ |
| 33 | |
| 34 | def setUp(self): |
| 35 | # create dummy image file |
| 36 | self.image_file = os.path.join(self.tempdir, |
| 37 | constants.BASE_IMAGE_NAME + '.bin') |
| 38 | osutils.WriteFile(self.image_file, '') |
LaMont Jones | ea6dda6 | 2018-12-04 16:11:45 -0700 | [diff] [blame] | 39 | fake_partitions = ( |
Mike Frysinger | a2aeb37 | 2019-09-20 16:15:43 -0400 | [diff] [blame] | 40 | image_lib.PartitionInfo(1, 0, 0, 0, 'fs', 'STATE', 'flag'), |
| 41 | image_lib.PartitionInfo(2, 0, 0, 0, 'fs', 'KERN-A', 'flag'), |
| 42 | image_lib.PartitionInfo(3, 0, 0, 0, 'fs', 'ROOT-A', 'flag'), |
LaMont Jones | ea6dda6 | 2018-12-04 16:11:45 -0700 | [diff] [blame] | 43 | ) |
Mike Frysinger | a2aeb37 | 2019-09-20 16:15:43 -0400 | [diff] [blame] | 44 | self.PatchObject(image_lib, 'GetImageDiskPartitionInfo', |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 45 | autospec=True, return_value=fake_partitions) |
Mike Nichols | a8e8f24 | 2019-09-09 14:10:29 -0600 | [diff] [blame] | 46 | self.PatchObject(image_lib.LoopbackPartitions, '_Mount', autospec=True) |
| 47 | self.PatchObject(image_lib.LoopbackPartitions, '_Unmount', autospec=True) |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 48 | |
| 49 | |
| 50 | class FindImageTest(TestImageTest): |
| 51 | """Test FindImage() function.""" |
| 52 | |
| 53 | def _testFindOkay(self, image_path): |
| 54 | res = test_image.FindImage(image_path) |
| 55 | self.assertEqual( |
| 56 | res, |
| 57 | os.path.join(self.tempdir, constants.BASE_IMAGE_NAME + '.bin') |
| 58 | ) |
| 59 | |
| 60 | def testFindWithDirectory(self): |
| 61 | self._testFindOkay(self.tempdir) |
| 62 | |
| 63 | def testFindWithFile(self): |
| 64 | self._testFindOkay(self.image_file) |
| 65 | |
| 66 | def testFindWithInvalid(self): |
| 67 | self.assertRaises(ValueError, test_image.FindImage, |
| 68 | os.path.join(self.tempdir, '404')) |
| 69 | |
| 70 | def testFindWithInvalidDirectory(self): |
| 71 | os.unlink(self.image_file) |
| 72 | self.assertRaises(ValueError, test_image.FindImage, |
| 73 | os.path.join(self.tempdir)) |
| 74 | |
| 75 | |
| 76 | class MainTest(TestImageTest): |
| 77 | """Test the main invocation of the script.""" |
| 78 | |
| 79 | def testChdir(self): |
| 80 | """Verify the CWD is in a temp directory.""" |
| 81 | |
Mike Frysinger | 3e04661 | 2017-10-18 17:19:23 -0400 | [diff] [blame] | 82 | class CwdTest(image_test_lib.ImageTestCase): |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 83 | """A dummy test class to verify current working directory.""" |
| 84 | |
| 85 | _expected_dir = None |
| 86 | |
| 87 | def SetCwd(self, cwd): |
| 88 | self._expected_dir = cwd |
| 89 | |
| 90 | def testExpectedCwd(self): |
| 91 | self.assertEqual(self._expected_dir, os.getcwd()) |
| 92 | |
| 93 | self.assertNotEqual('/tmp', os.getcwd()) |
| 94 | os.chdir('/tmp') |
| 95 | |
| 96 | test = CwdTest('testExpectedCwd') |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame] | 97 | suite = image_test_lib.ImageTestSuite() |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 98 | suite.addTest(test) |
| 99 | self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True, |
Mike Frysinger | 3e04661 | 2017-10-18 17:19:23 -0400 | [diff] [blame] | 100 | return_value=suite) |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 101 | |
| 102 | # Set up the expected directory. |
| 103 | expected_dir = os.path.join(self.tempdir, 'my-subdir') |
| 104 | os.mkdir(expected_dir) |
| 105 | test.SetCwd(expected_dir) |
| 106 | self.PatchObject(tempfile, 'mkdtemp', autospec=True, |
| 107 | return_value=expected_dir) |
| 108 | |
| 109 | argv = [self.tempdir] |
| 110 | self.assertEqual(0, test_image.main(argv)) |
| 111 | self.assertEqual('/tmp', os.getcwd()) |
| 112 | |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 113 | def testBoardAndDirectory(self): |
| 114 | """Verify that "--board", "--test_results_root" are passed to the tests.""" |
| 115 | |
Mike Frysinger | 3e04661 | 2017-10-18 17:19:23 -0400 | [diff] [blame] | 116 | class AttributeTest(image_test_lib.ImageTestCase): |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 117 | """Dummy test class to hold board and directory.""" |
| 118 | |
| 119 | def testOkay(self): |
| 120 | pass |
| 121 | |
| 122 | test = AttributeTest('testOkay') |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame] | 123 | suite = image_test_lib.ImageTestSuite() |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 124 | suite.addTest(test) |
| 125 | self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True, |
Mike Frysinger | 3e04661 | 2017-10-18 17:19:23 -0400 | [diff] [blame] | 126 | return_value=suite) |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 127 | argv = [ |
| 128 | '--board', |
| 129 | 'my-board', |
| 130 | '--test_results_root', |
| 131 | 'your-root', |
| 132 | self.tempdir |
| 133 | ] |
| 134 | test_image.main(argv) |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 135 | # pylint: disable=protected-access |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 136 | self.assertEqual('my-board', test._board) |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 137 | # pylint: disable=protected-access |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 138 | self.assertEqual('your-root', os.path.basename(test._result_dir)) |