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