Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions marble_node_registry/update.py
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored this file as well even though they're technically unrelated to the current bug fix.
Basically, we were over-using the org_data variable in a lot of cases where it wasn't necessary.

Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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"
)
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion node_registry.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@
"type": "string",
"enum": [
"offline",
"online"
"online",
"unresponsive",
"invalid_configuration"
]
},
"contact": {
Expand Down
11 changes: 11 additions & 0 deletions tests/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
from copy import deepcopy

import jsonschema
import pytest

from marble_node_registry import update
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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"""
Expand Down