task: adds event diagnostics for failed tests (#26864)

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
This commit is contained in:
Steven Hawkins 2024-02-07 10:11:03 -05:00 committed by GitHub
parent fdfe41bdda
commit d03b25b4b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -18,11 +18,13 @@
package org.keycloak.operator.testsuite.integration;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.MicroTime;
import io.fabric8.kubernetes.api.model.NamespaceBuilder;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.StatefulSet;
import io.fabric8.kubernetes.api.model.events.v1.Event;
import io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
@ -49,6 +51,7 @@ import io.quarkus.test.junit.callback.QuarkusTestMethodContext;
import org.awaitility.Awaitility;
import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.logging.Logger.Level;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
@ -70,10 +73,14 @@ import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jakarta.enterprise.inject.Instance;
@ -308,6 +315,7 @@ public class BaseOperatorTest implements QuarkusTestAfterEachCallback {
return;
}
Log.warnf("Test failed with %s: %s", context.getTestStatus().getTestErrorCause().getMessage(), context.getTestStatus().getTestErrorCause().getClass().getName());
logEvents();
savePodLogs();
// provide some helpful entries in the main log as well
logFailedKeycloaks();
@ -351,6 +359,30 @@ public class BaseOperatorTest implements QuarkusTestAfterEachCallback {
});
}
private void logEvents() {
List<Event> recentEventList = k8sclient.resources(Event.class).list().getItems();
var grouped = recentEventList.stream()
.sorted(Comparator.comparing(BaseOperatorTest::getTime, Comparator.nullsFirst(Comparator.reverseOrder())))
.collect(
Collectors.groupingBy(
event -> java.util.Arrays.asList(event.getType(), event.getReason(),
event.getMetadata().getName(), event.getNote()),
LinkedHashMap::new, Collectors.toList()))
.entrySet().iterator();
for (int i = 0; i < 50 && grouped.hasNext(); i++) {
var entry = grouped.next();
Log.logf("Normal".equals(entry.getValue().get(0).getType()) ? Level.INFO : Level.WARN,
"Event last seen %s repeated %s times - %s", getTime(entry.getValue().get(0)),
entry.getValue().size(), entry.getKey().stream().collect(Collectors.joining(" ")));
}
}
private static String getTime(Event event) {
return Optional.ofNullable(event.getEventTime()).map(MicroTime::getTime).orElse(event.getDeprecatedLastTimestamp());
}
@AfterAll
public static void after() throws FileNotFoundException {