zentol commented on a change in pull request #17133: URL: https://github.com/apache/flink/pull/17133#discussion_r705933671
########## File path: flink-architecture-tests/README.md ########## @@ -0,0 +1,12 @@ +# flink-architecture-tests + +This module contains architecture tests using [ArchUnit](https://www.archunit.org/). These tests +reside in their own module in order to control the classpath of which modules should be tested. +Running these tests together (rather than individually per module) allows caching the imported +classes for better performance. + +Tests are currently only execute against Java code, but not Scala code. This is due to missing +(explicit) support for Scala in ArchUnit. + +In order to add a module to be tested against, simply add it to the classpath in this module's +`pom.xml`. Otherwise no specific setup is required to run these tests. Review comment: ```suggestion In order to add a module to be tested against, add it as a test dependency in this module's `pom.xml`. Otherwise no specific setup is required to run these tests. ``` ########## File path: flink-architecture-tests/src/test/java/org/apache/flink/architecture/rules/ApiAnnotationRules.java ########## @@ -0,0 +1,168 @@ +/* + * 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.annotation.Experimental; +import org.apache.flink.annotation.Internal; +import org.apache.flink.annotation.Public; +import org.apache.flink.annotation.PublicEvolving; +import org.apache.flink.annotation.VisibleForTesting; + +import com.tngtech.archunit.base.DescribedPredicate; +import com.tngtech.archunit.core.domain.JavaMethodCall; +import com.tngtech.archunit.junit.ArchTest; +import com.tngtech.archunit.lang.ArchRule; + +import static com.tngtech.archunit.core.domain.JavaClass.Predicates.resideOutsideOfPackage; +import static com.tngtech.archunit.core.domain.properties.CanBeAnnotated.Predicates.annotatedWith; +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods; +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.Conditions.haveLeafArgumentTypes; +import static org.apache.flink.architecture.common.Conditions.haveLeafReturnTypes; +import static org.apache.flink.architecture.common.GivenJavaClasses.javaClassesThat; +import static org.apache.flink.architecture.common.GivenJavaClasses.noJavaClassesThat; +import static org.apache.flink.architecture.common.Predicates.areAnnotatedWithAtLeastOneOf; +import static org.apache.flink.architecture.common.SourcePredicates.areJavaClasses; +import static org.apache.flink.architecture.common.SourcePredicates.areProductionCode; + +/** Rules for API visibility annotations. */ +public class ApiAnnotationRules { + + @ArchTest + public static final ArchRule ANNOTATED_APIS = + freeze( + javaClassesThat() + .resideInAPackage("org.apache.flink..api..") + .and() + .arePublic() + .should( + fulfill( + areAnnotatedWithAtLeastOneOf( + Internal.class, + Experimental.class, + PublicEvolving.class, + Public.class, + Deprecated.class))) + .as( + "Classes in API packages should have at least one API visibility annotation.")); + + @ArchTest + public static final ArchRule NO_API_ANNOTATIONS_ON_NESTED_CLASSES = + freeze( + javaClassesThat() + .areNestedClasses() + .should() + .notBeAnnotatedWith(Experimental.class) + .andShould() + .notBeAnnotatedWith(PublicEvolving.class) + .andShould() + .notBeAnnotatedWith(Public.class) + .as( + "API visibility annotations must be declared on top-level (not nested) classes.")); + + /** + * This is a stronger requirement than {@link + * #PUBLIC_EVOLVING_API_METHODS_USE_ONLY_PUBLIC_EVOLVING_API_TYPES} and ensures that {@link + * Public} APIs do not use {@link PublicEvolving} APIs. + */ + @ArchTest + public static final ArchRule PUBLIC_API_METHODS_USE_ONLY_PUBLIC_API_TYPES = + freeze( + methods() + .that() + .areDeclaredInClassesThat( + areJavaClasses().and(annotatedWith(Public.class))) + .and() + .arePublic() + .and() + .areNotAnnotatedWith(Internal.class) Review comment: same as below ########## File path: flink-architecture-tests/src/test/resources/archunit.properties ########## @@ -0,0 +1,24 @@ +# +# 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. +# + +# Path under which known violations will be stored for frozen rules +freeze.store.default.path=violations + +# Enable this to update the violations files Review comment: How would this work, explicitly step-by-setp? Like let's say I fix one violation, and I now want to remove the frozen rule. ########## File path: flink-architecture-tests/src/test/java/org/apache/flink/architecture/common/Conditions.java ########## @@ -0,0 +1,143 @@ +/* + * 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.common; + +import com.tngtech.archunit.base.DescribedPredicate; +import com.tngtech.archunit.core.domain.JavaClass; +import com.tngtech.archunit.core.domain.JavaMethod; +import com.tngtech.archunit.core.domain.JavaParameterizedType; +import com.tngtech.archunit.core.domain.JavaType; +import com.tngtech.archunit.core.domain.properties.HasName; +import com.tngtech.archunit.lang.ArchCondition; +import com.tngtech.archunit.lang.ConditionEvents; +import com.tngtech.archunit.lang.SimpleConditionEvent; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.apache.flink.architecture.common.SourcePredicates.isJavaClass; + +/** Common conditions for architecture tests. */ +public class Conditions { + + /** Generic condition to check fulfillment of a predicate. */ + public static <T extends HasName> ArchCondition<T> fulfill(DescribedPredicate<T> predicate) { + return new ArchCondition<T>(predicate.getDescription()) { + @Override + public void check(T item, ConditionEvents events) { + if (!predicate.apply(item)) { + final String message = + String.format( + "%s does not satisfy: %s", + item.getName(), predicate.getDescription()); + events.add(SimpleConditionEvent.violated(item, message)); + } + } + }; + } + + /** + * Tests leaf return types of a method against the given predicate. + * + * <p>Given some {@link JavaType}, "leaf" types are recursively determined as follows: + * + * <ul> + * <li>If the type is an array type, check its base component type. + * <li>If the type is a generic type, check the type itself and all of its type arguments. + * <li>Otherwise, check just the type itself. + * </ul> + */ + public static ArchCondition<JavaMethod> haveLeafReturnTypes( Review comment: is there a simple way to test these conditions/predicates/rules etc.? ########## File path: flink-architecture-tests/src/test/java/org/apache/flink/architecture/rules/ApiAnnotationRules.java ########## @@ -0,0 +1,168 @@ +/* + * 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.annotation.Experimental; +import org.apache.flink.annotation.Internal; +import org.apache.flink.annotation.Public; +import org.apache.flink.annotation.PublicEvolving; +import org.apache.flink.annotation.VisibleForTesting; + +import com.tngtech.archunit.base.DescribedPredicate; +import com.tngtech.archunit.core.domain.JavaMethodCall; +import com.tngtech.archunit.junit.ArchTest; +import com.tngtech.archunit.lang.ArchRule; + +import static com.tngtech.archunit.core.domain.JavaClass.Predicates.resideOutsideOfPackage; +import static com.tngtech.archunit.core.domain.properties.CanBeAnnotated.Predicates.annotatedWith; +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods; +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.Conditions.haveLeafArgumentTypes; +import static org.apache.flink.architecture.common.Conditions.haveLeafReturnTypes; +import static org.apache.flink.architecture.common.GivenJavaClasses.javaClassesThat; +import static org.apache.flink.architecture.common.GivenJavaClasses.noJavaClassesThat; +import static org.apache.flink.architecture.common.Predicates.areAnnotatedWithAtLeastOneOf; +import static org.apache.flink.architecture.common.SourcePredicates.areJavaClasses; +import static org.apache.flink.architecture.common.SourcePredicates.areProductionCode; + +/** Rules for API visibility annotations. */ +public class ApiAnnotationRules { + + @ArchTest + public static final ArchRule ANNOTATED_APIS = + freeze( + javaClassesThat() + .resideInAPackage("org.apache.flink..api..") + .and() + .arePublic() + .should( + fulfill( + areAnnotatedWithAtLeastOneOf( + Internal.class, + Experimental.class, + PublicEvolving.class, + Public.class, + Deprecated.class))) + .as( + "Classes in API packages should have at least one API visibility annotation.")); + + @ArchTest + public static final ArchRule NO_API_ANNOTATIONS_ON_NESTED_CLASSES = + freeze( + javaClassesThat() + .areNestedClasses() + .should() + .notBeAnnotatedWith(Experimental.class) + .andShould() + .notBeAnnotatedWith(PublicEvolving.class) + .andShould() + .notBeAnnotatedWith(Public.class) + .as( + "API visibility annotations must be declared on top-level (not nested) classes.")); + + /** + * This is a stronger requirement than {@link + * #PUBLIC_EVOLVING_API_METHODS_USE_ONLY_PUBLIC_EVOLVING_API_TYPES} and ensures that {@link + * Public} APIs do not use {@link PublicEvolving} APIs. + */ + @ArchTest + public static final ArchRule PUBLIC_API_METHODS_USE_ONLY_PUBLIC_API_TYPES = + freeze( + methods() + .that() + .areDeclaredInClassesThat( + areJavaClasses().and(annotatedWith(Public.class))) + .and() + .arePublic() + .and() + .areNotAnnotatedWith(Internal.class) + .should( + haveLeafReturnTypes( + resideOutsideOfPackage("org.apache.flink..") + .or( + areAnnotatedWithAtLeastOneOf( + Public.class, + Deprecated.class)))) + .andShould( + haveLeafArgumentTypes( + resideOutsideOfPackage("org.apache.flink..") + .or( + areAnnotatedWithAtLeastOneOf( + Public.class, + Deprecated.class)))) + .as( + "Return and argument types of methods annotated with @Public must be annotated with @Public, too.")); + + @ArchTest + public static final ArchRule PUBLIC_EVOLVING_API_METHODS_USE_ONLY_PUBLIC_EVOLVING_API_TYPES = + freeze( + methods() + .that() + .areDeclaredInClassesThat( + areJavaClasses() + .and( + areAnnotatedWithAtLeastOneOf( + Public.class, PublicEvolving.class))) + .and() + .arePublic() + .and() + .areNotAnnotatedWith(Internal.class) Review comment: shouldn't this also exclude deprecated methods? If there is some deprecated method from years ago that uses some non-public argument/return type, then that's not something we intend to change? ########## File path: flink-architecture-tests/README.md ########## @@ -0,0 +1,12 @@ +# flink-architecture-tests + +This module contains architecture tests using [ArchUnit](https://www.archunit.org/). These tests +reside in their own module in order to control the classpath of which modules should be tested. +Running these tests together (rather than individually per module) allows caching the imported +classes for better performance. + +Tests are currently only execute against Java code, but not Scala code. This is due to missing Review comment: ```suggestion Tests are currently only executed against Java code, but not Scala code. This is due to missing ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
