diff --git a/junit5/pom.xml b/junit5/pom.xml index 295549fc..1863c833 100644 --- a/junit5/pom.xml +++ b/junit5/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 @@ -44,5 +45,4 @@ - \ No newline at end of file diff --git a/junit5/src/main/java/cz/xtf/junit5/extensions/OpenShiftRecorderService.java b/junit5/src/main/java/cz/xtf/junit5/extensions/OpenShiftRecorderService.java index c925d9a6..94723d61 100644 --- a/junit5/src/main/java/cz/xtf/junit5/extensions/OpenShiftRecorderService.java +++ b/junit5/src/main/java/cz/xtf/junit5/extensions/OpenShiftRecorderService.java @@ -32,6 +32,10 @@ import io.fabric8.openshift.api.model.DeploymentConfig; import io.fabric8.openshift.api.model.ImageStream; import io.fabric8.openshift.api.model.Route; +import io.fabric8.openshift.api.model.operatorhub.v1.OperatorGroup; +import io.fabric8.openshift.api.model.operatorhub.v1alpha1.ClusterServiceVersion; +import io.fabric8.openshift.api.model.operatorhub.v1alpha1.InstallPlan; +import io.fabric8.openshift.api.model.operatorhub.v1alpha1.Subscription; /** * Record OpenShift isolated state relative to a test. @@ -213,6 +217,10 @@ public void recordState(ExtensionContext context) throws IOException { !isMasterAndBuildNamespaceSame() ? getFilter(context, BUILD_FILTER_MASTER) : null); saveEvents(context, getFilter(context, EVENT_FILTER_MASTER), !isMasterAndBuildNamespaceSame() ? getFilter(context, EVENT_FILTER_BUILDS) : null); + saveClusterServiceVersions(context); + saveInstallPlans(context); + saveOperatorGroups(context); + saveSubscriptions(context); } private boolean isFilterInitializationComplete(ExtensionContext context) { @@ -521,6 +529,39 @@ protected void saveBuildLogs(ExtensionContext context, ResourcesFilterBuilder printer = ResourcesPrinterHelper + .forClusterServiceVersion(logPath)) { + OpenShifts.admin().operatorHub().clusterServiceVersions().inNamespace(OpenShifts.master().getNamespace()) + .list().getItems().stream().forEach(printer::row); + } + } + + protected void saveInstallPlans(ExtensionContext context) throws IOException { + final Path logPath = Paths.get(attachmentsDir(), dirNameForTest(context), "installPlans.log"); + try (final ResourcesPrinterHelper printer = ResourcesPrinterHelper.forInstallPlan(logPath)) { + OpenShifts.admin().operatorHub().installPlans().inNamespace(OpenShifts.master().getNamespace()) + .list().getItems().stream().forEach(printer::row); + } + } + + protected void saveOperatorGroups(ExtensionContext context) throws IOException { + final Path logPath = Paths.get(attachmentsDir(), dirNameForTest(context), "operatorGroups.log"); + try (final ResourcesPrinterHelper printer = ResourcesPrinterHelper.forOperatorGroup(logPath)) { + OpenShifts.admin().operatorHub().operatorGroups().inNamespace(OpenShifts.master().getNamespace()) + .list().getItems().stream().forEach(printer::row); + } + } + + protected void saveSubscriptions(ExtensionContext context) throws IOException { + final Path logPath = Paths.get(attachmentsDir(), dirNameForTest(context), "subscriptions.log"); + try (final ResourcesPrinterHelper printer = ResourcesPrinterHelper.forSubscription(logPath)) { + OpenShifts.admin().operatorHub().subscriptions().inNamespace(OpenShifts.master().getNamespace()) + .list().getItems().stream().forEach(printer::row); + } + } + private String attachmentsDir() { return JUnitConfig.recordDir() != null ? JUnitConfig.recordDir() : System.getProperty("user.dir"); } diff --git a/junit5/src/main/java/cz/xtf/junit5/extensions/helpers/ResourcesPrinterHelper.java b/junit5/src/main/java/cz/xtf/junit5/extensions/helpers/ResourcesPrinterHelper.java index d08f2cf4..f6bdd2c7 100644 --- a/junit5/src/main/java/cz/xtf/junit5/extensions/helpers/ResourcesPrinterHelper.java +++ b/junit5/src/main/java/cz/xtf/junit5/extensions/helpers/ResourcesPrinterHelper.java @@ -10,6 +10,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.function.Function; +import java.util.stream.Collectors; import io.fabric8.kubernetes.api.model.ConfigMap; import io.fabric8.kubernetes.api.model.ContainerStatus; @@ -23,6 +24,10 @@ import io.fabric8.openshift.api.model.DeploymentConfig; import io.fabric8.openshift.api.model.ImageStream; import io.fabric8.openshift.api.model.Route; +import io.fabric8.openshift.api.model.operatorhub.v1.OperatorGroup; +import io.fabric8.openshift.api.model.operatorhub.v1alpha1.ClusterServiceVersion; +import io.fabric8.openshift.api.model.operatorhub.v1alpha1.InstallPlan; +import io.fabric8.openshift.api.model.operatorhub.v1alpha1.Subscription; public class ResourcesPrinterHelper implements AutoCloseable { private final Path file; @@ -189,6 +194,72 @@ private static LinkedHashMap getServicesCols(Service service) { return map; } + public static ResourcesPrinterHelper forClusterServiceVersion(Path filePath) { + return new ResourcesPrinterHelper<>(filePath, + ResourcesPrinterHelper::getClusterServiceVersionCols); + } + + private static LinkedHashMap getClusterServiceVersionCols(ClusterServiceVersion csv) { + LinkedHashMap map = new LinkedHashMap<>(5); + map.put("NAME", csv.getMetadata().getName()); + map.put("CREATED", csv.getMetadata().getCreationTimestamp()); + map.put("PHASE", csv.getStatus().getPhase()); + map.put("REASON", csv.getStatus().getReason()); + map.put("MESSAGE", csv.getStatus().getMessage()); + return map; + } + + public static ResourcesPrinterHelper forInstallPlan(Path filePath) { + return new ResourcesPrinterHelper<>(filePath, + ResourcesPrinterHelper::getInstallPlanCols); + } + + private static LinkedHashMap getInstallPlanCols(InstallPlan ip) { + LinkedHashMap map = new LinkedHashMap<>(6); + map.put("NAME", ip.getMetadata().getName()); + map.put("CREATED", ip.getMetadata().getCreationTimestamp()); + map.put("PHASE", ip.getStatus().getPhase()); + map.put("CATALOG SOURCES", ip.getStatus().getCatalogSources().toString()); + map.put("CSVS", ip.getSpec().getClusterServiceVersionNames().toString()); + map.put("CONDITIONS", ip.getStatus().getConditions() + .stream() + .filter(cond -> "True".equalsIgnoreCase(cond.getStatus())) + .map(cond -> cond.getType()) + .collect(Collectors.joining(",", "[", "]"))); + return map; + } + + public static ResourcesPrinterHelper forOperatorGroup(Path filePath) { + return new ResourcesPrinterHelper<>(filePath, + ResourcesPrinterHelper::getOperatorGroupCols); + } + + private static LinkedHashMap getOperatorGroupCols(OperatorGroup og) { + LinkedHashMap map = new LinkedHashMap<>(3); + map.put("NAME", og.getMetadata().getName()); + map.put("TARGET NAMESPACE", og.getSpec().getTargetNamespaces().toString()); + map.put("NAMESPACES", og.getStatus().getNamespaces().toString()); + return map; + } + + public static ResourcesPrinterHelper forSubscription(Path filePath) { + return new ResourcesPrinterHelper<>(filePath, + ResourcesPrinterHelper::getSubscriptionCols); + } + + private static LinkedHashMap getSubscriptionCols(Subscription subscription) { + LinkedHashMap map = new LinkedHashMap<>(8); + map.put("NAME", subscription.getMetadata().getName()); + map.put("SOURCE", subscription.getSpec().getSource()); + map.put("CHANNEL", subscription.getSpec().getChannel()); + map.put("STARTING CSV", subscription.getSpec().getStartingCSV()); + map.put("INSTALLED CSV", subscription.getStatus().getInstalledCSV()); + map.put("CURRENT CSV", subscription.getStatus().getCurrentCSV()); + map.put("STATE", subscription.getStatus().getState()); + map.put("REASON", subscription.getStatus().getReason()); + return map; + } + public void row(X resource) { row(resourceToCols.apply(resource)); }