Skip to content

Commit

Permalink
updated tests to represent backend flow
Browse files Browse the repository at this point in the history
  • Loading branch information
witlox committed Dec 10, 2024
1 parent 84a2751 commit c3dd68e
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 31 deletions.
11 changes: 7 additions & 4 deletions horao/controllers/synchronization.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(
for dc in self.logical_infrastructure.infrastructure.keys():
dc.add_listeners(self.synchronize)

def synchronize(self, changes: Optional[List] = None) -> None:
def synchronize(self, changes: Optional[List] = None) -> datetime | None:
"""
Synchronize with all peers, if one of the following conditions is met:
- there was no previous synchronization time stamp.
Expand All @@ -66,9 +66,10 @@ def synchronize(self, changes: Optional[List] = None) -> None:
"""
if not self.peers:
return None
sync_time = datetime.now()
timedelta_exceeded = False
last_sync = self.session.load("last_sync")
if not last_sync or datetime.now() - last_sync < timedelta(
if not last_sync or sync_time - datetime.fromisoformat(last_sync) > timedelta(
seconds=self.sync_delta
):
timedelta_exceeded = True
Expand All @@ -86,11 +87,13 @@ def synchronize(self, changes: Optional[List] = None) -> None:
)
try:
lg = httpx.post(
"/synchronize",
f"{peer}/synchronize",
headers={"Peer": "true", "Authorization": f"Bearer {token}"},
json=json.dumps(self.logical_infrastructure, cls=HoraoEncoder),
)
lg.raise_for_status()
except httpx.HTTPError as e:
self.logger.error(f"Error synchronizing with {peer}: {e}")
self.session.save("last_sync", datetime.now())
self.session.save("last_sync", sync_time)
self.logical_infrastructure.clear_changes()
return sync_time
42 changes: 24 additions & 18 deletions horao/logical/data_center.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,32 @@ def invoke_listeners(self, changes: Optional[List] = None) -> None:
if isinstance(changes, List):
self.changes.extend(changes)
else:
self.changes.append(changes)
self.changes.append(changes) # type: ignore
for listener in self.listeners:
listener(changes)

def clear_changes(self) -> None:
"""
Clear the changes
:return: None
"""
for _, v in self.rows.read().items():
for cabinet in v:
for server in cabinet.servers:
server.disks.clear_history()
cabinet.servers.clear_history()
for chassis in cabinet.chassis:
for blade in chassis.blades:
for node in blade.nodes:
for module in node.modules:
module.disks.clear_history()
node.modules.clear_history()
blade.nodes.clear_history()
chassis.blades.clear_history()
cabinet.chassis.clear_history()
cabinet.switches.clear_history()
self.changes = []

@instrument_class_function(name="copy", level=logging.DEBUG)
def copy(self) -> Dict[int, List[Cabinet]]:
result = {}
Expand Down Expand Up @@ -145,23 +167,7 @@ def merge(self, other: DataCenter) -> None:
self[number][self[number].index(cabinet)].merge(cabinet)
else:
self[number] = row

# clear the change history
for _, v in self.rows.read().items():
for cabinet in v:
for server in cabinet.servers:
server.disks.clear_history()
cabinet.servers.clear_history()
for chassis in cabinet.chassis:
for blade in chassis.blades:
for node in blade.nodes:
for module in node.modules:
module.disks.clear_history()
node.modules.clear_history()
blade.nodes.clear_history()
chassis.blades.clear_history()
cabinet.chassis.clear_history()
cabinet.switches.clear_history()
self.clear_changes()

def __eq__(self, other) -> bool:
if not isinstance(other, DataCenter):
Expand Down
7 changes: 4 additions & 3 deletions horao/logical/infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

import logging
from typing import Dict, Iterable, List, Optional, Tuple, Any
from typing import Any, Dict, Iterable, List, Optional, Tuple

from horao.conceptual.claim import Claim, Reservation
from horao.conceptual.decorators import instrument_class_function
Expand Down Expand Up @@ -45,8 +45,9 @@ def changes(self) -> List[Any]:
"""
return [d.changes for d in self.infrastructure.keys()]

def clear(self) -> None:
self.infrastructure.clear()
def clear_changes(self) -> None:
for d in self.infrastructure.keys():
d.clear_changes()

def copy(self) -> LogicalInfrastructure:
return LogicalInfrastructure(
Expand Down
103 changes: 102 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pytest-cov = "^5.0.0"
pytest-asyncio = "^0.23.8"
httpx = "^0.27.2"
testcontainers = "^4.8.2"
pytest_httpserver = "^1.1.0"

[tool.poetry.group.test]
optional = true
Expand Down
Loading

0 comments on commit c3dd68e

Please sign in to comment.