blob: 9c718e0f65d3e94753bdd3aa9fe4691b5a2b5889 [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):
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):
Mike Frysinger417a85c2021-04-23 00:58:17 -040027 # Create empty image file.
Nam T. Nguyenf3816362014-06-13 09:26:27 -070028 self.image_file = os.path.join(self.tempdir,
29 constants.BASE_IMAGE_NAME + '.bin')
Mike Frysinger417a85c2021-04-23 00:58:17 -040030 osutils.AllocateFile(self.image_file, 1024 * 1024)
31 # In case sfdisk is in /sbin, search that too.
32 cros_build_lib.run(
33 ['sfdisk', self.image_file],
34 extra_env={'PATH': '/sbin:/usr/sbin:%s' % os.environ['PATH']},
35 input='label: dos')
LaMont Jonesea6dda62018-12-04 16:11:45 -070036 fake_partitions = (
Mike Frysingera2aeb372019-09-20 16:15:43 -040037 image_lib.PartitionInfo(1, 0, 0, 0, 'fs', 'STATE', 'flag'),
38 image_lib.PartitionInfo(2, 0, 0, 0, 'fs', 'KERN-A', 'flag'),
39 image_lib.PartitionInfo(3, 0, 0, 0, 'fs', 'ROOT-A', 'flag'),
LaMont Jonesea6dda62018-12-04 16:11:45 -070040 )
Mike Frysingera2aeb372019-09-20 16:15:43 -040041 self.PatchObject(image_lib, 'GetImageDiskPartitionInfo',
Nam T. Nguyenf3816362014-06-13 09:26:27 -070042 autospec=True, return_value=fake_partitions)
Mike Nicholsa8e8f242019-09-09 14:10:29 -060043 self.PatchObject(image_lib.LoopbackPartitions, '_Mount', autospec=True)
44 self.PatchObject(image_lib.LoopbackPartitions, '_Unmount', autospec=True)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070045
46
47class FindImageTest(TestImageTest):
48 """Test FindImage() function."""
49
50 def _testFindOkay(self, image_path):
51 res = test_image.FindImage(image_path)
52 self.assertEqual(
53 res,
54 os.path.join(self.tempdir, constants.BASE_IMAGE_NAME + '.bin')
55 )
56
57 def testFindWithDirectory(self):
58 self._testFindOkay(self.tempdir)
59
60 def testFindWithFile(self):
61 self._testFindOkay(self.image_file)
62
63 def testFindWithInvalid(self):
64 self.assertRaises(ValueError, test_image.FindImage,
65 os.path.join(self.tempdir, '404'))
66
67 def testFindWithInvalidDirectory(self):
68 os.unlink(self.image_file)
69 self.assertRaises(ValueError, test_image.FindImage,
70 os.path.join(self.tempdir))
71
72
73class MainTest(TestImageTest):
74 """Test the main invocation of the script."""
75
76 def testChdir(self):
77 """Verify the CWD is in a temp directory."""
78
Mike Frysinger3e046612017-10-18 17:19:23 -040079 class CwdTest(image_test_lib.ImageTestCase):
Mike Frysingera5c6e792022-03-15 23:42:12 -040080 """A stub test class to verify current working directory."""
Nam T. Nguyenf3816362014-06-13 09:26:27 -070081
82 _expected_dir = None
83
84 def SetCwd(self, cwd):
85 self._expected_dir = cwd
86
87 def testExpectedCwd(self):
88 self.assertEqual(self._expected_dir, os.getcwd())
89
90 self.assertNotEqual('/tmp', os.getcwd())
91 os.chdir('/tmp')
92
93 test = CwdTest('testExpectedCwd')
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -070094 suite = image_test_lib.ImageTestSuite()
Nam T. Nguyenf3816362014-06-13 09:26:27 -070095 suite.addTest(test)
96 self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
Mike Frysinger3e046612017-10-18 17:19:23 -040097 return_value=suite)
Nam T. Nguyenf3816362014-06-13 09:26:27 -070098
99 # Set up the expected directory.
100 expected_dir = os.path.join(self.tempdir, 'my-subdir')
101 os.mkdir(expected_dir)
102 test.SetCwd(expected_dir)
103 self.PatchObject(tempfile, 'mkdtemp', autospec=True,
104 return_value=expected_dir)
105
Mike Frysinger3bb61cb2022-04-14 16:07:44 -0400106 argv = [str(self.tempdir)]
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700107 self.assertEqual(0, test_image.main(argv))
108 self.assertEqual('/tmp', os.getcwd())
109
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700110 def testBoardAndDirectory(self):
111 """Verify that "--board", "--test_results_root" are passed to the tests."""
112
Mike Frysinger3e046612017-10-18 17:19:23 -0400113 class AttributeTest(image_test_lib.ImageTestCase):
Mike Frysingera5c6e792022-03-15 23:42:12 -0400114 """Stub test class to hold board and directory."""
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700115
116 def testOkay(self):
117 pass
118
119 test = AttributeTest('testOkay')
Nam T. Nguyenc8ef7742015-06-01 13:34:35 -0700120 suite = image_test_lib.ImageTestSuite()
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700121 suite.addTest(test)
122 self.PatchObject(unittest.TestLoader, 'loadTestsFromName', autospec=True,
Mike Frysinger3e046612017-10-18 17:19:23 -0400123 return_value=suite)
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700124 argv = [
125 '--board',
126 'my-board',
127 '--test_results_root',
128 'your-root',
Mike Frysinger3bb61cb2022-04-14 16:07:44 -0400129 str(self.tempdir),
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700130 ]
131 test_image.main(argv)
Mike Frysinger27e21b72018-07-12 14:20:21 -0400132 # pylint: disable=protected-access
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700133 self.assertEqual('my-board', test._board)
Mike Frysinger27e21b72018-07-12 14:20:21 -0400134 # pylint: disable=protected-access
Nam T. Nguyenf3816362014-06-13 09:26:27 -0700135 self.assertEqual('your-root', os.path.basename(test._result_dir))