blob: 0674220b69a7b4051437e6b6fabc80da513fac4e [file] [log] [blame]
Nam T. Nguyenf3816362014-06-13 09:26:27 -07001# 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
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 = (
39 image_lib.PartitionInfo(1, 0, 0, 0, "fs", "STATE", "flag"),
40 image_lib.PartitionInfo(2, 0, 0, 0, "fs", "KERN-A", "flag"),
41 image_lib.PartitionInfo(3, 0, 0, 0, "fs", "ROOT-A", "flag"),
42 )
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 )
Nam T. Nguyenf3816362014-06-13 09:26:27 -070053
54
55class FindImageTest(TestImageTest):
Alex Klein1699fab2022-09-08 08:46:06 -060056 """Test FindImage() function."""
Nam T. Nguyenf3816362014-06-13 09:26:27 -070057
Alex Klein1699fab2022-09-08 08:46:06 -060058 def _testFindOkay(self, image_path):
59 res = test_image.FindImage(image_path)
60 self.assertEqual(
61 res, os.path.join(self.tempdir, constants.BASE_IMAGE_NAME + ".bin")
62 )
Nam T. Nguyenf3816362014-06-13 09:26:27 -070063
Alex Klein1699fab2022-09-08 08:46:06 -060064 def testFindWithDirectory(self):
65 self._testFindOkay(self.tempdir)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070066
Alex Klein1699fab2022-09-08 08:46:06 -060067 def testFindWithFile(self):
68 self._testFindOkay(self.image_file)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070069
Alex Klein1699fab2022-09-08 08:46:06 -060070 def testFindWithInvalid(self):
71 self.assertRaises(
72 ValueError, test_image.FindImage, os.path.join(self.tempdir, "404")
73 )
Nam T. Nguyenf3816362014-06-13 09:26:27 -070074
Alex Klein1699fab2022-09-08 08:46:06 -060075 def testFindWithInvalidDirectory(self):
76 os.unlink(self.image_file)
77 self.assertRaises(
78 ValueError, test_image.FindImage, os.path.join(self.tempdir)
79 )
Nam T. Nguyenf3816362014-06-13 09:26:27 -070080
81
82class MainTest(TestImageTest):
Alex Klein1699fab2022-09-08 08:46:06 -060083 """Test the main invocation of the script."""
Nam T. Nguyenf3816362014-06-13 09:26:27 -070084
Alex Klein1699fab2022-09-08 08:46:06 -060085 def testChdir(self):
86 """Verify the CWD is in a temp directory."""
Nam T. Nguyenf3816362014-06-13 09:26:27 -070087
Alex Klein1699fab2022-09-08 08:46:06 -060088 class CwdTest(image_test_lib.ImageTestCase):
89 """A stub test class to verify current working directory."""
Nam T. Nguyenf3816362014-06-13 09:26:27 -070090
Alex Klein1699fab2022-09-08 08:46:06 -060091 _expected_dir = None
Nam T. Nguyenf3816362014-06-13 09:26:27 -070092
Alex Klein1699fab2022-09-08 08:46:06 -060093 def SetCwd(self, cwd):
94 self._expected_dir = cwd
Nam T. Nguyenf3816362014-06-13 09:26:27 -070095
Alex Klein1699fab2022-09-08 08:46:06 -060096 def testExpectedCwd(self):
97 self.assertEqual(self._expected_dir, os.getcwd())
Nam T. Nguyenf3816362014-06-13 09:26:27 -070098
Alex Klein1699fab2022-09-08 08:46:06 -060099 self.assertNotEqual("/tmp", os.getcwd())
100 os.chdir("/tmp")
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700101
Alex Klein1699fab2022-09-08 08:46:06 -0600102 test = CwdTest("testExpectedCwd")
103 suite = image_test_lib.ImageTestSuite()
104 suite.addTest(test)
105 self.PatchObject(
106 unittest.TestLoader,
107 "loadTestsFromName",
108 autospec=True,
109 return_value=suite,
110 )
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700111
Alex Klein1699fab2022-09-08 08:46:06 -0600112 # Set up the expected directory.
113 expected_dir = os.path.join(self.tempdir, "my-subdir")
114 os.mkdir(expected_dir)
115 test.SetCwd(expected_dir)
116 self.PatchObject(
117 tempfile, "mkdtemp", autospec=True, return_value=expected_dir
118 )
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700119
Alex Klein1699fab2022-09-08 08:46:06 -0600120 argv = [str(self.tempdir)]
121 self.assertEqual(0, test_image.main(argv))
122 self.assertEqual("/tmp", os.getcwd())
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700123
Alex Klein1699fab2022-09-08 08:46:06 -0600124 def testBoardAndDirectory(self):
125 """Verify that "--board", "--test_results_root" are passed to the tests."""
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700126
Alex Klein1699fab2022-09-08 08:46:06 -0600127 class AttributeTest(image_test_lib.ImageTestCase):
128 """Stub test class to hold board and directory."""
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700129
Alex Klein1699fab2022-09-08 08:46:06 -0600130 def testOkay(self):
131 pass
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700132
Alex Klein1699fab2022-09-08 08:46:06 -0600133 test = AttributeTest("testOkay")
134 suite = image_test_lib.ImageTestSuite()
135 suite.addTest(test)
136 self.PatchObject(
137 unittest.TestLoader,
138 "loadTestsFromName",
139 autospec=True,
140 return_value=suite,
141 )
142 argv = [
143 "--board",
144 "my-board",
145 "--test_results_root",
146 "your-root",
147 str(self.tempdir),
148 ]
149 test_image.main(argv)
150 # pylint: disable=protected-access
151 self.assertEqual("my-board", test._board)
152 # pylint: disable=protected-access
153 self.assertEqual("your-root", os.path.basename(test._result_dir))