Jae Hoon Kim | 2f8c62a | 2021-06-22 15:10:43 -0700 | [diff] [blame] | 1 | # Copyright 2021 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 | """Unittests for build_minios.py""" |
| 6 | |
| 7 | import os |
| 8 | import tempfile |
| 9 | from unittest import mock |
| 10 | |
| 11 | from chromite.lib import constants |
| 12 | from chromite.lib import cros_test_lib |
| 13 | from chromite.lib import minios |
| 14 | from chromite.scripts import build_minios |
| 15 | |
| 16 | |
| 17 | class BuildMiniosTest(cros_test_lib.RunCommandTempDirTestCase): |
| 18 | """Unit tests for build_minios.""" |
| 19 | |
| 20 | def setUp(self): |
| 21 | self.create_minios_mock_return = '/some/kernel/path' |
| 22 | self.create_minios_mock = self.PatchObject( |
| 23 | minios, 'CreateMiniOsKernelImage', |
| 24 | return_value=self.create_minios_mock_return) |
| 25 | |
| 26 | self.insert_minios_mock = self.PatchObject( |
| 27 | minios, 'InsertMiniOsKernelImage') |
| 28 | |
| 29 | # Patch to assert against the tempdir that's created under the anchored |
| 30 | # tempdir created by cros_test_lib. |
| 31 | self._tempdir = os.path.join(self.tempdir, 'test-dir') |
| 32 | self.PatchObject(tempfile, 'mkdtemp', return_value=self._tempdir) |
| 33 | |
| 34 | def testDefaultArguments(self): |
| 35 | """Test that default arguments of build_minios are formatted correct.""" |
| 36 | test_board = 'test-board' |
Vyshu | 9215390 | 2021-06-16 13:31:53 -0400 | [diff] [blame] | 37 | test_version = '0.0.0.0' |
Jae Hoon Kim | 2f8c62a | 2021-06-22 15:10:43 -0700 | [diff] [blame] | 38 | test_image = '/some/image/path' |
| 39 | build_minios.main([ |
| 40 | # --board is a required argument. |
| 41 | '--board', test_board, |
Vyshu | 9215390 | 2021-06-16 13:31:53 -0400 | [diff] [blame] | 42 | # --version is a required argument. |
| 43 | '--version', test_version, |
Jae Hoon Kim | 2f8c62a | 2021-06-22 15:10:43 -0700 | [diff] [blame] | 44 | # --image is a required argument. |
| 45 | '--image', test_image, |
| 46 | ]) |
| 47 | |
| 48 | self.assertEqual(self.create_minios_mock.mock_calls, [mock.call( |
| 49 | test_board, |
Vyshu | 9215390 | 2021-06-16 13:31:53 -0400 | [diff] [blame] | 50 | test_version, |
Jae Hoon Kim | 2f8c62a | 2021-06-22 15:10:43 -0700 | [diff] [blame] | 51 | self._tempdir, |
| 52 | constants.VBOOT_DEVKEYS_DIR, |
| 53 | constants.RECOVERY_PUBLIC_KEY, |
Jae Hoon Kim | 4e35db8 | 2021-06-16 13:05:44 -0700 | [diff] [blame] | 54 | constants.MINIOS_DATA_PRIVATE_KEY, |
| 55 | constants.MINIOS_KEYBLOCK, |
Jae Hoon Kim | 2f8c62a | 2021-06-22 15:10:43 -0700 | [diff] [blame] | 56 | None, |
Henry Barnor | e0dfd56 | 2022-02-16 10:01:54 -0800 | [diff] [blame] | 57 | True, |
| 58 | False, |
Jae Hoon Kim | 2f8c62a | 2021-06-22 15:10:43 -0700 | [diff] [blame] | 59 | )]) |
| 60 | |
| 61 | self.assertEqual(self.insert_minios_mock.mock_calls, [mock.call( |
| 62 | test_image, self.create_minios_mock_return, |
| 63 | )]) |
| 64 | |
| 65 | def testOverridenArguments(self): |
| 66 | """Test that overridden arguments of build_minios are formatted correct.""" |
| 67 | test_board = 'test-board' |
Vyshu | 9215390 | 2021-06-16 13:31:53 -0400 | [diff] [blame] | 68 | test_version = '1.0.0.0' |
Jae Hoon Kim | 2f8c62a | 2021-06-22 15:10:43 -0700 | [diff] [blame] | 69 | test_image = '/some/image/path' |
| 70 | test_keys_dir = '/some/path/test-keys-dir' |
| 71 | test_public_key = 'test-public-key' |
| 72 | test_private_key = 'test-private-key' |
| 73 | test_keyblock = 'test-keyblock' |
| 74 | test_serial = 'test-serial' |
| 75 | build_minios.main([ |
| 76 | # --board is a required argument. |
| 77 | '--board', test_board, |
Vyshu | 9215390 | 2021-06-16 13:31:53 -0400 | [diff] [blame] | 78 | # --version is a required argument. |
| 79 | '--version', test_version, |
Jae Hoon Kim | 2f8c62a | 2021-06-22 15:10:43 -0700 | [diff] [blame] | 80 | # --image is a required argument. |
| 81 | '--image', test_image, |
| 82 | '--keys-dir', test_keys_dir, |
| 83 | '--public-key', test_public_key, |
| 84 | '--private-key', test_private_key, |
| 85 | '--keyblock', test_keyblock, |
| 86 | '--serial', test_serial, |
Henry Barnor | e0dfd56 | 2022-02-16 10:01:54 -0800 | [diff] [blame] | 87 | '--force-build', |
Jae Hoon Kim | 2f8c62a | 2021-06-22 15:10:43 -0700 | [diff] [blame] | 88 | ]) |
| 89 | |
| 90 | self.assertEqual(self.create_minios_mock.mock_calls, [mock.call( |
| 91 | test_board, |
Vyshu | 9215390 | 2021-06-16 13:31:53 -0400 | [diff] [blame] | 92 | test_version, |
Jae Hoon Kim | 2f8c62a | 2021-06-22 15:10:43 -0700 | [diff] [blame] | 93 | self._tempdir, |
| 94 | test_keys_dir, |
| 95 | test_public_key, |
| 96 | test_private_key, |
| 97 | test_keyblock, |
| 98 | test_serial, |
Henry Barnor | e0dfd56 | 2022-02-16 10:01:54 -0800 | [diff] [blame] | 99 | True, |
| 100 | False, |
| 101 | )]) |
| 102 | |
| 103 | self.assertEqual(self.insert_minios_mock.mock_calls, [mock.call( |
| 104 | test_image, self.create_minios_mock_return, |
| 105 | )]) |
| 106 | |
| 107 | def testModForDev(self): |
| 108 | """Test that default arguments of build_minios are formatted correct.""" |
| 109 | test_board = 'test-board' |
| 110 | test_version = '0.0.0.0' |
| 111 | test_image = '/some/image/path' |
| 112 | build_minios.main([ |
| 113 | # --board is a required argument. |
| 114 | '--board', test_board, |
| 115 | # --version is a required argument. |
| 116 | '--version', test_version, |
| 117 | # --image is a required argument. |
| 118 | '--image', test_image, |
| 119 | '--mod-for-dev', |
| 120 | ]) |
| 121 | |
| 122 | self.assertEqual(self.create_minios_mock.mock_calls, [mock.call( |
| 123 | test_board, |
| 124 | test_version, |
| 125 | self._tempdir, |
| 126 | constants.VBOOT_DEVKEYS_DIR, |
| 127 | constants.RECOVERY_PUBLIC_KEY, |
| 128 | constants.MINIOS_DATA_PRIVATE_KEY, |
| 129 | constants.MINIOS_KEYBLOCK, |
| 130 | None, |
| 131 | False, |
| 132 | True, |
| 133 | )]) |
| 134 | |
| 135 | self.assertEqual(self.insert_minios_mock.mock_calls, [mock.call( |
| 136 | test_image, self.create_minios_mock_return, |
| 137 | )]) |
| 138 | |
| 139 | def testModForDevWithForceBuild(self): |
| 140 | """Test that default arguments of build_minios are formatted correct.""" |
| 141 | test_board = 'test-board' |
| 142 | test_version = '0.0.0.0' |
| 143 | test_image = '/some/image/path' |
| 144 | build_minios.main([ |
| 145 | # --board is a required argument. |
| 146 | '--board', test_board, |
| 147 | # --version is a required argument. |
| 148 | '--version', test_version, |
| 149 | # --image is a required argument. |
| 150 | '--image', test_image, |
| 151 | '--mod-for-dev', |
| 152 | '--force-build', |
| 153 | ]) |
| 154 | |
| 155 | self.assertEqual(self.create_minios_mock.mock_calls, [mock.call( |
| 156 | test_board, |
| 157 | test_version, |
| 158 | self._tempdir, |
| 159 | constants.VBOOT_DEVKEYS_DIR, |
| 160 | constants.RECOVERY_PUBLIC_KEY, |
| 161 | constants.MINIOS_DATA_PRIVATE_KEY, |
| 162 | constants.MINIOS_KEYBLOCK, |
| 163 | None, |
| 164 | True, |
| 165 | True, |
Jae Hoon Kim | 2f8c62a | 2021-06-22 15:10:43 -0700 | [diff] [blame] | 166 | )]) |
| 167 | |
| 168 | self.assertEqual(self.insert_minios_mock.mock_calls, [mock.call( |
| 169 | test_image, self.create_minios_mock_return, |
| 170 | )]) |