Skip to content

Commit

Permalink
Add mark unavaialble test
Browse files Browse the repository at this point in the history
  • Loading branch information
JBorrow committed Nov 14, 2024
1 parent b08ee02 commit 20563cf
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
2 changes: 1 addition & 1 deletion librarian_background/rolling_deletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def core(self, session: Session):
force=self.force_deletion,
mark_unavailable=self.mark_unavailable,
)
logger.info("Deleted instance {} successfully", instance.id)
logger.info("Deleted data for instance {} successfully", instance.id)
deleted += 1
except FileNotFoundError:
logger.error(
Expand Down
3 changes: 3 additions & 0 deletions librarian_server/orm/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from datetime import datetime, timezone
from pathlib import Path

from loguru import logger
from sqlalchemy.orm import Session

from hera_librarian.deletion import DeletionPolicy
Expand Down Expand Up @@ -112,8 +113,10 @@ def delete(
self.store.store_manager.delete(Path(self.path))

if mark_unavailable:
logger.info("Marking instance {} as unavailable", self.id)
self.available = False
else:
logger.info("Deleting instance {}", self.id)
session.delete(self)

if commit:
Expand Down
84 changes: 73 additions & 11 deletions tests/background_unit_test/test_rolling_deletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,7 @@
from hera_librarian.deletion import DeletionPolicy


def test_rolling_deletion_with_single_instance(
test_client, test_server, test_orm, garbage_file
):
"""
Delete a single instance.
"""
from librarian_background.rolling_deletion import RollingDeletion

_, get_session, _ = test_server

session = get_session()
def prep_file(garbage_file, test_orm, session):

store = session.query(test_orm.StoreMetadata).filter_by(ingestable=True).first()

Expand Down Expand Up @@ -46,6 +36,24 @@ def test_rolling_deletion_with_single_instance(
session.add_all([file, instance])
session.commit()

return store, file, instance


def test_rolling_deletion_with_single_instance(
test_client, test_server, test_orm, garbage_file
):
"""
Delete a single instance.
"""
from librarian_background.rolling_deletion import RollingDeletion

_, get_session, _ = test_server

session = get_session()

store, file, instance = prep_file(garbage_file, test_orm, session)

FILE_NAME = file.name
INSTANCE_ID = instance.id

# Run the task
Expand All @@ -62,6 +70,10 @@ def test_rolling_deletion_with_single_instance(

assert task

session.close()

session = get_session()

# Check that the instance is gone
assert (
session.query(test_orm.Instance).filter_by(id=INSTANCE_ID).one_or_none() is None
Expand All @@ -73,3 +85,53 @@ def test_rolling_deletion_with_single_instance(
)

return


def test_rolling_deletion_with_single_instance_unavailable(
test_client, test_server, test_orm, garbage_file
):
"""
Delete a single instance.
"""
from librarian_background.rolling_deletion import RollingDeletion

_, get_session, _ = test_server

session = get_session()

store, file, instance = prep_file(garbage_file, test_orm, session)

FILE_NAME = file.name
INSTANCE_ID = instance.id

# Run the task
task = RollingDeletion(
name="Rolling deletion",
soft_timeout="6:00:00",
store_name=store.name,
age_in_days=0.0000000000000000001,
number_of_remote_copies=0,
verify_downstream_checksums=False,
mark_unavailable=True,
force_deletion=False,
)()

assert task

# bgtask uses a different session
session.close()

session = get_session()

# Check that the instance is gone
re_queried = (
session.query(test_orm.Instance).filter_by(id=INSTANCE_ID).one_or_none()
)
assert not re_queried.available

# Delete the file we created
session.get(test_orm.File, FILE_NAME).delete(
session=session, commit=True, force=True
)

return

0 comments on commit 20563cf

Please sign in to comment.