You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by ai...@apache.org on 2022/01/31 09:01:11 UTC

[flink] 03/06: [FLINK-25220][test] develop ArchUnit rules and test base for ITCase

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

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

commit d7c0753afb747099cfa2e1cec53a38b14504b2ee
Author: Jing Ge <ge...@gmail.com>
AuthorDate: Thu Jan 20 11:01:36 2022 +0100

    [FLINK-25220][test] develop ArchUnit rules and test base for ITCase
---
 .../flink-architecture-tests-test/pom.xml          | 23 +++++-
 .../architecture/TestCodeArchitectureTestBase.java | 36 +++++++++
 .../flink/architecture/rules/ITCaseRules.java      | 91 ++++++++++++++++++++++
 .../src/test/resources/archunit.properties         |  5 +-
 .../src/test/resources/log4j2-test.properties}     | 18 ++---
 5 files changed, 161 insertions(+), 12 deletions(-)

diff --git a/flink-architecture-tests/flink-architecture-tests-test/pom.xml b/flink-architecture-tests/flink-architecture-tests-test/pom.xml
index e227322..a5e41ef 100644
--- a/flink-architecture-tests/flink-architecture-tests-test/pom.xml
+++ b/flink-architecture-tests/flink-architecture-tests-test/pom.xml
@@ -39,18 +39,31 @@ under the License.
 		<dependency>
 			<groupId>org.apache.flink</groupId>
 			<artifactId>flink-architecture-tests-base</artifactId>
-			<version>${project.version}</version>
-			<scope>test</scope>
+			<scope>compile</scope>
 		</dependency>
 
 		<dependency>
 			<groupId>com.tngtech.archunit</groupId>
 			<artifactId>archunit</artifactId>
+			<scope>compile</scope>
 		</dependency>
 
 		<dependency>
 			<groupId>com.tngtech.archunit</groupId>
 			<artifactId>archunit-junit5</artifactId>
+			<scope>compile</scope>
+		</dependency>
+
+		<dependency>
+			<groupId>org.junit.jupiter</groupId>
+			<artifactId>junit-jupiter</artifactId>
+			<scope>compile</scope>
+		</dependency>
+
+		<dependency>
+			<groupId>org.junit.vintage</groupId>
+			<artifactId>junit-vintage-engine</artifactId>
+			<scope>compile</scope>
 		</dependency>
 
 		<!-- Test Utils -->
@@ -58,6 +71,12 @@ under the License.
 		<dependency>
 			<groupId>org.apache.flink</groupId>
 			<artifactId>flink-test-utils</artifactId>
+			<scope>compile</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.apache.flink</groupId>
+			<artifactId>flink-test-utils-junit</artifactId>
+			<scope>compile</scope>
 		</dependency>
 	</dependencies>
 
diff --git a/flink-architecture-tests/flink-architecture-tests-test/src/main/java/org/apache/flink/architecture/TestCodeArchitectureTestBase.java b/flink-architecture-tests/flink-architecture-tests-test/src/main/java/org/apache/flink/architecture/TestCodeArchitectureTestBase.java
new file mode 100644
index 0000000..a33ad9b
--- /dev/null
+++ b/flink-architecture-tests/flink-architecture-tests-test/src/main/java/org/apache/flink/architecture/TestCodeArchitectureTestBase.java
@@ -0,0 +1,36 @@
+/*
+ * 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.flink.architecture;
+
+import org.apache.flink.architecture.rules.ITCaseRules;
+
+import com.tngtech.archunit.junit.ArchTest;
+import com.tngtech.archunit.junit.ArchTests;
+
+/**
+ * Central setup of architectural tests for the test code. This class should be extended for common
+ * tests that are required for all submodules.
+ *
+ * <p>Architectural tests built in submodules should include this class via {@link
+ * ArchTests#in(Class)} to cover the common part.
+ */
+public class TestCodeArchitectureTestBase {
+
+    @ArchTest public static final ArchTests ITCASE = ArchTests.in(ITCaseRules.class);
+}
diff --git a/flink-architecture-tests/flink-architecture-tests-test/src/main/java/org/apache/flink/architecture/rules/ITCaseRules.java b/flink-architecture-tests/flink-architecture-tests-test/src/main/java/org/apache/flink/architecture/rules/ITCaseRules.java
new file mode 100644
index 0000000..01ec704
--- /dev/null
+++ b/flink-architecture-tests/flink-architecture-tests-test/src/main/java/org/apache/flink/architecture/rules/ITCaseRules.java
@@ -0,0 +1,91 @@
+/*
+ * 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.flink.architecture.rules;
+
+import org.apache.flink.core.testutils.AllCallbackWrapper;
+import org.apache.flink.runtime.testutils.MiniClusterExtension;
+import org.apache.flink.test.util.AbstractTestBase;
+import org.apache.flink.test.util.MiniClusterWithClientResource;
+
+import com.tngtech.archunit.junit.ArchTest;
+import com.tngtech.archunit.lang.ArchRule;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import static com.tngtech.archunit.core.domain.JavaModifier.ABSTRACT;
+import static com.tngtech.archunit.library.freeze.FreezingArchRule.freeze;
+import static org.apache.flink.architecture.common.Conditions.fulfill;
+import static org.apache.flink.architecture.common.GivenJavaClasses.javaClassesThat;
+import static org.apache.flink.architecture.common.Predicates.arePublicFinalOfTypeWithAnnotation;
+import static org.apache.flink.architecture.common.Predicates.arePublicStaticFinalOfType;
+import static org.apache.flink.architecture.common.Predicates.arePublicStaticFinalOfTypeWithAnnotation;
+import static org.apache.flink.architecture.common.Predicates.containAnyFieldsInClassHierarchyThat;
+
+/** Rules for Integration Tests. */
+public class ITCaseRules {
+
+    @ArchTest
+    public static final ArchRule INTEGRATION_TEST_ENDING_WITH_ITCASE =
+            freeze(
+                    javaClassesThat()
+                            .areAssignableTo(AbstractTestBase.class)
+                            .and()
+                            .doNotHaveModifier(ABSTRACT)
+                            .should()
+                            .haveSimpleNameEndingWith("ITCase"));
+
+    @ArchTest
+    public static final ArchRule ITCASE_USE_MINICLUSTER =
+            freeze(
+                    javaClassesThat()
+                            .haveSimpleNameEndingWith("ITCase")
+                            .and()
+                            .areTopLevelClasses()
+                            .and()
+                            .doNotHaveModifier(ABSTRACT)
+                            .should(
+                                    fulfill(
+                                            // JUnit 5 violation check
+                                            containAnyFieldsInClassHierarchyThat(
+                                                            arePublicStaticFinalOfType(
+                                                                    MiniClusterExtension.class))
+                                                    .and(
+                                                            containAnyFieldsInClassHierarchyThat(
+                                                                    arePublicStaticFinalOfTypeWithAnnotation(
+                                                                            AllCallbackWrapper
+                                                                                    .class,
+                                                                            RegisterExtension
+                                                                                    .class)))
+                                                    // JUnit 4 violation check, which should be
+                                                    // removed
+                                                    // after the JUnit 4->5 migration is closed.
+                                                    .or(
+                                                            containAnyFieldsInClassHierarchyThat(
+                                                                    arePublicStaticFinalOfTypeWithAnnotation(
+                                                                            MiniClusterWithClientResource
+                                                                                    .class,
+                                                                            ClassRule.class)))
+                                                    .or(
+                                                            containAnyFieldsInClassHierarchyThat(
+                                                                    arePublicFinalOfTypeWithAnnotation(
+                                                                            MiniClusterWithClientResource
+                                                                                    .class,
+                                                                            Rule.class))))));
+}
diff --git a/flink-architecture-tests/src/test/resources/archunit.properties b/flink-architecture-tests/flink-architecture-tests-test/src/test/resources/archunit.properties
similarity index 85%
copy from flink-architecture-tests/src/test/resources/archunit.properties
copy to flink-architecture-tests/flink-architecture-tests-test/src/test/resources/archunit.properties
index df5890a..15be88c 100644
--- a/flink-architecture-tests/src/test/resources/archunit.properties
+++ b/flink-architecture-tests/flink-architecture-tests-test/src/test/resources/archunit.properties
@@ -19,10 +19,13 @@
 # By default we allow removing existing violations, but fail when new violations are added.
 freeze.store.default.allowStoreUpdate=true
 
+# Enable this if a new (frozen) rule has been added in order to create the initial store and record the existing violations.
+#freeze.store.default.allowStoreCreation=true
+
 # Enable this to add allow new violations to be recorded.
 # NOTE: Adding new violations should be avoided when possible. If the rule was correct to flag a new
 #       violation, please try to avoid creating the violation. If the violation was created due to a
 #       shortcoming of the rule, file a JIRA issue so the rule can be improved.
 #freeze.refreeze=true
 
-freeze.store.default.path=violations
+freeze.store.default.path=archunit-violations
diff --git a/flink-architecture-tests/src/test/resources/archunit.properties b/flink-architecture-tests/flink-architecture-tests-test/src/test/resources/log4j2-test.properties
similarity index 59%
rename from flink-architecture-tests/src/test/resources/archunit.properties
rename to flink-architecture-tests/flink-architecture-tests-test/src/test/resources/log4j2-test.properties
index df5890a..b1c818d 100644
--- a/flink-architecture-tests/src/test/resources/archunit.properties
+++ b/flink-architecture-tests/flink-architecture-tests-test/src/test/resources/log4j2-test.properties
@@ -16,13 +16,13 @@
 # limitations under the License.
 #
 
-# By default we allow removing existing violations, but fail when new violations are added.
-freeze.store.default.allowStoreUpdate=true
+# Set root logger level to OFF to not flood build logs
+# set manually to INFO for debugging purposes
+rootLogger.level = OFF
+rootLogger.appenderRef.test.ref = TestLogger
 
-# Enable this to add allow new violations to be recorded.
-# NOTE: Adding new violations should be avoided when possible. If the rule was correct to flag a new
-#       violation, please try to avoid creating the violation. If the violation was created due to a
-#       shortcoming of the rule, file a JIRA issue so the rule can be improved.
-#freeze.refreeze=true
-
-freeze.store.default.path=violations
+appender.testlogger.name = TestLogger
+appender.testlogger.type = CONSOLE
+appender.testlogger.target = SYSTEM_ERR
+appender.testlogger.layout.type = PatternLayout
+appender.testlogger.layout.pattern = %-4r [%t] %-5p %c %x - %m%n