Skip to content

Commit

Permalink
HDDS-9432. Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
swamirishi committed Oct 18, 2023
1 parent 46cbccb commit 8dd165a
Showing 1 changed file with 37 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@
import org.apache.hadoop.ozone.om.helpers.RepeatedOmKeyInfo;
import org.apache.hadoop.ozone.om.helpers.SnapshotInfo;
import org.apache.hadoop.util.Time;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.junit.rules.TemporaryFolder;

import org.apache.hadoop.hdds.conf.OzoneConfiguration;
Expand All @@ -62,12 +64,12 @@ public class TestOMSnapshotCreateResponse {

@Rule
public TemporaryFolder folder = new TemporaryFolder();

private OMMetadataManager omMetadataManager;
private BatchOperation batchOperation;
private OzoneConfiguration ozoneConfiguration;

@Before
@BeforeEach
public void setup() throws Exception {
ozoneConfiguration = new OzoneConfiguration();
String fsPath = folder.newFolder().getAbsolutePath();
Expand All @@ -77,15 +79,16 @@ public void setup() throws Exception {
batchOperation = omMetadataManager.getStore().initBatchOperation();
}

@After
@AfterEach
public void tearDown() {
if (batchOperation != null) {
batchOperation.close();
}
}

@Test
public void testAddToDBBatch() throws Exception {
@ParameterizedTest
@ValueSource(ints = {0, 1, 5, 10, 25})
public void testAddToDBBatch(int numberOfKeys) throws Exception {
String volumeName = UUID.randomUUID().toString();
String bucketName = UUID.randomUUID().toString();
String snapshotName = UUID.randomUUID().toString();
Expand All @@ -97,15 +100,15 @@ public void testAddToDBBatch() throws Exception {
Time.now());

// confirm table is empty
Assert.assertEquals(0,
Assertions.assertEquals(0,
omMetadataManager
.countRowsInTable(omMetadataManager.getSnapshotInfoTable()));
.countRowsInTable(omMetadataManager.getSnapshotInfoTable()));

// Populate deletedTable and deletedDirectoryTable
Set<String> dtSentinelKeys =
addTestKeysToDeletedTable(volumeName, bucketName);
addTestKeysToDeletedTable(volumeName, bucketName, numberOfKeys);
Set<String> ddtSentinelKeys =
addTestKeysToDeletedDirTable(volumeName, bucketName);
addTestKeysToDeletedDirTable(volumeName, bucketName, numberOfKeys);

// commit to table
OMSnapshotCreateResponse omSnapshotCreateResponse =
Expand All @@ -114,17 +117,17 @@ public void testAddToDBBatch() throws Exception {
.setStatus(OzoneManagerProtocolProtos.Status.OK)
.setCreateSnapshotResponse(
CreateSnapshotResponse.newBuilder()
.setSnapshotInfo(snapshotInfo.getProtobuf())
.build()).build(), snapshotInfo);
.setSnapshotInfo(snapshotInfo.getProtobuf())
.build()).build(), snapshotInfo);
omSnapshotCreateResponse.addToDBBatch(omMetadataManager, batchOperation);
omMetadataManager.getStore().commitBatchOperation(batchOperation);

// Confirm snapshot directory was created
String snapshotDir = getSnapshotPath(ozoneConfiguration, snapshotInfo);
Assert.assertTrue((new File(snapshotDir)).exists());
Assertions.assertTrue((new File(snapshotDir)).exists());

// Confirm table has 1 entry
Assert.assertEquals(1, omMetadataManager
Assertions.assertEquals(1, omMetadataManager
.countRowsInTable(omMetadataManager.getSnapshotInfoTable()));

// Check contents of entry
Expand All @@ -133,17 +136,19 @@ public void testAddToDBBatch() throws Exception {
it = omMetadataManager.getSnapshotInfoTable().iterator()) {
Table.KeyValue<String, SnapshotInfo> keyValue = it.next();
storedInfo = keyValue.getValue();
Assert.assertEquals(snapshotInfo.getTableKey(), keyValue.getKey());
Assertions.assertEquals(snapshotInfo.getTableKey(), keyValue.getKey());
}
Assert.assertEquals(snapshotInfo, storedInfo);
Assertions.assertEquals(snapshotInfo, storedInfo);

// Check deletedTable and deletedDirectoryTable clean up work as expected
verifyEntriesLeftInDeletedTable(dtSentinelKeys);
verifyEntriesLeftInDeletedDirTable(ddtSentinelKeys);
}

private Set<String> addTestKeysToDeletedTable(
String volumeName, String bucketName) throws IOException {
private Set<String> addTestKeysToDeletedTable(String volumeName,
String bucketName,
int numberOfKeys)
throws IOException {

RepeatedOmKeyInfo dummyRepeatedKeyInfo = new RepeatedOmKeyInfo.Builder()
.setOmKeyInfos(new ArrayList<>()).build();
Expand Down Expand Up @@ -176,7 +181,7 @@ private Set<String> addTestKeysToDeletedTable(
}

// Add deletedTable key entries in the snapshot (bucket) scope
for (int i = 0; i < 10; i++) {
for (int i = 0; i < numberOfKeys; i++) {
String dtKey = omMetadataManager.getOzoneKey(volumeName, bucketName,
"dtkey" + i);
omMetadataManager.getDeletedTable().put(dtKey, dummyRepeatedKeyInfo);
Expand All @@ -189,12 +194,13 @@ private Set<String> addTestKeysToDeletedTable(

/**
* Populates deletedDirectoryTable for the test.
*
* @param volumeName volume name
* @param bucketName bucket name
* @return A set of DB keys
*/
private Set<String> addTestKeysToDeletedDirTable(
String volumeName, String bucketName) throws IOException {
String volumeName, String bucketName, int numberOfKeys) throws IOException {

OMSnapshotResponseTestUtil.addVolumeBucketInfoToTable(
omMetadataManager, volumeName, bucketName);
Expand All @@ -218,15 +224,15 @@ private Set<String> addTestKeysToDeletedDirTable(

char bucketIdLastChar = dbKeyPfx.charAt(offset);

String dbKeyPfxBefore = dbKeyPfx.substring(0, offset) +
String dbKeyPfxBefore = dbKeyPfx.substring(0, offset) +
(char) (bucketIdLastChar - 1) + dbKeyPfx.substring(offset);
for (int i = 0; i < 3; i++) {
String dtKey = dbKeyPfxBefore + "dir" + i;
omMetadataManager.getDeletedDirTable().put(dtKey, dummyOmKeyInfo);
sentinelKeys.add(dtKey);
}

String dbKeyPfxAfter = dbKeyPfx.substring(0, offset) +
String dbKeyPfxAfter = dbKeyPfx.substring(0, offset) +
(char) (bucketIdLastChar + 1) + dbKeyPfx.substring(offset);
for (int i = 0; i < 3; i++) {
String dtKey = dbKeyPfxAfter + "dir" + i;
Expand All @@ -235,7 +241,7 @@ private Set<String> addTestKeysToDeletedDirTable(
}

// Add key entries in the snapshot (bucket) scope
for (int i = 0; i < 10; i++) {
for (int i = 0; i < numberOfKeys; i++) {
String dtKey = dbKeyPfx + "dir" + i;
omMetadataManager.getDeletedDirTable().put(dtKey, dummyOmKeyInfo);
// These are the keys that should be deleted.
Expand All @@ -261,18 +267,18 @@ private void verifyEntriesLeftInTable(
Table<String, ?> table, Set<String> expectedKeys) throws IOException {

try (TableIterator<String, ? extends Table.KeyValue<String, ?>>
keyIter = table.iterator()) {
keyIter = table.iterator()) {
keyIter.seekToFirst();
while (keyIter.hasNext()) {
Table.KeyValue<String, ?> entry = keyIter.next();
String dbKey = entry.getKey();
Assert.assertTrue(table.getName() + " should contain key",
expectedKeys.contains(dbKey));
Assertions.assertTrue(expectedKeys.contains(dbKey),
table.getName() + " should contain key");
expectedKeys.remove(dbKey);
}
}

Assert.assertTrue(table.getName() + " is missing keys that should be there",
expectedKeys.isEmpty());
Assertions.assertTrue(expectedKeys.isEmpty(),
table.getName() + " is missing keys that should be there");
}
}

0 comments on commit 8dd165a

Please sign in to comment.