Skip to content

Commit

Permalink
Reset snapshots between tests (#1212)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Kurait <[email protected]>
  • Loading branch information
AndreKurait authored Jan 2, 2025
1 parent e8ba4a1 commit f6b0c4c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,43 @@ def clear_indices(cluster: Cluster):
clear_indices_path = "/*,-.*,-searchguard*,-sg7*,.migrations_working_state"
r = cluster.call_api(clear_indices_path, method=HttpMethod.DELETE, params={"ignore_unavailable": "true"})
return r.content


def clear_cluster(cluster: Cluster):
clear_indices(cluster)
clear_snapshots(cluster, 'migration_assistant_repo')


def clear_snapshots(cluster: Cluster, repository: str) -> None:
"""
Clears all snapshots from the specified repository.
:param cluster: Cluster object to interact with the Elasticsearch cluster.
:param repository: Name of the snapshot repository to clear snapshots from.
:raises Exception: For general errors during snapshot clearing, except when the repository is missing.
"""
try:
# List all snapshots in the repository
snapshots_path = f"/_snapshot/{repository}/_all"
response = call_api(cluster, snapshots_path, raise_error=True)
snapshots = response.json().get("snapshots", [])

if not snapshots:
logger.info(f"No snapshots found in repository '{repository}'.")
return

# Delete each snapshot
for snapshot in snapshots:
snapshot_name = snapshot["snapshot"]
delete_path = f"/_snapshot/{repository}/{snapshot_name}"
call_api(cluster, delete_path, method=HttpMethod.DELETE, raise_error=True)
logger.info(f"Deleted snapshot: {snapshot_name} from repository '{repository}'.")

except Exception as e:
# Handle 404 errors specifically for missing repository
if "repository_missing_exception" in str(e):
logger.info(f"Repository '{repository}' is missing. Skipping snapshot clearing.")
else:
# Re-raise other errors
logger.error(f"Error clearing snapshots from repository '{repository}': {e}")
raise e
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import pytest
import unittest
from http import HTTPStatus
from console_link.middleware.clusters import run_test_benchmarks, connection_check, clear_indices, ConnectionResult
from console_link.middleware.clusters import run_test_benchmarks, connection_check, clear_cluster, ConnectionResult
from console_link.models.cluster import Cluster
from console_link.models.backfill_base import Backfill
from console_link.models.command_result import CommandResult
from console_link.models.snapshot import Snapshot
from console_link.models.metadata import Metadata
from console_link.cli import Context
from common_operations import (get_document, create_document, create_index, check_doc_counts_match,
Expand All @@ -22,9 +21,9 @@ def preload_data(source_cluster: Cluster, target_cluster: Cluster):
target_con_result: ConnectionResult = connection_check(target_cluster)
assert target_con_result.connection_established is True

# Clear any existing non-system indices
clear_indices(source_cluster)
clear_indices(target_cluster)
# Clear all data from clusters
clear_cluster(source_cluster)
clear_cluster(target_cluster)

# Preload data that test cases will verify is migrated
# test_backfill_0001
Expand Down Expand Up @@ -52,11 +51,7 @@ def setup_backfill(request):
assert metadata is not None

backfill.create()
snapshot: Snapshot = pytest.console_env.snapshot
status_result: CommandResult = snapshot.status()
if status_result.success:
snapshot.delete()
snapshot_result: CommandResult = snapshot.create(wait=True)
snapshot_result: CommandResult = pytest.console_env.snapshot.create(wait=True)
assert snapshot_result.success
metadata_result: CommandResult = metadata.migrate()
assert metadata_result.success
Expand Down

0 comments on commit f6b0c4c

Please sign in to comment.