← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~lgp171188/lpcraft:sanitize-project-name-before-cleaning into lpcraft:main

 

Guruprasad has proposed merging ~lgp171188/lpcraft:sanitize-project-name-before-cleaning into lpcraft:main.

Commit message:
Sanitize the project name before cleaning

This fixes the bug where the derived instance name is incorrect
when the project name requires sanitization.


Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~lgp171188/lpcraft/+git/lpcraft/+merge/432567
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~lgp171188/lpcraft:sanitize-project-name-before-cleaning into lpcraft:main.
diff --git a/NEWS.rst b/NEWS.rst
index c4b617b..8403acb 100644
--- a/NEWS.rst
+++ b/NEWS.rst
@@ -2,6 +2,11 @@
 Version history
 ===============
 
+0.0.36 (unreleased)
+===================
+
+- Sanitize the project name before cleaning.
+
 0.0.35 (2022-10-27)
 ===================
 
diff --git a/lpcraft/providers/_lxd.py b/lpcraft/providers/_lxd.py
index f0edf3c..c69a72c 100644
--- a/lpcraft/providers/_lxd.py
+++ b/lpcraft/providers/_lxd.py
@@ -22,7 +22,7 @@ from lpcraft.env import (
     get_managed_environment_project_path,
 )
 from lpcraft.errors import CommandError
-from lpcraft.providers._base import Provider
+from lpcraft.providers._base import Provider, sanitize_lxd_instance_name
 from lpcraft.providers._buildd import (
     SERIES_TO_BUILDD_IMAGE_ALIAS,
     LPCraftBuilddBaseConfiguration,
@@ -143,6 +143,7 @@ class LXDProvider(Provider):
             except lxd.LXDError as error:
                 raise CommandError(str(error)) from error
 
+        project_name = sanitize_lxd_instance_name(project_name)
         for instance in instances:
             if re.match(
                 rf"^lpcraft-{re.escape(project_name)}-{re.escape(inode)}"
diff --git a/lpcraft/providers/tests/test_lxd.py b/lpcraft/providers/tests/test_lxd.py
index 7395b96..f12767c 100644
--- a/lpcraft/providers/tests/test_lxd.py
+++ b/lpcraft/providers/tests/test_lxd.py
@@ -60,6 +60,48 @@ class TestLXDProvider(TestCase):
             mock_lxc.mock_calls,
         )
 
+    def test_clean_project_environments_project_name_requires_sanitizing(self):
+        mock_lxc = Mock(spec=LXC)
+        mock_lxc.list_names.return_value = [
+            "do-not-delete-me",
+            "lpcraft-testproject-12345-focal-amd64",
+            "lpcraft-my-project-12345--",
+            "lpcraft-my-project-12345-focal-amd64",
+            "lpcraft-my-project-12345-bionic-arm64",
+            "lpcraft-my-project-123456--",
+            "lpcraft_12345_focal_amd64",
+        ]
+        provider = makeLXDProvider(lxc=mock_lxc)
+
+        self.assertEqual(
+            [
+                "lpcraft-my-project-12345-focal-amd64",
+                "lpcraft-my-project-12345-bionic-arm64",
+            ],
+            provider.clean_project_environments(
+                project_name="my.project", project_path=self.mock_path
+            ),
+        )
+
+        self.assertEqual(
+            [
+                call.list_names(project="test-project", remote="test-remote"),
+                call.delete(
+                    instance_name="lpcraft-my-project-12345-focal-amd64",
+                    force=True,
+                    project="test-project",
+                    remote="test-remote",
+                ),
+                call.delete(
+                    instance_name="lpcraft-my-project-12345-bionic-arm64",
+                    force=True,
+                    project="test-project",
+                    remote="test-remote",
+                ),
+            ],
+            mock_lxc.mock_calls,
+        )
+
     def test_clean_project_environments(self):
         mock_lxc = Mock(spec=LXC)
         mock_lxc.list_names.return_value = [
diff --git a/setup.cfg b/setup.cfg
index ab39404..a28787b 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,6 +1,6 @@
 [metadata]
 name = lpcraft
-version = 0.0.35
+version = 0.0.36.dev0
 description = Runner for Launchpad CI jobs
 long_description = file: README.rst
 long_description_content_type = text/x-rst