This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1026 from mzeo/nicer-fix
Fix for pull request #1022, set default report writer
- Loading branch information
Showing
3 changed files
with
164 additions
and
116 deletions.
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
helios-testing/src/main/java/com/spotify/helios/testing/TemporaryJobJsonReports.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
* Copyright (c) 2016 Spotify AB. | ||
* | ||
* Licensed 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 com.spotify.helios.testing; | ||
|
||
import com.fasterxml.jackson.core.JsonEncoding; | ||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.spotify.helios.common.Json; | ||
import com.spotify.helios.testing.descriptors.TemporaryJobEvent; | ||
|
||
import org.junit.runner.Description; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
|
||
import static java.lang.String.format; | ||
|
||
/** | ||
* Reports a stream of TemporaryJobEvent events to a JSON file. | ||
*/ | ||
public class TemporaryJobJsonReports extends TemporaryJobReports { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(TemporaryJobJsonReports.class); | ||
|
||
private final Path outputDir; | ||
|
||
public TemporaryJobJsonReports(final Path outputDir) { | ||
this.outputDir = outputDir; | ||
final File path = this.outputDir.toFile(); | ||
if (!path.mkdirs() && !path.isDirectory()) { | ||
throw new IllegalStateException(format("failed to create directory \"%s\"", outputDir)); | ||
} | ||
} | ||
|
||
public ReportWriter getWriterForTest(final Description testDescription) { | ||
return new JsonReportWriter( | ||
outputDir, testDescription.getClassName(), testDescription.getMethodName()); | ||
} | ||
|
||
public ReportWriter getDefaultWriter() { | ||
return new JsonReportWriter(System.err); | ||
} | ||
|
||
private static class JsonReportWriter extends ReportWriter { | ||
|
||
private final JsonGenerator jg; | ||
|
||
private final String testClassName; | ||
private final String testName; | ||
|
||
private JsonReportWriter( | ||
final Path outputDir, final String testClassName, final String testName) { | ||
this.testClassName = testClassName; | ||
this.testName = testName; | ||
|
||
final String logFilename = format("%s.%s.json", testClassName, testName).replace('$', '_'); | ||
final File logFile = outputDir.resolve(logFilename).toFile(); | ||
|
||
JsonGenerator jg = null; | ||
try { | ||
jg = new JsonFactory().createGenerator(logFile, JsonEncoding.UTF8); | ||
jg.writeStartArray(); | ||
} catch (IOException e) { | ||
log.error("exception creating event log: {} - {}", logFile.getAbsolutePath(), e); | ||
} | ||
this.jg = jg; | ||
} | ||
|
||
private JsonReportWriter(final OutputStream outputStream) { | ||
this.testClassName = null; | ||
this.testName = null; | ||
|
||
JsonGenerator jg = null; | ||
try { | ||
jg = new JsonFactory().createGenerator(outputStream, JsonEncoding.UTF8); | ||
} catch (IOException e) { | ||
log.error("exception creating event log: {}", e); | ||
} | ||
this.jg = jg; | ||
} | ||
|
||
public Step step(final String step) { | ||
return new Step(this, step); | ||
} | ||
|
||
protected void writeEvent(final String step, final double timestamp, final double duration, | ||
final Boolean success, final Map<String, Object> tags) { | ||
final TemporaryJobEvent event = new TemporaryJobEvent( | ||
timestamp, | ||
duration, | ||
testClassName, | ||
testName, | ||
step, | ||
success, | ||
tags | ||
); | ||
|
||
writeEvent(event); | ||
} | ||
|
||
private void writeEvent(final TemporaryJobEvent event) { | ||
if (jg == null) { | ||
return; | ||
} | ||
|
||
try { | ||
Json.writer().writeValue(jg, event); | ||
} catch (IOException e) { | ||
log.error("exception writing event to log: {} - {}", event, e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
if (jg != null) { | ||
jg.close(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters