Skip to content

Commit

Permalink
HADOOP-19425. Fix CheckStyle & Junit Test.
Browse files Browse the repository at this point in the history
  • Loading branch information
fanshilun committed Feb 8, 2025
1 parent a44ce7d commit b52214a
Show file tree
Hide file tree
Showing 42 changed files with 755 additions and 748 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.hadoop.conf.Configuration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -50,15 +51,15 @@ public abstract class AbstractWasbTestBase extends AbstractWasbTestWithTimeout
protected AzureBlobStorageTestAccount testAccount;

@BeforeEach
public void setUp() throws Exception {
public void setUp(TestInfo testInfo) throws Exception {
AzureBlobStorageTestAccount account = createTestAccount();
assumeNotNull("test account", account);
bindToTestAccount(account);
}

@AfterEach
public void tearDown() throws Exception {
describe("closing test account and filesystem");
public void tearDown(TestInfo info) throws Exception {
describe(info,"closing test account and filesystem");
testAccount = cleanupTestAccount(testAccount);
IOUtils.closeStream(fs);
fs = null;
Expand Down Expand Up @@ -146,31 +147,37 @@ protected Path path(String filepath) throws IOException {
/**
* Return a path bonded to this method name, unique to this fork during
* parallel execution.
* @param testInfo Provides information about the currently executing test method.
* This can include details such as the name of the test method, display name.
* @return a method name unique to (fork, method).
* @throws IOException IO problems
*/
protected Path methodPath() throws IOException {
return path(methodName.getMethodName());
protected Path methodPath(TestInfo testInfo) throws IOException {
return path(testInfo.getDisplayName());
}

/**
* Return a blob path bonded to this method name, unique to this fork during
* parallel execution.
* @param testInfo Provides information about the currently executing test method.
* This can include details such as the name of the test method, display name.
* @return a method name unique to (fork, method).
* @throws IOException IO problems
*/
protected Path methodBlobPath() throws IOException {
return blobPath(methodName.getMethodName());
protected Path methodBlobPath(TestInfo testInfo) throws IOException {
return blobPath(testInfo.getDisplayName());
}

/**
* Describe a test in the logs.
* @param testInfo Provides information about the currently executing test method.
* This can include details such as the name of the test method, display name.
* @param text text to print
* @param args arguments to format in the printing
*/
protected void describe(String text, Object... args) {
protected void describe(TestInfo testInfo, String text, Object... args) {
LOG.info("\n\n{}: {}\n",
methodName.getMethodName(),
testInfo.getDisplayName(),
String.format(text, args));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,20 @@
package org.apache.hadoop.fs.azure;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.junit.rules.Timeout;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.Timeout;

import org.apache.hadoop.fs.azure.integration.AzureTestConstants;

/**
* Base class for any Wasb test with timeouts & named threads.
* This class does not attempt to bind to Azure.
*/
@Timeout(AzureTestConstants.AZURE_TEST_TIMEOUT)
public class AbstractWasbTestWithTimeout extends Assertions {

/**
* The name of the current method.
*/
@Rule
public TestName methodName = new TestName();
/**
* Set the timeout for every test.
* This is driven by the value returned by {@link #getTestTimeoutMillis()}.
*/
@Rule
public Timeout testTimeout = new Timeout(getTestTimeoutMillis());

/**
* Name the junit thread for the class. This will overridden
* before the individual test methods are run.
Expand All @@ -58,8 +46,8 @@ public static void nameTestThread() {
* Name the thread to the current test method.
*/
@BeforeEach
public void nameThread() {
Thread.currentThread().setName("JUnit-" + methodName.getMethodName());
public void nameThread(TestInfo testInfo) {
Thread.currentThread().setName("JUnit-" + testInfo.getDisplayName());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.blob.*;
import com.microsoft.azure.storage.core.Base64;
import org.junit.jupiter.api.Assertions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -50,6 +49,7 @@
import static org.apache.hadoop.fs.azure.AzureNativeFileSystemStore.KEY_USE_LOCAL_SAS_KEY_MODE;
import static org.apache.hadoop.fs.azure.AzureNativeFileSystemStore.KEY_USE_SECURE_MODE;
import static org.apache.hadoop.fs.azure.integration.AzureTestUtils.verifyWasbAccountNameInConfig;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* Helper class to create WASB file systems backed by either a mock in-memory
Expand Down Expand Up @@ -212,9 +212,9 @@ public Number getLatestMetricValue(String metricName, Number defaultValue)
* @return
*/
private boolean wasGeneratedByMe(MetricsRecord currentRecord) {
Assertions.assertNotNull(fs, "null filesystem");
Assertions.assertNotNull(
fs.getInstrumentation().getFileSystemInstanceId(), "null filesystemn instance ID");
assertNotNull(fs, "null filesystem");
assertNotNull(fs.getInstrumentation().getFileSystemInstanceId(),
"null filesystemn instance ID");
String myFsId = fs.getInstrumentation().getFileSystemInstanceId().toString();
for (MetricsTag currentTag : currentRecord.tags()) {
if (currentTag.name().equalsIgnoreCase("wasbFileSystemId")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

import static org.apache.hadoop.fs.azure.AzureNativeFileSystemStore.NO_ACCESS_TO_CONTAINER_MSG;
import static org.apache.hadoop.test.LambdaTestUtils.intercept;
import static org.junit.Assume.assumeNotNull;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

/**
* Error handling.
Expand Down Expand Up @@ -157,7 +157,7 @@ public void testTransientErrorOnDelete() throws Exception {
// Need to do this test against a live storage account
AzureBlobStorageTestAccount testAccount =
AzureBlobStorageTestAccount.create();
assumeNotNull(testAccount);
assumeTrue(testAccount != null);
try {
NativeAzureFileSystem fs = testAccount.getFileSystem();
injectTransientError(fs, new ConnectionRecognizer() {
Expand Down Expand Up @@ -200,7 +200,7 @@ public void testTransientErrorOnCommitBlockList() throws Exception {
// Need to do this test against a live storage account
AzureBlobStorageTestAccount testAccount =
AzureBlobStorageTestAccount.create();
assumeNotNull(testAccount);
assumeTrue(testAccount != null);
try {
NativeAzureFileSystem fs = testAccount.getFileSystem();
injectTransientError(fs, new ConnectionRecognizer() {
Expand All @@ -224,7 +224,7 @@ public void testTransientErrorOnRead() throws Exception {
// Need to do this test against a live storage account
AzureBlobStorageTestAccount testAccount =
AzureBlobStorageTestAccount.create();
assumeNotNull(testAccount);
assumeTrue(testAccount != null);
try {
NativeAzureFileSystem fs = testAccount.getFileSystem();
Path testFile = new Path("/a/b");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import static org.apache.hadoop.fs.azure.AzureNativeFileSystemStore.KEY_CHECK_BLOCK_MD5;
import static org.apache.hadoop.fs.azure.AzureNativeFileSystemStore.KEY_STORE_BLOB_MD5;
import static org.junit.Assume.assumeNotNull;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -37,6 +37,7 @@

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

import com.microsoft.azure.storage.Constants;
import com.microsoft.azure.storage.OperationContext;
Expand Down Expand Up @@ -65,21 +66,21 @@ public void tearDown() throws Exception {
* Test that by default we don't store the blob-level MD5.
*/
@Test
public void testBlobMd5StoreOffByDefault() throws Exception {
public void testBlobMd5StoreOffByDefault(TestInfo testInfo) throws Exception {
testAccount = AzureBlobStorageTestAccount.create();
testStoreBlobMd5(false);
testStoreBlobMd5(false, testInfo);
}

/**
* Test that we get blob-level MD5 storage and validation if we specify that
* in the configuration.
*/
@Test
public void testStoreBlobMd5() throws Exception {
public void testStoreBlobMd5(TestInfo testInfo) throws Exception {
Configuration conf = new Configuration();
conf.setBoolean(KEY_STORE_BLOB_MD5, true);
testAccount = AzureBlobStorageTestAccount.create(conf);
testStoreBlobMd5(true);
testStoreBlobMd5(true, testInfo);
}

/**
Expand All @@ -91,12 +92,12 @@ private static String trim(String s, String toTrim) {
toTrim);
}

private void testStoreBlobMd5(boolean expectMd5Stored) throws Exception {
assumeNotNull(testAccount);
private void testStoreBlobMd5(boolean expectMd5Stored, TestInfo testInfo) throws Exception {
assumeTrue(testAccount != null);
// Write a test file.
NativeAzureFileSystem fs = testAccount.getFileSystem();
Path testFilePath = AzureTestUtils.pathForTests(fs,
methodName.getMethodName());
testInfo.getDisplayName());
String testFileKey = trim(testFilePath.toUri().getPath(), "/");
OutputStream outStream = fs.create(testFilePath);
outStream.write(new byte[] { 5, 15 });
Expand Down Expand Up @@ -211,7 +212,7 @@ private static boolean isGetRange(HttpURLConnection connection) {

private void testCheckBlockMd5(final boolean expectMd5Checked)
throws Exception {
assumeNotNull(testAccount);
assumeTrue(testAccount != null);
Path testFilePath = new Path("/testFile");

// Add a hook to check that for GET/PUT requests we set/don't set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
import java.util.Random;
import java.util.concurrent.Callable;

import org.junit.FixMethodOrder;
import org.junit.jupiter.api.Test;
import org.junit.runners.MethodSorters;
import org.junit.jupiter.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -53,7 +51,7 @@
* (KEY_INPUT_STREAM_VERSION=1) and the new
* <code>BlockBlobInputStream</code> (KEY_INPUT_STREAM_VERSION=2).
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@TestMethodOrder(MethodOrderer.Alphanumeric.class)

public class ITestBlockBlobInputStream extends AbstractAzureScaleTest {
private static final Logger LOG = LoggerFactory.getLogger(
Expand All @@ -73,9 +71,10 @@ public class ITestBlockBlobInputStream extends AbstractAzureScaleTest {
private FileStatus testFileStatus;
private Path hugefile;

@BeforeEach
@Override
public void setUp() throws Exception {
super.setUp();
public void setUp(TestInfo testInfo) throws Exception {
super.setUp(testInfo);
Configuration conf = new Configuration();
conf.setInt(AzureNativeFileSystemStore.KEY_INPUT_STREAM_VERSION, 1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

package org.apache.hadoop.fs.azure;

import static org.junit.Assume.assumeNotNull;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.io.FileNotFoundException;
import java.util.EnumSet;
Expand All @@ -32,7 +33,6 @@
import org.apache.hadoop.test.LambdaTestUtils;

import org.junit.jupiter.api.AfterEach;
import org.junit.Assume;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -61,7 +61,7 @@ public void setMode() {
@Test
public void testContainerExistAfterDoesNotExist() throws Exception {
testAccount = blobStorageTestAccount();
assumeNotNull(testAccount);
assumeTrue(testAccount != null);
CloudBlobContainer container = testAccount.getRealContainer();
FileSystem fs = testAccount.getFileSystem();

Expand Down Expand Up @@ -101,7 +101,7 @@ protected AzureBlobStorageTestAccount blobStorageTestAccount()
@Test
public void testContainerCreateAfterDoesNotExist() throws Exception {
testAccount = blobStorageTestAccount();
assumeNotNull(testAccount);
assumeTrue(testAccount != null);
CloudBlobContainer container = testAccount.getRealContainer();
FileSystem fs = testAccount.getFileSystem();

Expand All @@ -127,7 +127,7 @@ public void testContainerCreateAfterDoesNotExist() throws Exception {
@Test
public void testContainerCreateOnWrite() throws Exception {
testAccount = blobStorageTestAccount();
assumeNotNull(testAccount);
assumeTrue(testAccount != null);
CloudBlobContainer container = testAccount.getRealContainer();
FileSystem fs = testAccount.getFileSystem();

Expand Down Expand Up @@ -170,10 +170,10 @@ public String call() throws Exception {
@Test
public void testContainerChecksWithSas() throws Exception {

Assume.assumeFalse(runningInSASMode);
assumeFalse(runningInSASMode);
testAccount = AzureBlobStorageTestAccount.create("",
EnumSet.of(CreateOptions.UseSas));
assumeNotNull(testAccount);
assumeTrue(testAccount != null);
CloudBlobContainer container = testAccount.getRealContainer();
FileSystem fs = testAccount.getFileSystem();

Expand Down
Loading

0 comments on commit b52214a

Please sign in to comment.