Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sdk/python/sandstorm/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import os
import ssl
import urllib.error
import urllib.parse
import urllib.request
from typing import Any, Optional

Expand Down Expand Up @@ -129,7 +130,8 @@ def list_snapshots(self) -> list[dict]:

def delete_snapshot(self, tag: str) -> None:
"""``DELETE /v1/snapshots/:tag`` — drop both registry and disk files."""
self._request("DELETE", f"/v1/snapshots/{tag}")
encoded_tag = urllib.parse.quote(tag, safe="")
self._request("DELETE", f"/v1/snapshots/{encoded_tag}")

# --- sandboxes ------------------------------------------------

Expand Down
30 changes: 29 additions & 1 deletion sdk/python/tests/test_sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from urllib.parse import unquote, urlsplit

import pytest

Expand All @@ -27,6 +28,7 @@ class FakeMaster:
def __init__(self):
self.requests = []
self.sandboxes = {}
self.snapshots = set()
self.reports_until_running = 0
self.last_error = None
self.error_attempts = 0
Expand Down Expand Up @@ -62,11 +64,19 @@ def _handle(self):
).start()

def answer(self, method, path, body, headers):
parts = path.split("/")[2:]
parts = urlsplit(path).path.split("/")[2:]
if method == "POST" and parts == ["sandboxes"]:
return self.create(body)
if method == "GET" and parts == ["templates"]:
return 200, ["python"]
if method == "GET" and parts == ["snapshots"]:
return 200, [{"tag": tag} for tag in sorted(self.snapshots)]
if method == "DELETE" and len(parts) == 2 and parts[0] == "snapshots":
tag = unquote(parts[1])
if tag not in self.snapshots:
return 404, {"error": f"no snapshot {tag}"}
self.snapshots.remove(tag)
return 204, None
sandbox_id = parts[1]
sb = self.sandboxes.get(sandbox_id)
if sb is None:
Expand Down Expand Up @@ -367,6 +377,24 @@ def test_connect_to_nothing_raises(controller):
# ---- the Controller's own calls ----------------------------------------------


@pytest.mark.parametrize(
("tag", "other_tag"),
[
("checkpoint#draft", "checkpoint"),
("checkpoint?draft", "checkpoint"),
("checkpoint%23draft", "checkpoint#draft"),
("周报 #1", "周报"),
("checkpoint-draft", "checkpoint"),
],
)
def test_delete_snapshot_removes_only_the_requested_tag(master, controller, tag, other_tag):
master.snapshots.update([tag, other_tag])

controller.delete_snapshot(tag)

assert controller.list_snapshots() == [{"tag": other_tag}]


def test_branch_sends_the_tag_and_nothing_else(master, controller):
sb = controller.create_sandbox(template="python")
assert controller.branch_sandbox(sb["id"], "checkpoint-1")["tag"] == "checkpoint-1"
Expand Down
Loading