From 583bc9ab965a11987d9afbd54aa674617f9cf695 Mon Sep 17 00:00:00 2001 From: Lucian Petrut Date: Wed, 6 Nov 2024 10:55:01 +0000 Subject: [PATCH] Update "instances" fixture, removing the k8s snap when cleaning up At the moment, the "instances" fixture removes the test instances without uninstalling the k8s snap. In case of LXD instances, this can leak certain resources on the host, for example loopback devices. We'll update the fixture to remove the k8s snap as part of the instance cleanup procedure. --- tests/integration/tests/conftest.py | 2 ++ tests/integration/tests/test_cleanup.py | 40 ++--------------------- tests/integration/tests/test_util/util.py | 40 +++++++++++++++++++++++ 3 files changed, 44 insertions(+), 38 deletions(-) diff --git a/tests/integration/tests/conftest.py b/tests/integration/tests/conftest.py index e20e42a85..0faca6f03 100644 --- a/tests/integration/tests/conftest.py +++ b/tests/integration/tests/conftest.py @@ -192,6 +192,8 @@ def instances( LOG.debug("Generating inspection reports for test instances") _generate_inspection_report(h, instance.id) + util.remove_k8s_snap(instance) + h.delete_instance(instance.id) diff --git a/tests/integration/tests/test_cleanup.py b/tests/integration/tests/test_cleanup.py index fb19a1ebf..18ce44400 100644 --- a/tests/integration/tests/test_cleanup.py +++ b/tests/integration/tests/test_cleanup.py @@ -5,7 +5,7 @@ from typing import List import pytest -from test_util import config, harness, util +from test_util import harness, util LOG = logging.getLogger(__name__) @@ -16,40 +16,4 @@ def test_node_cleanup(instances: List[harness.Instance]): util.wait_for_dns(instance) util.wait_for_network(instance) - LOG.info("Uninstall k8s...") - instance.exec(["snap", "remove", config.SNAP_NAME, "--purge"]) - - LOG.info("Waiting for shims to go away...") - util.stubbornly(retries=5, delay_s=5).on(instance).until( - lambda p: all( - x not in p.stdout.decode() - for x in ["containerd-shim", "cilium", "coredns", "/pause"] - ) - ).exec(["ps", "-fea"]) - - LOG.info("Waiting for kubelet and containerd mounts to go away...") - util.stubbornly(retries=5, delay_s=5).on(instance).until( - lambda p: all( - x not in p.stdout.decode() - for x in ["/var/lib/kubelet/pods", "/run/containerd/io.containerd"] - ) - ).exec(["mount"]) - - # NOTE(neoaggelos): Temporarily disable this as it fails on strict. - # For details, `snap changes` then `snap change $remove_k8s_snap_change`. - # Example output follows: - # - # 2024-02-23T14:10:42Z ERROR ignoring failure in hook "remove": - # ----- - # ... - # ip netns delete cni-UUID1 - # Cannot remove namespace file "/run/netns/cni-UUID1": Device or resource busy - # ip netns delete cni-UUID2 - # Cannot remove namespace file "/run/netns/cni-UUID2": Device or resource busy - # ip netns delete cni-UUID3 - # Cannot remove namespace file "/run/netns/cni-UUID3": Device or resource busy - - # LOG.info("Waiting for CNI network namespaces to go away...") - # util.stubbornly(retries=5, delay_s=5).on(instance).until( - # lambda p: "cni-" not in p.stdout.decode() - # ).exec(["ip", "netns", "list"]) + util.remove_k8s_snap(instance) diff --git a/tests/integration/tests/test_util/util.py b/tests/integration/tests/test_util/util.py index cb09b8c6e..3e9278f1d 100644 --- a/tests/integration/tests/test_util/util.py +++ b/tests/integration/tests/test_util/util.py @@ -185,6 +185,46 @@ def setup_k8s_snap( instance.exec(["/snap/k8s/current/k8s/hack/init.sh"], stdout=subprocess.DEVNULL) +def remove_k8s_snap(instance: harness.Instance): + LOG.info("Uninstall k8s...") + instance.exec(["snap", "remove", config.SNAP_NAME, "--purge"]) + + LOG.info("Waiting for shims to go away...") + stubbornly(retries=5, delay_s=5).on(instance).until( + lambda p: all( + x not in p.stdout.decode() + for x in ["containerd-shim", "cilium", "coredns", "/pause"] + ) + ).exec(["ps", "-fea"]) + + LOG.info("Waiting for kubelet and containerd mounts to go away...") + stubbornly(retries=5, delay_s=5).on(instance).until( + lambda p: all( + x not in p.stdout.decode() + for x in ["/var/lib/kubelet/pods", "/run/containerd/io.containerd"] + ) + ).exec(["mount"]) + + # NOTE(neoaggelos): Temporarily disable this as it fails on strict. + # For details, `snap changes` then `snap change $remove_k8s_snap_change`. + # Example output follows: + # + # 2024-02-23T14:10:42Z ERROR ignoring failure in hook "remove": + # ----- + # ... + # ip netns delete cni-UUID1 + # Cannot remove namespace file "/run/netns/cni-UUID1": Device or resource busy + # ip netns delete cni-UUID2 + # Cannot remove namespace file "/run/netns/cni-UUID2": Device or resource busy + # ip netns delete cni-UUID3 + # Cannot remove namespace file "/run/netns/cni-UUID3": Device or resource busy + + # LOG.info("Waiting for CNI network namespaces to go away...") + # stubbornly(retries=5, delay_s=5).on(instance).until( + # lambda p: "cni-" not in p.stdout.decode() + # ).exec(["ip", "netns", "list"]) + + def wait_until_k8s_ready( control_node: harness.Instance, instances: List[harness.Instance],