Skip to content

Commit

Permalink
If payload empty => update all roles
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Vrachev <[email protected]>
  • Loading branch information
MVrachev committed Apr 11, 2024
1 parent 6e815fe commit 5848aa4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 13 deletions.
19 changes: 12 additions & 7 deletions repository_service_tuf_worker/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ class Roles(enum.Enum):
TIMESTAMP = Timestamp.type
BINS = "bins"

@staticmethod
def online_roles() -> List[str]:
online_roles = [Targets.type, Snapshot.type, Timestamp.type, "bins"]
settings_repository = get_worker_settings()
settings_repository.reload()
if not settings_repository.get_fresh("TARGETS_ONLINE_KEY", True):
online_roles = [Snapshot.type, Timestamp.type, "bins"]

return online_roles


ALL_REPOSITORY_ROLES_NAMES = [rolename.value for rolename in Roles]
OFFLINE_KEYS = {
Expand Down Expand Up @@ -1270,13 +1280,8 @@ def force_online_metadata_update(
"New metadata updates requre completed bootstrap",
details=None,
)
if len(payload["roles"]) < 1:
return self._task_result(
TaskName.FORCE_ONLINE_METADATA_UPDATE,
"Force new online metadata update failed",
error="No online metadata roles given",
details=None,
)
if len(payload["roles"]) == 0:
payload["roles"] = Roles.online_roles()

# There is a specific order in which we should update the online roles:
# 1. targets and all other target roles (like bins)
Expand Down
73 changes: 67 additions & 6 deletions tests/unit/tuf_repository_service_worker/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,46 @@
from repository_service_tuf_worker.models import targets_schema


class TestRoles:
def test_roles_online_roles_method(self, monkeypatch):
fake_settings = pretend.stub(
reload=pretend.call_recorder(lambda: None),
get_fresh=pretend.call_recorder(lambda *a: True),
)
fake_get_worker_settings = pretend.call_recorder(lambda: fake_settings)
monkeypatch.setattr(
"repository_service_tuf_worker.repository.get_worker_settings",
fake_get_worker_settings,
)

result = repository.Roles.online_roles()
assert result == [Targets.type, Snapshot.type, Timestamp.type, "bins"]
assert fake_settings.reload.calls == [pretend.call()]
assert fake_settings.get_fresh.calls == [
pretend.call("TARGETS_ONLINE_KEY", True)
]
assert fake_get_worker_settings.calls == [pretend.call()]

def test_roles_online_roles_method_targets_is_online(self, monkeypatch):
fake_settings = pretend.stub(
reload=pretend.call_recorder(lambda: None),
get_fresh=pretend.call_recorder(lambda *a: False),
)
fake_get_worker_settings = pretend.call_recorder(lambda: fake_settings)
monkeypatch.setattr(
"repository_service_tuf_worker.repository.get_worker_settings",
fake_get_worker_settings,
)

result = repository.Roles.online_roles()
assert result == [Snapshot.type, Timestamp.type, "bins"]
assert fake_settings.reload.calls == [pretend.call()]
assert fake_settings.get_fresh.calls == [
pretend.call("TARGETS_ONLINE_KEY", True)
]
assert fake_get_worker_settings.calls == [pretend.call()]


class TestMetadataRepository:
def test_basic_init(self, monkeypatch):
fake_configure = pretend.call_recorder(lambda *a: None)
Expand Down Expand Up @@ -3662,23 +3702,44 @@ def test__force_online_metadata_update_payload_empty(
mocked_datetime,
):
fake_settings = pretend.stub(
get_fresh=pretend.call_recorder(lambda *a: "123")
get_fresh=pretend.call_recorder(lambda a: "123")
)
monkeypatch.setattr(
repository,
"get_repository_settings",
lambda *a, **kw: fake_settings,
)
result = test_repo.force_online_metadata_update({"roles": []})

@contextmanager
def mocked_lock(lock, timeout):
yield lock, timeout

test_repo._redis = pretend.stub(
lock=pretend.call_recorder(mocked_lock),
)
test_repo._run_force_online_metadata_update = pretend.call_recorder(
lambda a: [Targets.type, Snapshot.type, Timestamp.type, "bins"]
)
payload = {"roles": []}
result = test_repo.force_online_metadata_update(payload)
expected_roles = [Targets.type, Snapshot.type, Timestamp.type, "bins"]
assert result == {
"task": repository.TaskName.FORCE_ONLINE_METADATA_UPDATE,
"status": False,
"message": "Force new online metadata update failed",
"error": "No online metadata roles given",
"details": None,
"status": True,
"message": "Force new online metadata update succeeded",
"error": None,
"details": {
"updated_roles": expected_roles,
},
"last_update": mocked_datetime.now(),
}
assert fake_settings.get_fresh.calls == [pretend.call("BOOTSTRAP")]
assert test_repo._redis.lock.calls == [
pretend.call(repository.LOCK_TARGETS, timeout=test_repo._timeout)
]
assert test_repo._run_force_online_metadata_update.calls == [
pretend.call(expected_roles)
]

def test__force_online_metadata_update_timeout(
self, test_repo, monkeypatch, mocked_datetime
Expand Down

0 comments on commit 5848aa4

Please sign in to comment.