Skip to content

Commit

Permalink
HDDS-9954. Simplify assertions in hadoop-ozone unit tests (#5818)
Browse files Browse the repository at this point in the history
  • Loading branch information
adoroszlai authored Dec 19, 2023
1 parent fdd616e commit 3bd5c61
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class TestOzoneECClient {
private String volumeName = UUID.randomUUID().toString();
private String bucketName = UUID.randomUUID().toString();
private byte[][] inputChunks = new byte[dataBlocks][chunkSize];
private final XceiverClientFactory factoryStub =
private final MockXceiverClientFactory factoryStub =
new MockXceiverClientFactory();
private OzoneConfiguration conf = createConfiguration();
private MultiNodePipelineBlockAllocator allocator =
Expand Down Expand Up @@ -148,7 +148,7 @@ public void testPutECKeyAndCheckDNStoredData() throws IOException {
OzoneKey key = bucket.getKey(keyName);
Assertions.assertEquals(keyName, key.getName());
Map<DatanodeDetails, MockDatanodeStorage> storages =
((MockXceiverClientFactory) factoryStub).getStorages();
factoryStub.getStorages();
DatanodeDetails[] dnDetails =
storages.keySet().toArray(new DatanodeDetails[storages.size()]);
Arrays.sort(dnDetails);
Expand Down Expand Up @@ -177,7 +177,7 @@ public void testPutECKeyAndCheckParityData() throws IOException {
OzoneKey key = bucket.getKey(keyName);
Assertions.assertEquals(keyName, key.getName());
Map<DatanodeDetails, MockDatanodeStorage> storages =
((MockXceiverClientFactory) factoryStub).getStorages();
factoryStub.getStorages();
DatanodeDetails[] dnDetails =
storages.keySet().toArray(new DatanodeDetails[storages.size()]);
Arrays.sort(dnDetails);
Expand All @@ -203,7 +203,7 @@ public void testPutECKeyAndReadContent() throws IOException {
byte[] fileContent = new byte[chunkSize];
for (int i = 0; i < dataBlocks; i++) {
Assertions.assertEquals(inputChunks[i].length, is.read(fileContent));
Assertions.assertTrue(Arrays.equals(inputChunks[i], fileContent));
Assertions.assertArrayEquals(fileContent, inputChunks[i]);
}
// A further read should give EOF
Assertions.assertEquals(-1, is.read(fileContent));
Expand Down Expand Up @@ -325,7 +325,7 @@ public void testSmallerThanChunkSize() throws IOException {
.getKeyLocationListList().get(0);

Map<DatanodeDetails, MockDatanodeStorage> storages =
((MockXceiverClientFactory) factoryStub).getStorages();
factoryStub.getStorages();
OzoneManagerProtocolProtos.KeyLocation keyLocations =
blockList.getKeyLocations(0);

Expand Down Expand Up @@ -380,11 +380,11 @@ public void testPutBlockHasBlockGroupLen() throws IOException {
byte[] fileContent = new byte[chunkSize];
for (int i = 0; i < dataBlocks; i++) {
Assertions.assertEquals(inputChunks[i].length, is.read(fileContent));
Assertions.assertTrue(Arrays.equals(inputChunks[i], fileContent));
Assertions.assertArrayEquals(fileContent, inputChunks[i]);
}

Map<DatanodeDetails, MockDatanodeStorage> storages =
((MockXceiverClientFactory) factoryStub).getStorages();
factoryStub.getStorages();
OzoneManagerProtocolProtos.KeyLocationList blockList =
transportStub.getKeys().get(volumeName).get(bucketName).get(keyName).
getKeyLocationListList().get(0);
Expand Down Expand Up @@ -561,11 +561,10 @@ public void testPartialStripeWithPartialLastChunk()
byte[] fileContent = new byte[chunkSize];
for (int i = 0; i < 2; i++) {
Assertions.assertEquals(inputChunks[i].length, is.read(fileContent));
Assertions.assertTrue(Arrays.equals(inputChunks[i], fileContent));
Assertions.assertArrayEquals(inputChunks[i], fileContent);
}
Assertions.assertEquals(lastChunk.length, is.read(fileContent));
Assertions.assertTrue(Arrays.equals(lastChunk,
Arrays.copyOf(fileContent, lastChunk.length)));
Assertions.assertArrayEquals(lastChunk, Arrays.copyOf(fileContent, lastChunk.length));
// A further read should give EOF
Assertions.assertEquals(-1, is.read(fileContent));
}
Expand Down Expand Up @@ -602,8 +601,7 @@ public void test10D4PConfigWithPartialStripe()
try (OzoneInputStream is = bucket.readKey(keyName)) {
byte[] fileContent = new byte[chunkSize];
Assertions.assertEquals(inSize, is.read(fileContent));
Assertions.assertTrue(Arrays.equals(partialChunk,
Arrays.copyOf(fileContent, inSize)));
Assertions.assertArrayEquals(partialChunk, Arrays.copyOf(fileContent, inSize));
}
}

Expand Down Expand Up @@ -648,8 +646,7 @@ public void testStripeWriteRetriesOn2Failures() throws Exception {
nodesIndexesToMarkFailure);
// It should have used 3rd block group also. So, total initialized nodes
// count should be clusterSize.
Assertions.assertTrue(((MockXceiverClientFactory) factoryStub)
.getStorages().size() == clusterSize);
Assertions.assertEquals(clusterSize, factoryStub.getStorages().size());
}

@Test
Expand All @@ -669,8 +666,7 @@ public void testStripeWriteRetriesOn3Failures() throws Exception {
nodesIndexesToMarkFailure);
// It should have used 3rd block group also. So, total initialized nodes
// count should be clusterSize.
Assertions.assertTrue(((MockXceiverClientFactory) factoryStub)
.getStorages().size() == clusterSize);
Assertions.assertEquals(clusterSize, factoryStub.getStorages().size());
}

// The mocked impl throws IllegalStateException when there are not enough
Expand Down Expand Up @@ -742,8 +738,7 @@ public void testStripeWriteRetriesOnFailures(OzoneConfiguration con,
out.write(inputChunks[i]);
}
waitForFlushingThreadToFinish((ECKeyOutputStream) out.getOutputStream());
Assertions.assertTrue(
((MockXceiverClientFactory) factoryStub).getStorages().size() == 5);
Assertions.assertEquals(5, factoryStub.getStorages().size());
List<DatanodeDetails> failedDNs = new ArrayList<>();
List<HddsProtos.DatanodeDetailsProto> dns = blkAllocator.getClusterDns();

Expand All @@ -753,7 +748,7 @@ public void testStripeWriteRetriesOnFailures(OzoneConfiguration con,
}

// First let's set storage as bad
((MockXceiverClientFactory) factoryStub).setFailedStorages(failedDNs);
factoryStub.setFailedStorages(failedDNs);

// Writer should be able to write by using 3rd block group.
for (int i = 0; i < numChunksToWriteAfterFailure; i++) {
Expand All @@ -770,15 +765,11 @@ public void testStripeWriteRetriesOnFailures(OzoneConfiguration con,
byte[] fileContent = new byte[chunkSize];
for (int i = 0; i < dataBlocks; i++) {
Assertions.assertEquals(inputChunks[i].length, is.read(fileContent));
Assertions.assertTrue(Arrays.equals(inputChunks[i], fileContent),
"Expected: " + new String(inputChunks[i],
UTF_8) + " \n " + "Actual: " + new String(fileContent, UTF_8));
Assertions.assertArrayEquals(inputChunks[i], fileContent);
}
for (int i = 0; i < numChunksToWriteAfterFailure; i++) {
Assertions.assertEquals(inputChunks[i].length, is.read(fileContent));
Assertions.assertTrue(Arrays.equals(inputChunks[i], fileContent),
"Expected: " + new String(inputChunks[i],
UTF_8) + " \n " + "Actual: " + new String(fileContent, UTF_8));
Assertions.assertArrayEquals(inputChunks[i], fileContent);
}
}
}
Expand Down Expand Up @@ -808,7 +799,7 @@ public void testNodeFailuresWhileWriting(int[] nodesIndexesToMarkFailure,
}

// First let's set storage as bad
((MockXceiverClientFactory) factoryStub).setFailedStorages(failedDNs);
factoryStub.setFailedStorages(failedDNs);

for (int i = 0; i < numChunksToWriteAfterFailure; i++) {
out.write(inputChunks[i % dataBlocks]);
Expand All @@ -825,17 +816,12 @@ public void testNodeFailuresWhileWriting(int[] nodesIndexesToMarkFailure,
byte[] fileContent = new byte[chunkSize];
for (int i = 0; i < dataBlocks; i++) {
Assertions.assertEquals(inputChunks[i].length, is.read(fileContent));
Assertions.assertTrue(Arrays.equals(inputChunks[i], fileContent),
"Expected: " + new String(inputChunks[i],
UTF_8) + " \n " + "Actual: " + new String(fileContent, UTF_8));
Assertions.assertArrayEquals(inputChunks[i], fileContent);
}
for (int i = 0; i < numChunksToWriteAfterFailure; i++) {
Assertions.assertEquals(inputChunks[i % dataBlocks].length,
is.read(fileContent));
Assertions.assertTrue(
Arrays.equals(inputChunks[i % dataBlocks], fileContent),
"Expected: " + new String(inputChunks[i % dataBlocks],
UTF_8) + " \n " + "Actual: " + new String(fileContent, UTF_8));
Assertions.assertArrayEquals(inputChunks[i % dataBlocks], fileContent);
}
}
}
Expand Down Expand Up @@ -888,14 +874,14 @@ private void testExcludeFailedDN(IntStream failedDNIndex,
List<DatanodeDetails> closedDNs = closedDNIndex
.mapToObj(i -> DatanodeDetails.getFromProtoBuf(dns.get(i)))
.collect(Collectors.toList());
((MockXceiverClientFactory) factoryStub).mockStorageFailure(closedDNs,
factoryStub.mockStorageFailure(closedDNs,
new ContainerNotOpenException("Mocked"));

// Then let's mark failed datanodes
List<DatanodeDetails> failedDNs = failedDNIndex
.mapToObj(i -> DatanodeDetails.getFromProtoBuf(dns.get(i)))
.collect(Collectors.toList());
((MockXceiverClientFactory) factoryStub).setFailedStorages(failedDNs);
factoryStub.setFailedStorages(failedDNs);

for (int i = 0; i < dataBlocks; i++) {
out.write(inputChunks[i % dataBlocks]);
Expand Down Expand Up @@ -955,7 +941,7 @@ public void testLargeWriteOfMultipleStripesWithStripeFailure()
}

// First let's set storage as bad
((MockXceiverClientFactory) factoryStub).setFailedStorages(failedDNs);
factoryStub.setFailedStorages(failedDNs);

for (int i = 0; i < numChunksToWriteAfterFailure; i++) {
out.write(inputChunks[i % dataBlocks]);
Expand All @@ -974,18 +960,12 @@ public void testLargeWriteOfMultipleStripesWithStripeFailure()
for (int i = 0; i < dataBlocks * numFullStripesBeforeFailure; i++) {
Assertions.assertEquals(inputChunks[i % dataBlocks].length,
is.read(fileContent));
Assertions.assertTrue(
Arrays.equals(inputChunks[i % dataBlocks], fileContent),
"Expected: " + new String(inputChunks[i % dataBlocks], UTF_8)
+ " \n " + "Actual: " + new String(fileContent, UTF_8));
Assertions.assertArrayEquals(inputChunks[i % dataBlocks], fileContent);
}
for (int i = 0; i < numChunksToWriteAfterFailure; i++) {
Assertions.assertEquals(inputChunks[i % dataBlocks].length,
is.read(fileContent));
Assertions.assertTrue(
Arrays.equals(inputChunks[i % dataBlocks], fileContent),
"Expected: " + new String(inputChunks[i % dataBlocks],
UTF_8) + " \n " + "Actual: " + new String(fileContent, UTF_8));
Assertions.assertArrayEquals(inputChunks[i % dataBlocks], fileContent);
}
}
}
Expand Down Expand Up @@ -1033,7 +1013,7 @@ public void testPartialStripeWithPartialChunkRetry()
}

// First let's set storage as bad
((MockXceiverClientFactory) factoryStub).setFailedStorages(failedDNs);
factoryStub.setFailedStorages(failedDNs);

}

Expand All @@ -1042,16 +1022,13 @@ public void testPartialStripeWithPartialChunkRetry()
for (int i = 0; i < numFullChunks; i++) {
Assertions.assertEquals(inputChunks[i % dataBlocks].length,
is.read(fileContent));
Assertions.assertTrue(
Arrays.equals(inputChunks[i % dataBlocks], fileContent),
"Expected: " + new String(inputChunks[i % dataBlocks],
UTF_8) + " \n " + "Actual: " + new String(fileContent, UTF_8));
Assertions.assertArrayEquals(inputChunks[i % dataBlocks], fileContent);
}

byte[] partialChunkToRead = new byte[partialChunkSize];
Assertions
.assertEquals(partialChunkToRead.length, is.read(partialChunkToRead));
Assertions.assertTrue(Arrays.equals(partialChunk, partialChunkToRead));
Assertions.assertArrayEquals(partialChunk, partialChunkToRead);

Assertions.assertEquals(-1, is.read(partialChunkToRead));
}
Expand Down Expand Up @@ -1126,7 +1103,7 @@ public void testDiscardPreAllocatedBlocksPreventRetryExceeds()
.getFromProtoBuf(dns.get(nodesIndexesToMarkFailure[j])));
}
// First let's set storage as bad
((MockXceiverClientFactory) factoryStub).setFailedStorages(failedDNs);
factoryStub.setFailedStorages(failedDNs);

// Writes that will retry due to failed DNs
try {
Expand All @@ -1151,9 +1128,7 @@ public void testDiscardPreAllocatedBlocksPreventRetryExceeds()
for (int i = 0; i < dataBlocks * numStripesTotal; i++) {
Assertions.assertEquals(inputChunks[i % dataBlocks].length,
is.read(fileContent));
Assertions.assertArrayEquals(inputChunks[i % dataBlocks], fileContent,
"Expected: " + new String(inputChunks[i % dataBlocks], UTF_8)
+ " \n " + "Actual: " + new String(fileContent, UTF_8));
Assertions.assertArrayEquals(inputChunks[i % dataBlocks], fileContent);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
import static org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.BUCKET_NOT_FOUND;
import static org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.VOLUME_NOT_FOUND;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.params.provider.Arguments.arguments;
Expand Down Expand Up @@ -282,8 +282,7 @@ public void testListBuckets() throws Exception {
for (OmBucketInfo omBucketInfo : omBucketInfoList) {
assertTrue(omBucketInfo.getBucketName().startsWith(
prefixBucketNameWithOzoneOwner));
assertFalse(omBucketInfo.getBucketName().equals(
prefixBucketNameWithOzoneOwner + 10));
assertNotEquals(prefixBucketNameWithOzoneOwner + 10, omBucketInfo.getBucketName());
}


Expand Down Expand Up @@ -426,8 +425,7 @@ public void testListKeys() throws Exception {
for (OmKeyInfo omKeyInfo : omKeyInfoList) {
assertTrue(omKeyInfo.getKeyName().startsWith(
prefixKeyA));
assertFalse(omKeyInfo.getBucketName().equals(
prefixKeyA + 38));
assertNotEquals(prefixKeyA + 38, omKeyInfo.getBucketName());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,17 +348,17 @@ public void testS3SecretCacheSizePostDoubleBufferFlush() throws IOException {

S3SecretCache cache = secretManager.cache();
// Check if all the three secrets are cached.
Assertions.assertTrue(cache.get(userPrincipalId1) != null);
Assertions.assertTrue(cache.get(userPrincipalId2) != null);
Assertions.assertTrue(cache.get(userPrincipalId3) != null);
Assertions.assertNotNull(cache.get(userPrincipalId1));
Assertions.assertNotNull(cache.get(userPrincipalId2));
Assertions.assertNotNull(cache.get(userPrincipalId3));

// Flush the current buffer.
doubleBuffer.flushCurrentBuffer();

// Check if all the three secrets are cleared from the cache.
Assertions.assertTrue(cache.get(userPrincipalId3) == null);
Assertions.assertTrue(cache.get(userPrincipalId2) == null);
Assertions.assertTrue(cache.get(userPrincipalId1) == null);
Assertions.assertNull(cache.get(userPrincipalId3));
Assertions.assertNull(cache.get(userPrincipalId2));
Assertions.assertNull(cache.get(userPrincipalId1));
} finally {
// cleanup metrics
OzoneManagerDoubleBufferMetrics metrics =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void testContainerKeyPrefixCodec() throws IOException {

Codec<ContainerKeyPrefix> codec = ContainerKeyPrefixCodec.get();
byte[] persistedFormat = codec.toPersistedFormat(containerKeyPrefix);
Assertions.assertTrue(persistedFormat != null);
Assertions.assertNotNull(persistedFormat);
ContainerKeyPrefix fromPersistedFormat =
codec.fromPersistedFormat(persistedFormat);
Assertions.assertEquals(containerKeyPrefix, fromPersistedFormat);
Expand All @@ -50,7 +50,7 @@ public void testIntegerCodec() throws IOException {
Integer i = 1000;
Codec<Integer> codec = IntegerCodec.get();
byte[] persistedFormat = codec.toPersistedFormat(i);
Assertions.assertTrue(persistedFormat != null);
Assertions.assertNotNull(persistedFormat);
Integer fromPersistedFormat =
codec.fromPersistedFormat(persistedFormat);
Assertions.assertEquals(i, fromPersistedFormat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,14 @@ public void testGetBlocksPendingDeletionPrevKeyParam() throws Exception {
containerStateBlockInfoListMap =
(Map<String, List<ContainerBlocksInfoWrapper>>)
blocksPendingDeletion.getEntity();
Assertions.assertTrue(containerStateBlockInfoListMap.size() == 0);
Assertions.assertEquals(0, containerStateBlockInfoListMap.size());

blocksPendingDeletion =
blocksEndPoint.getBlocksPendingDeletion(1, 4);
containerStateBlockInfoListMap =
(Map<String, List<ContainerBlocksInfoWrapper>>)
blocksPendingDeletion.getEntity();
Assertions.assertTrue(containerStateBlockInfoListMap.size() == 0);
Assertions.assertEquals(0, containerStateBlockInfoListMap.size());
}

protected ContainerWithPipeline getTestContainer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void testNoDisabledFeatures() {
List<FeatureProvider.Feature> allDisabledFeatures =
(List<FeatureProvider.Feature>) disabledFeatures.getEntity();
Assertions.assertNotNull(allDisabledFeatures);
Assertions.assertTrue(allDisabledFeatures.size() == 0);
Assertions.assertEquals(0, allDisabledFeatures.size());
}

@Test
Expand Down Expand Up @@ -141,6 +141,6 @@ public void testGetHeatMapNotInDisabledFeaturesListWhenHeatMapFlagIsTrue() {
List<FeatureProvider.Feature> allDisabledFeatures =
(List<FeatureProvider.Feature>) disabledFeatures.getEntity();
Assertions.assertNotNull(allDisabledFeatures);
Assertions.assertTrue(allDisabledFeatures.size() == 0);
Assertions.assertEquals(0, allDisabledFeatures.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import static org.apache.hadoop.ozone.OzoneAcl.AclScope.ACCESS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

/**
* This is a utility class for common code for test cases.
Expand Down Expand Up @@ -185,8 +186,8 @@ public void testNSSummaryBasicInfoNoPath(
NamespaceSummaryResponse invalidObj =
(NamespaceSummaryResponse) invalidResponse.getEntity();
assertEquals(ResponseStatus.PATH_NOT_FOUND, invalidObj.getStatus());
assertEquals(null, invalidObj.getCountStats());
assertEquals(null, invalidObj.getObjectDBInfo());
assertNull(invalidObj.getCountStats());
assertNull(invalidObj.getObjectDBInfo());
}

public void testNSSummaryBasicInfoKey(
Expand Down
Loading

0 comments on commit 3bd5c61

Please sign in to comment.