From 078fba15b5bb73f743f1a511ef4dfe96a93b9a15 Mon Sep 17 00:00:00 2001 From: erabii Date: Wed, 30 Aug 2023 17:06:16 +0300 Subject: [PATCH] Improve finding tests (1) (#1420) --- .../action.yaml | 24 +++-- .../action.yaml | 6 +- .../composites/test-times/action.yaml | 4 +- .github/workflows/maven.yaml | 4 +- .../pom.xml | 5 -- ...sProfileEnvironmentPostProcessorTest.java} | 2 +- .../pom.xml | 5 -- .../pom.xml | 5 -- .../pom.xml | 5 -- .../pom.xml | 6 -- spring-cloud-kubernetes-test-support/pom.xml | 28 +++++- .../integration/tests/commons/K8SUtils.java | 2 +- .../tests/commons/fabric8_client/Util.java | 2 +- .../tests/commons/native_client/Util.java | 2 +- .../DisabledTestsCondition.java | 1 + .../tests/discovery/TestsDiscovery.java | 88 +++++++++++++++++++ .../main/resources/junit-platform.properties | 1 - src/checkstyle/checkstyle-suppressions.xml | 1 + 18 files changed, 146 insertions(+), 45 deletions(-) rename spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/profile/{AbstractKubernetesProfileEnvironmentPostProcessorTest.java => KubernetesProfileEnvironmentPostProcessorTest.java} (99%) create mode 100644 spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/tests/discovery/TestsDiscovery.java delete mode 100644 spring-cloud-kubernetes-test-support/src/main/resources/junit-platform.properties diff --git a/.github/workflows/composites/maven-build-with-dry-run-for-tests/action.yaml b/.github/workflows/composites/maven-build-with-dry-run-for-tests/action.yaml index dd709b8062..f2b6696192 100644 --- a/.github/workflows/composites/maven-build-with-dry-run-for-tests/action.yaml +++ b/.github/workflows/composites/maven-build-with-dry-run-for-tests/action.yaml @@ -3,14 +3,26 @@ description: maven-build-with-dry-run-for-tests runs: using: "composite" steps: - - name: maven-build-with-dry-run-for-tests + + - name: run 'package' on the project + shell: bash + run: | + ./mvnw install -B \ + -Dskip.build.image=true \ + -DskipTests -DskipITs \ + -T 1C -q + + - name: find all classpath entries + shell: bash + run: | + ./mvnw -q exec:exec -Dexec.classpathScope="test" -Dexec.executable="echo" -Dexec.args="%classpath" | tr : "\n" > /tmp/deps.txt + + - name: find all tests shell: bash run: | - # find all the tests that are supposed to be run, but don't actually run them. - # this is achieved via: 'spring.cloud.k8s.skip.tests=true' in DisabledTestsCondition - ./mvnw test -B -Dskip.build.image=true -Dspring.cloud.k8s.skip.tests=true \ - -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=error \ - -T 1C -q > /tmp/tests.txt + cd spring-cloud-kubernetes-test-support + .././mvnw -q exec:java -Dexec.mainClass="org.springframework.cloud.kubernetes.tests.discovery.TestsDiscovery" > /tmp/tests.txt + cd .. - name: show result if: always() diff --git a/.github/workflows/composites/run-and-save-test-times-when-cache-present/action.yaml b/.github/workflows/composites/run-and-save-test-times-when-cache-present/action.yaml index 4093ab1aee..147475f458 100644 --- a/.github/workflows/composites/run-and-save-test-times-when-cache-present/action.yaml +++ b/.github/workflows/composites/run-and-save-test-times-when-cache-present/action.yaml @@ -8,8 +8,8 @@ runs: uses: actions/cache/restore@v3 with: path: /tmp/sorted.txt - key: ${{ runner.os }}-spring-cloud-kubernetes-existing-test-times-cache-${{ github.run_id }} - restore-keys: ${{ runner.os }}-spring-cloud-kubernetes-existing-test-times-cache- + key: ${{ runner.os }}-spring-cloud-k8s-existing-test-times-cache-${{ github.run_id }} + restore-keys: ${{ runner.os }}-spring-cloud-k8s-existing-test-times-cache- - name: show cached test times shell: bash @@ -138,7 +138,7 @@ runs: # the previous cache will contain more tests then we currently have, so some # matrix instances will have no tests to run if [[ "$sum" -eq "0" ]]; then - echo "no tests to run in current index, most probably this PR removed some tests" + echo "no tests (with known times) to run in current index" tests_to_run_in_current_index='none' fi diff --git a/.github/workflows/composites/test-times/action.yaml b/.github/workflows/composites/test-times/action.yaml index 97f9a32074..33d2c13bfe 100644 --- a/.github/workflows/composites/test-times/action.yaml +++ b/.github/workflows/composites/test-times/action.yaml @@ -47,7 +47,7 @@ runs: # save with the current run_id, but restore it without it. This means two things: # 1) if we re-run, cache will be available - # 2) if there is a new run, we restore as '${{ runner.os }}-spring-cloud-kubernetes-existing-test-times-cache-' + # 2) if there is a new run, we restore as '${{ runner.os }}-spring-cloud-k8s-existing-test-times-cache-' # meaning there could be many of them already present and this is not an exact match # github in this case will pick up the latest one, exactly what we want. - name: save test times in cache @@ -55,4 +55,4 @@ runs: if: env.BASE_BRANCH != '2.1.x' with: path: /tmp/sorted.txt - key: ${{ runner.os }}-spring-cloud-kubernetes-existing-test-times-cache-${{ github.run_id }} + key: ${{ runner.os }}-spring-cloud-k8s-existing-test-times-cache-${{ github.run_id }} diff --git a/.github/workflows/maven.yaml b/.github/workflows/maven.yaml index 65802dcf54..11dd61fedf 100644 --- a/.github/workflows/maven.yaml +++ b/.github/workflows/maven.yaml @@ -67,8 +67,8 @@ jobs: uses: actions/cache/restore@v3 with: path: /tmp/sorted.txt - key: ${{ runner.os }}-spring-cloud-kubernetes-existing-test-times-cache-${{ github.run_id }} - restore-keys: ${{ runner.os }}-spring-cloud-kubernetes-existing-test-times-cache- + key: ${{ runner.os }}-spring-cloud-k8s-existing-test-times-cache-${{ github.run_id }} + restore-keys: ${{ runner.os }}-spring-cloud-k8s-existing-test-times-cache- - name: check test times cache exists id: check_files diff --git a/spring-cloud-kubernetes-client-discovery/pom.xml b/spring-cloud-kubernetes-client-discovery/pom.xml index 365907aede..72974abed3 100644 --- a/spring-cloud-kubernetes-client-discovery/pom.xml +++ b/spring-cloud-kubernetes-client-discovery/pom.xml @@ -50,11 +50,6 @@ spring-boot-starter-test test - - org.junit.vintage - junit-vintage-engine - test - org.springframework.boot spring-boot-starter-web diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/profile/AbstractKubernetesProfileEnvironmentPostProcessorTest.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/profile/KubernetesProfileEnvironmentPostProcessorTest.java similarity index 99% rename from spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/profile/AbstractKubernetesProfileEnvironmentPostProcessorTest.java rename to spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/profile/KubernetesProfileEnvironmentPostProcessorTest.java index c66fcf72f1..aaad3614e8 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/profile/AbstractKubernetesProfileEnvironmentPostProcessorTest.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/profile/KubernetesProfileEnvironmentPostProcessorTest.java @@ -39,7 +39,7 @@ /** * @author wind57 */ -public class AbstractKubernetesProfileEnvironmentPostProcessorTest { +public class KubernetesProfileEnvironmentPostProcessorTest { private static final String FOUNT_IT = "foundIt"; diff --git a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/pom.xml b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/pom.xml index b2149b83a1..cc9c63cc6b 100644 --- a/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/pom.xml +++ b/spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/pom.xml @@ -66,11 +66,6 @@ spring-rabbit-test test - - org.junit.vintage - junit-vintage-engine - test - org.mockito mockito-inline diff --git a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/pom.xml b/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/pom.xml index 30c5e5a978..d6bc61dbc0 100644 --- a/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/pom.xml +++ b/spring-cloud-kubernetes-examples/kubernetes-leader-election-example/pom.xml @@ -49,11 +49,6 @@ spring-boot-starter-test test - - org.junit.vintage - junit-vintage-engine - test - diff --git a/spring-cloud-kubernetes-fabric8-config/pom.xml b/spring-cloud-kubernetes-fabric8-config/pom.xml index 1e327e30d8..40d1e2b30c 100644 --- a/spring-cloud-kubernetes-fabric8-config/pom.xml +++ b/spring-cloud-kubernetes-fabric8-config/pom.xml @@ -97,11 +97,6 @@ spring-boot-starter-test test - - org.junit.vintage - junit-vintage-engine - test - org.springframework.cloud spring-cloud-test-support diff --git a/spring-cloud-kubernetes-fabric8-discovery/pom.xml b/spring-cloud-kubernetes-fabric8-discovery/pom.xml index ef4323e057..9d9a513ef6 100644 --- a/spring-cloud-kubernetes-fabric8-discovery/pom.xml +++ b/spring-cloud-kubernetes-fabric8-discovery/pom.xml @@ -68,12 +68,6 @@ test - - org.junit.vintage - junit-vintage-engine - test - - org.springframework.boot spring-boot-starter-web diff --git a/spring-cloud-kubernetes-test-support/pom.xml b/spring-cloud-kubernetes-test-support/pom.xml index 4de47972f4..61e165db2d 100644 --- a/spring-cloud-kubernetes-test-support/pom.xml +++ b/spring-cloud-kubernetes-test-support/pom.xml @@ -13,9 +13,15 @@ 4.0.3 - 1.18.0 + 1.19.0 + + + org.junit.platform + junit-platform-launcher + + org.springframework.boot spring-boot-starter-test @@ -43,4 +49,24 @@ + + + + org.codehaus.mojo + exec-maven-plugin + 3.1.0 + + + + java + + + + + org.springframework.cloud.kubernetes.tests.discovery.TestsDiscovery + + + + + diff --git a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/K8SUtils.java b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/K8SUtils.java index ad86be2176..dee9bf7fc4 100644 --- a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/K8SUtils.java +++ b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/K8SUtils.java @@ -69,7 +69,7 @@ import org.springframework.util.StringUtils; import static org.awaitility.Awaitility.await; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.fail; /** * @author Ryan Baxter diff --git a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/fabric8_client/Util.java b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/fabric8_client/Util.java index 85a04deba9..74060aa53d 100644 --- a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/fabric8_client/Util.java +++ b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/fabric8_client/Util.java @@ -50,7 +50,7 @@ import org.springframework.cloud.kubernetes.integration.tests.commons.Phase; import static org.awaitility.Awaitility.await; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.fail; import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.loadImage; import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.pomVersion; import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.pullImage; diff --git a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java index 0dc5efee3d..0cddfbbb87 100644 --- a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java +++ b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java @@ -60,7 +60,7 @@ import org.springframework.cloud.kubernetes.integration.tests.commons.Phase; import static org.awaitility.Awaitility.await; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.fail; import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.loadImage; import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.pomVersion; import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.pullImage; diff --git a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/tests/commons/junit_extension/DisabledTestsCondition.java b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/tests/commons/junit_extension/DisabledTestsCondition.java index f141f07c93..b4f92e152a 100644 --- a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/tests/commons/junit_extension/DisabledTestsCondition.java +++ b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/tests/commons/junit_extension/DisabledTestsCondition.java @@ -35,6 +35,7 @@ * * @author wind57 */ +@Deprecated public class DisabledTestsCondition implements ExecutionCondition { private static final boolean SKIP_RUNNING_TESTS = "true".equals(System.getProperty("spring.cloud.k8s.skip.tests")); diff --git a/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/tests/discovery/TestsDiscovery.java b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/tests/discovery/TestsDiscovery.java new file mode 100644 index 0000000000..528613c37b --- /dev/null +++ b/spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/tests/discovery/TestsDiscovery.java @@ -0,0 +1,88 @@ +/* + * Copyright 2013-2023 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.cloud.kubernetes.tests.discovery; + +import java.io.File; +import java.net.URI; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.platform.engine.discovery.DiscoverySelectors; +import org.junit.platform.launcher.Launcher; +import org.junit.platform.launcher.LauncherDiscoveryRequest; +import org.junit.platform.launcher.TestIdentifier; +import org.junit.platform.launcher.TestPlan; +import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; +import org.junit.platform.launcher.core.LauncherFactory; + +/** + * @author wind57 + */ +public class TestsDiscovery { + + public static void main(String[] args) throws Exception { + List targetClasses = entireClasspath().stream().filter(x -> x.contains("target/classes")).toList(); + List targetTestClasses = targetClasses.stream().map(x -> x.replace("classes", "test-classes")).toList(); + List jars = entireClasspath().stream().filter(x -> x.contains(".jar")).toList(); + + List urls = Stream.of(targetClasses, targetTestClasses, jars).flatMap(List::stream) + .map(x -> toURL(new File(x).toPath().toUri())).toList(); + + Set paths = Stream.of(targetClasses, targetTestClasses, jars).flatMap(List::stream).map(Paths::get) + .collect(Collectors.toSet()); + + replaceClassloader(urls); + + LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() + .selectors(DiscoverySelectors.selectClasspathRoots(paths)).build(); + + Launcher launcher = LauncherFactory.openSession().getLauncher(); + TestPlan testPlan = launcher.discover(request); + testPlan.getRoots().stream().flatMap(x -> testPlan.getChildren(x).stream()) + .map(TestIdentifier::getLegacyReportingName).sorted().forEach(test -> { + System.out.println("spring.cloud.k8s.test.to.run -> " + test); + }); + } + + private static void replaceClassloader(List classpathURLs) { + ClassLoader parentClassLoader = Thread.currentThread().getContextClassLoader(); + URLClassLoader classLoader = URLClassLoader.newInstance(classpathURLs.toArray(new URL[0]), parentClassLoader); + Thread.currentThread().setContextClassLoader(classLoader); + } + + // /tmp/deps.txt are created by the pipeline + private static List entireClasspath() throws Exception { + return Files.lines(Paths.get("/tmp/deps.txt")).distinct().collect(Collectors.toList()); + } + + private static URL toURL(URI uri) { + try { + return uri.toURL(); + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + +} diff --git a/spring-cloud-kubernetes-test-support/src/main/resources/junit-platform.properties b/spring-cloud-kubernetes-test-support/src/main/resources/junit-platform.properties deleted file mode 100644 index 6efc0d5e85..0000000000 --- a/spring-cloud-kubernetes-test-support/src/main/resources/junit-platform.properties +++ /dev/null @@ -1 +0,0 @@ -junit.jupiter.extensions.autodetection.enabled=true diff --git a/src/checkstyle/checkstyle-suppressions.xml b/src/checkstyle/checkstyle-suppressions.xml index deeaaf2fd4..97dc2deb9d 100644 --- a/src/checkstyle/checkstyle-suppressions.xml +++ b/src/checkstyle/checkstyle-suppressions.xml @@ -16,4 +16,5 @@ +