diff --git a/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/DorisContainer.java b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/DorisContainer.java index 8291fd6e5f2..9f70db4ebb7 100644 --- a/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/DorisContainer.java +++ b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/DorisContainer.java @@ -58,13 +58,15 @@ protected void setupContainer() { @Override public void start() { - try { - super.start(); - Preconditions.check("Doris container startup failed!", checkContainerStatus(5)); - Preconditions.check("Doris container password change failed!", changePassword()); - } finally { - copyDorisLog(); - } + super.start(); + Preconditions.check("Doris container startup failed!", checkContainerStatus(5)); + Preconditions.check("Doris container password change failed!", changePassword()); + } + + @Override + public void close() { + copyDorisLog(); + super.close(); } private void copyDorisLog() { diff --git a/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/HiveContainer.java b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/HiveContainer.java index db4a5f8e5ce..edb63b21e33 100644 --- a/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/HiveContainer.java +++ b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/HiveContainer.java @@ -54,12 +54,14 @@ protected void setupContainer() { @Override public void start() { - try { - super.start(); - Preconditions.check("Hive container startup failed!", checkContainerStatus(10)); - } finally { - copyHiveLog(); - } + super.start(); + Preconditions.check("Hive container startup failed!", checkContainerStatus(10)); + } + + @Override + public void close() { + copyHiveLog(); + super.close(); } private void copyHiveLog() { diff --git a/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/KafkaContainer.java b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/KafkaContainer.java index f40922acc8f..ecd9ea5c6c4 100644 --- a/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/KafkaContainer.java +++ b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/KafkaContainer.java @@ -47,12 +47,14 @@ public static Builder builder() { @Override public void start() { - try { - super.start(); - Preconditions.checkArgument(checkContainerStatus(5), "Kafka container startup failed!"); - } finally { - copyKafkaLogs(); - } + super.start(); + Preconditions.checkArgument(checkContainerStatus(5), "Kafka container startup failed!"); + } + + @Override + public void close() { + copyKafkaLogs(); + super.close(); } private void copyKafkaLogs() { diff --git a/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/util/AbstractIT.java b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/util/AbstractIT.java index d9c95872ed5..52753182251 100644 --- a/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/util/AbstractIT.java +++ b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/util/AbstractIT.java @@ -40,7 +40,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -@ExtendWith(PrintFuncNameExtension.class) +@ExtendWith({PrintFuncNameExtension.class, CloseContainerExtension.class}) public class AbstractIT { protected static final ContainerSuite containerSuite = ContainerSuite.getInstance(); diff --git a/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/util/CloseContainerExtension.java b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/util/CloseContainerExtension.java new file mode 100644 index 00000000000..8020e969411 --- /dev/null +++ b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/util/CloseContainerExtension.java @@ -0,0 +1,43 @@ +/* + * Copyright 2024 Datastrato Pvt Ltd. + * This software is licensed under the Apache License version 2. + */ +package com.datastrato.gravitino.integration.test.util; + +import com.datastrato.gravitino.integration.test.container.ContainerSuite; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is an extension for juint 5, which aims to perform certain operations (such as resource + * recycling, etc.) after all test executions are completed (regardless of success or failure). You + * can Refer to {@link AbstractIT} for more information. + */ +public class CloseContainerExtension implements BeforeAllCallback { + @Override + public void beforeAll(ExtensionContext extensionContext) { + synchronized (CloseContainerExtension.class) { + extensionContext + .getRoot() + .getStore(ExtensionContext.Namespace.GLOBAL) + .getOrComputeIfAbsent(CloseableContainer.class); + } + } + + private static class CloseableContainer implements ExtensionContext.Store.CloseableResource { + private static final Logger LOGGER = LoggerFactory.getLogger(CloseableContainer.class); + private static final ContainerSuite CONTAINER_SUITE = ContainerSuite.getInstance(); + + @Override + public void close() { + try { + CONTAINER_SUITE.close(); + LOGGER.info("Containers were closed successfully"); + } catch (Exception e) { + LOGGER.warn("Containers were not closed as expected", e); + } + } + } +}