Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.test;

import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

/**
* This is a custom JUnit5 `RegisterExtension`
* we created to obtain the methond name of the executing function.
*/
public class TestName implements BeforeEachCallback {

private volatile String name;

@Override
public void beforeEach(ExtensionContext extensionContext) throws Exception {
name = extensionContext.getTestMethod().get().getName();
}

public String getMethodName() {
return this.name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.azure.integration.AzureTestConstants;
import org.apache.hadoop.io.IOUtils;

import static org.junit.Assume.assumeNotNull;
import static org.apache.hadoop.fs.azure.integration.AzureTestUtils.*;

/**
Expand All @@ -49,14 +48,14 @@ public abstract class AbstractWasbTestBase extends AbstractWasbTestWithTimeout
protected NativeAzureFileSystem fs;
protected AzureBlobStorageTestAccount testAccount;

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

@After
@AfterEach
public void tearDown() throws Exception {
describe("closing test account and filesystem");
testAccount = cleanupTestAccount(testAccount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,42 @@

package org.apache.hadoop.fs.azure;

import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.junit.rules.Timeout;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Timeout;
import org.apache.hadoop.fs.azure.integration.AzureTestConstants;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.apache.hadoop.test.TestName;

import static org.junit.jupiter.api.Assumptions.assumeTrue;

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

/**
* The name of the current method.
*/
@Rule
@RegisterExtension
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.
*/
@BeforeClass
@BeforeAll
public static void nameTestThread() {
Thread.currentThread().setName("JUnit");
}

/**
* Name the thread to the current test method.
*/
@Before
@BeforeEach
public void nameThread() {
Thread.currentThread().setName("JUnit-" + methodName.getMethodName());
}
Expand All @@ -70,4 +66,11 @@ protected int getTestTimeoutMillis() {
return AzureTestConstants.AZURE_TEST_TIMEOUT;
}

public static void assumeNotNull(Object objects) {
assumeTrue(objects != null);
}

public static void assumeNotNull(Object objects, String message) {
assumeTrue(objects != null, message);
}
}
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.Assert;
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) {
Assert.assertNotNull("null filesystem", fs);
Assert.assertNotNull("null filesystemn instance ID",
fs.getInstrumentation().getFileSystemInstanceId());
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 @@ -23,7 +23,7 @@
import java.io.OutputStream;
import java.util.Arrays;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import com.microsoft.azure.storage.OperationContext;
import com.microsoft.azure.storage.SendingRequestEvent;
import com.microsoft.azure.storage.StorageEvent;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
Expand All @@ -41,7 +41,6 @@

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;

/**
* Error handling.
Expand Down Expand Up @@ -76,7 +75,7 @@ public void testAccessUnauthorizedPublicContainer() throws Exception {
try {
FileSystem.get(noAccessPath.toUri(), new Configuration())
.open(noAccessPath);
assertTrue("Should've thrown.", false);
assertTrue(false, "Should've thrown.");
} catch (AzureException ex) {
GenericTestUtils.assertExceptionContains(
String.format(NO_ACCESS_TO_CONTAINER_MSG, account, container), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

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 java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -35,8 +34,9 @@
import org.apache.hadoop.fs.azure.AzureNativeFileSystemStore.TestHookOperationContext;
import org.apache.hadoop.fs.azure.integration.AzureTestUtils;

import org.junit.After;
import org.junit.Test;
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 All @@ -56,7 +56,7 @@
public class ITestBlobDataValidation extends AbstractWasbTestWithTimeout {
private AzureBlobStorageTestAccount testAccount;

@After
@AfterEach
public void tearDown() throws Exception {
testAccount = AzureTestUtils.cleanupTestAccount(testAccount);
}
Expand All @@ -65,21 +65,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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shoudn't need to do this. instead just capture once in setup and store

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your suggestion! I will improve it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@steveloughran Could you please review this PR again? Thank you very much!

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 +91,12 @@ private static String trim(String s, String toTrim) {
toTrim);
}

private void testStoreBlobMd5(boolean expectMd5Stored) throws Exception {
private void testStoreBlobMd5(boolean expectMd5Stored, TestInfo testInfo) throws Exception {
assumeNotNull(testAccount);
// 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 All @@ -109,7 +109,7 @@ private void testStoreBlobMd5(boolean expectMd5Stored) throws Exception {
if (expectMd5Stored) {
assertNotNull(obtainedMd5);
} else {
assertNull("Expected no MD5, found: " + obtainedMd5, obtainedMd5);
assertNull(obtainedMd5, "Expected no MD5, found: " + obtainedMd5);
}

// Mess with the content so it doesn't match the MD5.
Expand Down Expand Up @@ -137,8 +137,8 @@ private void testStoreBlobMd5(boolean expectMd5Stored) throws Exception {
}
StorageException cause = (StorageException)ex.getCause();
assertNotNull(cause);
assertEquals("Unexpected cause: " + cause,
StorageErrorCodeStrings.INVALID_MD5, cause.getErrorCode());
assertEquals(StorageErrorCodeStrings.INVALID_MD5, cause.getErrorCode(),
"Unexpected cause: " + cause);
}
}

Expand Down Expand Up @@ -192,7 +192,7 @@ private void checkObtainedMd5(String obtainedMd5) {
if (expectMd5) {
assertNotNull(obtainedMd5);
} else {
assertNull("Expected no MD5, found: " + obtainedMd5, obtainedMd5);
assertNull(obtainedMd5, "Expected no MD5, found: " + obtainedMd5);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.Arrays;
import java.util.Date;

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

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
Expand Down
Loading