From 3f614bf70ba7c51e5e3fb4b103fd36ebedb7ab17 Mon Sep 17 00:00:00 2001 From: Zongwen Li Date: Wed, 28 Sep 2022 14:31:12 +0800 Subject: [PATCH] [Improve][e2e] support @DisabledOnContainer --- .../e2e/connector/redis/RedisIT.java | 2 + .../junit/ContainerTestingExtension.java | 14 +++++- .../e2e/common/junit/DisabledOnContainer.java | 44 +++++++++++++++++++ .../TestCaseInvocationContextProvider.java | 16 ++++++- 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/junit/DisabledOnContainer.java diff --git a/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-redis-e2e/src/test/java/org/apache/seatunnel/e2e/connector/redis/RedisIT.java b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-redis-e2e/src/test/java/org/apache/seatunnel/e2e/connector/redis/RedisIT.java index 8b97543a42d..86e853ccd8f 100644 --- a/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-redis-e2e/src/test/java/org/apache/seatunnel/e2e/connector/redis/RedisIT.java +++ b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-redis-e2e/src/test/java/org/apache/seatunnel/e2e/connector/redis/RedisIT.java @@ -29,6 +29,7 @@ import org.apache.seatunnel.e2e.common.TestResource; import org.apache.seatunnel.e2e.common.TestSuiteBase; import org.apache.seatunnel.e2e.common.container.TestContainer; +import org.apache.seatunnel.e2e.common.junit.DisabledOnContainer; import org.apache.seatunnel.format.json.JsonSerializationSchema; import lombok.extern.slf4j.Slf4j; @@ -56,6 +57,7 @@ import scala.Tuple2; +@DisabledOnContainer(value = "spark:2.4.3", disabledReason = "json-format conflicts with the Jackson version of Spark-2.4.3, see:https://github.com/apache/incubator-seatunnel/issues/2929") @Slf4j public class RedisIT extends TestSuiteBase implements TestResource { private static final String IMAGE = "redis:latest"; diff --git a/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/junit/ContainerTestingExtension.java b/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/junit/ContainerTestingExtension.java index 0dd9955ddd6..b2175c20e57 100644 --- a/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/junit/ContainerTestingExtension.java +++ b/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/junit/ContainerTestingExtension.java @@ -25,10 +25,14 @@ import org.junit.jupiter.api.extension.BeforeAllCallback; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.platform.commons.support.AnnotationSupport; +import org.junit.platform.commons.util.AnnotationUtils; import java.lang.annotation.Annotation; +import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; public class ContainerTestingExtension implements BeforeAllCallback, AfterAllCallback { public static final ExtensionContext.Namespace TEST_RESOURCE_NAMESPACE = @@ -57,7 +61,15 @@ public void beforeAll(ExtensionContext context) throws Exception { TestContainersFactory.class); checkExactlyOneAnnotatedField(containersFactories, TestContainers.class); - List testContainers = containersFactories.get(0).create(); + + // Filters disabled containers + final List disabledContainers = new ArrayList<>(); + AnnotationUtils.findAnnotation(context.getRequiredTestInstance().getClass(), DisabledOnContainer.class) + .ifPresent(annotation -> Collections.addAll(disabledContainers, annotation.value())); + List testContainers = containersFactories.get(0).create() + .stream() + .filter(container -> !disabledContainers.contains(container.identifier())) + .collect(Collectors.toList()); context.getStore(TEST_RESOURCE_NAMESPACE) .put(TEST_CONTAINERS_STORE_KEY, testContainers); } diff --git a/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/junit/DisabledOnContainer.java b/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/junit/DisabledOnContainer.java new file mode 100644 index 00000000000..d908d5e2738 --- /dev/null +++ b/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/junit/DisabledOnContainer.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.seatunnel.e2e.common.junit; + +import org.apache.seatunnel.e2e.common.container.TestContainer; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ ElementType.TYPE, ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +public @interface DisabledOnContainer { + + /** + * {@link TestContainer#identifier()} + */ + String[] value(); + + /** + * Custom reason to provide if the test container is disabled. + * + *

If a custom reason is supplied, it will be combined with the default + * reason for this annotation. If a custom reason is not supplied, the default + * reason will be used. + */ + String disabledReason() default ""; +} diff --git a/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/junit/TestCaseInvocationContextProvider.java b/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/junit/TestCaseInvocationContextProvider.java index 37a1be45fa8..f73f5af7f07 100644 --- a/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/junit/TestCaseInvocationContextProvider.java +++ b/seatunnel-e2e/seatunnel-e2e-common/src/test/java/org/apache/seatunnel/e2e/common/junit/TestCaseInvocationContextProvider.java @@ -34,9 +34,13 @@ import org.junit.jupiter.api.extension.ParameterResolver; import org.junit.jupiter.api.extension.TestTemplateInvocationContext; import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider; +import org.junit.platform.commons.util.AnnotationUtils; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; import java.util.stream.Stream; @Slf4j @@ -53,8 +57,16 @@ public boolean supportsTestTemplate(ExtensionContext context) { @SuppressWarnings("unchecked") @Override public Stream provideTestTemplateInvocationContexts(ExtensionContext context) { - List testContainers = (List) context.getStore(TEST_RESOURCE_NAMESPACE) - .get(TEST_CONTAINERS_STORE_KEY); + final List disabledContainers = new ArrayList<>(); + AnnotationUtils.findAnnotation(context.getRequiredTestMethod(), DisabledOnContainer.class) + .ifPresent(annotation -> Collections.addAll(disabledContainers, annotation.value())); + + // Filters disabled containers + List testContainers = ((List) context.getStore(TEST_RESOURCE_NAMESPACE) + .get(TEST_CONTAINERS_STORE_KEY)) + .stream() + .filter(container -> !disabledContainers.contains(container.identifier())) + .collect(Collectors.toList()); ContainerExtendedFactory containerExtendedFactory = (ContainerExtendedFactory) context.getStore(TEST_RESOURCE_NAMESPACE) .get(TEST_EXTENDED_FACTORY_STORE_KEY);