|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | + |
| 19 | +""" |
| 20 | +Tests for tuning update_settings_uninstall.sh script. |
| 21 | +
|
| 22 | +The uninstall script removes drop-in files written by update_settings.sh. |
| 23 | +The systemd drop-in path uses a glob (`/etc/systemd/system/*.d/...`) so on a |
| 24 | +node where no matching files exist the glob can either expand to nothing or |
| 25 | +be passed literally to `rm`. Under `set -e` this used to abort the whole |
| 26 | +uninstall. These tests guard the `rm -f` form that tolerates both cases. |
| 27 | +""" |
| 28 | + |
| 29 | +from tests.helpers.assertions import ( |
| 30 | + assert_exit_code, |
| 31 | + assert_file_exists, |
| 32 | +) |
| 33 | +from tests.helpers.docker_test import DockerTestRunner |
| 34 | + |
| 35 | + |
| 36 | +SKYHOOK_RESOURCE_ID = "1_tuning_1.1.4" |
| 37 | +PACKAGE_NAME_FROM_RESOURCE_ID = "tuning" |
| 38 | +DROP_IN_FILENAME = f"999-{PACKAGE_NAME_FROM_RESOURCE_ID}-tuning.conf" |
| 39 | + |
| 40 | + |
| 41 | +def test_uninstall_succeeds_when_no_service_drop_in_files_exist(base_image): |
| 42 | + """Regression: uninstall must not fail when the systemd drop-in glob matches nothing. |
| 43 | +
|
| 44 | + Previously the script ran |
| 45 | + rm /etc/systemd/system/*.d/999-${package_name}-tuning.conf |
| 46 | + On a clean node with no matching files, bash leaves the glob literal and |
| 47 | + `rm` exits non-zero, which trips `set -e` and aborts the whole uninstall. |
| 48 | + The fix is `rm -f`, which silently tolerates missing operands. |
| 49 | + """ |
| 50 | + runner = DockerTestRunner(package="tuning", base_image=base_image) |
| 51 | + try: |
| 52 | + result = runner.run_script( |
| 53 | + script="update_settings_uninstall.sh", |
| 54 | + env_vars={"SKYHOOK_RESOURCE_ID": SKYHOOK_RESOURCE_ID}, |
| 55 | + ) |
| 56 | + assert_exit_code(result, 0) |
| 57 | + # No literal-glob diagnostic should appear in the output. |
| 58 | + assert "No such file or directory" not in result.stdout, result.stdout |
| 59 | + assert "*.d/" not in result.stdout, result.stdout |
| 60 | + finally: |
| 61 | + runner.cleanup() |
| 62 | + |
| 63 | + |
| 64 | +def test_uninstall_removes_existing_service_drop_in_file(base_image): |
| 65 | + """Happy path: uninstall removes the drop-in file when one is present. |
| 66 | +
|
| 67 | + Seeds /etc/systemd/system/containerd.service.d/999-tuning-tuning.conf in |
| 68 | + the container before running the uninstall script, then asserts the file |
| 69 | + is gone and the script exited cleanly. |
| 70 | + """ |
| 71 | + runner = DockerTestRunner(package="tuning", base_image=base_image) |
| 72 | + try: |
| 73 | + result = runner.run_script( |
| 74 | + script="update_settings_uninstall.sh", |
| 75 | + env_vars={"SKYHOOK_RESOURCE_ID": SKYHOOK_RESOURCE_ID}, |
| 76 | + ) |
| 77 | + assert_exit_code(result, 0) |
| 78 | + |
| 79 | + drop_in_dir = "/etc/systemd/system/containerd.service.d" |
| 80 | + drop_in_path = f"{drop_in_dir}/{DROP_IN_FILENAME}" |
| 81 | + seed = runner.container.exec_run( |
| 82 | + ["/bin/bash", "-c", f"mkdir -p {drop_in_dir} && echo '[Service]' > {drop_in_path}"], |
| 83 | + workdir="/", |
| 84 | + ) |
| 85 | + assert seed.exit_code == 0, seed.output.decode("utf-8", errors="replace") |
| 86 | + assert_file_exists(runner, drop_in_path) |
| 87 | + |
| 88 | + uninstall_path = "/skyhook-package/skyhook_dir/update_settings_uninstall.sh" |
| 89 | + exec_result = runner.container.exec_run( |
| 90 | + ["/bin/bash", "-c", f"{uninstall_path} 2>&1"], |
| 91 | + workdir="/skyhook-package", |
| 92 | + environment={ |
| 93 | + "SKYHOOK_DIR": "/skyhook-package", |
| 94 | + "STEP_ROOT": "/skyhook-package/skyhook_dir", |
| 95 | + "SKYHOOK_RESOURCE_ID": SKYHOOK_RESOURCE_ID, |
| 96 | + }, |
| 97 | + ) |
| 98 | + assert exec_result.exit_code == 0, exec_result.output.decode("utf-8", errors="replace") |
| 99 | + assert not runner.file_exists(drop_in_path), ( |
| 100 | + f"Expected {drop_in_path} to be removed by uninstall, but it still exists" |
| 101 | + ) |
| 102 | + finally: |
| 103 | + runner.cleanup() |
0 commit comments