HBASE-14393 Have TestHFileEncryption clean up after itself so it don't go all zombie on us
This commit is contained in:
parent
a11bb2a933
commit
7d018ec789
|
@ -63,6 +63,8 @@ public class TestHFileEncryption {
|
|||
@BeforeClass
|
||||
public static void setUp() throws Exception {
|
||||
Configuration conf = TEST_UTIL.getConfiguration();
|
||||
// Disable block cache in this test.
|
||||
conf.setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 0.0f);
|
||||
conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
|
||||
conf.set(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY, "hbase");
|
||||
conf.setInt("hfile.format.version", 3);
|
||||
|
@ -156,32 +158,38 @@ public class TestHFileEncryption {
|
|||
public void testHFileEncryptionMetadata() throws Exception {
|
||||
Configuration conf = TEST_UTIL.getConfiguration();
|
||||
CacheConfig cacheConf = new CacheConfig(conf);
|
||||
|
||||
HFileContext fileContext = new HFileContextBuilder()
|
||||
.withEncryptionContext(cryptoContext)
|
||||
.build();
|
||||
.withEncryptionContext(cryptoContext)
|
||||
.build();
|
||||
|
||||
// write a simple encrypted hfile
|
||||
Path path = new Path(TEST_UTIL.getDataTestDir(), "cryptometa.hfile");
|
||||
FSDataOutputStream out = fs.create(path);
|
||||
HFile.Writer writer = HFile.getWriterFactory(conf, cacheConf)
|
||||
.withOutputStream(out)
|
||||
.withFileContext(fileContext)
|
||||
.create();
|
||||
KeyValue kv = new KeyValue("foo".getBytes(), "f1".getBytes(), null, "value".getBytes());
|
||||
writer.append(kv);
|
||||
writer.close();
|
||||
out.close();
|
||||
.withOutputStream(out)
|
||||
.withFileContext(fileContext)
|
||||
.create();
|
||||
try {
|
||||
KeyValue kv = new KeyValue("foo".getBytes(), "f1".getBytes(), null, "value".getBytes());
|
||||
writer.append(kv);
|
||||
} finally {
|
||||
writer.close();
|
||||
out.close();
|
||||
}
|
||||
|
||||
// read it back in and validate correct crypto metadata
|
||||
HFile.Reader reader = HFile.createReader(fs, path, cacheConf, conf);
|
||||
reader.loadFileInfo();
|
||||
FixedFileTrailer trailer = reader.getTrailer();
|
||||
assertNotNull(trailer.getEncryptionKey());
|
||||
Encryption.Context readerContext = reader.getFileContext().getEncryptionContext();
|
||||
assertEquals(readerContext.getCipher().getName(), cryptoContext.getCipher().getName());
|
||||
assertTrue(Bytes.equals(readerContext.getKeyBytes(),
|
||||
cryptoContext.getKeyBytes()));
|
||||
try {
|
||||
reader.loadFileInfo();
|
||||
FixedFileTrailer trailer = reader.getTrailer();
|
||||
assertNotNull(trailer.getEncryptionKey());
|
||||
Encryption.Context readerContext = reader.getFileContext().getEncryptionContext();
|
||||
assertEquals(readerContext.getCipher().getName(), cryptoContext.getCipher().getName());
|
||||
assertTrue(Bytes.equals(readerContext.getKeyBytes(),
|
||||
cryptoContext.getKeyBytes()));
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test(timeout=6000000)
|
||||
|
@ -209,41 +217,53 @@ public class TestHFileEncryption {
|
|||
.withOutputStream(out)
|
||||
.withFileContext(fileContext)
|
||||
.create();
|
||||
for (KeyValue kv: testKvs) {
|
||||
writer.append(kv);
|
||||
try {
|
||||
for (KeyValue kv: testKvs) {
|
||||
writer.append(kv);
|
||||
}
|
||||
} finally {
|
||||
writer.close();
|
||||
out.close();
|
||||
}
|
||||
writer.close();
|
||||
out.close();
|
||||
|
||||
// read it back in
|
||||
LOG.info("Reading with " + fileContext);
|
||||
HFile.Reader reader = HFile.createReader(fs, path, cacheConf, conf);
|
||||
reader.loadFileInfo();
|
||||
FixedFileTrailer trailer = reader.getTrailer();
|
||||
assertNotNull(trailer.getEncryptionKey());
|
||||
HFileScanner scanner = reader.getScanner(false, false);
|
||||
assertTrue("Initial seekTo failed", scanner.seekTo());
|
||||
int i = 0;
|
||||
do {
|
||||
Cell kv = scanner.getCell();
|
||||
assertTrue("Read back an unexpected or invalid KV",
|
||||
HFileScanner scanner = null;
|
||||
HFile.Reader reader = HFile.createReader(fs, path, cacheConf, conf);
|
||||
try {
|
||||
reader.loadFileInfo();
|
||||
FixedFileTrailer trailer = reader.getTrailer();
|
||||
assertNotNull(trailer.getEncryptionKey());
|
||||
scanner = reader.getScanner(false, false);
|
||||
assertTrue("Initial seekTo failed", scanner.seekTo());
|
||||
do {
|
||||
Cell kv = scanner.getCell();
|
||||
assertTrue("Read back an unexpected or invalid KV",
|
||||
testKvs.contains(KeyValueUtil.ensureKeyValue(kv)));
|
||||
i++;
|
||||
} while (scanner.next());
|
||||
reader.close();
|
||||
i++;
|
||||
} while (scanner.next());
|
||||
} finally {
|
||||
reader.close();
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
assertEquals("Did not read back as many KVs as written", i, testKvs.size());
|
||||
|
||||
// Test random seeks with pread
|
||||
LOG.info("Random seeking with " + fileContext);
|
||||
reader = HFile.createReader(fs, path, cacheConf, conf);
|
||||
scanner = reader.getScanner(false, true);
|
||||
assertTrue("Initial seekTo failed", scanner.seekTo());
|
||||
for (i = 0; i < 100; i++) {
|
||||
KeyValue kv = testKvs.get(RNG.nextInt(testKvs.size()));
|
||||
assertEquals("Unable to find KV as expected: " + kv, scanner.seekTo(kv), 0);
|
||||
try {
|
||||
scanner = reader.getScanner(false, true);
|
||||
assertTrue("Initial seekTo failed", scanner.seekTo());
|
||||
for (i = 0; i < 100; i++) {
|
||||
KeyValue kv = testKvs.get(RNG.nextInt(testKvs.size()));
|
||||
assertEquals("Unable to find KV as expected: " + kv, scanner.seekTo(kv), 0);
|
||||
}
|
||||
} finally {
|
||||
scanner.close();
|
||||
reader.close();
|
||||
}
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue