From 4b8ad764340a9bd034e92c83f836fad6f4d05d85 Mon Sep 17 00:00:00 2001 From: Thuan Vo Date: Tue, 16 Apr 2024 15:05:39 -0400 Subject: [PATCH] test(scorecard): add container logs for report sidecard test (#772) * test(scorecard): add container logs for report sidecard test odified: internal/test/scorecard/common_utils.go * test(scorecard): clean up --- ...yostat-operator.clusterserviceversion.yaml | 2 +- bundle/tests/scorecard/config.yaml | 12 +++--- config/scorecard/patches/custom.config.yaml | 12 +++--- internal/test/scorecard/common_utils.go | 42 +++++++++++++++---- internal/test/scorecard/logger.go | 41 ++++++++++++++---- internal/test/scorecard/tests.go | 5 +++ 6 files changed, 84 insertions(+), 30 deletions(-) diff --git a/bundle/manifests/cryostat-operator.clusterserviceversion.yaml b/bundle/manifests/cryostat-operator.clusterserviceversion.yaml index 1f978f085..02e4261ff 100644 --- a/bundle/manifests/cryostat-operator.clusterserviceversion.yaml +++ b/bundle/manifests/cryostat-operator.clusterserviceversion.yaml @@ -54,7 +54,7 @@ metadata: capabilities: Seamless Upgrades categories: Monitoring, Developer Tools containerImage: quay.io/cryostat/cryostat-operator:2.5.0-dev - createdAt: "2024-04-04T17:17:25Z" + createdAt: "2024-04-05T22:43:31Z" description: JVM monitoring and profiling tool operatorframework.io/initialization-resource: |- { diff --git a/bundle/tests/scorecard/config.yaml b/bundle/tests/scorecard/config.yaml index f536a45d3..3bdd1b03a 100644 --- a/bundle/tests/scorecard/config.yaml +++ b/bundle/tests/scorecard/config.yaml @@ -70,7 +70,7 @@ stages: - entrypoint: - cryostat-scorecard-tests - operator-install - image: quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240404171629 + image: quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240405224154 labels: suite: cryostat test: operator-install @@ -80,7 +80,7 @@ stages: - entrypoint: - cryostat-scorecard-tests - cryostat-cr - image: quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240404171629 + image: quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240405224154 labels: suite: cryostat test: cryostat-cr @@ -90,7 +90,7 @@ stages: - entrypoint: - cryostat-scorecard-tests - cryostat-multi-namespace - image: quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240404171629 + image: quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240405224154 labels: suite: cryostat test: cryostat-multi-namespace @@ -100,7 +100,7 @@ stages: - entrypoint: - cryostat-scorecard-tests - cryostat-recording - image: quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240404171629 + image: quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240405224154 labels: suite: cryostat test: cryostat-recording @@ -110,7 +110,7 @@ stages: - entrypoint: - cryostat-scorecard-tests - cryostat-config-change - image: quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240404171629 + image: quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240405224154 labels: suite: cryostat test: cryostat-config-change @@ -120,7 +120,7 @@ stages: - entrypoint: - cryostat-scorecard-tests - cryostat-report - image: quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240404171629 + image: quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240405224154 labels: suite: cryostat test: cryostat-report diff --git a/config/scorecard/patches/custom.config.yaml b/config/scorecard/patches/custom.config.yaml index 333688c77..fb8b6b2ce 100644 --- a/config/scorecard/patches/custom.config.yaml +++ b/config/scorecard/patches/custom.config.yaml @@ -8,7 +8,7 @@ entrypoint: - cryostat-scorecard-tests - operator-install - image: "quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240404171725" + image: "quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240405224154" labels: suite: cryostat test: operator-install @@ -18,7 +18,7 @@ entrypoint: - cryostat-scorecard-tests - cryostat-cr - image: "quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240404171725" + image: "quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240405224154" labels: suite: cryostat test: cryostat-cr @@ -28,7 +28,7 @@ entrypoint: - cryostat-scorecard-tests - cryostat-multi-namespace - image: "quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240404171725" + image: "quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240405224154" labels: suite: cryostat test: cryostat-multi-namespace @@ -38,7 +38,7 @@ entrypoint: - cryostat-scorecard-tests - cryostat-recording - image: "quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240404171725" + image: "quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240405224154" labels: suite: cryostat test: cryostat-recording @@ -48,7 +48,7 @@ entrypoint: - cryostat-scorecard-tests - cryostat-config-change - image: "quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240404171725" + image: "quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240405224154" labels: suite: cryostat test: cryostat-config-change @@ -58,7 +58,7 @@ entrypoint: - cryostat-scorecard-tests - cryostat-report - image: "quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240404171725" + image: "quay.io/cryostat/cryostat-operator-scorecard:2.5.0-20240405224154" labels: suite: cryostat test: cryostat-report diff --git a/internal/test/scorecard/common_utils.go b/internal/test/scorecard/common_utils.go index 6bc7e6afa..981259bd4 100644 --- a/internal/test/scorecard/common_utils.go +++ b/internal/test/scorecard/common_utils.go @@ -600,26 +600,52 @@ func (r *TestResources) getCryostatPodNameForCR(cr *operatorv1beta1.Cryostat) (s selector := metav1.LabelSelector{ MatchLabels: map[string]string{ "app": cr.Name, + "kind": "cryostat", "component": "cryostat", }, } - opts := metav1.ListOptions{ - LabelSelector: labels.Set(selector.MatchLabels).String(), + + names, err := r.getPodnamesForSelector(cr.Namespace, selector) + if err != nil { + return "", err } - ctx, cancel := context.WithTimeout(context.TODO(), testTimeout) - defer cancel() + if len(names) == 0 { + return "", fmt.Errorf("no matching cryostat pods for cr: %s", cr.Name) + } + return names[0].ObjectMeta.Name, nil +} + +func (r *TestResources) getReportPodNameForCR(cr *operatorv1beta1.Cryostat) (string, error) { + selector := metav1.LabelSelector{ + MatchLabels: map[string]string{ + "app": cr.Name, + "kind": "cryostat", + "component": "reports", + }, + } - pods, err := r.Client.CoreV1().Pods(cr.Namespace).List(ctx, opts) + names, err := r.getPodnamesForSelector(cr.Namespace, selector) if err != nil { return "", err } - if len(pods.Items) == 0 { - return "", fmt.Errorf("no matching cryostat pods for cr: %s", cr.Name) + if len(names) == 0 { + return "", fmt.Errorf("no matching report sidecar pods for cr: %s", cr.Name) } + return names[0].ObjectMeta.Name, nil +} + +func (r *TestResources) getPodnamesForSelector(namespace string, selector metav1.LabelSelector) ([]corev1.Pod, error) { + labelSelector := labels.Set(selector.MatchLabels).String() - return pods.Items[0].ObjectMeta.Name, nil + ctx, cancel := context.WithTimeout(context.TODO(), testTimeout) + defer cancel() + + pods, err := r.Client.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{ + LabelSelector: labelSelector, + }) + return pods.Items, err } func (r *TestResources) cleanupClusterCryostat(name string, namespace string, targetNamespaces []string) { diff --git a/internal/test/scorecard/logger.go b/internal/test/scorecard/logger.go index dd38d79b7..414439639 100644 --- a/internal/test/scorecard/logger.go +++ b/internal/test/scorecard/logger.go @@ -25,12 +25,16 @@ import ( ) type ContainerLog struct { + Namespace string + Pod string Container string Log string } func (r *TestResources) logContainer(namespace, podName, containerName string) { containerLog := &ContainerLog{ + Namespace: namespace, + Pod: podName, Container: containerName, } buf := &strings.Builder{} @@ -77,27 +81,46 @@ func (r *TestResources) CollectContainersLogsToResult() { logs := r.CollectLogs() for _, log := range logs { if log != nil { - r.Log += fmt.Sprintf("\n%s CONTAINER LOG:\n\n\t%s\n", strings.ToUpper(log.Container), log.Log) + r.Log += fmt.Sprintf("\nNAMESPACE: %s\nPOD: %s\nCONTAINER: %s\nLOG:\n\t%s\n", + strings.ToUpper(log.Namespace), + strings.ToUpper(log.Pod), + strings.ToUpper(log.Container), + log.Log, + ) } } } func (r *TestResources) StartLogs(cr *operatorv1beta1.Cryostat) error { - podName, err := r.getCryostatPodNameForCR(cr) + cryostatPodName, err := r.getCryostatPodNameForCR(cr) if err != nil { return fmt.Errorf("failed to get pod name for CR: %s", err.Error()) } - containerNames := []string{ - cr.Name, - cr.Name + "-grafana", - cr.Name + "-jfr-datasource", + logSelections := map[string][]string{ + cryostatPodName: { + cr.Name, + cr.Name + "-grafana", + cr.Name + "-jfr-datasource", + }, } + bufferSize := 3 - r.LogChannel = make(chan *ContainerLog, len(containerNames)) + if cr.Spec.ReportOptions != nil && cr.Spec.ReportOptions.Replicas > 0 { + reportPodName, err := r.getReportPodNameForCR(cr) + if err != nil { + return fmt.Errorf("failed to get pod name for report sidecar: %s", err.Error()) + } + logSelections[reportPodName] = []string{cr.Name + "-reports"} + bufferSize++ + } - for _, containerName := range containerNames { - go r.logContainer(cr.Namespace, podName, containerName) + r.LogChannel = make(chan *ContainerLog, bufferSize) + + for pod, containers := range logSelections { + for _, container := range containers { + go r.logContainer(cr.Namespace, pod, container) + } } return nil diff --git a/internal/test/scorecard/tests.go b/internal/test/scorecard/tests.go index c5d7e5b86..751105a28 100644 --- a/internal/test/scorecard/tests.go +++ b/internal/test/scorecard/tests.go @@ -342,5 +342,10 @@ func CryostatReportTest(bundle *apimanifests.Bundle, namespace string, openShift return r.fail(fmt.Sprintf("failed to reach the application: %s", err.Error())) } + err = r.StartLogs(cr) + if err != nil { + r.Log += fmt.Sprintf("failed to retrieve logs for the application: %s", err.Error()) + } + return r.TestResult }