Merge ~jugmac00/lpci:fix-lpcraft-for-projects-with-underscores into lpci:main

Proposed by Jürgen Gmach
Status: Merged
Merge reported by: Jürgen Gmach
Merged at revision: 7ae439b0949d5459be32da8d7ab0c236ca75df67
Proposed branch: ~jugmac00/lpci:fix-lpcraft-for-projects-with-underscores
Merge into: lpci:main
Diff against target: 93 lines (+47/-1)
3 files modified
lpcraft/providers/_base.py (+15/-1)
lpcraft/providers/tests/test_base.py (+18/-0)
lpcraft/providers/tests/test_lxd.py (+14/-0)
Reviewer Review Type Date Requested Status
Colin Watson (community) Approve
Review via email: mp+414412@code.launchpad.net

Commit message

Sanitize input for generating instance name

To post a comment you must log in.
Revision history for this message
Colin Watson (cjwatson) :
review: Approve
Revision history for this message
Jürgen Gmach (jugmac00) :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lpcraft/providers/_base.py b/lpcraft/providers/_base.py
2index 559143b..cbfaa64 100644
3--- a/lpcraft/providers/_base.py
4+++ b/lpcraft/providers/_base.py
5@@ -8,6 +8,7 @@ __all__ = [
6 ]
7
8 import os
9+import re
10 from abc import ABC, abstractmethod
11 from contextlib import contextmanager
12 from pathlib import Path
13@@ -16,6 +17,18 @@ from typing import Dict, Generator, List, Optional
14 from craft_providers import bases, lxd
15
16
17+def sanitize_lxd_instance_name(name: str) -> str:
18+ """LXD instance names need to follow a certain pattern
19+
20+ Make sure we follow this pattern:
21+ https://linuxcontainers.org/lxd/docs/master/instances/
22+ """
23+ # There is no need to check for all edge cases, as e.g. we control how
24+ # the string starts and ends anyway.
25+ name = re.sub(r"[^A-Za-z0-9-]", "-", name)
26+ return name[:63]
27+
28+
29 class Provider(ABC):
30 """A build environment provider for lpcraft."""
31
32@@ -60,10 +73,11 @@ class Provider(ABC):
33 :param series: Distribution series name.
34 :param architecture: Targeted architecture name.
35 """
36- return (
37+ name = (
38 f"lpcraft-{project_name}-{project_path.stat().st_ino}"
39 f"-{series}-{architecture}"
40 )
41+ return sanitize_lxd_instance_name(name)
42
43 def get_command_environment(self) -> Dict[str, Optional[str]]:
44 """Construct the required environment."""
45diff --git a/lpcraft/providers/tests/test_base.py b/lpcraft/providers/tests/test_base.py
46new file mode 100644
47index 0000000..e96482d
48--- /dev/null
49+++ b/lpcraft/providers/tests/test_base.py
50@@ -0,0 +1,18 @@
51+# Copyright 2022 Canonical Ltd. This software is licensed under the
52+# GNU General Public License version 3 (see the file LICENSE).
53+
54+import pytest
55+
56+from lpcraft.providers._base import sanitize_lxd_instance_name
57+
58+
59+@pytest.mark.parametrize(
60+ "test_input, expected",
61+ [
62+ ("et_xmlfile", "et-xmlfile"), # no underscores
63+ ("et.xmlfile", "et-xmlfile"), # no dots
64+ ("a" * 100, "a" * 63), # max len is 63
65+ ],
66+)
67+def test_sanitize_lxd_instance_name(test_input, expected):
68+ assert expected == sanitize_lxd_instance_name(test_input)
69diff --git a/lpcraft/providers/tests/test_lxd.py b/lpcraft/providers/tests/test_lxd.py
70index cc8255e..dcc586a 100644
71--- a/lpcraft/providers/tests/test_lxd.py
72+++ b/lpcraft/providers/tests/test_lxd.py
73@@ -234,6 +234,20 @@ class TestLXDProvider(TestCase):
74 ),
75 )
76
77+ def test_get_sanitized_instance_name(self):
78+ # e.g. underscores are not allowed
79+ provider = self.makeLXDProvider()
80+
81+ self.assertEqual(
82+ "lpcraft-my-project-12345-focal-amd64",
83+ provider.get_instance_name(
84+ project_name="my_project",
85+ project_path=self.mock_path,
86+ series="focal",
87+ architecture="amd64",
88+ ),
89+ )
90+
91 @patch("os.environ", {"IGNORE": "sentinel", "PATH": "not-using-host-path"})
92 def test_get_command_environment_minimal(self):
93 provider = self.makeLXDProvider()

Subscribers

People subscribed via source and target branches