From b551777393644529603669742df7a65054af8c8a Mon Sep 17 00:00:00 2001 From: Nashwan Azhari Date: Tue, 17 Dec 2024 17:39:54 +0200 Subject: [PATCH] integration: fix test_containerd_path_cleanup_on_failed_init. Previously, the test was inducing the `bootstrap` failure by attempting to pre-allocate the port of the kube-controller-manager. This methodology wasn't applicable on any non-LocalHarness testing setups, so the test was updated to induce the failure by pre-creating the containerd socket directory (`/run/containerd`), which should lead to the same cleanup logic being triggered. Signed-off-by: Nashwan Azhari --- tests/integration/tests/test_cleanup.py | 62 +++++++++++++++---------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/tests/integration/tests/test_cleanup.py b/tests/integration/tests/test_cleanup.py index 9633fb518..ce7754d3e 100644 --- a/tests/integration/tests/test_cleanup.py +++ b/tests/integration/tests/test_cleanup.py @@ -11,11 +11,11 @@ LOG = logging.getLogger(__name__) -KUBE_CONTROLLER_MANAGER_SNAP_PORT = 10257 +CONTAINERD_SOCKET_DIRECTORY_CLASSIC = "/run/containerd" CONTAINERD_PATHS = [ "/etc/containerd", - "/run/containerd", + CONTAINERD_SOCKET_DIRECTORY_CLASSIC, "/var/lib/containerd", ] CNI_PATH = "/opt/cni/bin" @@ -121,8 +121,11 @@ def test_containerd_path_cleanup_on_failed_init( containerd-related paths it may have created as part of the failed `bootstrap`. - It induces a bootstrap failure by pre-binding a required k8s service - port (10257 for the kube-controller-manager) before running `k8s bootstrap`. + It introduces a bootsrap failure by pre-creating the containerd socket + path before running `k8s-bootstrap`. + + The bootstrap/join-cluster aborting behavior was added in this PR: + https://github.com/canonical/k8s-snap/pull/772 NOTE: a failed `join-cluster` will trigger the exact same cleanup hook, so the test implicitly applies to it as well. @@ -130,29 +133,40 @@ def test_containerd_path_cleanup_on_failed_init( instance = instances[0] expected_code = 1 expected_message = ( - "Encountered error(s) while verifying port availability for Kubernetes " - "services: Port 10257 (needed by: kube-controller-manager) is already in use." + "The path '%s' required for the containerd socket already exists. " + "This may mean that another service is already using that path, and it " + "conflicts with the k8s snap. Please make sure that there is no other " + "service installed that uses the same path, and remove the existing " + "directory. (dev-only): You can change the default k8s containerd " + "base path with the containerd-base-dir option in the " + "bootstrap / join-cluster config file." + ) % CONTAINERD_SOCKET_DIRECTORY_CLASSIC + + util.setup_k8s_snap(instance, tmp_path, config.SNAP, connect_interfaces=False) + + # Pre-create the containerd socket directory in the test instance: + instance.exec(["mkdir", "-p", CONTAINERD_SOCKET_DIRECTORY_CLASSIC], check=True) + + proc = instance.exec( + ["k8s", "bootstrap"], capture_output=True, text=True, check=False ) - with util.open_port(KUBE_CONTROLLER_MANAGER_SNAP_PORT) as _: - util.setup_k8s_snap(instance, tmp_path, config.SNAP, connect_interfaces=False) - - proc = instance.exec( - ["k8s", "bootstrap"], capture_output=True, text=True, check=False + if proc.returncode != expected_code: + raise AssertionError( + f"Expected `k8s bootstrap` to exit with code {expected_code}, " + f"but it exited with {proc.returncode}.\n" + f"Stdout was: \n{proc.stdout}.\nStderr was: \n{proc.stderr}" ) - if proc.returncode != expected_code: - raise AssertionError( - f"Expected `k8s bootstrap` to exit with code {expected_code}, " - f"but it exited with {proc.returncode}.\n" - f"Stdout was: \n{proc.stdout}.\nStderr was: \n{proc.stderr}" - ) + if expected_message not in proc.stderr: + raise AssertionError( + f"Expected to find port-related warning '{expected_message}' in " + "stderr of the `k8s bootstrap` command.\n" + f"Stdout was: \n{proc.stdout}.\nStderr was: \n{proc.stderr}" + ) - if expected_message not in proc.stderr: - raise AssertionError( - f"Expected to find port-related warning '{expected_message}' in " - "stderr of the `k8s bootstrap` command.\n" - f"Stdout was: \n{proc.stdout}.\nStderr was: \n{proc.stderr}" - ) + _assert_paths_not_exist(instance, CONTAINERD_PATHS) - _assert_paths_not_exist(instance, CONTAINERD_PATHS) + # Remove the directory and ensure the bootstrap succeeds: + instance.exec(["rmdir", CONTAINERD_SOCKET_DIRECTORY_CLASSIC], check=True) + instance.exec(["k8s", "bootstrap"])