Skip to content

Commit

Permalink
fix: support abstract socket listen-streams
Browse files Browse the repository at this point in the history
Rudimentary support for validating abstract sockets in
listen-streams. The complete check will take place at `snap pack
--check-skeleton` at the end of the process.

Signed-off-by: Sergio Schvezov <[email protected]>
  • Loading branch information
sergiusens committed Sep 6, 2023
1 parent 89fb02a commit 8c01788
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 3 additions & 1 deletion snapcraft/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ def _validate_list_stream(cls, listen_stream):
f"{listen_stream!r} is not an integer between 1 and 65535 (inclusive)."
)
elif isinstance(listen_stream, str):
if not re.match(r"^[A-Za-z0-9/._#:$-]*$", listen_stream):
if not listen_stream.startswith("@snap.") and not re.match(
r"^[A-Za-z0-9/._#:$-]*$", listen_stream
):
raise ValueError(
f"{listen_stream!r} is not a valid socket path (e.g. /tmp/mysocket.sock)."
)
Expand Down
18 changes: 16 additions & 2 deletions tests/unit/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,9 @@ def test_app_command_chain(self, command_chain, app_yaml_data):
assert project.apps is not None
assert project.apps["app1"].command_chain == command_chain

@pytest.mark.parametrize("listen_stream", [1, 100, 65535, "/tmp/mysocket.sock"])
@pytest.mark.parametrize(
"listen_stream", [1, 100, 65535, "/tmp/mysocket.sock", "@snap.foo"]
)
def test_app_sockets_valid_listen_stream(self, listen_stream, socket_yaml_data):
data = socket_yaml_data(listen_stream=listen_stream)

Expand All @@ -1063,13 +1065,25 @@ def test_app_sockets_valid_listen_stream(self, listen_stream, socket_yaml_data):
assert project.apps["app1"].sockets["socket1"].listen_stream == listen_stream

@pytest.mark.parametrize("listen_stream", [-1, 0, 65536])
def test_app_sockets_invalid_listen_stream(self, listen_stream, socket_yaml_data):
def test_app_sockets_invalid_int_listen_stream(
self, listen_stream, socket_yaml_data
):
data = socket_yaml_data(listen_stream=listen_stream)

error = f".*{listen_stream} is not an integer between 1 and 65535"
with pytest.raises(errors.ProjectValidationError, match=error):
Project.unmarshal(data)

@pytest.mark.parametrize("listen_stream", ["@foo"])
def test_app_sockets_invalid_socket_listen_stream(
self, listen_stream, socket_yaml_data
):
data = socket_yaml_data(listen_stream=listen_stream)

error = f".*{listen_stream!r} is not a valid socket path.*"
with pytest.raises(errors.ProjectValidationError, match=error):
Project.unmarshal(data)

def test_app_sockets_missing_listen_stream(self, socket_yaml_data):
data = socket_yaml_data()

Expand Down

0 comments on commit 8c01788

Please sign in to comment.