Trent Apted | 7d2777b | 2023-06-29 13:35:03 +1000 | [diff] [blame] | 1 | # Copyright 2023 The ChromiumOS Authors |
| 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 build_sdk_subtools.""" |
| 6 | |
Trent Apted | 7d2777b | 2023-06-29 13:35:03 +1000 | [diff] [blame] | 7 | from pathlib import Path |
| 8 | from unittest import mock |
| 9 | |
| 10 | import pytest |
| 11 | |
| 12 | from chromite.lib import commandline |
| 13 | from chromite.lib import cros_build_lib |
| 14 | from chromite.scripts import build_sdk_subtools |
Trent Apted | bd3cb41 | 2023-08-18 11:37:02 +1000 | [diff] [blame] | 15 | from chromite.service import sdk_subtools |
Trent Apted | 7d2777b | 2023-06-29 13:35:03 +1000 | [diff] [blame] | 16 | |
| 17 | |
Trent Apted | 7c6b774 | 2023-09-04 09:48:34 +1000 | [diff] [blame] | 18 | # The argument passed to cros_sdk to ensure the correct SDK is being used. |
| 19 | SDK_CHROOT_ARG = Path("/mnt/host/source/out/build/amd64-subtools-host") |
| 20 | |
| 21 | |
Trent Apted | 7d2777b | 2023-06-29 13:35:03 +1000 | [diff] [blame] | 22 | @pytest.fixture(name="outside_chroot") |
| 23 | def outside_chroot_fixture(): |
| 24 | """Mocks IsInsideChroot to be False.""" |
| 25 | with mock.patch.object( |
| 26 | cros_build_lib, "IsInsideChroot", return_value=False |
| 27 | ) as outside_chroot: |
| 28 | yield outside_chroot |
| 29 | |
| 30 | |
| 31 | @pytest.fixture(name="mock_emerge") |
| 32 | def mock_emerge_fixture(): |
| 33 | """Stubs the build_sdk_subtools emerge helper and sets it up to run.""" |
| 34 | with mock.patch.multiple( |
Trent Apted | bd3cb41 | 2023-08-18 11:37:02 +1000 | [diff] [blame] | 35 | "chromite.service.sdk_subtools", |
Trent Apted | 7d2777b | 2023-06-29 13:35:03 +1000 | [diff] [blame] | 36 | _run_system_emerge=mock.DEFAULT, |
Trent Apted | bd3cb41 | 2023-08-18 11:37:02 +1000 | [diff] [blame] | 37 | is_inside_subtools_chroot=mock.DEFAULT, |
Trent Apted | 7d2777b | 2023-06-29 13:35:03 +1000 | [diff] [blame] | 38 | ) as mocks: |
Trent Apted | bd3cb41 | 2023-08-18 11:37:02 +1000 | [diff] [blame] | 39 | mocks["is_inside_subtools_chroot"].return_value = True |
Trent Apted | 7d2777b | 2023-06-29 13:35:03 +1000 | [diff] [blame] | 40 | yield mocks["_run_system_emerge"] |
| 41 | |
| 42 | |
Trent Apted | 16007b8 | 2023-07-06 14:51:57 +1000 | [diff] [blame] | 43 | @pytest.fixture(name="mock_exporter", autouse=True) |
| 44 | def mock_exporter_fixture(): |
| 45 | """Stubs the exporter for InstalledSubtools to avoid side-effects.""" |
Trent Apted | c94115a | 2023-09-20 16:39:18 +1000 | [diff] [blame^] | 46 | with mock.patch( |
| 47 | "chromite.lib.subtool_lib.InstalledSubtools" |
| 48 | ) as installed, mock.patch( |
| 49 | "chromite.lib.subtool_lib.BundledSubtools" |
| 50 | ) as bundled: |
| 51 | yield {"installed": installed, "bundled": bundled} |
Trent Apted | 16007b8 | 2023-07-06 14:51:57 +1000 | [diff] [blame] | 52 | |
| 53 | |
Trent Apted | bd3cb41 | 2023-08-18 11:37:02 +1000 | [diff] [blame] | 54 | @pytest.fixture(autouse=True) |
| 55 | def build_sdk_subtools_consistency_check(): |
| 56 | """Die quickly if the version file is left over in the test SDK. |
| 57 | |
| 58 | This can happen if the build API entrypoint was tested on the local machine. |
| 59 | Tests in this file will fail in confusing ways if this is ever the case. |
| 60 | """ |
| 61 | version_file = sdk_subtools.SUBTOOLS_CHROOT_VERSION_FILE |
| 62 | assert ( |
| 63 | not version_file.exists() |
| 64 | ), f"{version_file} exists in the chroot (stray?)." |
| 65 | |
| 66 | |
Trent Apted | 7d2777b | 2023-06-29 13:35:03 +1000 | [diff] [blame] | 67 | def test_must_run_outside_sdk(caplog) -> None: |
| 68 | """Tests build_sdk_subtools complains if run in the chroot.""" |
| 69 | with pytest.raises(cros_build_lib.DieSystemExit) as error_info: |
| 70 | build_sdk_subtools.main() |
| 71 | assert error_info.value.code == 1 |
| 72 | assert "build_sdk_subtools must be run outside the chroot" in caplog.text |
| 73 | |
| 74 | |
| 75 | def test_cros_sdk(run_mock, outside_chroot) -> None: |
| 76 | """Tests the steps leading up to the cros_sdk invocation.""" |
| 77 | # Fake a failure from cros_sdk to ensure it propagates. |
| 78 | run_mock.SetDefaultCmdResult(returncode=42) |
| 79 | |
| 80 | assert build_sdk_subtools.main() == 42 |
| 81 | assert outside_chroot.called |
| 82 | assert run_mock.call_count == 1 |
| 83 | assert run_mock.call_args_list[0].args[0] == [ |
| 84 | "cros_sdk", |
| 85 | "--chroot", |
Trent Apted | 7c6b774 | 2023-09-04 09:48:34 +1000 | [diff] [blame] | 86 | SDK_CHROOT_ARG, |
Trent Apted | 7d2777b | 2023-06-29 13:35:03 +1000 | [diff] [blame] | 87 | "--create", |
| 88 | "--skip-chroot-upgrade", |
| 89 | ] |
| 90 | |
| 91 | |
| 92 | def test_cros_sdk_clean(run_mock, outside_chroot) -> None: |
| 93 | """Tests steps leading up to the cros_sdk invocation with --clean.""" |
| 94 | run_mock.SetDefaultCmdResult(returncode=42) |
| 95 | |
| 96 | assert build_sdk_subtools.main(["--clean"]) == 42 |
| 97 | assert outside_chroot.called |
| 98 | assert run_mock.call_count == 1 |
| 99 | cros_sdk_cmd = run_mock.call_args_list[0].args[0] |
| 100 | assert cros_sdk_cmd[0] == "cros_sdk" |
| 101 | assert "--delete" in cros_sdk_cmd |
| 102 | |
| 103 | |
| 104 | def test_cros_sdk_output_dir(run_mock, outside_chroot) -> None: |
| 105 | """Tests steps leading up to the cros_sdk invocation with --output-dir.""" |
| 106 | run_mock.SetDefaultCmdResult(returncode=42) |
| 107 | |
| 108 | assert build_sdk_subtools.main(["--output-dir", "/foo"]) == 42 |
| 109 | assert outside_chroot.called |
| 110 | cros_sdk_cmd = run_mock.call_args_list[0].args[0] |
Trent Apted | 7c6b774 | 2023-09-04 09:48:34 +1000 | [diff] [blame] | 111 | chroot_arg_index = cros_sdk_cmd.index(Path("/mnt/host/source/out/foo")) |
Trent Apted | 7d2777b | 2023-06-29 13:35:03 +1000 | [diff] [blame] | 112 | assert cros_sdk_cmd[0] == "cros_sdk" |
| 113 | assert cros_sdk_cmd[chroot_arg_index - 1] == "--chroot" |
| 114 | |
| 115 | |
| 116 | def test_chroot_required_after_cros_sdk(run_mock, outside_chroot) -> None: |
| 117 | """Tests that build_sdk_subtools will ask for chroot when setup.""" |
| 118 | with pytest.raises(commandline.ChrootRequiredError) as error_info: |
| 119 | build_sdk_subtools.main(["--no-setup-chroot"]) |
| 120 | |
| 121 | assert run_mock.call_count == 0 |
| 122 | assert outside_chroot.called |
| 123 | assert error_info.value.cmd == ["build_sdk_subtools", "--no-setup-chroot"] |
| 124 | assert error_info.value.chroot_args == [ |
| 125 | "--chroot", |
Trent Apted | 7c6b774 | 2023-09-04 09:48:34 +1000 | [diff] [blame] | 126 | SDK_CHROOT_ARG, |
Trent Apted | 7d2777b | 2023-06-29 13:35:03 +1000 | [diff] [blame] | 127 | ] |
| 128 | |
| 129 | |
| 130 | def test_chroots_into_output_dir(run_mock, outside_chroot) -> None: |
| 131 | """Tests that --output-dir is consumed properly after setup.""" |
| 132 | with pytest.raises(commandline.ChrootRequiredError) as error_info: |
| 133 | build_sdk_subtools.main(["--no-setup-chroot", "--output-dir", "/foo"]) |
| 134 | |
| 135 | assert run_mock.call_count == 0 |
| 136 | assert outside_chroot.called |
| 137 | assert error_info.value.chroot_args == [ |
| 138 | "--chroot", |
Trent Apted | 7c6b774 | 2023-09-04 09:48:34 +1000 | [diff] [blame] | 139 | Path("/mnt/host/source/out/foo"), |
Trent Apted | 7d2777b | 2023-06-29 13:35:03 +1000 | [diff] [blame] | 140 | ] |
| 141 | |
| 142 | |
| 143 | def test_setup_sdk_invocation(run_mock, outside_chroot) -> None: |
| 144 | """Tests the SDK setup invocation, before it becomes a subtools chroot.""" |
Trent Apted | bd3cb41 | 2023-08-18 11:37:02 +1000 | [diff] [blame] | 145 | # Fake success from cros_sdk, failure from setup_base_sdk(). |
Trent Apted | 7d2777b | 2023-06-29 13:35:03 +1000 | [diff] [blame] | 146 | run_mock.SetDefaultCmdResult(returncode=0) |
| 147 | run_mock.AddCmdResult( |
| 148 | ["sudo", "--", "build_sdk_subtools", "--relaunch-for-setup"], |
| 149 | returncode=42, |
| 150 | ) |
| 151 | |
Trent Apted | 2d9111b | 2023-08-04 16:20:35 +1000 | [diff] [blame] | 152 | assert build_sdk_subtools.main() == 42 |
Trent Apted | 7d2777b | 2023-06-29 13:35:03 +1000 | [diff] [blame] | 153 | assert run_mock.call_count == 2 |
| 154 | assert outside_chroot.called |
| 155 | |
| 156 | sudo_run_cmd = run_mock.call_args_list[1] |
| 157 | |
| 158 | assert sudo_run_cmd.args[0] == [ |
| 159 | "sudo", |
| 160 | "--", |
| 161 | "build_sdk_subtools", |
| 162 | "--relaunch-for-setup", |
| 163 | ] |
| 164 | assert sudo_run_cmd.kwargs["enter_chroot"] is True |
| 165 | assert sudo_run_cmd.kwargs["chroot_args"] == [ |
| 166 | "--chroot", |
Trent Apted | 7c6b774 | 2023-09-04 09:48:34 +1000 | [diff] [blame] | 167 | SDK_CHROOT_ARG, |
Trent Apted | 7d2777b | 2023-06-29 13:35:03 +1000 | [diff] [blame] | 168 | ] |
| 169 | # Stop here: Actually running `--relaunch-for-setup` principally wants to |
| 170 | # mutate the SDK state as root, which is too messy as a unit test. |
| 171 | |
| 172 | |
| 173 | def test_default_package(mock_emerge) -> None: |
| 174 | """Tests a default virtual package is provided to update packages.""" |
| 175 | assert build_sdk_subtools.main() == 0 |
| 176 | assert mock_emerge.call_count == 1 |
| 177 | emerge_cmd = mock_emerge.call_args.args[0] |
| 178 | assert Path("/mnt/host/source/chromite/bin/parallel_emerge") in emerge_cmd |
| 179 | assert emerge_cmd[-1] == "virtual/target-sdk-subtools" |
| 180 | |
| 181 | |
| 182 | def test_provided_package(mock_emerge) -> None: |
| 183 | """Tests the default package can be passed in from command line.""" |
| 184 | assert build_sdk_subtools.main(["vim"]) == 0 |
| 185 | assert mock_emerge.call_args.args[0][-1] == "vim" |
| 186 | |
| 187 | |
| 188 | def test_skip_package_update(mock_emerge) -> None: |
| 189 | """Tests --skip-package-update will not try to emerge anything.""" |
| 190 | assert build_sdk_subtools.main(["--no-update-packages"]) == 0 |
| 191 | assert mock_emerge.call_count == 0 |
Trent Apted | 16007b8 | 2023-07-06 14:51:57 +1000 | [diff] [blame] | 192 | |
| 193 | |
Trent Apted | c94115a | 2023-09-20 16:39:18 +1000 | [diff] [blame^] | 194 | def test_invokes_uploader(mock_emerge, mock_exporter) -> None: |
| 195 | """The exporter is invoked to bundle, but to upload [] by default.""" |
Trent Apted | 16007b8 | 2023-07-06 14:51:57 +1000 | [diff] [blame] | 196 | assert build_sdk_subtools.main([]) == 0 |
| 197 | assert mock_emerge.call_count == 1 |
Trent Apted | c94115a | 2023-09-20 16:39:18 +1000 | [diff] [blame^] | 198 | assert mock_exporter["installed"].called |
| 199 | installed_subtools = mock_exporter["installed"].return_value |
Trent Apted | 16007b8 | 2023-07-06 14:51:57 +1000 | [diff] [blame] | 200 | assert installed_subtools.bundle_all.called |
Trent Apted | c94115a | 2023-09-20 16:39:18 +1000 | [diff] [blame^] | 201 | installed_subtools.prepare_uploads.assert_called_once_with([]) |
Trent Apted | de10922 | 2023-09-05 12:04:58 +1000 | [diff] [blame] | 202 | |
| 203 | |
Trent Apted | c94115a | 2023-09-20 16:39:18 +1000 | [diff] [blame^] | 204 | def test_upload_option(mock_emerge, mock_exporter) -> None: |
| 205 | """Tests that the exporter is invoked with provided uploads.""" |
| 206 | cmdline = ["--upload", "subtool1", "subtool2", "--", "dev-some/package"] |
Trent Apted | de10922 | 2023-09-05 12:04:58 +1000 | [diff] [blame] | 207 | assert build_sdk_subtools.main(cmdline) == 0 |
| 208 | assert mock_emerge.call_args.args[0][-1] == "dev-some/package" |
Trent Apted | c94115a | 2023-09-20 16:39:18 +1000 | [diff] [blame^] | 209 | installed_subtools = mock_exporter["installed"].return_value |
| 210 | installed_subtools.prepare_uploads.assert_called_once_with( |
| 211 | ["subtool1", "subtool2"] |
Trent Apted | de10922 | 2023-09-05 12:04:58 +1000 | [diff] [blame] | 212 | ) |
| 213 | |
| 214 | |
| 215 | def test_production_option(mock_emerge, mock_exporter) -> None: |
Trent Apted | c94115a | 2023-09-20 16:39:18 +1000 | [diff] [blame^] | 216 | """Tests that --production is passed to the uploader.""" |
Trent Apted | de10922 | 2023-09-05 12:04:58 +1000 | [diff] [blame] | 217 | assert build_sdk_subtools.main(["--production"]) == 0 |
| 218 | assert mock_emerge.call_count == 1 |
Trent Apted | c94115a | 2023-09-20 16:39:18 +1000 | [diff] [blame^] | 219 | mock_exporter["bundled"].return_value.upload.assert_called_once_with(True) |