forked from opensearch-project/opensearch-migrations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andre Kurait <[email protected]>
- Loading branch information
1 parent
4d1b8a7
commit 1e81a14
Showing
9 changed files
with
316 additions
and
433 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
MetadataMigration/src/test/java/org/opensearch/migrations/BaseMigrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package org.opensearch.migrations; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import org.opensearch.migrations.bulkload.common.FileSystemSnapshotCreator; | ||
import org.opensearch.migrations.bulkload.common.OpenSearchClient; | ||
import org.opensearch.migrations.bulkload.common.http.ConnectionContextTestParams; | ||
import org.opensearch.migrations.bulkload.framework.SearchClusterContainer; | ||
import org.opensearch.migrations.bulkload.http.ClusterOperations; | ||
import org.opensearch.migrations.bulkload.worker.SnapshotRunner; | ||
import org.opensearch.migrations.commands.MigrationItemResult; | ||
import org.opensearch.migrations.metadata.tracing.MetadataMigrationTestContext; | ||
import org.opensearch.migrations.snapshot.creation.tracing.SnapshotTestContext; | ||
|
||
import lombok.Getter; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
/** | ||
* Base test class providing shared setup and utility methods for migration tests. | ||
*/ | ||
@Slf4j | ||
abstract class BaseMigrationTest { | ||
|
||
@TempDir | ||
protected File localDirectory; | ||
|
||
@Getter | ||
protected SearchClusterContainer sourceCluster; | ||
@Getter | ||
protected SearchClusterContainer targetCluster; | ||
|
||
protected ClusterOperations sourceOperations; | ||
protected ClusterOperations targetOperations; | ||
|
||
/** | ||
* Starts the source and target clusters. | ||
*/ | ||
protected void startClusters() { | ||
CompletableFuture.allOf( | ||
CompletableFuture.runAsync(sourceCluster::start), | ||
CompletableFuture.runAsync(targetCluster::start) | ||
).join(); | ||
|
||
sourceOperations = new ClusterOperations(sourceCluster.getUrl()); | ||
targetOperations = new ClusterOperations(targetCluster.getUrl()); | ||
} | ||
|
||
/** | ||
* Sets up a snapshot repository and takes a snapshot of the source cluster. | ||
* | ||
* @param snapshotName Name of the snapshot. | ||
* @return The name of the created snapshot. | ||
*/ | ||
@SneakyThrows | ||
protected String createSnapshot(String snapshotName) { | ||
var snapshotContext = SnapshotTestContext.factory().noOtelTracking(); | ||
var sourceClient = new OpenSearchClient(ConnectionContextTestParams.builder() | ||
.host(sourceCluster.getUrl()) | ||
.insecure(true) | ||
.build() | ||
.toConnectionContext()); | ||
var snapshotCreator = new org.opensearch.migrations.bulkload.common.FileSystemSnapshotCreator( | ||
snapshotName, | ||
sourceClient, | ||
SearchClusterContainer.CLUSTER_SNAPSHOT_DIR, | ||
List.of(), | ||
snapshotContext.createSnapshotCreateContext() | ||
); | ||
org.opensearch.migrations.bulkload.worker.SnapshotRunner.runAndWaitForCompletion(snapshotCreator); | ||
sourceCluster.copySnapshotData(localDirectory.toString()); | ||
return snapshotName; | ||
} | ||
|
||
/** | ||
* Prepares migration arguments for snapshot-based migrations. | ||
* | ||
* @param snapshotName Name of the snapshot. | ||
* @return Prepared migration arguments. | ||
*/ | ||
protected MigrateOrEvaluateArgs prepareSnapshotMigrationArgs(String snapshotName) { | ||
var arguments = new MigrateOrEvaluateArgs(); | ||
arguments.fileSystemRepoPath = localDirectory.getAbsolutePath(); | ||
arguments.snapshotName = snapshotName; | ||
arguments.sourceVersion = sourceCluster.getContainerVersion().getVersion(); | ||
arguments.targetArgs.host = targetCluster.getUrl(); | ||
return arguments; | ||
} | ||
|
||
/** | ||
* Executes the migration command (either migrate or evaluate). | ||
* | ||
* @param arguments Migration arguments. | ||
* @param command The migration command to execute. | ||
* @return The result of the migration. | ||
*/ | ||
protected MigrationItemResult executeMigration(MigrateOrEvaluateArgs arguments, MetadataCommands command) { | ||
var metadataContext = MetadataMigrationTestContext.factory().noOtelTracking(); | ||
var metadata = new MetadataMigration(); | ||
|
||
if (MetadataCommands.MIGRATE.equals(command)) { | ||
return metadata.migrate(arguments).execute(metadataContext); | ||
} else { | ||
return metadata.evaluate(arguments).execute(metadataContext); | ||
} | ||
} | ||
|
||
/** | ||
* Creates an OpenSearch client for the given cluster. | ||
* | ||
* @param cluster The cluster container. | ||
* @return An OpenSearch client. | ||
*/ | ||
protected OpenSearchClient createClient(SearchClusterContainer cluster) { | ||
return new OpenSearchClient(ConnectionContextTestParams.builder() | ||
.host(cluster.getUrl()) | ||
.insecure(true) | ||
.build() | ||
.toConnectionContext()); | ||
} | ||
|
||
protected SnapshotTestContext createSnapshotContext() { | ||
return SnapshotTestContext.factory().noOtelTracking(); | ||
} | ||
|
||
protected FileSystemSnapshotCreator createSnapshotCreator(String snapshotName, OpenSearchClient client, SnapshotTestContext context) { | ||
return new FileSystemSnapshotCreator( | ||
snapshotName, | ||
client, | ||
SearchClusterContainer.CLUSTER_SNAPSHOT_DIR, | ||
List.of(), | ||
context.createSnapshotCreateContext() | ||
); | ||
} | ||
|
||
@SneakyThrows | ||
protected void runSnapshotAndCopyData(FileSystemSnapshotCreator snapshotCreator, SearchClusterContainer cluster) { | ||
SnapshotRunner.runAndWaitForCompletion(snapshotCreator); | ||
cluster.copySnapshotData(localDirectory.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.