Skip to content

Commit

Permalink
integration: fix test_containerd_path_cleanup_on_failed_init.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Nashwan Azhari committed Dec 17, 2024
1 parent 63d73b2 commit b551777
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions tests/integration/tests/test_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -121,38 +121,52 @@ 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.
"""
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"])

0 comments on commit b551777

Please sign in to comment.