Skip to content

Commit 14a61d4

Browse files
committed
Adjust check for mock kube server to avoid need for class preloading
1 parent 807bfbc commit 14a61d4

File tree

2 files changed

+16
-47
lines changed

2 files changed

+16
-47
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.quarkus.kubernetes.client.deployment;
22

3-
import java.util.Arrays;
43
import java.util.function.BooleanSupplier;
54

65
/**
@@ -13,13 +12,22 @@
1312
* to avoid starting a Kubernetes test container in such a case.
1413
*/
1514
class NoQuarkusTestKubernetesClient implements BooleanSupplier {
16-
static final String IO_QUARKUS_TEST_KUBERNETES_CLIENT_PACKAGE = "io.quarkus.test.kubernetes.client";
17-
static final Boolean IO_QUARKUS_TEST_KUBERNETES_CLIENT_AVAILABLE = Arrays.stream(Package.getPackages())
18-
.map(Package::getName)
19-
.anyMatch(p -> p.startsWith(IO_QUARKUS_TEST_KUBERNETES_CLIENT_PACKAGE));
15+
static final String IO_QUARKUS_TEST_KUBERNETES_CLIENT_PACKAGE_CLASS = "io.quarkus.test.kubernetes.client.AbstractKubernetesTestResource";
16+
// We cannot assume what order classes are loaded in, so check if a known class can be loaded rather than looking at what's already loaded
17+
static final boolean IO_QUARKUS_TEST_KUBERNETES_CLIENT_AVAILABLE = isClassAvailable(
18+
IO_QUARKUS_TEST_KUBERNETES_CLIENT_PACKAGE_CLASS);
2019

2120
@Override
2221
public boolean getAsBoolean() {
2322
return !IO_QUARKUS_TEST_KUBERNETES_CLIENT_AVAILABLE;
2423
}
24+
25+
private static boolean isClassAvailable(String className) {
26+
try {
27+
Class.forName(className);
28+
return true;
29+
} catch (ClassNotFoundException e) {
30+
return false;
31+
}
32+
}
2533
}

test-framework/junit5/src/main/java/io/quarkus/test/junit/classloading/FacadeClassLoader.java

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,6 @@ public Class<?> loadClass(String name) throws ClassNotFoundException {
323323
}
324324

325325
if (isQuarkusTest && !isIntegrationTest) {
326-
327-
preloadTestResourceClasses(inspectionClass);
328326
QuarkusClassLoader runtimeClassLoader = getQuarkusClassLoader(inspectionClass, profile);
329327
Class<?> clazz = runtimeClassLoader.loadClass(name);
330328

@@ -334,14 +332,13 @@ public Class<?> loadClass(String name) throws ClassNotFoundException {
334332
}
335333

336334
} catch (NoSuchMethodException e) {
337-
// TODO better handling of these
338-
System.err.println("Could not get method " + e);
335+
log.error("Could not get method " + e);
339336
throw new RuntimeException(e);
340337
} catch (InvocationTargetException e) {
341-
System.err.println("Could not invoke " + e);
338+
log.error("Could not invoke " + e);
342339
throw new RuntimeException(e);
343340
} catch (IllegalAccessException e) {
344-
System.err.println("Could not access " + e);
341+
log.error("Could not access " + e);
345342
throw new RuntimeException(e);
346343
}
347344

@@ -378,42 +375,6 @@ private boolean registersQuarkusTestExtensionWithExtendsWith(Class<?> inspection
378375

379376
}
380377

381-
/*
382-
* What's this for?
383-
* It's a bit like detecting the location in a privacy test or detecting the lab environment in an emissions test and then
384-
* deciding how to behave.
385-
* We're special-casing behaviour for a hard-coded selection of test packages. Yuck!
386-
* TODO Hopefully, once https://github.com/quarkusio/quarkus/issues/45785 is done, it will not be needed.
387-
* Some tests, especially in kubernetes-client and openshift-client, check config to decide whether to start a dev service.
388-
* That happens at augmentation, which happens before test execution.
389-
* In the old model, the test class would have already been loaded by JUnit first, and it would have had a chance to write
390-
* config to the system properties.
391-
* That config would influence whether dev services were started.
392-
* TODO even without 45785 it might be nice to find a better way, perhaps rewriting the AbstractKubernetesTestResource test
393-
* resource to work differently?
394-
*
395-
*/
396-
private void preloadTestResourceClasses(Class<?> fromCanary) {
397-
try {
398-
Class<Annotation> ca = (Class<Annotation>) peekingClassLoader
399-
.loadClass("io.quarkus.test.common.QuarkusTestResource");
400-
List<Annotation> ans = AnnotationSupport.findRepeatableAnnotations(fromCanary, ca);
401-
for (Annotation a : ans) {
402-
Method m = a
403-
.getClass()
404-
.getMethod(VALUE);
405-
Class<?> resourceClass = (Class<?>) m.invoke(a);
406-
// Only do this hack for the resources we know need it, since it can cause failures in other areas
407-
if (resourceClass.getName().contains("Kubernetes")) {
408-
getParent().loadClass(resourceClass.getName());
409-
}
410-
}
411-
} catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
412-
// In some projects, these classes are not on the canary classpath. That's fine, we know there's nothing to preload.
413-
log.debug("Canary classloader could not preload test resources:" + e);
414-
}
415-
}
416-
417378
private boolean registersQuarkusTestExtensionOnField(Class<?> inspectionClass) {
418379

419380
try {

0 commit comments

Comments
 (0)