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 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 9 | import os |
| 10 | import tempfile |
| 11 | import unittest |
| 12 | |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 13 | from chromite.cbuildbot import constants |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 14 | from chromite.lib import cros_build_lib |
| 15 | from chromite.lib import cros_test_lib |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame^] | 16 | from chromite.lib import image_test_lib |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 17 | from chromite.lib import osutils |
| 18 | from chromite.scripts import test_image |
| 19 | |
| 20 | |
| 21 | class TestImageTest(cros_test_lib.MockTempDirTestCase): |
| 22 | """Common class for tests ImageTest. |
| 23 | |
| 24 | This sets up proper directory with test image. The image file is zero-byte. |
| 25 | """ |
| 26 | |
| 27 | def setUp(self): |
| 28 | # create dummy image file |
| 29 | self.image_file = os.path.join(self.tempdir, |
| 30 | constants.BASE_IMAGE_NAME + '.bin') |
| 31 | osutils.WriteFile(self.image_file, '') |
| 32 | fake_partitions = { |
| 33 | 1: cros_build_lib.PartitionInfo(1, 0, 0, 0, 'fs', 'STATE', 'flag'), |
| 34 | 2: cros_build_lib.PartitionInfo(2, 0, 0, 0, 'fs', 'KERN-A', 'flag'), |
| 35 | 3: cros_build_lib.PartitionInfo(3, 0, 0, 0, 'fs', 'ROOT-A', 'flag'), |
| 36 | } |
| 37 | self.PatchObject(cros_build_lib, 'GetImageDiskPartitionInfo', |
| 38 | autospec=True, return_value=fake_partitions) |
| 39 | self.PatchObject(osutils.MountImageContext, '_Mount', autospec=True) |
| 40 | self.PatchObject(osutils.MountImageContext, '_Unmount', autospec=True) |
| 41 | |
| 42 | |
| 43 | class FindImageTest(TestImageTest): |
| 44 | """Test FindImage() function.""" |
| 45 | |
| 46 | def _testFindOkay(self, image_path): |
| 47 | res = test_image.FindImage(image_path) |
| 48 | self.assertEqual( |
| 49 | res, |
| 50 | os.path.join(self.tempdir, constants.BASE_IMAGE_NAME + '.bin') |
| 51 | ) |
| 52 | |
| 53 | def testFindWithDirectory(self): |
| 54 | self._testFindOkay(self.tempdir) |
| 55 | |
| 56 | def testFindWithFile(self): |
| 57 | self._testFindOkay(self.image_file) |
| 58 | |
| 59 | def testFindWithInvalid(self): |
| 60 | self.assertRaises(ValueError, test_image.FindImage, |
| 61 | os.path.join(self.tempdir, '404')) |
| 62 | |
| 63 | def testFindWithInvalidDirectory(self): |
| 64 | os.unlink(self.image_file) |
| 65 | self.assertRaises(ValueError, test_image.FindImage, |
| 66 | os.path.join(self.tempdir)) |
| 67 | |
| 68 | |
| 69 | class MainTest(TestImageTest): |
| 70 | """Test the main invocation of the script.""" |
| 71 | |
| 72 | def testChdir(self): |
| 73 | """Verify the CWD is in a temp directory.""" |
| 74 | |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame^] | 75 | class CwdTest(image_test_lib.NonForgivingImageTestCase): |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 76 | """A dummy test class to verify current working directory.""" |
| 77 | |
| 78 | _expected_dir = None |
| 79 | |
| 80 | def SetCwd(self, cwd): |
| 81 | self._expected_dir = cwd |
| 82 | |
| 83 | def testExpectedCwd(self): |
| 84 | self.assertEqual(self._expected_dir, os.getcwd()) |
| 85 | |
| 86 | self.assertNotEqual('/tmp', os.getcwd()) |
| 87 | os.chdir('/tmp') |
| 88 | |
| 89 | test = CwdTest('testExpectedCwd') |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame^] | 90 | suite = image_test_lib.ImageTestSuite() |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 91 | suite.addTest(test) |
| 92 | self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True, |
| 93 | return_value=[suite]) |
| 94 | |
| 95 | # Set up the expected directory. |
| 96 | expected_dir = os.path.join(self.tempdir, 'my-subdir') |
| 97 | os.mkdir(expected_dir) |
| 98 | test.SetCwd(expected_dir) |
| 99 | self.PatchObject(tempfile, 'mkdtemp', autospec=True, |
| 100 | return_value=expected_dir) |
| 101 | |
| 102 | argv = [self.tempdir] |
| 103 | self.assertEqual(0, test_image.main(argv)) |
| 104 | self.assertEqual('/tmp', os.getcwd()) |
| 105 | |
| 106 | def _testForgiveness(self, forgiveness, expected_result): |
| 107 | |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame^] | 108 | class ForgivenessTest(image_test_lib.ImageTestCase): |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 109 | """A dummy test that is sometime forgiving, sometime not. |
| 110 | |
| 111 | Its only test (testFail) always fail. |
| 112 | """ |
| 113 | |
| 114 | _forgiving = True |
| 115 | |
| 116 | def SetForgiving(self, value): |
| 117 | self._forgiving = value |
| 118 | |
| 119 | def IsForgiving(self): |
| 120 | return self._forgiving |
| 121 | |
| 122 | def testFail(self): |
| 123 | self.fail() |
| 124 | |
| 125 | test = ForgivenessTest('testFail') |
| 126 | test.SetForgiving(forgiveness) |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame^] | 127 | suite = image_test_lib.ImageTestSuite() |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 128 | suite.addTest(test) |
| 129 | self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True, |
| 130 | return_value=[suite]) |
| 131 | argv = [self.tempdir] |
| 132 | self.assertEqual(expected_result, test_image.main(argv)) |
| 133 | |
| 134 | def testForgiving(self): |
| 135 | self._testForgiveness(True, 0) |
| 136 | |
| 137 | def testNonForgiving(self): |
| 138 | self._testForgiveness(False, 1) |
| 139 | |
| 140 | def testBoardAndDirectory(self): |
| 141 | """Verify that "--board", "--test_results_root" are passed to the tests.""" |
| 142 | |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame^] | 143 | class AttributeTest(image_test_lib.ForgivingImageTestCase): |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 144 | """Dummy test class to hold board and directory.""" |
| 145 | |
| 146 | def testOkay(self): |
| 147 | pass |
| 148 | |
| 149 | test = AttributeTest('testOkay') |
Nam T. Nguyen | c8ef774 | 2015-06-01 13:34:35 -0700 | [diff] [blame^] | 150 | suite = image_test_lib.ImageTestSuite() |
Nam T. Nguyen | f381636 | 2014-06-13 09:26:27 -0700 | [diff] [blame] | 151 | suite.addTest(test) |
| 152 | self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True, |
| 153 | return_value=[suite]) |
| 154 | argv = [ |
| 155 | '--board', |
| 156 | 'my-board', |
| 157 | '--test_results_root', |
| 158 | 'your-root', |
| 159 | self.tempdir |
| 160 | ] |
| 161 | test_image.main(argv) |
| 162 | # pylint: disable=W0212 |
| 163 | self.assertEqual('my-board', test._board) |
| 164 | # pylint: disable=W0212 |
| 165 | self.assertEqual('your-root', os.path.basename(test._result_dir)) |