blob: 841e2707342e922b58b3a9d1c8040749733605ff [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
Yu-Ju Hongb4a692e2014-07-17 13:54:56 -07009import sys
Nam T. Nguyenf3816362014-06-13 09:26:27 -070010import tempfile
11import unittest
12
Yu-Ju Hongb4a692e2014-07-17 13:54:56 -070013sys.path.insert(0, os.path.abspath('%s/../..' % os.path.dirname(__file__)))
Nam T. Nguyenf3816362014-06-13 09:26:27 -070014from chromite.cbuildbot import constants
15from chromite.cros.tests import image_test
16from chromite.lib import cros_build_lib
17from chromite.lib import cros_test_lib
18from chromite.lib import osutils
19from chromite.scripts import test_image
20
21
22class 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, '')
33 fake_partitions = {
34 1: cros_build_lib.PartitionInfo(1, 0, 0, 0, 'fs', 'STATE', 'flag'),
35 2: cros_build_lib.PartitionInfo(2, 0, 0, 0, 'fs', 'KERN-A', 'flag'),
36 3: cros_build_lib.PartitionInfo(3, 0, 0, 0, 'fs', 'ROOT-A', 'flag'),
37 }
38 self.PatchObject(cros_build_lib, 'GetImageDiskPartitionInfo',
39 autospec=True, return_value=fake_partitions)
40 self.PatchObject(osutils.MountImageContext, '_Mount', autospec=True)
41 self.PatchObject(osutils.MountImageContext, '_Unmount', autospec=True)
42
43
44class 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
70class 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
76 class CwdTest(image_test.NonForgivingImageTestCase):
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')
91 suite = image_test.ImageTestSuite()
92 suite.addTest(test)
93 self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
94 return_value=[suite])
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
107 def _testForgiveness(self, forgiveness, expected_result):
108
109 class ForgivenessTest(image_test.ImageTestCase):
110 """A dummy test that is sometime forgiving, sometime not.
111
112 Its only test (testFail) always fail.
113 """
114
115 _forgiving = True
116
117 def SetForgiving(self, value):
118 self._forgiving = value
119
120 def IsForgiving(self):
121 return self._forgiving
122
123 def testFail(self):
124 self.fail()
125
126 test = ForgivenessTest('testFail')
127 test.SetForgiving(forgiveness)
128 suite = image_test.ImageTestSuite()
129 suite.addTest(test)
130 self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
131 return_value=[suite])
132 argv = [self.tempdir]
133 self.assertEqual(expected_result, test_image.main(argv))
134
135 def testForgiving(self):
136 self._testForgiveness(True, 0)
137
138 def testNonForgiving(self):
139 self._testForgiveness(False, 1)
140
141 def testBoardAndDirectory(self):
142 """Verify that "--board", "--test_results_root" are passed to the tests."""
143
144 class AttributeTest(image_test.ForgivingImageTestCase):
145 """Dummy test class to hold board and directory."""
146
147 def testOkay(self):
148 pass
149
150 test = AttributeTest('testOkay')
151 suite = image_test.ImageTestSuite()
152 suite.addTest(test)
153 self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
154 return_value=[suite])
155 argv = [
156 '--board',
157 'my-board',
158 '--test_results_root',
159 'your-root',
160 self.tempdir
161 ]
162 test_image.main(argv)
163 # pylint: disable=W0212
164 self.assertEqual('my-board', test._board)
165 # pylint: disable=W0212
166 self.assertEqual('your-root', os.path.basename(test._result_dir))
167
168
169if __name__ == '__main__':
170 cros_test_lib.main()