Skip to content

Commit 54b9698

Browse files
committed
exception message as a constant; test refactor
1 parent 60853e0 commit 54b9698

File tree

3 files changed

+39
-35
lines changed

3 files changed

+39
-35
lines changed

hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/AzureBlobFileSystem.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
import static org.apache.hadoop.fs.CommonConfigurationKeys.IOSTATISTICS_LOGGING_LEVEL_DEFAULT;
117117
import static org.apache.hadoop.fs.Options.OpenFileOptions.FS_OPTION_OPENFILE_STANDARD_OPTIONS;
118118
import static org.apache.hadoop.fs.azurebfs.AbfsStatistic.*;
119+
import static org.apache.hadoop.fs.azurebfs.constants.AbfsHttpConstants.FS_INIT_FAILED_CPK_CONFIG_IN_NON_HNS_ACCOUNT;
119120
import static org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.DATA_BLOCKS_BUFFER;
120121
import static org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_BLOCK_UPLOAD_ACTIVE_BLOCKS;
121122
import static org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_BLOCK_UPLOAD_BUFFER_DIR;
@@ -236,7 +237,7 @@ public void initialize(URI uri, Configuration configuration)
236237
*/
237238
close();
238239
throw new PathIOException(uri.getPath(),
239-
"Non HNS account can not have CPK configs enabled.");
240+
FS_INIT_FAILED_CPK_CONFIG_IN_NON_HNS_ACCOUNT);
240241
}
241242

242243
LOG.trace("Initiate check for delegation token manager");

hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/constants/AbfsHttpConstants.java

+3
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,8 @@ public static ApiVersion getCurrentVersion() {
165165
*/
166166
public static final Integer HTTP_STATUS_CATEGORY_QUOTIENT = 100;
167167

168+
public static final String FS_INIT_FAILED_CPK_CONFIG_IN_NON_HNS_ACCOUNT =
169+
"Non HNS account can not have CPK configs enabled.";
170+
168171
private AbfsHttpConstants() {}
169172
}

hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/ITestAbfsCustomEncryption.java

+34-34
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.hadoop.fs.azurebfs.services.AbfsClientUtils;
3636
import org.apache.hadoop.fs.azurebfs.utils.TracingContext;
3737
import org.assertj.core.api.Assertions;
38+
import org.assertj.core.api.Assumptions;
3839
import org.junit.Test;
3940
import org.junit.runner.RunWith;
4041
import org.junit.runners.Parameterized;
@@ -59,6 +60,7 @@
5960
import org.apache.hadoop.test.LambdaTestUtils;
6061
import org.apache.hadoop.util.Lists;
6162

63+
import static org.apache.hadoop.fs.azurebfs.constants.AbfsHttpConstants.FS_INIT_FAILED_CPK_CONFIG_IN_NON_HNS_ACCOUNT;
6264
import static org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_ENCRYPTION_CONTEXT_PROVIDER_TYPE;
6365
import static org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_ENCRYPTION_ENCODED_CLIENT_PROVIDED_KEY;
6466
import static org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_ENCRYPTION_ENCODED_CLIENT_PROVIDED_KEY_SHA;
@@ -177,36 +179,11 @@ public ITestAbfsCustomEncryption() throws Exception {
177179

178180
@Test
179181
public void testCustomEncryptionCombinations() throws Exception {
180-
final AzureBlobFileSystem fs;
181-
try {
182-
fs = getOrCreateFS();
183-
} catch (PathIOException ex) {
184-
if (ex.getMessage()
185-
.contains("Non HNS account can not have CPK configs enabled.")) {
186-
return;
187-
} else {
188-
throw ex;
189-
}
190-
}
182+
AzureBlobFileSystem fs = getOrCreateFS();
191183
Path testPath = path("/testFile");
192184
String relativePath = fs.getAbfsStore().getRelativePath(testPath);
193-
final MockEncryptionContextProvider ecp;
194-
try {
195-
ecp = (MockEncryptionContextProvider) createEncryptedFile(testPath);
196-
} catch (PathIOException ex) {
197-
if (ex.getMessage()
198-
.contains("Non HNS account can not have CPK configs enabled.")) {
199-
return;
200-
} else {
201-
throw ex;
202-
}
203-
}
204-
Assertions.assertThat(
205-
getConfiguration().getBoolean(FS_AZURE_TEST_NAMESPACE_ENABLED_ACCOUNT,
206-
false))
207-
.describedAs(
208-
"Encryption tests should run only on namespace enabled account")
209-
.isTrue();
185+
MockEncryptionContextProvider ecp
186+
= (MockEncryptionContextProvider) createEncryptedFile(testPath);
210187
AbfsRestOperation op = callOperation(fs, new Path(relativePath), ecp);
211188
if (op == null) {
212189
return;
@@ -396,9 +373,7 @@ private AzureBlobFileSystem getECProviderEnabledFS() throws Exception {
396373
+ getAccountName());
397374
configuration.unset(FS_AZURE_ENCRYPTION_ENCODED_CLIENT_PROVIDED_KEY_SHA + "."
398375
+ getAccountName());
399-
AzureBlobFileSystem fs = (AzureBlobFileSystem) FileSystem.newInstance(configuration);
400-
fileSystemsOpenedInTest.add(fs);
401-
return fs;
376+
return getAzureBlobFileSystem(configuration);
402377
}
403378

404379
private AzureBlobFileSystem getCPKEnabledFS() throws IOException {
@@ -411,9 +386,34 @@ private AzureBlobFileSystem getCPKEnabledFS() throws IOException {
411386
conf.set(FS_AZURE_ENCRYPTION_ENCODED_CLIENT_PROVIDED_KEY_SHA + "."
412387
+ getAccountName(), cpkEncodedSHA);
413388
conf.unset(FS_AZURE_ENCRYPTION_CONTEXT_PROVIDER_TYPE);
414-
AzureBlobFileSystem fs = (AzureBlobFileSystem) FileSystem.newInstance(conf);
415-
fileSystemsOpenedInTest.add(fs);
416-
return fs;
389+
return getAzureBlobFileSystem(conf);
390+
}
391+
392+
private AzureBlobFileSystem getAzureBlobFileSystem(final Configuration conf) {
393+
try {
394+
AzureBlobFileSystem fs = (AzureBlobFileSystem) FileSystem.newInstance(
395+
conf);
396+
fileSystemsOpenedInTest.add(fs);
397+
Assertions.assertThat(
398+
getConfiguration().getBoolean(FS_AZURE_TEST_NAMESPACE_ENABLED_ACCOUNT,
399+
false))
400+
.describedAs("Encryption tests should run only on namespace enabled account")
401+
.isTrue();
402+
return fs;
403+
} catch (IOException ex) {
404+
Assertions.assertThat(ex.getMessage())
405+
.describedAs("Exception message should contain the expected message")
406+
.contains(FS_INIT_FAILED_CPK_CONFIG_IN_NON_HNS_ACCOUNT);
407+
Assertions.assertThat(
408+
getConfiguration().getBoolean(FS_AZURE_TEST_NAMESPACE_ENABLED_ACCOUNT,
409+
false))
410+
.describedAs("Encryption tests should run only on namespace enabled account")
411+
.isFalse();
412+
413+
//Skip the test
414+
Assumptions.assumeThat(true).isFalse();
415+
return null;
416+
}
417417
}
418418

419419
private AzureBlobFileSystem getOrCreateFS() throws Exception {

0 commit comments

Comments
 (0)