Skip to content

Commit

Permalink
test(scorecard): add container logs for report sidecard test (#772)
Browse files Browse the repository at this point in the history
* test(scorecard): add container logs for report sidecard test

odified:   internal/test/scorecard/common_utils.go

* test(scorecard): clean up
  • Loading branch information
tthvo authored Apr 16, 2024
1 parent 6b63e3f commit 4b8ad76
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: |-
{
Expand Down
12 changes: 6 additions & 6 deletions bundle/tests/scorecard/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions config/scorecard/patches/custom.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
42 changes: 34 additions & 8 deletions internal/test/scorecard/common_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
41 changes: 32 additions & 9 deletions internal/test/scorecard/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions internal/test/scorecard/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 4b8ad76

Please sign in to comment.