Skip to content

Commit

Permalink
snapshot assertAtLeast->assertExactly
Browse files Browse the repository at this point in the history
Signed-off-by: Maciej Mierzwa <[email protected]>
  • Loading branch information
MaciejMierzwa committed Nov 9, 2023
1 parent a833b1c commit 4b6934c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,10 @@ public void cleanData() throws ExecutionException, InterruptedException {
try {
clusterClient.deleteRepository(new DeleteRepositoryRequest(TEST_SNAPSHOT_REPOSITORY_NAME)).actionGet();
} catch (RepositoryMissingException e) {
log.info("Repository '{}' does not exist. This is expected in most of test cases", TEST_SNAPSHOT_REPOSITORY_NAME, e);
log.debug("Repository '{}' does not exist. This is expected in most of test cases", TEST_SNAPSHOT_REPOSITORY_NAME, e);
}
internalClient.close();
log.info("Cleaning data after test took {}", stopwatch.stop());
log.debug("Cleaning data after test took {}", stopwatch.stop());
}

@Test
Expand Down Expand Up @@ -1764,6 +1764,7 @@ public void shouldDeleteSnapshotRepository_negative() throws IOException {
@Test // Bug which can be reproduced with the below test: https://github.com/opensearch-project/security/issues/2169
public void shouldCreateSnapshot_positive() throws IOException {
final String snapshotName = "snapshot-positive-test";
long snapshotGetCount;
try (RestHighLevelClient restHighLevelClient = cluster.getRestHighLevelClient(LIMITED_WRITE_USER)) {
SnapshotSteps steps = new SnapshotSteps(restHighLevelClient);
steps.createSnapshotRepository(TEST_SNAPSHOT_REPOSITORY_NAME, cluster.getSnapshotDirPath(), "fs");
Expand All @@ -1772,15 +1773,15 @@ public void shouldCreateSnapshot_positive() throws IOException {

assertThat(response, notNullValue());
assertThat(response.status(), equalTo(RestStatus.ACCEPTED));
steps.waitForSnapshotCreation(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName);
snapshotGetCount = steps.waitForSnapshotCreation(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName);
assertThat(internalClient, clusterContainSuccessSnapshot(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName));
}
auditLogsRule.assertExactly(1, userAuthenticated(LIMITED_WRITE_USER).withRestRequest(PUT, "/_snapshot/test-snapshot-repository"));
auditLogsRule.assertExactlyOne(
userAuthenticated(LIMITED_WRITE_USER).withRestRequest(PUT, "/_snapshot/test-snapshot-repository/snapshot-positive-test")
);
auditLogsRule.assertAtLeast(
1,
auditLogsRule.assertExactly(
snapshotGetCount,
userAuthenticated(LIMITED_WRITE_USER).withEffectiveUser(LIMITED_WRITE_USER)
.withRestRequest(GET, "/_snapshot/test-snapshot-repository/snapshot-positive-test")
);
Expand Down Expand Up @@ -1811,12 +1812,13 @@ public void shouldCreateSnapshot_negative() throws IOException {
@Test
public void shouldDeleteSnapshot_positive() throws IOException {
String snapshotName = "delete-snapshot-positive";
long snapshotGetCount;
try (RestHighLevelClient restHighLevelClient = cluster.getRestHighLevelClient(LIMITED_WRITE_USER)) {
SnapshotSteps steps = new SnapshotSteps(restHighLevelClient);
restHighLevelClient.snapshot();
steps.createSnapshotRepository(TEST_SNAPSHOT_REPOSITORY_NAME, cluster.getSnapshotDirPath(), "fs");
steps.createSnapshot(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName, SONG_INDEX_NAME);
steps.waitForSnapshotCreation(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName);
snapshotGetCount = steps.waitForSnapshotCreation(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName);

var response = steps.deleteSnapshot(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName);

Expand All @@ -1830,8 +1832,8 @@ public void shouldDeleteSnapshot_positive() throws IOException {
auditLogsRule.assertExactlyOne(
userAuthenticated(LIMITED_WRITE_USER).withRestRequest(DELETE, "/_snapshot/test-snapshot-repository/delete-snapshot-positive")
);
auditLogsRule.assertAtLeast(
1,
auditLogsRule.assertExactly(
snapshotGetCount,
userAuthenticated(LIMITED_WRITE_USER).withRestRequest(GET, "/_snapshot/test-snapshot-repository/delete-snapshot-positive")
);
auditLogsRule.assertExactly(1, grantedPrivilege(LIMITED_WRITE_USER, "PutRepositoryRequest"));
Expand All @@ -1843,11 +1845,12 @@ public void shouldDeleteSnapshot_positive() throws IOException {
@Test
public void shouldDeleteSnapshot_negative() throws IOException {
String snapshotName = "delete-snapshot-negative";
long snapshotGetCount;
try (RestHighLevelClient restHighLevelClient = cluster.getRestHighLevelClient(LIMITED_WRITE_USER)) {
SnapshotSteps steps = new SnapshotSteps(restHighLevelClient);
steps.createSnapshotRepository(TEST_SNAPSHOT_REPOSITORY_NAME, cluster.getSnapshotDirPath(), "fs");
steps.createSnapshot(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName, SONG_INDEX_NAME);
steps.waitForSnapshotCreation(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName);
snapshotGetCount = steps.waitForSnapshotCreation(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName);
}
try (RestHighLevelClient restHighLevelClient = cluster.getRestHighLevelClient(LIMITED_READ_USER)) {
SnapshotSteps steps = new SnapshotSteps(restHighLevelClient);
Expand All @@ -1862,8 +1865,8 @@ public void shouldDeleteSnapshot_negative() throws IOException {
auditLogsRule.assertExactlyOne(
userAuthenticated(LIMITED_READ_USER).withRestRequest(DELETE, "/_snapshot/test-snapshot-repository/delete-snapshot-negative")
);
auditLogsRule.assertAtLeast(
1,
auditLogsRule.assertExactly(
snapshotGetCount,
userAuthenticated(LIMITED_WRITE_USER).withRestRequest(GET, "/_snapshot/test-snapshot-repository/delete-snapshot-negative")
);
auditLogsRule.assertExactly(1, grantedPrivilege(LIMITED_WRITE_USER, "PutRepositoryRequest"));
Expand All @@ -1875,6 +1878,7 @@ public void shouldDeleteSnapshot_negative() throws IOException {
@Test
public void shouldRestoreSnapshot_positive() throws IOException {
final String snapshotName = "restore-snapshot-positive";
long snapshotGetCount;
try (RestHighLevelClient restHighLevelClient = cluster.getRestHighLevelClient(LIMITED_WRITE_USER)) {
SnapshotSteps steps = new SnapshotSteps(restHighLevelClient);
// 1. create some documents
Expand All @@ -1894,7 +1898,7 @@ public void shouldRestoreSnapshot_positive() throws IOException {
steps.createSnapshot(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName, WRITE_SONG_INDEX_NAME);

// 4. wait till snapshot is ready
steps.waitForSnapshotCreation(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName);
snapshotGetCount = steps.waitForSnapshotCreation(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName);

// 5. introduce some changes
bulkRequest = new BulkRequest();
Expand Down Expand Up @@ -1941,8 +1945,8 @@ public void shouldRestoreSnapshot_positive() throws IOException {
);
auditLogsRule.assertAtLeast(1, userAuthenticated(LIMITED_WRITE_USER).withRestRequest(POST, "/restored_write_song_index/_count"));
auditLogsRule.assertExactly(2, userAuthenticated(LIMITED_WRITE_USER).withRestRequest(POST, "/_bulk"));
auditLogsRule.assertAtLeast(
1,
auditLogsRule.assertExactly(
snapshotGetCount,
userAuthenticated(LIMITED_WRITE_USER).withRestRequest(GET, "/_snapshot/test-snapshot-repository/restore-snapshot-positive")
);
auditLogsRule.assertExactly(1, grantedPrivilege(LIMITED_WRITE_USER, "PutRepositoryRequest"));
Expand All @@ -1958,6 +1962,7 @@ public void shouldRestoreSnapshot_positive() throws IOException {
public void shouldRestoreSnapshot_failureForbiddenIndex() throws IOException {
final String snapshotName = "restore-snapshot-negative-forbidden-index";
String restoreToIndex = "forbidden_index";
long snapshotGetCount;
Settings indexSettings = Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).build();
IndexOperationsHelper.createIndex(cluster, WRITE_SONG_INDEX_NAME, indexSettings);
try (RestHighLevelClient restHighLevelClient = cluster.getRestHighLevelClient(LIMITED_WRITE_USER)) {
Expand All @@ -1977,7 +1982,7 @@ public void shouldRestoreSnapshot_failureForbiddenIndex() throws IOException {
steps.createSnapshot(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName, WRITE_SONG_INDEX_NAME);

// 4. wait till snapshot is ready
steps.waitForSnapshotCreation(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName);
snapshotGetCount = steps.waitForSnapshotCreation(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName);

// 5. restore the snapshot
assertThatThrownBy(
Expand All @@ -2003,8 +2008,8 @@ public void shouldRestoreSnapshot_failureForbiddenIndex() throws IOException {
)
);
auditLogsRule.assertExactlyOne(userAuthenticated(LIMITED_WRITE_USER).withRestRequest(POST, "/_bulk"));
auditLogsRule.assertAtLeast(
1,
auditLogsRule.assertExactly(
snapshotGetCount,
userAuthenticated(LIMITED_WRITE_USER).withRestRequest(
GET,
"/_snapshot/test-snapshot-repository/restore-snapshot-negative-forbidden-index"
Expand All @@ -2022,6 +2027,7 @@ public void shouldRestoreSnapshot_failureForbiddenIndex() throws IOException {
@Test
public void shouldRestoreSnapshot_failureOperationForbidden() throws IOException {
String snapshotName = "restore-snapshot-negative-forbidden-operation";
long snapshotGetCount;
Settings indexSettings = Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).build();
IndexOperationsHelper.createIndex(cluster, WRITE_SONG_INDEX_NAME, indexSettings);
try (RestHighLevelClient restHighLevelClient = cluster.getRestHighLevelClient(LIMITED_WRITE_USER)) {
Expand All @@ -2040,7 +2046,7 @@ public void shouldRestoreSnapshot_failureOperationForbidden() throws IOException
steps.createSnapshot(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName, WRITE_SONG_INDEX_NAME);

// 4. wait till snapshot is ready
steps.waitForSnapshotCreation(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName);
snapshotGetCount = steps.waitForSnapshotCreation(TEST_SNAPSHOT_REPOSITORY_NAME, snapshotName);
}
// 5. restore the snapshot
try (RestHighLevelClient restHighLevelClient = cluster.getRestHighLevelClient(LIMITED_READ_USER)) {
Expand Down Expand Up @@ -2068,8 +2074,8 @@ public void shouldRestoreSnapshot_failureOperationForbidden() throws IOException
)
);
auditLogsRule.assertExactlyOne(userAuthenticated(LIMITED_WRITE_USER).withRestRequest(POST, "/_bulk"));
auditLogsRule.assertAtLeast(
1,
auditLogsRule.assertExactly(
snapshotGetCount,
userAuthenticated(LIMITED_WRITE_USER).withRestRequest(
GET,
"/_snapshot/test-snapshot-repository/restore-snapshot-negative-forbidden-operation"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.awaitility.Awaitility;

Expand Down Expand Up @@ -59,18 +60,21 @@ public CreateSnapshotResponse createSnapshot(String repositoryName, String snaps
return snapshotClient.create(createSnapshotRequest, DEFAULT);
}

public void waitForSnapshotCreation(String repositoryName, String snapshotName) {
public int waitForSnapshotCreation(String repositoryName, String snapshotName) {
AtomicInteger count = new AtomicInteger();
GetSnapshotsRequest getSnapshotsRequest = new GetSnapshotsRequest(repositoryName, new String[] { snapshotName });
Awaitility.await()
.pollDelay(250, TimeUnit.MILLISECONDS)
.pollInterval(2, TimeUnit.SECONDS)
.alias("wait for snapshot creation")
.ignoreExceptions()
.until(() -> {
count.incrementAndGet();
GetSnapshotsResponse snapshotsResponse = snapshotClient.get(getSnapshotsRequest, DEFAULT);
SnapshotInfo snapshotInfo = snapshotsResponse.getSnapshots().get(0);
return SnapshotState.SUCCESS.equals(snapshotInfo.state());
});
return count.get();
}

// CS-SUPPRESS-SINGLE: RegexpSingleline It is not possible to use phrase "cluster manager" instead of master here
Expand Down

0 comments on commit 4b6934c

Please sign in to comment.