From 0b380d227f7b047571f8adf0a2467281ac3a5758 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 27 Mar 2024 14:49:14 -0400 Subject: [PATCH] test(scorecard): fix rebasing skipped commit (#780) (#782) * Merge pull request #8 from ebaron/scorecard-methods test(scorecard): use methods for more easily passing data * update bundle image (cherry picked from commit b3970953d2bfcc2cf2add227a8a0687f6a65b52c) Co-authored-by: Ming Yu Wang <90855268+mwangggg@users.noreply.github.com> --- ...yostat-operator.clusterserviceversion.yaml | 2 +- bundle/tests/scorecard/config.yaml | 10 +- config/scorecard/patches/custom.config.yaml | 10 +- .../images/custom-scorecard-tests/main.go | 10 +- internal/test/scorecard/common_utils.go | 113 ++++++-------- internal/test/scorecard/logger.go | 36 +++-- internal/test/scorecard/openshift.go | 3 +- internal/test/scorecard/tests.go | 142 +++++++++--------- 8 files changed, 151 insertions(+), 175 deletions(-) diff --git a/bundle/manifests/cryostat-operator.clusterserviceversion.yaml b/bundle/manifests/cryostat-operator.clusterserviceversion.yaml index 295b91d3d..d9a0669a7 100644 --- a/bundle/manifests/cryostat-operator.clusterserviceversion.yaml +++ b/bundle/manifests/cryostat-operator.clusterserviceversion.yaml @@ -53,7 +53,7 @@ metadata: capabilities: Seamless Upgrades categories: Monitoring, Developer Tools containerImage: quay.io/cryostat/cryostat-operator:3.0.0-dev - createdAt: "2024-03-27T18:00:37Z" + createdAt: "2024-03-27T18:30:58Z" 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 2d65c780a..30f793a8d 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:3.0.0-20240327175250 + image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327182927 labels: suite: cryostat test: operator-install @@ -80,7 +80,7 @@ stages: - entrypoint: - cryostat-scorecard-tests - cryostat-cr - image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327175250 + image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327182927 labels: suite: cryostat test: cryostat-cr @@ -90,7 +90,7 @@ stages: - entrypoint: - cryostat-scorecard-tests - cryostat-recording - image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327175250 + image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327182927 labels: suite: cryostat test: cryostat-recording @@ -100,7 +100,7 @@ stages: - entrypoint: - cryostat-scorecard-tests - cryostat-config-change - image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327175250 + image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327182927 labels: suite: cryostat test: cryostat-config-change @@ -110,7 +110,7 @@ stages: - entrypoint: - cryostat-scorecard-tests - cryostat-report - image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327175250 + image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327182927 labels: suite: cryostat test: cryostat-report diff --git a/config/scorecard/patches/custom.config.yaml b/config/scorecard/patches/custom.config.yaml index 5b91daab8..cba5454f8 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:3.0.0-20240327175250" + image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327182927" labels: suite: cryostat test: operator-install @@ -18,7 +18,7 @@ entrypoint: - cryostat-scorecard-tests - cryostat-cr - image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327175250" + image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327182927" labels: suite: cryostat test: cryostat-cr @@ -28,7 +28,7 @@ entrypoint: - cryostat-scorecard-tests - cryostat-recording - image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327175250" + image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327182927" labels: suite: cryostat test: cryostat-recording @@ -38,7 +38,7 @@ entrypoint: - cryostat-scorecard-tests - cryostat-config-change - image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327175250" + image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327182927" labels: suite: cryostat test: cryostat-config-change @@ -48,7 +48,7 @@ entrypoint: - cryostat-scorecard-tests - cryostat-report - image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327175250" + image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240327182927" labels: suite: cryostat test: cryostat-report diff --git a/internal/images/custom-scorecard-tests/main.go b/internal/images/custom-scorecard-tests/main.go index b3041281c..cce16f5a6 100644 --- a/internal/images/custom-scorecard-tests/main.go +++ b/internal/images/custom-scorecard-tests/main.go @@ -111,15 +111,15 @@ func runTests(testNames []string, bundle *apimanifests.Bundle, namespace string, for _, testName := range testNames { switch testName { case tests.OperatorInstallTestName: - results = append(results, tests.OperatorInstallTest(bundle, namespace)) + results = append(results, *tests.OperatorInstallTest(bundle, namespace, openShiftCertManager)) case tests.CryostatCRTestName: - results = append(results, tests.CryostatCRTest(bundle, namespace, openShiftCertManager)) + results = append(results, *tests.CryostatCRTest(bundle, namespace, openShiftCertManager)) case tests.CryostatRecordingTestName: - results = append(results, tests.CryostatRecordingTest(bundle, namespace, openShiftCertManager)) + results = append(results, *tests.CryostatRecordingTest(bundle, namespace, openShiftCertManager)) case tests.CryostatConfigChangeTestName: - results = append(results, tests.CryostatConfigChangeTest(bundle, namespace, openShiftCertManager)) + results = append(results, *tests.CryostatConfigChangeTest(bundle, namespace, openShiftCertManager)) case tests.CryostatReportTestName: - results = append(results, tests.CryostatReportTest(bundle, namespace, openShiftCertManager)) + results = append(results, *tests.CryostatReportTest(bundle, namespace, openShiftCertManager)) default: log.Fatalf("unknown test found: %s", testName) } diff --git a/internal/test/scorecard/common_utils.go b/internal/test/scorecard/common_utils.go index ea134a2ac..99c639ee0 100644 --- a/internal/test/scorecard/common_utils.go +++ b/internal/test/scorecard/common_utils.go @@ -35,7 +35,6 @@ import ( "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/wait" - "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/scheme" ) @@ -51,10 +50,9 @@ type TestResources struct { *scapiv1alpha3.TestResult } -func waitForDeploymentAvailability(ctx context.Context, client *CryostatClientset, namespace string, - name string, r *scapiv1alpha3.TestResult) error { +func (r *TestResources) waitForDeploymentAvailability(ctx context.Context, namespace string, name string) error { err := wait.PollImmediateUntilWithContext(ctx, time.Second, func(ctx context.Context) (done bool, err error) { - deploy, err := client.AppsV1().Deployments(namespace).Get(ctx, name, metav1.GetOptions{}) + deploy, err := r.Client.AppsV1().Deployments(namespace).Get(ctx, name, metav1.GetOptions{}) if err != nil { if kerrors.IsNotFound(err) { r.Log += fmt.Sprintf("deployment %s is not yet found\n", name) @@ -79,7 +77,7 @@ func waitForDeploymentAvailability(ctx context.Context, client *CryostatClientse return false, nil }) if err != nil { - logErr := logWorkloadEvents(r, client, namespace, name) + logErr := r.logWorkloadEvents(namespace, name) if logErr != nil { r.Log += fmt.Sprintf("failed to look up deployment errors: %s\n", logErr.Error()) } @@ -87,20 +85,20 @@ func waitForDeploymentAvailability(ctx context.Context, client *CryostatClientse return err } -func logError(r *scapiv1alpha3.TestResult, message string) { +func (r *TestResources) logError(message string) { r.State = scapiv1alpha3.FailState r.Errors = append(r.Errors, message) } -func fail(r scapiv1alpha3.TestResult, message string) scapiv1alpha3.TestResult { +func (r *TestResources) fail(message string) *scapiv1alpha3.TestResult { r.State = scapiv1alpha3.FailState r.Errors = append(r.Errors, message) - return r + return r.TestResult } -func logWorkloadEvents(r *scapiv1alpha3.TestResult, client *CryostatClientset, namespace string, name string) error { +func (r *TestResources) logWorkloadEvents(namespace string, name string) error { ctx := context.Background() - deploy, err := client.AppsV1().Deployments(namespace).Get(ctx, name, metav1.GetOptions{}) + deploy, err := r.Client.AppsV1().Deployments(namespace).Get(ctx, name, metav1.GetOptions{}) if err != nil { if kerrors.IsNotFound(err) { return nil @@ -115,7 +113,7 @@ func logWorkloadEvents(r *scapiv1alpha3.TestResult, client *CryostatClientset, n } r.Log += fmt.Sprintf("deployment %s warning events:\n", deploy.Name) - err = logEvents(r, client, namespace, scheme.Scheme, deploy) + err = r.logEvents(namespace, scheme.Scheme, deploy) if err != nil { return err } @@ -125,7 +123,7 @@ func logWorkloadEvents(r *scapiv1alpha3.TestResult, client *CryostatClientset, n if err != nil { return err } - replicaSets, err := client.AppsV1().ReplicaSets(namespace).List(ctx, metav1.ListOptions{ + replicaSets, err := r.Client.AppsV1().ReplicaSets(namespace).List(ctx, metav1.ListOptions{ LabelSelector: selector.String(), }) if err != nil { @@ -138,14 +136,14 @@ func logWorkloadEvents(r *scapiv1alpha3.TestResult, client *CryostatClientset, n condition.Reason, condition.Message) } r.Log += fmt.Sprintf("replica set %s warning events:\n", rs.Name) - err = logEvents(r, client, namespace, scheme.Scheme, &rs) + err = r.logEvents(namespace, scheme.Scheme, &rs) if err != nil { return err } } // Look up pods for deployment and log conditions and events - pods, err := client.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{ + pods, err := r.Client.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{ LabelSelector: selector.String(), }) if err != nil { @@ -159,7 +157,7 @@ func logWorkloadEvents(r *scapiv1alpha3.TestResult, client *CryostatClientset, n condition.Reason, condition.Message) } r.Log += fmt.Sprintf("pod %s warning events:\n", pod.Name) - err = logEvents(r, client, namespace, scheme.Scheme, &pod) + err = r.logEvents(namespace, scheme.Scheme, &pod) if err != nil { return err } @@ -167,9 +165,8 @@ func logWorkloadEvents(r *scapiv1alpha3.TestResult, client *CryostatClientset, n return nil } -func logEvents(r *scapiv1alpha3.TestResult, client *CryostatClientset, namespace string, - scheme *runtime.Scheme, obj runtime.Object) error { - events, err := client.CoreV1().Events(namespace).Search(scheme, obj) +func (r *TestResources) logEvents(namespace string, scheme *runtime.Scheme, obj runtime.Object) error { + events, err := r.Client.CoreV1().Events(namespace).Search(scheme, obj) if err != nil { return err } @@ -181,11 +178,11 @@ func logEvents(r *scapiv1alpha3.TestResult, client *CryostatClientset, namespace return nil } -func LogWorkloadEventsOnError(r *scapiv1alpha3.TestResult, client *CryostatClientset, namespace string, name string) { +func (r *TestResources) LogWorkloadEventsOnError(namespace string, name string) { if len(r.Errors) > 0 { r.Log += "\nWORKLOAD EVENTS:\n" for _, deployName := range []string{name, name + "-reports"} { - logErr := logWorkloadEvents(r, client, namespace, deployName) + logErr := r.logWorkloadEvents(namespace, deployName) if logErr != nil { r.Log += fmt.Sprintf("failed to get workload logs: %s", logErr) } @@ -208,28 +205,26 @@ func newTestResources(testName string) *TestResources { } } -func setupCRTestResources(tr *TestResources, openShiftCertManager bool) error { - r := tr.TestResult - +func (r *TestResources) setupCRTestResources(openShiftCertManager bool) error { // Create a new Kubernetes REST client for this test client, err := NewClientset() if err != nil { - logError(r, fmt.Sprintf("failed to create client: %s", err.Error())) + r.logError(fmt.Sprintf("failed to create client: %s", err.Error())) return err } - tr.Client = client + r.Client = client openshift, err := isOpenShift(client) if err != nil { - logError(r, fmt.Sprintf("could not determine whether platform is OpenShift: %s", err.Error())) + r.logError(fmt.Sprintf("could not determine whether platform is OpenShift: %s", err.Error())) return err } - tr.OpenShift = openshift + r.OpenShift = openshift if openshift && openShiftCertManager { - err := installOpenShiftCertManager(r) + err := r.installOpenShiftCertManager() if err != nil { - logError(r, fmt.Sprintf("failed to install cert-manager Operator for Red Hat OpenShift: %s", err.Error())) + r.logError(fmt.Sprintf("failed to install cert-manager Operator for Red Hat OpenShift: %s", err.Error())) return err } } @@ -318,27 +313,24 @@ func newCryostatCR(name string, namespace string, withIngress bool) *operatorv1b return cr } -func createAndWaitTillCryostatAvailable(cr *operatorv1beta1.Cryostat, resources *TestResources) (*operatorv1beta1.Cryostat, error) { - client := resources.Client - r := resources.TestResult - - cr, err := client.OperatorCRDs().Cryostats(cr.Namespace).Create(context.Background(), cr) +func (r *TestResources) createAndWaitTillCryostatAvailable(cr *operatorv1beta1.Cryostat) (*operatorv1beta1.Cryostat, error) { + cr, err := r.Client.OperatorCRDs().Cryostats(cr.Namespace).Create(context.Background(), cr) if err != nil { - logError(r, fmt.Sprintf("failed to create Cryostat CR: %s", err.Error())) + r.logError(fmt.Sprintf("failed to create Cryostat CR: %s", err.Error())) return nil, err } // Poll the deployment until it becomes available or we timeout ctx, cancel := context.WithTimeout(context.Background(), testTimeout) defer cancel() - err = waitForDeploymentAvailability(ctx, client, cr.Namespace, cr.Name, r) + err = r.waitForDeploymentAvailability(ctx, cr.Namespace, cr.Name) if err != nil { - logError(r, fmt.Sprintf("Cryostat main deployment did not become available: %s", err.Error())) + r.logError(fmt.Sprintf("Cryostat main deployment did not become available: %s", err.Error())) return nil, err } err = wait.PollImmediateUntilWithContext(ctx, time.Second, func(ctx context.Context) (done bool, err error) { - cr, err = client.OperatorCRDs().Cryostats(cr.Namespace).Get(ctx, cr.Name) + cr, err = r.Client.OperatorCRDs().Cryostats(cr.Namespace).Get(ctx, cr.Name) if err != nil { return false, fmt.Errorf("failed to get Cryostat CR: %s", err.Error()) } @@ -349,7 +341,7 @@ func createAndWaitTillCryostatAvailable(cr *operatorv1beta1.Cryostat, resources return false, nil }) if err != nil { - logError(r, fmt.Sprintf("application URL not found in CR: %s", err.Error())) + r.logError(fmt.Sprintf("application URL not found in CR: %s", err.Error())) return nil, err } r.Log += fmt.Sprintf("application is available at %s\n", cr.Status.ApplicationURL) @@ -357,8 +349,8 @@ func createAndWaitTillCryostatAvailable(cr *operatorv1beta1.Cryostat, resources return cr, nil } -func waitTillCryostatReady(base *url.URL, resources *TestResources) error { - return sendHealthRequest(base, resources, func(resp *http.Response, r *scapiv1alpha3.TestResult) (done bool, err error) { +func (r *TestResources) waitTillCryostatReady(base *url.URL) error { + return r.sendHealthRequest(base, func(resp *http.Response, r *scapiv1alpha3.TestResult) (done bool, err error) { health := &HealthResponse{} err = ReadJSON(resp, health) if err != nil { @@ -375,14 +367,11 @@ func waitTillCryostatReady(base *url.URL, resources *TestResources) error { }) } -func waitTillReportReady(name string, namespace string, port int32, resources *TestResources) error { - client := resources.Client - r := resources.TestResult - +func (r *TestResources) waitTillReportReady(name string, namespace string, port int32) error { ctx, cancel := context.WithTimeout(context.Background(), testTimeout) defer cancel() - err := waitForDeploymentAvailability(ctx, client, namespace, name, r) + err := r.waitForDeploymentAvailability(ctx, namespace, name) if err != nil { return fmt.Errorf("report sidecar deployment did not become available: %s", err.Error()) } @@ -393,15 +382,14 @@ func waitTillReportReady(name string, namespace string, port int32, resources *T return fmt.Errorf("application URL is invalid: %s", err.Error()) } - return sendHealthRequest(base, resources, func(resp *http.Response, r *scapiv1alpha3.TestResult) (done bool, err error) { + return r.sendHealthRequest(base, func(resp *http.Response, r *scapiv1alpha3.TestResult) (done bool, err error) { r.Log += fmt.Sprintf("reports sidecar is ready at %s\n", base.String()) return true, nil }) } -func sendHealthRequest(base *url.URL, resources *TestResources, healthCheck func(resp *http.Response, r *scapiv1alpha3.TestResult) (done bool, err error)) error { +func (r *TestResources) sendHealthRequest(base *url.URL, healthCheck func(resp *http.Response, r *scapiv1alpha3.TestResult) (done bool, err error)) error { client := NewHttpClient() - r := resources.TestResult ctx, cancel := context.WithTimeout(context.Background(), testTimeout) defer cancel() @@ -430,16 +418,13 @@ func sendHealthRequest(base *url.URL, resources *TestResources, healthCheck func } return false, fmt.Errorf("API request failed with status code %d: %s", resp.StatusCode, ReadError(resp)) } - return healthCheck(resp, r) + return healthCheck(resp, r.TestResult) }) return err } -func updateAndWaitTillCryostatAvailable(cr *operatorv1beta1.Cryostat, resources *TestResources) error { - client := resources.Client - r := resources.TestResult - - cr, err := client.OperatorCRDs().Cryostats(cr.Namespace).Update(context.Background(), cr) +func (r *TestResources) updateAndWaitTillCryostatAvailable(cr *operatorv1beta1.Cryostat) error { + cr, err := r.Client.OperatorCRDs().Cryostats(cr.Namespace).Update(context.Background(), cr) if err != nil { r.Log += fmt.Sprintf("failed to update Cryostat CR: %s", err.Error()) return err @@ -449,7 +434,7 @@ func updateAndWaitTillCryostatAvailable(cr *operatorv1beta1.Cryostat, resources ctx, cancel := context.WithTimeout(context.Background(), testTimeout) defer cancel() err = wait.PollImmediateUntilWithContext(ctx, time.Second, func(ctx context.Context) (done bool, err error) { - deploy, err := client.AppsV1().Deployments(cr.Namespace).Get(ctx, cr.Name, metav1.GetOptions{}) + deploy, err := r.Client.AppsV1().Deployments(cr.Namespace).Get(ctx, cr.Name, metav1.GetOptions{}) if err != nil { if kerrors.IsNotFound(err) { r.Log += fmt.Sprintf("deployment %s is not yet found\n", cr.Name) @@ -501,10 +486,8 @@ func updateAndWaitTillCryostatAvailable(cr *operatorv1beta1.Cryostat, resources return err } -func cleanupAndLogs(r *scapiv1alpha3.TestResult, tr *TestResources, name string, namespace string) { - client := tr.Client - - LogWorkloadEventsOnError(r, client, namespace, name) +func (r *TestResources) cleanupAndLogs(name string, namespace string) { + r.LogWorkloadEventsOnError(namespace, name) cr := &operatorv1beta1.Cryostat{ ObjectMeta: metav1.ObjectMeta{ @@ -513,19 +496,19 @@ func cleanupAndLogs(r *scapiv1alpha3.TestResult, tr *TestResources, name string, }, } ctx := context.Background() - err := client.OperatorCRDs().Cryostats(cr.Namespace).Delete(ctx, cr.Name, &metav1.DeleteOptions{}) + err := r.Client.OperatorCRDs().Cryostats(cr.Namespace).Delete(ctx, cr.Name, &metav1.DeleteOptions{}) if err != nil { if !kerrors.IsNotFound(err) { r.Log += fmt.Sprintf("failed to delete Cryostat: %s\n", err.Error()) } } - if tr.LogChannel != nil { - CollectContainersLogsToResult(r, tr.LogChannel) + if r.LogChannel != nil { + r.CollectContainersLogsToResult() } } -func getCryostatPodNameForCR(clientset *kubernetes.Clientset, cr *operatorv1beta1.Cryostat) (string, error) { +func (r *TestResources) getCryostatPodNameForCR(cr *operatorv1beta1.Cryostat) (string, error) { selector := metav1.LabelSelector{ MatchLabels: map[string]string{ "app": cr.Name, @@ -539,7 +522,7 @@ func getCryostatPodNameForCR(clientset *kubernetes.Clientset, cr *operatorv1beta ctx, cancel := context.WithTimeout(context.TODO(), testTimeout) defer cancel() - pods, err := clientset.CoreV1().Pods(cr.Namespace).List(ctx, opts) + pods, err := r.Client.CoreV1().Pods(cr.Namespace).List(ctx, opts) if err != nil { return "", err } diff --git a/internal/test/scorecard/logger.go b/internal/test/scorecard/logger.go index 6b02761e0..dd38d79b7 100644 --- a/internal/test/scorecard/logger.go +++ b/internal/test/scorecard/logger.go @@ -21,9 +21,7 @@ import ( "strings" operatorv1beta1 "github.com/cryostatio/cryostat-operator/api/v1beta1" - scapiv1alpha3 "github.com/operator-framework/api/pkg/apis/scorecard/v1alpha3" v1 "k8s.io/api/core/v1" - "k8s.io/client-go/kubernetes" ) type ContainerLog struct { @@ -31,22 +29,22 @@ type ContainerLog struct { Log string } -func LogContainer(clientset *kubernetes.Clientset, namespace, podName, containerName string, ch chan *ContainerLog) { +func (r *TestResources) logContainer(namespace, podName, containerName string) { containerLog := &ContainerLog{ Container: containerName, } buf := &strings.Builder{} - err := GetContainerLogs(clientset, namespace, podName, containerName, buf) + err := r.GetContainerLogs(namespace, podName, containerName, buf) if err != nil { buf.WriteString(fmt.Sprintf("%s\n", err.Error())) } containerLog.Log = buf.String() - ch <- containerLog + r.LogChannel <- containerLog } -func GetContainerLogs(clientset *kubernetes.Clientset, namespace, podName, containerName string, dest io.Writer) error { +func (r *TestResources) GetContainerLogs(namespace, podName, containerName string, dest io.Writer) error { ctx, cancel := context.WithTimeout(context.TODO(), testTimeout) defer cancel() @@ -54,7 +52,7 @@ func GetContainerLogs(clientset *kubernetes.Clientset, namespace, podName, conta Follow: true, Container: containerName, } - stream, err := clientset.CoreV1().Pods(namespace).GetLogs(podName, logOptions).Stream(ctx) + stream, err := r.Client.CoreV1().Pods(namespace).GetLogs(podName, logOptions).Stream(ctx) if err != nil { return fmt.Errorf("failed to get logs for container %s in pod %s: %s", containerName, podName, err.Error()) } @@ -67,27 +65,27 @@ func GetContainerLogs(clientset *kubernetes.Clientset, namespace, podName, conta return nil } -func CollectLogs(ch chan *ContainerLog) []*ContainerLog { +func (r *TestResources) CollectLogs() []*ContainerLog { logs := make([]*ContainerLog, 0) - for i := 0; i < cap(ch); i++ { - logs = append(logs, <-ch) + for i := 0; i < cap(r.LogChannel); i++ { + logs = append(logs, <-r.LogChannel) } return logs } -func CollectContainersLogsToResult(result *scapiv1alpha3.TestResult, ch chan *ContainerLog) { - logs := CollectLogs(ch) +func (r *TestResources) CollectContainersLogsToResult() { + logs := r.CollectLogs() for _, log := range logs { if log != nil { - result.Log += fmt.Sprintf("\n%s CONTAINER LOG:\n\n\t%s\n", strings.ToUpper(log.Container), log.Log) + r.Log += fmt.Sprintf("\n%s CONTAINER LOG:\n\n\t%s\n", strings.ToUpper(log.Container), log.Log) } } } -func StartLogs(clientset *kubernetes.Clientset, cr *operatorv1beta1.Cryostat) (chan *ContainerLog, error) { - podName, err := getCryostatPodNameForCR(clientset, cr) +func (r *TestResources) StartLogs(cr *operatorv1beta1.Cryostat) error { + podName, err := r.getCryostatPodNameForCR(cr) if err != nil { - return nil, fmt.Errorf("failed to get pod name for CR: %s", err.Error()) + return fmt.Errorf("failed to get pod name for CR: %s", err.Error()) } containerNames := []string{ @@ -96,11 +94,11 @@ func StartLogs(clientset *kubernetes.Clientset, cr *operatorv1beta1.Cryostat) (c cr.Name + "-jfr-datasource", } - ch := make(chan *ContainerLog, len(containerNames)) + r.LogChannel = make(chan *ContainerLog, len(containerNames)) for _, containerName := range containerNames { - go LogContainer(clientset, cr.Namespace, podName, containerName, ch) + go r.logContainer(cr.Namespace, podName, containerName) } - return ch, nil + return nil } diff --git a/internal/test/scorecard/openshift.go b/internal/test/scorecard/openshift.go index 247068a48..7880221ce 100644 --- a/internal/test/scorecard/openshift.go +++ b/internal/test/scorecard/openshift.go @@ -21,7 +21,6 @@ import ( "time" "github.com/blang/semver/v4" - scapiv1alpha3 "github.com/operator-framework/api/pkg/apis/scorecard/v1alpha3" operatorsv1 "github.com/operator-framework/api/pkg/operators/v1" operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1" ctrl "sigs.k8s.io/controller-runtime" @@ -39,7 +38,7 @@ import ( corev1client "k8s.io/client-go/kubernetes/typed/core/v1" ) -func installOpenShiftCertManager(r *scapiv1alpha3.TestResult) error { +func (r *TestResources) installOpenShiftCertManager() error { ctx := context.Background() // Get in-cluster REST config from pod diff --git a/internal/test/scorecard/tests.go b/internal/test/scorecard/tests.go index 12a8bf2a3..9d3d251a6 100644 --- a/internal/test/scorecard/tests.go +++ b/internal/test/scorecard/tests.go @@ -37,76 +37,74 @@ const ( ) // OperatorInstallTest checks that the operator installed correctly -func OperatorInstallTest(bundle *apimanifests.Bundle, namespace string) (result scapiv1alpha3.TestResult) { - r := newEmptyTestResult(OperatorInstallTestName) +func OperatorInstallTest(bundle *apimanifests.Bundle, namespace string, openShiftCertManager bool) *scapiv1alpha3.TestResult { + r := newTestResources(OperatorInstallTestName) // Create a new Kubernetes REST client for this test - client, err := NewClientset() + err := r.setupCRTestResources(openShiftCertManager) if err != nil { - return fail(*r, fmt.Sprintf("failed to create client: %s", err.Error())) + return r.fail(fmt.Sprintf("failed to set up %s test: %s", OperatorInstallTestName, err.Error())) } + defer r.cleanupAndLogs(OperatorInstallTestName, namespace) // Poll the deployment until it becomes available or we timeout ctx, cancel := context.WithTimeout(context.Background(), testTimeout) defer cancel() - err = waitForDeploymentAvailability(ctx, client, namespace, operatorDeploymentName, r) + err = r.waitForDeploymentAvailability(ctx, namespace, operatorDeploymentName) if err != nil { - return fail(*r, fmt.Sprintf("operator deployment did not become available: %s", err.Error())) + return r.fail(fmt.Sprintf("operator deployment did not become available: %s", err.Error())) } - return *r + return r.TestResult } // CryostatCRTest checks that the operator installs Cryostat in response to a Cryostat CR -func CryostatCRTest(bundle *apimanifests.Bundle, namespace string, openShiftCertManager bool) (result scapiv1alpha3.TestResult) { - tr := newTestResources(CryostatCRTestName) - r := tr.TestResult +func CryostatCRTest(bundle *apimanifests.Bundle, namespace string, openShiftCertManager bool) *scapiv1alpha3.TestResult { + r := newTestResources(CryostatCRTestName) - err := setupCRTestResources(tr, openShiftCertManager) + err := r.setupCRTestResources(openShiftCertManager) if err != nil { - return fail(*r, fmt.Sprintf("failed to set up %s test: %s", CryostatCRTestName, err.Error())) + return r.fail(fmt.Sprintf("failed to set up %s test: %s", CryostatCRTestName, err.Error())) } - defer cleanupAndLogs(&result, tr, CryostatCRTestName, namespace) + defer r.cleanupAndLogs(CryostatCRTestName, namespace) // Create a default Cryostat CR - _, err = createAndWaitTillCryostatAvailable(newCryostatCR(CryostatCRTestName, namespace, !tr.OpenShift), tr) + _, err = r.createAndWaitTillCryostatAvailable(newCryostatCR(CryostatCRTestName, namespace, !r.OpenShift)) if err != nil { - return fail(*r, fmt.Sprintf("%s test failed: %s", CryostatCRTestName, err.Error())) + return r.fail(fmt.Sprintf("%s test failed: %s", CryostatCRTestName, err.Error())) } - return *r + return r.TestResult } -func CryostatConfigChangeTest(bundle *apimanifests.Bundle, namespace string, openShiftCertManager bool) (result scapiv1alpha3.TestResult) { - tr := newTestResources(CryostatConfigChangeTestName) - r := tr.TestResult +func CryostatConfigChangeTest(bundle *apimanifests.Bundle, namespace string, openShiftCertManager bool) *scapiv1alpha3.TestResult { + r := newTestResources(CryostatConfigChangeTestName) - err := setupCRTestResources(tr, openShiftCertManager) + err := r.setupCRTestResources(openShiftCertManager) if err != nil { - return fail(*r, fmt.Sprintf("failed to set up %s test: %s", CryostatConfigChangeTestName, err.Error())) + return r.fail(fmt.Sprintf("failed to set up %s test: %s", CryostatConfigChangeTestName, err.Error())) } - defer cleanupAndLogs(&result, tr, CryostatConfigChangeTestName, namespace) + defer r.cleanupAndLogs(CryostatConfigChangeTestName, namespace) // Create a default Cryostat CR with default empty dir - cr := newCryostatCR(CryostatConfigChangeTestName, namespace, !tr.OpenShift) + cr := newCryostatCR(CryostatConfigChangeTestName, namespace, !r.OpenShift) cr.Spec.StorageOptions = &operatorv1beta1.StorageConfiguration{ EmptyDir: &operatorv1beta1.EmptyDirConfig{ Enabled: true, }, } - _, err = createAndWaitTillCryostatAvailable(cr, tr) + _, err = r.createAndWaitTillCryostatAvailable(cr) if err != nil { - return fail(*r, fmt.Sprintf("failed to determine application URL: %s", err.Error())) + return r.fail(fmt.Sprintf("failed to determine application URL: %s", err.Error())) } // Switch Cryostat CR to PVC for redeployment ctx, cancel := context.WithTimeout(context.Background(), testTimeout) defer cancel() - client := tr.Client - cr, err = client.OperatorCRDs().Cryostats(namespace).Get(ctx, CryostatConfigChangeTestName) + cr, err = r.Client.OperatorCRDs().Cryostats(namespace).Get(ctx, CryostatConfigChangeTestName) if err != nil { - return fail(*r, fmt.Sprintf("failed to get Cryostat CR: %s", err.Error())) + return r.fail(fmt.Sprintf("failed to get Cryostat CR: %s", err.Error())) } cr.Spec.StorageOptions = &operatorv1beta1.StorageConfiguration{ PVC: &operatorv1beta1.PersistentVolumeClaimConfig{ @@ -122,54 +120,53 @@ func CryostatConfigChangeTest(bundle *apimanifests.Bundle, namespace string, ope } // Wait for redeployment of Cryostat CR - err = updateAndWaitTillCryostatAvailable(cr, tr) + err = r.updateAndWaitTillCryostatAvailable(cr) if err != nil { - return fail(*r, fmt.Sprintf("Cryostat redeployment did not become available: %s", err.Error())) + return r.fail(fmt.Sprintf("Cryostat redeployment did not become available: %s", err.Error())) } r.Log += "Cryostat deployment has successfully updated with new spec template\n" base, err := url.Parse(cr.Status.ApplicationURL) if err != nil { - return fail(*r, fmt.Sprintf("application URL is invalid: %s", err.Error())) + return r.fail(fmt.Sprintf("application URL is invalid: %s", err.Error())) } - err = waitTillCryostatReady(base, tr) + err = r.waitTillCryostatReady(base) if err != nil { - return fail(*r, fmt.Sprintf("failed to reach the application: %s", err.Error())) + return r.fail(fmt.Sprintf("failed to reach the application: %s", err.Error())) } - return *r + return r.TestResult } // TODO add a built in discovery test too -func CryostatRecordingTest(bundle *apimanifests.Bundle, namespace string, openShiftCertManager bool) (result scapiv1alpha3.TestResult) { - tr := newTestResources(CryostatRecordingTestName) - r := tr.TestResult +func CryostatRecordingTest(bundle *apimanifests.Bundle, namespace string, openShiftCertManager bool) *scapiv1alpha3.TestResult { + r := newTestResources(CryostatRecordingTestName) - err := setupCRTestResources(tr, openShiftCertManager) + err := r.setupCRTestResources(openShiftCertManager) if err != nil { - return fail(*r, fmt.Sprintf("failed to set up %s test: %s", CryostatRecordingTestName, err.Error())) + return r.fail(fmt.Sprintf("failed to set up %s test: %s", CryostatRecordingTestName, err.Error())) } - defer cleanupAndLogs(&result, tr, CryostatRecordingTestName, namespace) + defer r.cleanupAndLogs(CryostatRecordingTestName, namespace) // Create a default Cryostat CR - cr, err := createAndWaitTillCryostatAvailable(newCryostatCR(CryostatRecordingTestName, namespace, !tr.OpenShift), tr) + cr, err := r.createAndWaitTillCryostatAvailable(newCryostatCR(CryostatRecordingTestName, namespace, !r.OpenShift)) if err != nil { - return fail(*r, fmt.Sprintf("failed to determine application URL: %s", err.Error())) + return r.fail(fmt.Sprintf("failed to determine application URL: %s", err.Error())) } - tr.LogChannel, err = StartLogs(tr.Client.Clientset, cr) + err = r.StartLogs(cr) if err != nil { r.Log += fmt.Sprintf("failed to retrieve logs for the application: %s", err.Error()) } base, err := url.Parse(cr.Status.ApplicationURL) if err != nil { - return fail(*r, fmt.Sprintf("application URL is invalid: %s", err.Error())) + return r.fail(fmt.Sprintf("application URL is invalid: %s", err.Error())) } - err = waitTillCryostatReady(base, tr) + err = r.waitTillCryostatReady(base) if err != nil { - return fail(*r, fmt.Sprintf("failed to reach the application: %s", err.Error())) + return r.fail(fmt.Sprintf("failed to reach the application: %s", err.Error())) } apiClient := NewCryostatRESTClientset(base) @@ -181,15 +178,15 @@ func CryostatRecordingTest(bundle *apimanifests.Bundle, namespace string, openSh } target, err := apiClient.Targets().Create(context.Background(), targetOptions) if err != nil { - return fail(*r, fmt.Sprintf("failed to create a target: %s", err.Error())) + return r.fail(fmt.Sprintf("failed to create a target: %s", err.Error())) } r.Log += fmt.Sprintf("created a custom target: %+v\n", target) connectUrl := target.ConnectUrl jmxSecretName := CryostatRecordingTestName + "-jmx-auth" - secret, err := tr.Client.CoreV1().Secrets(namespace).Get(context.Background(), jmxSecretName, metav1.GetOptions{}) + secret, err := r.Client.CoreV1().Secrets(namespace).Get(context.Background(), jmxSecretName, metav1.GetOptions{}) if err != nil { - return fail(*r, fmt.Sprintf("failed to get jmx credentials: %s", err.Error())) + return r.fail(fmt.Sprintf("failed to get jmx credentials: %s", err.Error())) } credential := &Credential{ @@ -200,7 +197,7 @@ func CryostatRecordingTest(bundle *apimanifests.Bundle, namespace string, openSh err = apiClient.CredentialClient.Create(context.Background(), credential) if err != nil { - return fail(*r, fmt.Sprintf("failed to create stored credential: %s", err.Error())) + return r.fail(fmt.Sprintf("failed to create stored credential: %s", err.Error())) } r.Log += fmt.Sprintf("created stored credential with match expression: %s\n", credential.MatchExpression) @@ -218,14 +215,14 @@ func CryostatRecordingTest(bundle *apimanifests.Bundle, namespace string, openSh } rec, err := apiClient.Recordings().Create(context.Background(), connectUrl, options) if err != nil { - return fail(*r, fmt.Sprintf("failed to create a recording: %s", err.Error())) + return r.fail(fmt.Sprintf("failed to create a recording: %s", err.Error())) } r.Log += fmt.Sprintf("created a recording: %+v\n", rec) // View the current recording list after creating one recs, err := apiClient.Recordings().List(context.Background(), connectUrl) if err != nil { - return fail(*r, fmt.Sprintf("failed to list recordings: %s", err.Error())) + return r.fail(fmt.Sprintf("failed to list recordings: %s", err.Error())) } r.Log += fmt.Sprintf("current list of recordings: %+v\n", recs) @@ -235,66 +232,65 @@ func CryostatRecordingTest(bundle *apimanifests.Bundle, namespace string, openSh // Archive the recording archiveName, err := apiClient.Recordings().Archive(context.Background(), connectUrl, rec.Name) if err != nil { - return fail(*r, fmt.Sprintf("failed to archive the recording: %s", err.Error())) + return r.fail(fmt.Sprintf("failed to archive the recording: %s", err.Error())) } r.Log += fmt.Sprintf("archived the recording %s at: %s\n", rec.Name, archiveName) archives, err := apiClient.Recordings().ListArchives(context.Background(), connectUrl) if err != nil { - return fail(*r, fmt.Sprintf("failed to list archives: %s", err.Error())) + return r.fail(fmt.Sprintf("failed to list archives: %s", err.Error())) } r.Log += fmt.Sprintf("current list of archives: %+v\n", archives) report, err := apiClient.Recordings().GenerateReport(context.Background(), connectUrl, rec) if err != nil { - return fail(*r, fmt.Sprintf("failed to generate report for the recording: %s", err.Error())) + return r.fail(fmt.Sprintf("failed to generate report for the recording: %s", err.Error())) } r.Log += fmt.Sprintf("generated report for the recording %s: %+v\n", rec.Name, report) // Stop the recording err = apiClient.Recordings().Stop(context.Background(), connectUrl, rec.Name) if err != nil { - return fail(*r, fmt.Sprintf("failed to stop the recording %s: %s", rec.Name, err.Error())) + return r.fail(fmt.Sprintf("failed to stop the recording %s: %s", rec.Name, err.Error())) } // Get the recording to verify its state rec, err = apiClient.Recordings().Get(context.Background(), connectUrl, rec.Name) if err != nil { - return fail(*r, fmt.Sprintf("failed to get the recordings: %s", err.Error())) + return r.fail(fmt.Sprintf("failed to get the recordings: %s", err.Error())) } if rec.State != "STOPPED" { - return fail(*r, fmt.Sprintf("recording %s failed to stop: %s", rec.Name, err.Error())) + return r.fail(fmt.Sprintf("recording %s failed to stop: %s", rec.Name, err.Error())) } r.Log += fmt.Sprintf("stopped the recording: %s\n", rec.Name) // Delete the recording err = apiClient.Recordings().Delete(context.Background(), connectUrl, rec.Name) if err != nil { - return fail(*r, fmt.Sprintf("failed to delete the recording %s: %s", rec.Name, err.Error())) + return r.fail(fmt.Sprintf("failed to delete the recording %s: %s", rec.Name, err.Error())) } r.Log += fmt.Sprintf("deleted the recording: %s\n", rec.Name) // View the current recording list after deleting one recs, err = apiClient.Recordings().List(context.Background(), connectUrl) if err != nil { - return fail(*r, fmt.Sprintf("failed to list recordings: %s", err.Error())) + return r.fail(fmt.Sprintf("failed to list recordings: %s", err.Error())) } r.Log += fmt.Sprintf("current list of recordings: %+v\n", recs) - return *r + return r.TestResult } -func CryostatReportTest(bundle *apimanifests.Bundle, namespace string, openShiftCertManager bool) (result scapiv1alpha3.TestResult) { - tr := newTestResources(CryostatReportTestName) - r := tr.TestResult +func CryostatReportTest(bundle *apimanifests.Bundle, namespace string, openShiftCertManager bool) *scapiv1alpha3.TestResult { + r := newTestResources(CryostatReportTestName) - err := setupCRTestResources(tr, openShiftCertManager) + err := r.setupCRTestResources(openShiftCertManager) if err != nil { - return fail(*r, fmt.Sprintf("failed to set up %s test: %s", CryostatReportTestName, err.Error())) + return r.fail(fmt.Sprintf("failed to set up %s test: %s", CryostatReportTestName, err.Error())) } - defer cleanupAndLogs(&result, tr, CryostatReportTestName, namespace) + defer r.cleanupAndLogs(CryostatReportTestName, namespace) port := int32(10000) - cr := newCryostatCR(CryostatReportTestName, namespace, !tr.OpenShift) + cr := newCryostatCR(CryostatReportTestName, namespace, !r.OpenShift) cr.Spec.ReportOptions = &operatorv1beta1.ReportConfiguration{ Replicas: 1, } @@ -305,16 +301,16 @@ func CryostatReportTest(bundle *apimanifests.Bundle, namespace string, openShift } // Create a default Cryostat CR - cr, err = createAndWaitTillCryostatAvailable(cr, tr) + cr, err = r.createAndWaitTillCryostatAvailable(cr) if err != nil { - return fail(*r, fmt.Sprintf("%s test failed: %s", CryostatReportTestName, err.Error())) + return r.fail(fmt.Sprintf("%s test failed: %s", CryostatReportTestName, err.Error())) } // Query health of report sidecar - err = waitTillReportReady(cr.Name+"-reports", cr.Namespace, port, tr) + err = r.waitTillReportReady(cr.Name+"-reports", cr.Namespace, port) if err != nil { - return fail(*r, fmt.Sprintf("failed to reach the application: %s", err.Error())) + return r.fail(fmt.Sprintf("failed to reach the application: %s", err.Error())) } - return *r + return r.TestResult }