diff --git a/marble_node_registry/update.py b/marble_node_registry/update.py index 52a9554..4a25316 100644 --- a/marble_node_registry/update.py +++ b/marble_node_registry/update.py @@ -70,8 +70,7 @@ def update_registry() -> None: try: data["version"] = version_response.json().get("version", "unknown") except requests.exceptions.JSONDecodeError: - registry[name] = org_data - registry[name]["status"] = "unresponsive" + data["status"] = "unresponsive" sys.stderr.write( f"invalid json returned when accessing version for Node named {name}: {version_response.text}\n" ) @@ -80,8 +79,7 @@ def update_registry() -> None: try: data["services"] = services_response.json().get("services", []) except requests.exceptions.JSONDecodeError: - registry[name] = org_data - registry[name]["status"] = "unresponsive" + data["status"] = "unresponsive" sys.stderr.write( f"invalid json returned when accessing services for Node named {name}: {services_response.text}\n" ) @@ -90,12 +88,11 @@ def update_registry() -> None: try: jsonschema.validate(instance=registry, schema=schema) except jsonschema.exceptions.ValidationError as e: - registry[name] = org_data - registry[name]["status"] = "invalid_configuration" + registry[name] = {**org_data, "status": "invalid_configuration"} # do not include services data if it is invalid sys.stderr.write(f"invalid configuration for Node named {name}: {e}\n") else: print(f"successfully updated Node named {name}") - registry[name]["last_updated"] = datetime.datetime.now(tz=datetime.timezone.utc).isoformat() + data["last_updated"] = datetime.datetime.now(tz=datetime.timezone.utc).isoformat() data["status"] = "online" _write_registry(registry) diff --git a/node_registry.schema.json b/node_registry.schema.json index 7859e5a..d85c7b1 100644 --- a/node_registry.schema.json +++ b/node_registry.schema.json @@ -79,7 +79,9 @@ "type": "string", "enum": [ "offline", - "online" + "online", + "unresponsive", + "invalid_configuration" ] }, "contact": { diff --git a/tests/test_update.py b/tests/test_update.py index 1a7a9ac..c1bb635 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -2,6 +2,7 @@ import os from copy import deepcopy +import jsonschema import pytest from marble_node_registry import update @@ -105,6 +106,10 @@ def example_node_name(example_registry_content): """Return the name of the node in example_registry_content""" return list(example_registry_content)[0] +@pytest.fixture +def node_registry_schema(): + return update._load_schema() + class TestEmptyRegistry: """Test when the registry is initially empty""" @@ -260,6 +265,9 @@ def test_last_updated_updated(self, example_node_name, example_registry_content, example_node_name ].get("last_updated") + def test_final_registry_valid(self, updated_registry, node_registry_schema): + jsonschema.validate(instance=updated_registry.call_args.args[0], schema=node_registry_schema) + class InvalidResponseTests: """ @@ -292,6 +300,9 @@ def test_last_updated_no_change(self, example_node_name, example_registry_conten example_node_name ].get("last_updated") + def test_final_registry_valid(self, updated_registry, node_registry_schema): + jsonschema.validate(instance=updated_registry.call_args.args[0], schema=node_registry_schema) + class TestInitialUpdateNoServices(ValidResponseTests, InitialTests): """Test when no updates have previously been run and there are no reported services"""