Skip to content

Commit ea578a6

Browse files
michael.yakmichaelyaakoby
authored andcommitted
Verify metadata is included in registration
1 parent e569adc commit ea578a6

File tree

7 files changed

+17
-4
lines changed

7 files changed

+17
-4
lines changed

pyctuator/pyctuator.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(
3838
logfile_max_size: int = 10000,
3939
logfile_formatter: str = default_logfile_format,
4040
auto_deregister: bool = True,
41-
metadata: Optional[dict] = {}
41+
metadata: Optional[dict] = None
4242
) -> None:
4343
"""The entry point for integrating pyctuator with a web-frameworks such as FastAPI and Flask.
4444
@@ -70,6 +70,10 @@ def __init__(
7070
:param registration_interval_sec: how often pyctuator will renew its registration with spring-boot-admin
7171
:param free_disk_space_down_threshold_bytes: amount of free space in bytes in "./" (the application's current
7272
working directory) below which the built-in disk-space health-indicator will fail
73+
:param auto_deregister: if true, pyctuator will automatically deregister from SBA during shutdown, needed for
74+
example when running in k8s so every time a new pod is created it is assigned a different IP address, resulting
75+
with SBA showing "offline" instances
76+
:param metadata: optional metadata key-value pairs that are displayed in SBA main page of an instance
7377
"""
7478

7579
self.auto_deregister = auto_deregister

tests/aiohttp_test_server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def __init__(self) -> None:
2424
"http://localhost:8888/pyctuator",
2525
"http://localhost:8001/register",
2626
registration_interval_sec=1,
27+
metadata=self.metadata,
2728
)
2829

2930
@self.routes.get("/logfile_test_repeater")

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import random
23
import secrets
34
import threading
45
import time
@@ -155,6 +156,7 @@ def endpoints(registration_tracker: RegistrationTrackerFixture) -> Endpoints:
155156

156157

157158
class PyctuatorServer(ABC):
159+
metadata: Optional[dict] = {f"k{i}": f"v{i}" for i in range(random.randrange(10))}
158160

159161
@abstractmethod
160162
def start(self) -> None:

tests/fast_api_test_server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def __init__(self) -> None:
2727
"http://localhost:8000/pyctuator",
2828
"http://localhost:8001/register",
2929
registration_interval_sec=1,
30+
metadata=self.metadata,
3031
)
3132

3233
@self.app.get("/logfile_test_repeater", tags=["pyctuator"])

tests/flask_test_server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def __init__(self) -> None:
2020
"http://localhost:5000",
2121
"http://localhost:5000/pyctuator",
2222
"http://localhost:8001/register",
23-
registration_interval_sec=1
23+
registration_interval_sec=1,
24+
metadata=self.metadata,
2425
)
2526

2627
@self.app.route("/shutdown", methods=["POST"])

tests/test_pyctuator_e2e.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
from tests.conftest import Endpoints, PyctuatorServer, RegistrationRequest, RegistrationTrackerFixture
2020
from tests.fast_api_test_server import FastApiPyctuatorServer
2121
from tests.flask_test_server import FlaskPyctuatorServer
22-
23-
2422
# mypy: ignore_errors
2523
from tests.tornado_test_server import TornadoPyctuatorServer
2624

@@ -198,6 +196,11 @@ def test_recurring_registration_and_deregistration(
198196
registration_start_time = datetime.fromisoformat(registration_tracker.start_time)
199197
assert registration_start_time > registration_tracker.test_start_time - timedelta(seconds=10)
200198

199+
# Verify that the randomly generated metadata created when the server starter are included in the registration
200+
metadata = registration_tracker.registration.metadata
201+
metadata_without_startup = {k: metadata[k] for k in metadata if k != "startup"}
202+
assert metadata_without_startup == pyctuator_server.metadata
203+
201204
# Ask to deregister (in real life, called by atexit) and verify it was registered
202205
pyctuator_server.atexit()
203206
assert registration_tracker.deregistration_time > registration_start_time

tests/tornado_test_server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def get(self) -> None:
5353
registration_url=f"http://localhost:8001/register",
5454
app_description="Demonstrate Spring Boot Admin Integration with Tornado",
5555
registration_interval_sec=1,
56+
metadata=self.metadata,
5657
)
5758

5859
self.io_loop: Optional[ioloop.IOLoop] = None

0 commit comments

Comments
 (0)