Skip to content

Commit

Permalink
gltestserver: Make the top-level directory configurable
Browse files Browse the repository at this point in the history
The TLD is the root for the tree of resources we are going to spawn,
so making this configurable allows us to run arbitrarily many
instances on a single developer machine. This is useful if we'd like
to run many tests in parallel.
  • Loading branch information
cdecker committed Nov 22, 2024
1 parent bfa7a9a commit 469e7c2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 30 deletions.
54 changes: 34 additions & 20 deletions libs/gl-testserver/gltestserver/__main__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import json
from dataclasses import dataclass

import time

from rich.console import Console
from rich.pretty import pprint
from rich import inspect
from pathlib import Path
from gltesting import fixtures
import gltesting
from inspect import isgeneratorfunction
import click
import logging
from rich.logging import RichHandler
from pathlib import Path
from pyln.testing.utils import BitcoinD
from rich.console import Console
from rich.logging import RichHandler
from rich.pretty import pprint
from typing import Any, List
import click
import gltesting
import json
import logging
import tempfile
import time


console = Console()
Expand Down Expand Up @@ -56,7 +54,7 @@ def metadata(self):
}


def build():
def build(base_dir: Path):
# List of teardown functions to call in reverse order.
finalizers = []

Expand All @@ -72,14 +70,14 @@ def callfixture(f, *args, **kwargs):
else:
return F(*args, **kwargs)

directory = Path("/tmp/gl-testserver")
directory = base_dir / "gl-testserver"

cert_directory = callfixture(fixtures.cert_directory, directory)
root_id = callfixture(fixtures.root_id, cert_directory)
users_id = callfixture(fixtures.users_id)
_root_id = callfixture(fixtures.root_id, cert_directory)
_users_id = callfixture(fixtures.users_id)
nobody_id = callfixture(fixtures.nobody_id, cert_directory)
scheduler_id = callfixture(fixtures.scheduler_id, cert_directory)
paths = callfixture(fixtures.paths)
_paths = callfixture(fixtures.paths)
bitcoind = callfixture(
fixtures.bitcoind,
directory=directory,
Expand Down Expand Up @@ -113,18 +111,34 @@ def cli():


@cli.command()
def run():
gl = build()
@click.option(
"--directory",
type=click.Path(),
help="""
Set the top-level directory for the testserver. This can be used to run
multiple instances isolated from each other, by giving each isntance a
different top-level directory. Defaults to '/tmp/'
""",
)
def run(directory):
"""Start a gl-testserver instance to test against."""
if not directory:
directory = Path(tempfile.gettempdir())
else:
directory = Path(directory)

gl = build(base_dir=directory)
try:
meta = gl.metadata()
metafile = gl.directory / "metadata.json"
metafile.parent.mkdir(parents=True, exist_ok=True)
logger.debug(f"Writing testserver metadata to {metafile}")
with metafile.open(mode="w") as f:
json.dump(meta, f)

pprint(meta)
logger.info(
f"Server is up and running with the above config values. To stop press Ctrl-C."
"Server is up and running with the above config values. To stop press Ctrl-C."
)
time.sleep(1800)
except Exception as e:
Expand Down
26 changes: 16 additions & 10 deletions libs/gl-testserver/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
# Ok, one exception, `TailableProc` is used to run and tail the
# `gl-testserver`.

import shutil
import tempfile
import os
import pytest
from pathlib import Path
from pyln.testing.utils import TailableProc
import json
import logging
import os
import pytest
import shutil
import signal
from pathlib import Path
import tempfile
import time


@pytest.fixture
Expand Down Expand Up @@ -79,7 +81,9 @@ def __init__(self, directory):
"python3",
str(Path(__file__).parent / ".." / "gltestserver" / "__main__.py"),
"run",
f"--directory={directory}",
]
self.directory = Path(directory)

def start(self):
TailableProc.start(self)
Expand All @@ -89,19 +93,21 @@ def stop(self):
self.proc.send_signal(signal.SIGTERM)
self.proc.wait()

def metadata(self):
metadata = json.load(
(self.directory / "gl-testserver" / "metadata.json").open(mode="r")
)
return metadata


@pytest.fixture
def testserver(directory):
ts = TestServer(directory=directory)
ts.start()


metadata = json.load(open(f'{directory}/metadata.json'))
pprint(metadata)

yield ts
ts.stop()


def test_start(testserver):
print(TailableProc)
print(testserver.metadata())

0 comments on commit 469e7c2

Please sign in to comment.