You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by ch...@apache.org on 2019/05/10 13:21:32 UTC

[flink] 02/02: [hotfix][tests] Backup logs for failed tests

This is an automated email from the ASF dual-hosted git repository.

chesnay pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git

commit c4cb904c90e64bde945117048c5af4cb93e0dddf
Author: zentol <ch...@apache.org>
AuthorDate: Tue Jan 29 13:24:11 2019 +0100

    [hotfix][tests] Backup logs for failed tests
---
 .../flink-end-to-end-tests-common/pom.xml          |  6 +++++
 .../apache/flink/tests/util/FlinkDistribution.java | 30 +++++++++++++++++++---
 2 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/flink-end-to-end-tests/flink-end-to-end-tests-common/pom.xml b/flink-end-to-end-tests/flink-end-to-end-tests-common/pom.xml
index c94400e..791f806 100644
--- a/flink-end-to-end-tests/flink-end-to-end-tests-common/pom.xml
+++ b/flink-end-to-end-tests/flink-end-to-end-tests-common/pom.xml
@@ -51,6 +51,12 @@ under the License.
 			<scope>compile</scope>
 		</dependency>
 		<dependency>
+			<groupId>org.apache.flink</groupId>
+			<artifactId>flink-test-utils-junit</artifactId>
+			<version>${project.version}</version>
+			<scope>compile</scope>
+		</dependency>
+		<dependency>
 			<!-- To ensure that flink-dist is built beforehand -->
 			<groupId>org.apache.flink</groupId>
 			<artifactId>flink-dist_${scala.binary.version}</artifactId>
diff --git a/flink-end-to-end-tests/flink-end-to-end-tests-common/src/main/java/org/apache/flink/tests/util/FlinkDistribution.java b/flink-end-to-end-tests/flink-end-to-end-tests-common/src/main/java/org/apache/flink/tests/util/FlinkDistribution.java
index 5192bf2..8236f4b 100644
--- a/flink-end-to-end-tests/flink-end-to-end-tests-common/src/main/java/org/apache/flink/tests/util/FlinkDistribution.java
+++ b/flink-end-to-end-tests/flink-end-to-end-tests-common/src/main/java/org/apache/flink/tests/util/FlinkDistribution.java
@@ -22,6 +22,7 @@ import org.apache.flink.configuration.Configuration;
 import org.apache.flink.configuration.GlobalConfiguration;
 import org.apache.flink.configuration.UnmodifiableConfiguration;
 import org.apache.flink.util.ExceptionUtils;
+import org.apache.flink.util.ExternalResource;
 
 import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode;
 import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
@@ -29,8 +30,8 @@ import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMap
 import okhttp3.OkHttpClient;
 import okhttp3.Request;
 import okhttp3.Response;
+import org.apache.commons.io.FileUtils;
 import org.junit.Assert;
-import org.junit.rules.ExternalResource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -57,7 +58,7 @@ import java.util.stream.Stream;
 /**
  * A wrapper around a Flink distribution.
  */
-public final class FlinkDistribution extends ExternalResource {
+public final class FlinkDistribution implements ExternalResource {
 
 	private static final Logger LOG = LoggerFactory.getLogger(FlinkDistribution.class);
 
@@ -68,6 +69,8 @@ public final class FlinkDistribution extends ExternalResource {
 
 	private final List<AutoClosablePath> filesToDelete = new ArrayList<>(4);
 
+	private final Optional<Path> logBackupDir;
+
 	private final Path opt;
 	private final Path lib;
 	private final Path conf;
@@ -81,6 +84,10 @@ public final class FlinkDistribution extends ExternalResource {
 		if (distDirProperty == null) {
 			Assert.fail("The distDir property was not set. You can set it when running maven via -DdistDir=<path> .");
 		}
+		final String backupDirProperty = System.getProperty("logBackupDir");
+		logBackupDir = backupDirProperty == null
+			? Optional.empty()
+			: Optional.of(Paths.get(backupDirProperty));
 		final Path flinkDir = Paths.get(distDirProperty);
 		bin = flinkDir.resolve("bin");
 		opt = flinkDir.resolve("opt");
@@ -90,7 +97,7 @@ public final class FlinkDistribution extends ExternalResource {
 	}
 
 	@Override
-	protected void before() throws IOException {
+	public void before() throws IOException {
 		defaultConfig = new UnmodifiableConfiguration(GlobalConfiguration.loadConfiguration(conf.toAbsolutePath().toString()));
 		final Path originalConfig = conf.resolve(FLINK_CONF_YAML);
 		final Path backupConfig = conf.resolve(FLINK_CONF_YAML_BACKUP);
@@ -99,7 +106,7 @@ public final class FlinkDistribution extends ExternalResource {
 	}
 
 	@Override
-	protected void after() {
+	public void afterTestSuccess() {
 		try {
 			stopFlinkCluster();
 		} catch (IOException e) {
@@ -124,6 +131,21 @@ public final class FlinkDistribution extends ExternalResource {
 		}
 	}
 
+	@Override
+	public void afterTestFailure() {
+		logBackupDir.ifPresent(backupLocation -> {
+			LOG.info("Backing up logs to {}.", backupLocation);
+			try {
+				Files.createDirectories(backupLocation);
+				FileUtils.copyDirectory(log.toFile(), backupLocation.toFile());
+			} catch (IOException e) {
+				LOG.warn("An error occurred while backing up logs.", e);
+			}
+		});
+
+		afterTestSuccess();
+	}
+
 	public void startFlinkCluster() throws IOException {
 		LOG.info("Starting Flink cluster.");
 		AutoClosableProcess.runBlocking(bin.resolve("start-cluster.sh").toAbsolutePath().toString());