Skip to content

Commit ac7266b

Browse files
committed
HADOOP-18257. test changes + review comments.
1 parent 3d87bbf commit ac7266b

File tree

5 files changed

+110
-20
lines changed

5 files changed

+110
-20
lines changed

hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/audit/AuditTool.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.IOException;
2323
import java.io.PrintWriter;
2424
import java.net.URI;
25-
import java.net.URISyntaxException;
2625
import java.util.Arrays;
2726
import java.util.List;
2827

@@ -76,7 +75,7 @@ public class AuditTool extends Configured implements Tool, Closeable {
7675
private static final int INVALID_ARGUMENT = EXIT_COMMAND_ARGUMENT_ERROR;
7776

7877
private static final String USAGE =
79-
"bin/hadoop " + AUDIT_TOOL +
78+
"hadoop " + AUDIT_TOOL +
8079
" s3a://<bucket_name>/<destination_dir_path>/" +
8180
" s3a://<bucket_name>/<audit_logs_dir_path>/" + "\n";
8281

hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/audit/mapreduce/S3AAuditLogMergerAndParser.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public HashMap<String, String> parseAuditLog(String singleAuditLog) {
9696
}
9797
}
9898
}
99+
LOG.info("MMT audit map: {}", auditLogMap);
99100
return auditLogMap;
100101
}
101102

@@ -123,7 +124,7 @@ public HashMap<String, String> parseReferrerHeader(String referrerHeader) {
123124

124125
int lengthOfReferrer = httpReferrer.length();
125126
int start = 0;
126-
LOG.debug("HttpReferrer headers string: {}", httpReferrer);
127+
LOG.info("HttpReferrer headers string: {}", httpReferrer);
127128
while (start < lengthOfReferrer) {
128129
// splits "key" and "value" of each header
129130
int equals = httpReferrer.indexOf("=", start);
@@ -146,7 +147,7 @@ public HashMap<String, String> parseReferrerHeader(String referrerHeader) {
146147
start = end + 1;
147148
}
148149

149-
LOG.debug("HttpReferrer headers map:{}", referrerHeaderMap);
150+
LOG.info("HttpReferrer headers map:{}", referrerHeaderMap);
150151
return referrerHeaderMap;
151152
}
152153

@@ -221,7 +222,7 @@ public boolean mergeAndParseAuditLogFiles(FileSystem fileSystem,
221222

222223
// Get the referrer header value from the audit logs.
223224
String referrerHeader = auditLogMap.get(REFERRER_HEADER_KEY);
224-
LOG.debug("Parsed referrer header : {}", referrerHeader);
225+
LOG.info("Parsed referrer header : {}", referrerHeader);
225226
// referrer header value isn't parse-able.
226227
if (StringUtils.isBlank(referrerHeader) || referrerHeader
227228
.equals("-")) {
@@ -280,4 +281,8 @@ public boolean mergeAndParseAuditLogFiles(FileSystem fileSystem,
280281
public long getAuditLogsParsed() {
281282
return auditLogsParsed;
282283
}
284+
285+
public long getReferrerHeaderLogParsed() {
286+
return referrerHeaderLogParsed;
287+
}
283288
}

hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/audit/TestS3AAuditLogMergerAndParser.java

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@
2424
import java.nio.file.Files;
2525
import java.util.Map;
2626

27+
import org.assertj.core.api.Assertions;
2728
import org.junit.Test;
2829
import org.slf4j.Logger;
2930
import org.slf4j.LoggerFactory;
3031

32+
import org.apache.commons.lang3.StringUtils;
3133
import org.apache.hadoop.fs.FileSystem;
3234
import org.apache.hadoop.fs.Path;
3335
import org.apache.hadoop.fs.s3a.AbstractS3ATestBase;
3436
import org.apache.hadoop.fs.s3a.audit.mapreduce.S3AAuditLogMergerAndParser;
37+
import org.apache.hadoop.util.Preconditions;
3538

3639
/**
3740
* This will implement different tests on S3AAuditLogMergerAndParser class.
@@ -170,12 +173,14 @@ public void testParseAuditLog() {
170173
public void testParseAuditLogEmptyAndNull() {
171174
Map<String, String> parseAuditLogResultEmpty =
172175
s3AAuditLogMergerAndParser.parseAuditLog("");
173-
assertTrue("the returned list should be empty for this test",
174-
parseAuditLogResultEmpty.isEmpty());
176+
Assertions.assertThat(parseAuditLogResultEmpty)
177+
.describedAs("Audit log map should be empty")
178+
.isEmpty();
175179
Map<String, String> parseAuditLogResultNull =
176180
s3AAuditLogMergerAndParser.parseAuditLog(null);
177-
assertTrue("the returned list should be empty for this test",
178-
parseAuditLogResultNull.isEmpty());
181+
Assertions.assertThat(parseAuditLogResultEmpty)
182+
.describedAs("Audit log map should be empty")
183+
.isEmpty();
179184
}
180185

181186
/**
@@ -220,21 +225,30 @@ public void testParseReferrerHeaderEmptyAndNull() {
220225
*/
221226
@Test
222227
public void testMergeAndParseAuditLogFiles() throws IOException {
223-
sampleDir = Files.createTempDirectory("sampleDir").toFile();
224-
sampleFile = File.createTempFile("sampleFile", ".txt", sampleDir);
225-
try (FileWriter fw = new FileWriter(sampleFile)) {
226-
fw.write(SAMPLE_LOG_ENTRY);
227-
fw.write(SAMPLE_LOG_ENTRY_1);
228-
fw.flush();
229-
}
228+
// Getting the audit dir and file path from test/resources/
229+
String auditLogDir = this.getClass().getClassLoader().getResource(
230+
"TestAuditLogs").toString();
231+
String auditSingleFile = this.getClass().getClassLoader().getResource(
232+
"TestAuditLogs/sampleLog1").toString();
233+
234+
Preconditions.checkArgument(!StringUtils.isAnyBlank(auditLogDir,
235+
auditSingleFile), String.format("Audit path should not be empty. Check "
236+
+ "test/resources. auditLogDir : %s, auditLogSingleFile :%s",
237+
auditLogDir, auditSingleFile));
238+
Path auditDirPath = new Path(auditLogDir);
239+
Path auditFilePath = new Path(auditSingleFile);
240+
230241
sampleDestDir = Files.createTempDirectory("sampleDestDir").toFile();
231-
Path logsPath = new Path(sampleDir.toURI());
232242
Path destPath = new Path(sampleDestDir.toURI());
233-
FileSystem fileSystem = logsPath.getFileSystem(getConfiguration());
243+
FileSystem fileSystem = auditFilePath.getFileSystem(getConfiguration());
234244
boolean mergeAndParseResult =
235245
s3AAuditLogMergerAndParser.mergeAndParseAuditLogFiles(fileSystem,
236-
logsPath, destPath);
237-
assertTrue("the result should be true", mergeAndParseResult);
246+
auditDirPath, destPath);
247+
assertTrue("The merge and parse failed for the audit log",
248+
mergeAndParseResult);
249+
// 35 audit logs with referrer in each of the 2 sample files.
250+
assertEquals("", s3AAuditLogMergerAndParser.getAuditLogsParsed(), 36 + 36);
251+
assertEquals("", s3AAuditLogMergerAndParser.getReferrerHeaderLogParsed(), 36 + 36);
238252
}
239253

240254
/**

0 commit comments

Comments
 (0)