From 8eacbbeac3c68045ced45e2ac09fd772e3e23591 Mon Sep 17 00:00:00 2001 From: Orfeas Kourkakis Date: Wed, 20 Nov 2024 20:12:44 +0200 Subject: [PATCH] fix(ci): Clean lxc instances on bundle integration tests --- requirements-integration-v2.in | 1 + requirements-integration-v2.txt | 2 ++ tests/README.md | 1 + tests/integration/helpers/lxc.py | 14 ++++++++++++++ tests/integration/test_kfp_functional_v1.py | 4 +++- tests/integration/test_kfp_functional_v2.py | 4 +++- 6 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tests/integration/helpers/lxc.py diff --git a/requirements-integration-v2.in b/requirements-integration-v2.in index dc095562..1aef6db5 100644 --- a/requirements-integration-v2.in +++ b/requirements-integration-v2.in @@ -12,3 +12,4 @@ pytest-operator pyyaml sh tenacity +jq diff --git a/requirements-integration-v2.txt b/requirements-integration-v2.txt index 1c49ff25..b49dd671 100644 --- a/requirements-integration-v2.txt +++ b/requirements-integration-v2.txt @@ -109,6 +109,8 @@ jedi==0.19.1 # via ipython jinja2==3.1.4 # via pytest-operator +jq==1.8.0 + # via -r requirements-integration-v2.in jsonschema==4.17.3 # via -r requirements-integration-v2.in juju==3.5.2.0 diff --git a/tests/README.md b/tests/README.md index 908f2b5c..474baee6 100644 --- a/tests/README.md +++ b/tests/README.md @@ -20,6 +20,7 @@ This directory has the following structure: │   ├── charmcraft.py │   ├── k8s_resources.py │   └── localize_bundle.py + │   └── lxc.py ├── kfp_globals.py ├── pipelines/ │   └── ... # Sample pipelines diff --git a/tests/integration/helpers/lxc.py b/tests/integration/helpers/lxc.py new file mode 100644 index 00000000..aab0107d --- /dev/null +++ b/tests/integration/helpers/lxc.py @@ -0,0 +1,14 @@ +import sh +import jq + +def clean_charmcraft_lxc_instances() -> None: + """ + Delete lxc instances in project "charmcraft" that are prefixed with "charmcraft-". + + Based on https://discourse.charmhub.io/t/how-to-quickly-clean-unused-lxd-instances-from-charmcraft-pack/15975 + """ + lxc_instances = sh.lxc.list(project="charmcraft", format="json") + lxc_instances_charmcraft = jq.compile('.[] | select(.name | startswith("charmcraft-")) | .name').input_text(lxc_instances).all() + for instance in lxc_instances_charmcraft: + print(f"Deleting lxc instance '{instance}'") + sh.lxc.delete(instance, project="charmcraft") diff --git a/tests/integration/test_kfp_functional_v1.py b/tests/integration/test_kfp_functional_v1.py index 9562e1f0..ce09b7d8 100644 --- a/tests/integration/test_kfp_functional_v1.py +++ b/tests/integration/test_kfp_functional_v1.py @@ -10,6 +10,7 @@ from helpers.k8s_resources import apply_manifests, fetch_response from helpers.localize_bundle import get_resources_from_charm_file from helpers.charmcraft import charmcraft_clean +from helpers.lxc import clean_charmcraft_lxc_instances from kfp_globals import ( CHARM_PATH_TEMPLATE, KFP_CHARMS, @@ -97,7 +98,8 @@ async def test_build_and_deploy(ops_test: OpsTest, request, lightkube_client): context.update([(f"{charm.replace('-', '_')}", charm_file)]) if charmcraft_clean_flag == True: - charmcraft_clean(charms_to_build) + # charmcraft_clean(charms_to_build) + clean_charmcraft_lxc_instances() # Render kfp-operators bundle file with locally built charms and their resources rendered_bundle = render_bundle( diff --git a/tests/integration/test_kfp_functional_v2.py b/tests/integration/test_kfp_functional_v2.py index 8d891425..2e75f0cc 100644 --- a/tests/integration/test_kfp_functional_v2.py +++ b/tests/integration/test_kfp_functional_v2.py @@ -10,6 +10,7 @@ from helpers.k8s_resources import apply_manifests, fetch_response from helpers.localize_bundle import get_resources_from_charm_file from helpers.charmcraft import charmcraft_clean +from helpers.lxc import clean_charmcraft_lxc_instances from kfp_globals import ( CHARM_PATH_TEMPLATE, KFP_CHARMS, @@ -99,7 +100,8 @@ async def test_build_and_deploy(ops_test: OpsTest, request, lightkube_client): context.update([(f"{charm.replace('-', '_')}", charm_file)]) if charmcraft_clean_flag == True: - charmcraft_clean(charms_to_build) + # charmcraft_clean(charms_to_build) + clean_charmcraft_lxc_instances() # Render kfp-operators bundle file with locally built charms and their resources rendered_bundle = render_bundle(