Skip to content

Commit

Permalink
NIFI-14099 Adjusted GetHDFSTest to use TempDir for test files
Browse files Browse the repository at this point in the history
- Copying files from test resources to TempDir avoids failures based on file creation timestamps

This closes #9743.

Signed-off-by: Mark Bathori <[email protected]>
  • Loading branch information
exceptionfactory authored and mark-bathori committed Feb 25, 2025
1 parent 96eea40 commit d018675
Showing 1 changed file with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.ietf.jgss.GSSException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.PrivilegedExceptionAction;
import java.util.Collection;
import java.util.HashSet;
Expand All @@ -55,6 +60,21 @@
@DisabledOnOs(OS.WINDOWS)
public class GetHDFSTest {

private static final String TESTDATA_SOURCE_DIR = "src/test/resources/testdata";

@TempDir
private static File tempDir;

@BeforeAll
static void setTestData() throws IOException {
final File sourceDir = Paths.get(TESTDATA_SOURCE_DIR).toFile();
final File[] sourceFiles = sourceDir.listFiles();
for (final File sourceFile : sourceFiles) {
final File destinationFile = new File(tempDir, sourceFile.getName());
Files.copy(sourceFile.toPath(), destinationFile.toPath());
}
}

@Test
public void getPathDifferenceTest() {
assertEquals("", GetHDFS.getPathDifference(new Path("/root"), new Path("/file")));
Expand Down Expand Up @@ -130,7 +150,7 @@ public void testValidators() {
public void testGetFilesWithFilter() {
GetHDFS proc = new GetHDFS();
TestRunner runner = TestRunners.newTestRunner(proc);
runner.setProperty(PutHDFS.DIRECTORY, "src/test/resources/testdata");
runner.setProperty(PutHDFS.DIRECTORY, tempDir.getAbsolutePath());
runner.setProperty(GetHDFS.FILE_FILTER_REGEX, "random.*");
runner.setProperty(GetHDFS.KEEP_SOURCE_FILE, "true");
runner.run();
Expand All @@ -156,7 +176,7 @@ public void testDirectoryDoesNotExist() {
public void testAutomaticDecompression() throws IOException {
GetHDFS proc = new GetHDFS();
TestRunner runner = TestRunners.newTestRunner(proc);
runner.setProperty(PutHDFS.DIRECTORY, "src/test/resources/testdata");
runner.setProperty(PutHDFS.DIRECTORY, tempDir.getAbsolutePath());
runner.setProperty(GetHDFS.FILE_FILTER_REGEX, "random.*.gz");
runner.setProperty(GetHDFS.KEEP_SOURCE_FILE, "true");
runner.setProperty(GetHDFS.COMPRESSION_CODEC, "AUTOMATIC");
Expand All @@ -175,7 +195,7 @@ public void testAutomaticDecompression() throws IOException {
public void testInferCompressionCodecDisabled() throws IOException {
GetHDFS proc = new GetHDFS();
TestRunner runner = TestRunners.newTestRunner(proc);
runner.setProperty(PutHDFS.DIRECTORY, "src/test/resources/testdata");
runner.setProperty(PutHDFS.DIRECTORY, tempDir.getAbsolutePath());
runner.setProperty(GetHDFS.FILE_FILTER_REGEX, "random.*.gz");
runner.setProperty(GetHDFS.KEEP_SOURCE_FILE, "true");
runner.setProperty(GetHDFS.COMPRESSION_CODEC, "NONE");
Expand All @@ -194,7 +214,7 @@ public void testInferCompressionCodecDisabled() throws IOException {
public void testFileExtensionNotACompressionCodec() throws IOException {
GetHDFS proc = new GetHDFS();
TestRunner runner = TestRunners.newTestRunner(proc);
runner.setProperty(PutHDFS.DIRECTORY, "src/test/resources/testdata");
runner.setProperty(PutHDFS.DIRECTORY, tempDir.getAbsolutePath());
runner.setProperty(GetHDFS.FILE_FILTER_REGEX, ".*.zip");
runner.setProperty(GetHDFS.KEEP_SOURCE_FILE, "true");
runner.setProperty(GetHDFS.COMPRESSION_CODEC, "AUTOMATIC");
Expand All @@ -207,28 +227,10 @@ public void testFileExtensionNotACompressionCodec() throws IOException {
assertEquals("13545423550275052.zip", flowFile.getAttribute(CoreAttributes.FILENAME.key()));
InputStream expected = getClass().getResourceAsStream("/testdata/13545423550275052.zip");
flowFile.assertContentEquals(expected);
}

@Test
public void testDirectoryUsesValidEL() throws IOException {
GetHDFS proc = new GetHDFS();
TestRunner runner = TestRunners.newTestRunner(proc);
runner.setProperty(PutHDFS.DIRECTORY, "src/test/resources/${literal('testdata'):substring(0,8)}");
runner.setProperty(GetHDFS.FILE_FILTER_REGEX, ".*.zip");
runner.setProperty(GetHDFS.KEEP_SOURCE_FILE, "true");
runner.setProperty(GetHDFS.COMPRESSION_CODEC, "AUTOMATIC");
runner.run();

List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(GetHDFS.REL_SUCCESS);
assertEquals(1, flowFiles.size());

MockFlowFile flowFile = flowFiles.get(0);
assertEquals("13545423550275052.zip", flowFile.getAttribute(CoreAttributes.FILENAME.key()));
InputStream expected = getClass().getResourceAsStream("/testdata/13545423550275052.zip");
flowFile.assertContentEquals(expected);
final List<ProvenanceEventRecord> provenanceEvents = runner.getProvenanceEvents();
assertEquals(1, provenanceEvents.size());
final ProvenanceEventRecord receiveEvent = provenanceEvents.get(0);
final ProvenanceEventRecord receiveEvent = provenanceEvents.getFirst();
assertEquals(ProvenanceEventType.RECEIVE, receiveEvent.getEventType());
// If it runs with a real HDFS, the protocol will be "hdfs://", but with a local filesystem, just assert the filename.
assertTrue(receiveEvent.getTransitUri().endsWith("13545423550275052.zip"));
Expand Down Expand Up @@ -281,7 +283,7 @@ private void directoryExistsWrappedInUGICall(boolean directoryExists) throws IOE

GetHDFS testSubject = new TestableGetHDFSForUGI(mockFileSystem, mockUserGroupInformation);
TestRunner runner = TestRunners.newTestRunner(testSubject);
runner.setProperty(GetHDFS.DIRECTORY, "src/test/resources/testdata");
runner.setProperty(GetHDFS.DIRECTORY, tempDir.getAbsolutePath());

// WHEN
Answer<?> answer = new Answer<>() {
Expand Down Expand Up @@ -319,7 +321,7 @@ public void testGSSExceptionOnExists() throws Exception {

GetHDFS testSubject = new TestableGetHDFSForUGI(mockFileSystem, mockUserGroupInformation);
TestRunner runner = TestRunners.newTestRunner(testSubject);
runner.setProperty(GetHDFS.DIRECTORY, "src/test/resources/testdata");
runner.setProperty(GetHDFS.DIRECTORY, tempDir.getAbsolutePath());
when(mockUserGroupInformation.doAs(any(PrivilegedExceptionAction.class))).thenThrow(new IOException(new GSSException(13)));
runner.run();

Expand Down

0 comments on commit d018675

Please sign in to comment.