Improve finding tests (1) (#1420)

This commit is contained in:
erabii
2023-08-30 17:06:16 +03:00
committed by GitHub
parent 87ffd673c7
commit 078fba15b5
18 changed files with 146 additions and 45 deletions

View File

@@ -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: |
# 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
./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: |
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()

View File

@@ -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

View File

@@ -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 }}

View File

@@ -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

View File

@@ -50,11 +50,6 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

View File

@@ -39,7 +39,7 @@ import org.springframework.test.context.support.TestPropertySourceUtils;
/**
* @author wind57
*/
public class AbstractKubernetesProfileEnvironmentPostProcessorTest {
public class KubernetesProfileEnvironmentPostProcessorTest {
private static final String FOUNT_IT = "foundIt";

View File

@@ -66,11 +66,6 @@
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>

View File

@@ -49,11 +49,6 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@@ -97,11 +97,6 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-test-support</artifactId>

View File

@@ -68,12 +68,6 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

View File

@@ -13,9 +13,15 @@
<properties>
<awaitility.version>4.0.3</awaitility.version>
<testcontainers.version>1.18.0</testcontainers.version>
<testcontainers.version>1.19.0</testcontainers.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
@@ -43,4 +49,24 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.springframework.cloud.kubernetes.tests.discovery.TestsDiscovery</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -69,7 +69,7 @@ import org.springframework.util.StreamUtils;
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

View File

@@ -50,7 +50,7 @@ import org.testcontainers.k3s.K3sContainer;
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;

View File

@@ -60,7 +60,7 @@ import org.testcontainers.k3s.K3sContainer;
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;

View File

@@ -35,6 +35,7 @@ import org.junit.jupiter.api.extension.ExtensionContext;
*
* @author wind57
*/
@Deprecated
public class DisabledTestsCondition implements ExecutionCondition {
private static final boolean SKIP_RUNNING_TESTS = "true".equals(System.getProperty("spring.cloud.k8s.skip.tests"));

View File

@@ -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<String> targetClasses = entireClasspath().stream().filter(x -> x.contains("target/classes")).toList();
List<String> targetTestClasses = targetClasses.stream().map(x -> x.replace("classes", "test-classes")).toList();
List<String> jars = entireClasspath().stream().filter(x -> x.contains(".jar")).toList();
List<URL> urls = Stream.of(targetClasses, targetTestClasses, jars).flatMap(List::stream)
.map(x -> toURL(new File(x).toPath().toUri())).toList();
Set<Path> 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<URL> 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<String> 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);
}
}
}

View File

@@ -1 +0,0 @@
junit.jupiter.extensions.autodetection.enabled=true

View File

@@ -16,4 +16,5 @@
<suppress files=".*DiscoveryServerApplication\.java" checks="HideUtilityClassConstructor"/>
<suppress files=".*KubernetesConfigServerApplication\.java" checks="HideUtilityClassConstructor"/>
<suppress files=".*Fabric8ConfigContext\.java" checks="WhitespaceAround" />
<suppress files=".*TestsDiscovery\.java" checks="HideUtilityClassConstructor" />
</suppressions>