blob: 33d5ca7069630cc1060ffa5e38be63a68b5c5c89 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2014 The ChromiumOS Authors
Nam T. Nguyenf3816362014-06-13 09:26:27 -07002# 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
7import os
8import tempfile
9import unittest
10
Aviv Keshetb7519e12016-10-04 00:50:00 -070011from chromite.lib import constants
Mike Frysinger417a85c2021-04-23 00:58:17 -040012from chromite.lib import cros_build_lib
Nam T. Nguyenf3816362014-06-13 09:26:27 -070013from chromite.lib import cros_test_lib
Mike Nicholsa8e8f242019-09-09 14:10:29 -060014from chromite.lib import image_lib
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070015from chromite.lib import image_test_lib
Nam T. Nguyenf3816362014-06-13 09:26:27 -070016from chromite.lib import osutils
17from chromite.scripts import test_image
18
19
20class TestImageTest(cros_test_lib.MockTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060021 """Common class for tests ImageTest.
Nam T. Nguyenf3816362014-06-13 09:26:27 -070022
Alex Klein1699fab2022-09-08 08:46:06 -060023 This sets up proper directory with test image. The image file is zero-byte.
24 """
Nam T. Nguyenf3816362014-06-13 09:26:27 -070025
Alex Klein1699fab2022-09-08 08:46:06 -060026 def setUp(self):
27 # Create empty image file.
28 self.image_file = os.path.join(
29 self.tempdir, constants.BASE_IMAGE_NAME + ".bin"
30 )
31 osutils.AllocateFile(self.image_file, 1024 * 1024)
32 # In case sfdisk is in /sbin, search that too.
33 cros_build_lib.run(
34 ["sfdisk", self.image_file],
35 extra_env={"PATH": "/sbin:/usr/sbin:%s" % os.environ["PATH"]},
36 input="label: dos",
37 )
38 fake_partitions = (
Mike Frysinger9fb2fea2023-01-09 19:48:25 -050039 image_lib.PartitionInfo(1, 0, 0, "fs", "STATE", "flag"),
40 image_lib.PartitionInfo(2, 0, 0, "fs", "KERN-A", "flag"),
41 image_lib.PartitionInfo(3, 0, 0, "fs", "ROOT-A", "flag"),
Alex Klein1699fab2022-09-08 08:46:06 -060042 )
43 self.PatchObject(
44 image_lib,
45 "GetImageDiskPartitionInfo",
46 autospec=True,
47 return_value=fake_partitions,
48 )
49 self.PatchObject(image_lib.LoopbackPartitions, "_Mount", autospec=True)
50 self.PatchObject(
51 image_lib.LoopbackPartitions, "_Unmount", autospec=True
52 )
Mike Frysinger0dd80402022-12-28 08:57:50 -050053 self.PatchObject(image_lib.LoopbackPartitions, "Attach", autospec=True)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070054
55
56class FindImageTest(TestImageTest):
Alex Klein1699fab2022-09-08 08:46:06 -060057 """Test FindImage() function."""
Nam T. Nguyenf3816362014-06-13 09:26:27 -070058
Alex Klein1699fab2022-09-08 08:46:06 -060059 def _testFindOkay(self, image_path):
60 res = test_image.FindImage(image_path)
61 self.assertEqual(
62 res, os.path.join(self.tempdir, constants.BASE_IMAGE_NAME + ".bin")
63 )
Nam T. Nguyenf3816362014-06-13 09:26:27 -070064
Alex Klein1699fab2022-09-08 08:46:06 -060065 def testFindWithDirectory(self):
66 self._testFindOkay(self.tempdir)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070067
Alex Klein1699fab2022-09-08 08:46:06 -060068 def testFindWithFile(self):
69 self._testFindOkay(self.image_file)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070070
Alex Klein1699fab2022-09-08 08:46:06 -060071 def testFindWithInvalid(self):
72 self.assertRaises(
73 ValueError, test_image.FindImage, os.path.join(self.tempdir, "404")
74 )
Nam T. Nguyenf3816362014-06-13 09:26:27 -070075
Alex Klein1699fab2022-09-08 08:46:06 -060076 def testFindWithInvalidDirectory(self):
77 os.unlink(self.image_file)
78 self.assertRaises(
79 ValueError, test_image.FindImage, os.path.join(self.tempdir)
80 )
Nam T. Nguyenf3816362014-06-13 09:26:27 -070081
82
83class MainTest(TestImageTest):
Alex Klein1699fab2022-09-08 08:46:06 -060084 """Test the main invocation of the script."""
Nam T. Nguyenf3816362014-06-13 09:26:27 -070085
Alex Klein1699fab2022-09-08 08:46:06 -060086 def testChdir(self):
87 """Verify the CWD is in a temp directory."""
Nam T. Nguyenf3816362014-06-13 09:26:27 -070088
Alex Klein1699fab2022-09-08 08:46:06 -060089 class CwdTest(image_test_lib.ImageTestCase):
90 """A stub test class to verify current working directory."""
Nam T. Nguyenf3816362014-06-13 09:26:27 -070091
Alex Klein1699fab2022-09-08 08:46:06 -060092 _expected_dir = None
Nam T. Nguyenf3816362014-06-13 09:26:27 -070093
Alex Klein1699fab2022-09-08 08:46:06 -060094 def SetCwd(self, cwd):
95 self._expected_dir = cwd
Nam T. Nguyenf3816362014-06-13 09:26:27 -070096
Alex Klein1699fab2022-09-08 08:46:06 -060097 def testExpectedCwd(self):
98 self.assertEqual(self._expected_dir, os.getcwd())
Nam T. Nguyenf3816362014-06-13 09:26:27 -070099
Alex Klein1699fab2022-09-08 08:46:06 -0600100 self.assertNotEqual("/tmp", os.getcwd())
101 os.chdir("/tmp")
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700102
Alex Klein1699fab2022-09-08 08:46:06 -0600103 test = CwdTest("testExpectedCwd")
104 suite = image_test_lib.ImageTestSuite()
105 suite.addTest(test)
106 self.PatchObject(
107 unittest.TestLoader,
108 "loadTestsFromName",
109 autospec=True,
110 return_value=suite,
111 )
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700112
Alex Klein1699fab2022-09-08 08:46:06 -0600113 # Set up the expected directory.
114 expected_dir = os.path.join(self.tempdir, "my-subdir")
115 os.mkdir(expected_dir)
116 test.SetCwd(expected_dir)
117 self.PatchObject(
118 tempfile, "mkdtemp", autospec=True, return_value=expected_dir
119 )
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700120
Alex Klein1699fab2022-09-08 08:46:06 -0600121 argv = [str(self.tempdir)]
122 self.assertEqual(0, test_image.main(argv))
123 self.assertEqual("/tmp", os.getcwd())
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700124
Alex Klein1699fab2022-09-08 08:46:06 -0600125 def testBoardAndDirectory(self):
126 """Verify that "--board", "--test_results_root" are passed to the tests."""
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700127
Alex Klein1699fab2022-09-08 08:46:06 -0600128 class AttributeTest(image_test_lib.ImageTestCase):
129 """Stub test class to hold board and directory."""
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700130
Alex Klein1699fab2022-09-08 08:46:06 -0600131 def testOkay(self):
132 pass
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700133
Alex Klein1699fab2022-09-08 08:46:06 -0600134 test = AttributeTest("testOkay")
135 suite = image_test_lib.ImageTestSuite()
136 suite.addTest(test)
137 self.PatchObject(
138 unittest.TestLoader,
139 "loadTestsFromName",
140 autospec=True,
141 return_value=suite,
142 )
143 argv = [
144 "--board",
145 "my-board",
146 "--test_results_root",
147 "your-root",
148 str(self.tempdir),
149 ]
150 test_image.main(argv)
151 # pylint: disable=protected-access
152 self.assertEqual("my-board", test._board)
153 # pylint: disable=protected-access
154 self.assertEqual("your-root", os.path.basename(test._result_dir))