Skip to content

Commit 789b694

Browse files
committed
feat: support for any port init test
1 parent 85cbf2f commit 789b694

File tree

6 files changed

+74
-20
lines changed

6 files changed

+74
-20
lines changed

appwrite_lab/_orchestrator.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
import shutil
34
import subprocess
45
import json
@@ -11,8 +12,8 @@
1112
from .models import LabService, Automation, SyncType
1213
from dotenv import dotenv_values
1314
from appwrite_lab.utils import console
14-
from .utils import is_cli, load_config
15-
from .config import APPWRITE_CLI_IMAGE, APPWRITE_PLAYWRIGHT_IMAGE
15+
from .utils import is_cli
16+
from .config import APPWRITE_PLAYWRIGHT_IMAGE
1617
from dataclasses import asdict
1718

1819

@@ -204,10 +205,15 @@ def deploy_appwrite_lab(
204205

205206
# Get appwrite pods
206207
project_pods = self.get_running_pods_by_project(name)
207-
if "appwrite" in project_pods:
208-
appwrite_pod = project_pods["appwrite"]
209-
port = appwrite_pod["Ports"].split("/")[0]
208+
if traefik_pod := project_pods.get("appwrite-traefik", None):
209+
ports = traefik_pod["Ports"].split(",")
210+
port = extract_port_from_pod_info(traefik_pod)
211+
assert len(ports) > 1, OrchestratorError(
212+
"Failed to extract port from pod info."
213+
)
214+
port = extract_port_from_pod_info(traefik_pod)
210215
url = f"http://localhost:{port}"
216+
print("url", url)
211217
else:
212218
url = ""
213219
lab = LabService(
@@ -223,9 +229,8 @@ def deploy_appwrite_lab(
223229
lab, Automation.CREATE_USER_AND_API_KEY
224230
)
225231
if type(api_key_res) is Response and api_key_res.error:
226-
api_key_res.message = (
227-
f"Lab '{name}' deployed, but failed to create API key."
228-
)
232+
api_key_res.message = f"Lab '{name}' deployed, but failed to create API key. Spinning down lab."
233+
self.teardown_service(name)
229234
return api_key_res
230235
lab.api_key = api_key_res.data
231236

@@ -292,7 +297,7 @@ def deploy_playwright_automation(
292297
"run",
293298
"--network",
294299
"host",
295-
# "--rm",
300+
"--rm",
296301
"-u",
297302
f"{os.getuid()}:{os.getgid()}",
298303
"-v",
@@ -458,3 +463,18 @@ def get_env_vars(name: str):
458463
Get the default environment variables.
459464
"""
460465
return dotenv_values(name)
466+
467+
468+
def extract_port_from_pod_info(pod_info: dict) -> int:
469+
"""Extract port from pod information returned by get_running_pods_by_project.
470+
471+
Args:
472+
pod_info: The pod information to extract the port from.
473+
"""
474+
if "Ports" in pod_info:
475+
# Handle format like "0.0.0.0:8005->80/tcp"
476+
ports_str = pod_info["Ports"]
477+
match = re.search(r":(\d+)->80/tcp", ports_str)
478+
if match:
479+
return int(match.group(1))
480+
raise OrchestratorError(f"Failed to extract port from pod info: {pod_info}")

appwrite_lab/labs.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from appwrite_lab.utils import load_config
22
from ._state import State
33
from ._orchestrator import ServiceOrchestrator, Response
4-
from .models import Automation
4+
from .models import Automation, LabService
55
from appwrite_lab.automations.models import AppwriteProjectCreation, AppwriteSyncProject
66

77
from pathlib import Path
@@ -25,12 +25,21 @@ def new(
2525
Deploy a new Appwrite lab.
2626
2727
Args:
28-
name (str): The name of the lab.
29-
version (str): The version of the lab.
30-
port (int): The port of the lab.
28+
name: The name of the lab.
29+
version: The version of the lab.
30+
port: The port of the lab.
3131
"""
3232
return self.orchestrator.deploy_appwrite_lab(name, version, port, meta)
3333

34+
def get_lab(self, name: str) -> LabService | None:
35+
"""
36+
Get a lab by name.
37+
38+
Args:
39+
name: The name of the lab.
40+
"""
41+
return self.orchestrator.get_lab(name)
42+
3443
def sync_with_appwrite_config(
3544
self, name: str, appwrite_json: str, sync_type: str = "all"
3645
):

appwrite_lab/models.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ class Automation(StrEnum):
88
CREATE_USER_AND_API_KEY = "create_user_and_api_key"
99
CREATE_API_KEY = "create_api_key"
1010
SYNC_PROJECT = "sync_project"
11-
CREATE_USER = "create_user"
11+
# CREATE_USER = "create_user"
1212
CREATE_PROJECT = "create_project"
13-
CREATE_DATABASE = "create_database"
14-
CREATE_COLLECTION = "create_collection"
15-
CREATE_DOCUMENT = "create_document"
16-
CREATE_FUNCTION = "create_function"
17-
# CREATE_TRIGGER = "create_trigger"
18-
CREATE_ROLE = "create_role"
13+
# CREATE_DATABASE = "create_database"
14+
# CREATE_COLLECTION = "create_collection"
15+
# CREATE_DOCUMENT = "create_document"
16+
# CREATE_FUNCTION = "create_function"
17+
# CREATE_ROLE = "create_role"
1918

2019

2120
class SyncType(StrEnum):

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ license = "MIT"
1313

1414
[tool.setuptools]
1515
packages = { find = { where = ["."] } }
16+
include-package-data = true
17+
18+
[tool.setuptools.package-data]
19+
appwrite_lab = ["templates/**/*"]
1620

1721
[project.scripts]
1822
appwrite-lab = "appwrite_lab.cli.entry:app"

tests/conftest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
from appwrite_lab.labs import Labs
3+
4+
5+
@pytest.fixture(scope="session")
6+
def lab_svc():
7+
return Labs()
8+
9+
10+
@pytest.fixture(scope="session")
11+
def lab(lab_svc: Labs):
12+
res = lab_svc.new(name="pytest-lab", version="1.7.4", port=8005)
13+
if not res.error:
14+
yield lab_svc.get_lab("pytest-lab")
15+
lab_svc.stop("pytest-lab")
16+
else:
17+
raise ValueError(res.message)

tests/test_labs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from appwrite_lab.models import LabService
2+
3+
4+
def test_labs_new(lab: LabService):
5+
assert lab.name == "pytest-lab"

0 commit comments

Comments
 (0)