-
Notifications
You must be signed in to change notification settings - Fork 90
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 #117 from iamdanfox/small-helpers
New `circleAwareLogDirectory` enables partial log capture on CI
- Loading branch information
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
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
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
34 changes: 34 additions & 0 deletions
34
src/main/java/com/palantir/docker/compose/logging/LogDirectory.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,34 @@ | ||
/* | ||
* Copyright 2016 Palantir Technologies, Inc. All rights reserved. | ||
*/ | ||
|
||
package com.palantir.docker.compose.logging; | ||
|
||
import java.util.Optional; | ||
|
||
public class LogDirectory { | ||
|
||
private LogDirectory() {} | ||
|
||
/** | ||
* For tests running on CircleCI, save logs into <code>$CIRCLE_ARTIFACTS/dockerLogs/<testClassName></code>. | ||
* This ensures partial logs can be recovered if the build is cancelled or times out, and | ||
* also avoids needless copying. | ||
* | ||
* Otherwise, save logs from local runs to a folder inside <code>$project/build/dockerLogs</code> named | ||
* after the test class. | ||
* | ||
* @param testClass the JUnit test class whose name will appear on the log folder | ||
*/ | ||
public static String circleAwareLogDirectory(Class<?> testClass) { | ||
String artifactRoot = Optional.ofNullable(System.getenv("CIRCLE_ARTIFACTS")).orElse("build"); | ||
return artifactRoot + "/dockerLogs/" + testClass.getSimpleName(); | ||
} | ||
|
||
/** | ||
* Save logs into a new folder, $project/build/dockerLogs/<testClassName>. | ||
*/ | ||
public static String gradleDockerLogsDirectory(Class<?> testClass) { | ||
return "build/dockerLogs/" + testClass.getSimpleName(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/test/java/com/palantir/docker/compose/logging/LogDirectoryTest.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,41 @@ | ||
/* | ||
* Copyright 2016 Palantir Technologies, Inc. All rights reserved. | ||
*/ | ||
|
||
package com.palantir.docker.compose.logging; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.contrib.java.lang.system.EnvironmentVariables; | ||
|
||
public class LogDirectoryTest { | ||
|
||
@Rule | ||
public final EnvironmentVariables variablesRule = new EnvironmentVariables(); | ||
|
||
@Test | ||
public void gradleDockerLogsDirectory_should_use_class_simple_name() { | ||
String directory = LogDirectory.gradleDockerLogsDirectory(SomeTestClass.class); | ||
assertThat(directory, is("build/dockerLogs/SomeTestClass")); | ||
} | ||
|
||
@Test | ||
public void circleAwareLogDirectory_should_match_gradleDockerLogsDirectory_by_default() { | ||
variablesRule.set("CIRCLE_ARTIFACTS", null); | ||
String directory = LogDirectory.circleAwareLogDirectory(SomeTestClass.class); | ||
assertThat(directory, is("build/dockerLogs/SomeTestClass")); | ||
} | ||
|
||
@Test | ||
public void circleAwareLogDirectory_should_use_circle_environment_variable_if_available() { | ||
variablesRule.set("CIRCLE_ARTIFACTS", "/tmp/circle-artifacts.g4DjuuD"); | ||
|
||
String directory = LogDirectory.circleAwareLogDirectory(SomeTestClass.class); | ||
assertThat(directory, is("/tmp/circle-artifacts.g4DjuuD/dockerLogs/SomeTestClass")); | ||
} | ||
|
||
private static class SomeTestClass {} | ||
} |