Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 078fba1

Browse files
authoredAug 30, 2023
Improve finding tests (1) (#1420)
1 parent 87ffd67 commit 078fba1

File tree

18 files changed

+146
-45
lines changed

18 files changed

+146
-45
lines changed
 

‎.github/workflows/composites/maven-build-with-dry-run-for-tests/action.yaml

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,26 @@ description: maven-build-with-dry-run-for-tests
33
runs:
44
using: "composite"
55
steps:
6-
- name: maven-build-with-dry-run-for-tests
6+
7+
- name: run 'package' on the project
8+
shell: bash
9+
run: |
10+
./mvnw install -B \
11+
-Dskip.build.image=true \
12+
-DskipTests -DskipITs \
13+
-T 1C -q
14+
15+
- name: find all classpath entries
16+
shell: bash
17+
run: |
18+
./mvnw -q exec:exec -Dexec.classpathScope="test" -Dexec.executable="echo" -Dexec.args="%classpath" | tr : "\n" > /tmp/deps.txt
19+
20+
- name: find all tests
721
shell: bash
822
run: |
9-
# find all the tests that are supposed to be run, but don't actually run them.
10-
# this is achieved via: 'spring.cloud.k8s.skip.tests=true' in DisabledTestsCondition
11-
./mvnw test -B -Dskip.build.image=true -Dspring.cloud.k8s.skip.tests=true \
12-
-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=error \
13-
-T 1C -q > /tmp/tests.txt
23+
cd spring-cloud-kubernetes-test-support
24+
.././mvnw -q exec:java -Dexec.mainClass="org.springframework.cloud.kubernetes.tests.discovery.TestsDiscovery" > /tmp/tests.txt
25+
cd ..
1426
1527
- name: show result
1628
if: always()

‎.github/workflows/composites/run-and-save-test-times-when-cache-present/action.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ runs:
88
uses: actions/cache/restore@v3
99
with:
1010
path: /tmp/sorted.txt
11-
key: ${{ runner.os }}-spring-cloud-kubernetes-existing-test-times-cache-${{ github.run_id }}
12-
restore-keys: ${{ runner.os }}-spring-cloud-kubernetes-existing-test-times-cache-
11+
key: ${{ runner.os }}-spring-cloud-k8s-existing-test-times-cache-${{ github.run_id }}
12+
restore-keys: ${{ runner.os }}-spring-cloud-k8s-existing-test-times-cache-
1313

1414
- name: show cached test times
1515
shell: bash
@@ -138,7 +138,7 @@ runs:
138138
# the previous cache will contain more tests then we currently have, so some
139139
# matrix instances will have no tests to run
140140
if [[ "$sum" -eq "0" ]]; then
141-
echo "no tests to run in current index, most probably this PR removed some tests"
141+
echo "no tests (with known times) to run in current index"
142142
tests_to_run_in_current_index='none'
143143
fi
144144

‎.github/workflows/composites/test-times/action.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ runs:
4747

4848
# save with the current run_id, but restore it without it. This means two things:
4949
# 1) if we re-run, cache will be available
50-
# 2) if there is a new run, we restore as '${{ runner.os }}-spring-cloud-kubernetes-existing-test-times-cache-'
50+
# 2) if there is a new run, we restore as '${{ runner.os }}-spring-cloud-k8s-existing-test-times-cache-'
5151
# meaning there could be many of them already present and this is not an exact match
5252
# github in this case will pick up the latest one, exactly what we want.
5353
- name: save test times in cache
5454
uses: actions/cache/save@v3
5555
if: env.BASE_BRANCH != '2.1.x'
5656
with:
5757
path: /tmp/sorted.txt
58-
key: ${{ runner.os }}-spring-cloud-kubernetes-existing-test-times-cache-${{ github.run_id }}
58+
key: ${{ runner.os }}-spring-cloud-k8s-existing-test-times-cache-${{ github.run_id }}

‎.github/workflows/maven.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ jobs:
6767
uses: actions/cache/restore@v3
6868
with:
6969
path: /tmp/sorted.txt
70-
key: ${{ runner.os }}-spring-cloud-kubernetes-existing-test-times-cache-${{ github.run_id }}
71-
restore-keys: ${{ runner.os }}-spring-cloud-kubernetes-existing-test-times-cache-
70+
key: ${{ runner.os }}-spring-cloud-k8s-existing-test-times-cache-${{ github.run_id }}
71+
restore-keys: ${{ runner.os }}-spring-cloud-k8s-existing-test-times-cache-
7272

7373
- name: check test times cache exists
7474
id: check_files

‎spring-cloud-kubernetes-client-discovery/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@
5050
<artifactId>spring-boot-starter-test</artifactId>
5151
<scope>test</scope>
5252
</dependency>
53-
<dependency>
54-
<groupId>org.junit.vintage</groupId>
55-
<artifactId>junit-vintage-engine</artifactId>
56-
<scope>test</scope>
57-
</dependency>
5853
<dependency>
5954
<groupId>org.springframework.boot</groupId>
6055
<artifactId>spring-boot-starter-web</artifactId>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
/**
4040
* @author wind57
4141
*/
42-
public class AbstractKubernetesProfileEnvironmentPostProcessorTest {
42+
public class KubernetesProfileEnvironmentPostProcessorTest {
4343

4444
private static final String FOUNT_IT = "foundIt";
4545

‎spring-cloud-kubernetes-controllers/spring-cloud-kubernetes-configuration-watcher/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@
6666
<artifactId>spring-rabbit-test</artifactId>
6767
<scope>test</scope>
6868
</dependency>
69-
<dependency>
70-
<groupId>org.junit.vintage</groupId>
71-
<artifactId>junit-vintage-engine</artifactId>
72-
<scope>test</scope>
73-
</dependency>
7469
<dependency>
7570
<groupId>org.mockito</groupId>
7671
<artifactId>mockito-inline</artifactId>

‎spring-cloud-kubernetes-examples/kubernetes-leader-election-example/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@
4949
<artifactId>spring-boot-starter-test</artifactId>
5050
<scope>test</scope>
5151
</dependency>
52-
<dependency>
53-
<groupId>org.junit.vintage</groupId>
54-
<artifactId>junit-vintage-engine</artifactId>
55-
<scope>test</scope>
56-
</dependency>
5752
</dependencies>
5853

5954
<build>

‎spring-cloud-kubernetes-fabric8-config/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,6 @@
9797
<artifactId>spring-boot-starter-test</artifactId>
9898
<scope>test</scope>
9999
</dependency>
100-
<dependency>
101-
<groupId>org.junit.vintage</groupId>
102-
<artifactId>junit-vintage-engine</artifactId>
103-
<scope>test</scope>
104-
</dependency>
105100
<dependency>
106101
<groupId>org.springframework.cloud</groupId>
107102
<artifactId>spring-cloud-test-support</artifactId>

‎spring-cloud-kubernetes-fabric8-discovery/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,6 @@
6868
<scope>test</scope>
6969
</dependency>
7070

71-
<dependency>
72-
<groupId>org.junit.vintage</groupId>
73-
<artifactId>junit-vintage-engine</artifactId>
74-
<scope>test</scope>
75-
</dependency>
76-
7771
<dependency>
7872
<groupId>org.springframework.boot</groupId>
7973
<artifactId>spring-boot-starter-web</artifactId>

‎spring-cloud-kubernetes-test-support/pom.xml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@
1313

1414
<properties>
1515
<awaitility.version>4.0.3</awaitility.version>
16-
<testcontainers.version>1.18.0</testcontainers.version>
16+
<testcontainers.version>1.19.0</testcontainers.version>
1717
</properties>
1818
<dependencies>
19+
20+
<dependency>
21+
<groupId>org.junit.platform</groupId>
22+
<artifactId>junit-platform-launcher</artifactId>
23+
</dependency>
24+
1925
<dependency>
2026
<groupId>org.springframework.boot</groupId>
2127
<artifactId>spring-boot-starter-test</artifactId>
@@ -43,4 +49,24 @@
4349

4450
</dependencies>
4551

52+
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.codehaus.mojo</groupId>
56+
<artifactId>exec-maven-plugin</artifactId>
57+
<version>3.1.0</version>
58+
<executions>
59+
<execution>
60+
<goals>
61+
<goal>java</goal>
62+
</goals>
63+
</execution>
64+
</executions>
65+
<configuration>
66+
<mainClass>org.springframework.cloud.kubernetes.tests.discovery.TestsDiscovery</mainClass>
67+
</configuration>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
4672
</project>

‎spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/K8SUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
import org.springframework.util.StringUtils;
7070

7171
import static org.awaitility.Awaitility.await;
72-
import static org.junit.Assert.fail;
72+
import static org.junit.jupiter.api.Assertions.fail;
7373

7474
/**
7575
* @author Ryan Baxter

‎spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/fabric8_client/Util.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
import org.springframework.cloud.kubernetes.integration.tests.commons.Phase;
5151

5252
import static org.awaitility.Awaitility.await;
53-
import static org.junit.Assert.fail;
53+
import static org.junit.jupiter.api.Assertions.fail;
5454
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.loadImage;
5555
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.pomVersion;
5656
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.pullImage;

‎spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
import org.springframework.cloud.kubernetes.integration.tests.commons.Phase;
6161

6262
import static org.awaitility.Awaitility.await;
63-
import static org.junit.Assert.fail;
63+
import static org.junit.jupiter.api.Assertions.fail;
6464
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.loadImage;
6565
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.pomVersion;
6666
import static org.springframework.cloud.kubernetes.integration.tests.commons.Commons.pullImage;

‎spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/tests/commons/junit_extension/DisabledTestsCondition.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
*
3636
* @author wind57
3737
*/
38+
@Deprecated
3839
public class DisabledTestsCondition implements ExecutionCondition {
3940

4041
private static final boolean SKIP_RUNNING_TESTS = "true".equals(System.getProperty("spring.cloud.k8s.skip.tests"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2013-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.tests.discovery;
18+
19+
import java.io.File;
20+
import java.net.URI;
21+
import java.net.URL;
22+
import java.net.URLClassLoader;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.nio.file.Paths;
26+
import java.util.List;
27+
import java.util.Set;
28+
import java.util.stream.Collectors;
29+
import java.util.stream.Stream;
30+
31+
import org.junit.platform.engine.discovery.DiscoverySelectors;
32+
import org.junit.platform.launcher.Launcher;
33+
import org.junit.platform.launcher.LauncherDiscoveryRequest;
34+
import org.junit.platform.launcher.TestIdentifier;
35+
import org.junit.platform.launcher.TestPlan;
36+
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
37+
import org.junit.platform.launcher.core.LauncherFactory;
38+
39+
/**
40+
* @author wind57
41+
*/
42+
public class TestsDiscovery {
43+
44+
public static void main(String[] args) throws Exception {
45+
List<String> targetClasses = entireClasspath().stream().filter(x -> x.contains("target/classes")).toList();
46+
List<String> targetTestClasses = targetClasses.stream().map(x -> x.replace("classes", "test-classes")).toList();
47+
List<String> jars = entireClasspath().stream().filter(x -> x.contains(".jar")).toList();
48+
49+
List<URL> urls = Stream.of(targetClasses, targetTestClasses, jars).flatMap(List::stream)
50+
.map(x -> toURL(new File(x).toPath().toUri())).toList();
51+
52+
Set<Path> paths = Stream.of(targetClasses, targetTestClasses, jars).flatMap(List::stream).map(Paths::get)
53+
.collect(Collectors.toSet());
54+
55+
replaceClassloader(urls);
56+
57+
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
58+
.selectors(DiscoverySelectors.selectClasspathRoots(paths)).build();
59+
60+
Launcher launcher = LauncherFactory.openSession().getLauncher();
61+
TestPlan testPlan = launcher.discover(request);
62+
testPlan.getRoots().stream().flatMap(x -> testPlan.getChildren(x).stream())
63+
.map(TestIdentifier::getLegacyReportingName).sorted().forEach(test -> {
64+
System.out.println("spring.cloud.k8s.test.to.run -> " + test);
65+
});
66+
}
67+
68+
private static void replaceClassloader(List<URL> classpathURLs) {
69+
ClassLoader parentClassLoader = Thread.currentThread().getContextClassLoader();
70+
URLClassLoader classLoader = URLClassLoader.newInstance(classpathURLs.toArray(new URL[0]), parentClassLoader);
71+
Thread.currentThread().setContextClassLoader(classLoader);
72+
}
73+
74+
// /tmp/deps.txt are created by the pipeline
75+
private static List<String> entireClasspath() throws Exception {
76+
return Files.lines(Paths.get("/tmp/deps.txt")).distinct().collect(Collectors.toList());
77+
}
78+
79+
private static URL toURL(URI uri) {
80+
try {
81+
return uri.toURL();
82+
}
83+
catch (Exception e) {
84+
throw new RuntimeException(e);
85+
}
86+
}
87+
88+
}

‎spring-cloud-kubernetes-test-support/src/main/resources/junit-platform.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎src/checkstyle/checkstyle-suppressions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
<suppress files=".*DiscoveryServerApplication\.java" checks="HideUtilityClassConstructor"/>
1717
<suppress files=".*KubernetesConfigServerApplication\.java" checks="HideUtilityClassConstructor"/>
1818
<suppress files=".*Fabric8ConfigContext\.java" checks="WhitespaceAround" />
19+
<suppress files=".*TestsDiscovery\.java" checks="HideUtilityClassConstructor" />
1920
</suppressions>

0 commit comments

Comments
 (0)
Failed to load comments.