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