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