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