Skip to content

Commit 48b05f6

Browse files
committed
gltestserver: Make the top-level directory configurable
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.
1 parent 44ab1a1 commit 48b05f6

File tree

2 files changed

+50
-30
lines changed

2 files changed

+50
-30
lines changed

libs/gl-testserver/gltestserver/__main__.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
import json
21
from dataclasses import dataclass
3-
4-
import time
5-
6-
from rich.console import Console
7-
from rich.pretty import pprint
8-
from rich import inspect
9-
from pathlib import Path
102
from gltesting import fixtures
11-
import gltesting
123
from inspect import isgeneratorfunction
13-
import click
14-
import logging
15-
from rich.logging import RichHandler
4+
from pathlib import Path
165
from pyln.testing.utils import BitcoinD
6+
from rich.console import Console
7+
from rich.logging import RichHandler
8+
from rich.pretty import pprint
179
from typing import Any, List
10+
import click
11+
import gltesting
12+
import json
13+
import logging
14+
import tempfile
15+
import time
1816

1917

2018
console = Console()
@@ -56,7 +54,7 @@ def metadata(self):
5654
}
5755

5856

59-
def build():
57+
def build(base_dir: Path):
6058
# List of teardown functions to call in reverse order.
6159
finalizers = []
6260

@@ -72,14 +70,14 @@ def callfixture(f, *args, **kwargs):
7270
else:
7371
return F(*args, **kwargs)
7472

75-
directory = Path("/tmp/gl-testserver")
73+
directory = base_dir / "gl-testserver"
7674

7775
cert_directory = callfixture(fixtures.cert_directory, directory)
78-
root_id = callfixture(fixtures.root_id, cert_directory)
79-
users_id = callfixture(fixtures.users_id)
76+
_root_id = callfixture(fixtures.root_id, cert_directory)
77+
_users_id = callfixture(fixtures.users_id)
8078
nobody_id = callfixture(fixtures.nobody_id, cert_directory)
8179
scheduler_id = callfixture(fixtures.scheduler_id, cert_directory)
82-
paths = callfixture(fixtures.paths)
80+
_paths = callfixture(fixtures.paths)
8381
bitcoind = callfixture(
8482
fixtures.bitcoind,
8583
directory=directory,
@@ -113,18 +111,34 @@ def cli():
113111

114112

115113
@cli.command()
116-
def run():
117-
gl = build()
114+
@click.option(
115+
"--directory",
116+
type=click.Path(),
117+
help="""
118+
Set the top-level directory for the testserver. This can be used to run
119+
multiple instances isolated from each other, by giving each isntance a
120+
different top-level directory. Defaults to '/tmp/'
121+
""",
122+
)
123+
def run(directory):
124+
"""Start a gl-testserver instance to test against."""
125+
if not directory:
126+
directory = Path(tempfile.gettempdir())
127+
else:
128+
directory = Path(directory)
129+
130+
gl = build(base_dir=directory)
118131
try:
119132
meta = gl.metadata()
120133
metafile = gl.directory / "metadata.json"
134+
metafile.parent.mkdir(parents=True, exist_ok=True)
121135
logger.debug(f"Writing testserver metadata to {metafile}")
122136
with metafile.open(mode="w") as f:
123137
json.dump(meta, f)
124138

125139
pprint(meta)
126140
logger.info(
127-
f"Server is up and running with the above config values. To stop press Ctrl-C."
141+
"Server is up and running with the above config values. To stop press Ctrl-C."
128142
)
129143
time.sleep(1800)
130144
except Exception as e:

libs/gl-testserver/tests/test_server.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
# Ok, one exception, `TailableProc` is used to run and tail the
55
# `gl-testserver`.
66

7-
import shutil
8-
import tempfile
9-
import os
10-
import pytest
7+
from pathlib import Path
118
from pyln.testing.utils import TailableProc
129
import json
10+
import logging
11+
import os
12+
import pytest
13+
import shutil
1314
import signal
14-
from pathlib import Path
15+
import tempfile
16+
import time
1517

1618

1719
@pytest.fixture
@@ -79,7 +81,9 @@ def __init__(self, directory):
7981
"python3",
8082
str(Path(__file__).parent / ".." / "gltestserver" / "__main__.py"),
8183
"run",
84+
f"--directory={directory}",
8285
]
86+
self.directory = Path(directory)
8387

8488
def start(self):
8589
TailableProc.start(self)
@@ -89,19 +93,21 @@ def stop(self):
8993
self.proc.send_signal(signal.SIGTERM)
9094
self.proc.wait()
9195

96+
def metadata(self):
97+
metadata = json.load(
98+
(self.directory / "gl-testserver" / "metadata.json").open(mode="r")
99+
)
100+
return metadata
101+
92102

93103
@pytest.fixture
94104
def testserver(directory):
95105
ts = TestServer(directory=directory)
96106
ts.start()
97107

98-
99-
metadata = json.load(open(f'{directory}/metadata.json'))
100-
pprint(metadata)
101-
102108
yield ts
103109
ts.stop()
104110

105111

106112
def test_start(testserver):
107-
print(TailableProc)
113+
print(testserver.metadata())

0 commit comments

Comments
 (0)