Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1026 from mzeo/nicer-fix
Browse files Browse the repository at this point in the history
Fix for pull request #1022, set default report writer
  • Loading branch information
mattnworb authored Nov 16, 2016
2 parents 7066781 + d4f9f8a commit 4b8ac24
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 116 deletions.
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();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,124 +19,33 @@

import com.google.common.collect.Maps;

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.Closeable;
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.
* Reports a stream of TemporaryJobEvent events
*/
public class TemporaryJobReports {
private static final Logger log = LoggerFactory.getLogger(TemporaryJobReports.class);

private final Path outputDir;

TemporaryJobReports(final Path outputDir) {
this.outputDir = outputDir;
this.outputDir.toFile().mkdirs();
}

public ReportWriter getWriterForTest(final Description testDescription) {
return new ReportWriter(outputDir, testDescription.getClassName(),
testDescription.getMethodName());
}

public ReportWriter getWriterForStream(final OutputStream outputStream) {
return new ReportWriter(outputStream);
}

public static class ReportWriter implements Closeable {

private final JsonGenerator jg;
public abstract class TemporaryJobReports {

private final String testClassName;
private final String testName;
public abstract ReportWriter getWriterForTest(final Description testDescription);

private ReportWriter(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);
} finally {
this.jg = jg;
}
}

private ReportWriter(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);
} finally {
this.jg = jg;
}
}
public abstract ReportWriter getDefaultWriter();

public abstract static class ReportWriter implements Closeable {
public Step step(final String step) {
return new Step(this, step);
}

private 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);
}
}
protected abstract void writeEvent(
final String step, final double timestamp, final double duration, final Boolean success,
final Map<String, Object> tags);

@Override
public void close() throws IOException {
if (jg != null) {
jg.close();
}
}

}

public static class Step {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,19 @@ public class TemporaryJobs implements TestRule {
throw Throwables.propagate(e);
}

final Path testReportDirectory = Paths.get(fromNullable(builder.testReportDirectory)
.or(DEFAULT_TEST_REPORT_DIRECTORY));
this.reports = new TemporaryJobReports(testReportDirectory);
final TemporaryJobReports.ReportWriter defaultReportWriter = builder.defaultReportWriter;
if (builder.reports == null) {
final Path testReportDirectory = Paths.get(
fromNullable(builder.testReportDirectory).or(DEFAULT_TEST_REPORT_DIRECTORY));
this.reports = new TemporaryJobJsonReports(testReportDirectory);
} else {
this.reports = builder.reports;
}

this.reportWriter = new ThreadLocal<TemporaryJobReports.ReportWriter>() {
@Override
protected TemporaryJobReports.ReportWriter initialValue() {
if (defaultReportWriter == null) {
log.warn("unable to determine test context, writing event log to stdout");
return TemporaryJobs.this.reports.getWriterForStream(System.out);
} else {
return defaultReportWriter;
}
log.warn("unable to determine test context, writing to default event log");
return TemporaryJobs.this.reports.getDefaultWriter();
}
};

Expand Down Expand Up @@ -528,7 +528,7 @@ public static class Builder {
private String jobDeployedMessageFormat;
private HostPickingStrategy hostPickingStrategy = HostPickingStrategies.randomOneHost();
private long deployTimeoutMillis = DEFAULT_DEPLOY_TIMEOUT_MILLIS;
private TemporaryJobReports.ReportWriter defaultReportWriter;
private TemporaryJobReports reports;

Builder(String profile, Config rootConfig, Map<String, String> env,
HeliosClient.Builder clientBuilder) {
Expand Down Expand Up @@ -720,6 +720,11 @@ public Builder prefixDirectory(final String prefixDirectory) {
return this;
}

public Builder reports(final TemporaryJobReports reports) {
this.reports = reports;
return this;
}

public Builder testReportDirectory(final String testReportDirectory) {
this.testReportDirectory = testReportDirectory;
return this;
Expand All @@ -735,12 +740,6 @@ public Builder deployTimeoutMillis(final long timeout) {
return this;
}

public Builder defaultReportWriter(
final TemporaryJobReports.ReportWriter reportWriter) {
this.defaultReportWriter = reportWriter;
return this;
}

public TemporaryJobs build() {
return new TemporaryJobs(this, config);
}
Expand Down

0 comments on commit 4b8ac24

Please sign in to comment.