From c14db6d1084fd3702c187ef145e0174e64ead998 Mon Sep 17 00:00:00 2001 From: Thuan Vo Date: Tue, 23 Jul 2024 19:41:06 -0700 Subject: [PATCH] test(scorecard): use testdata from local filesystem instead of cm. Notes: Dockerfile needs updating to include the testdata directory. --- Makefile | 6 ++++-- internal/test/scorecard/clients.go | 11 +++++++++-- internal/test/scorecard/common_utils.go | 2 +- internal/test/scorecard/tests.go | 18 ++++++++++-------- internal/test/scorecard/types.go | 4 ++-- 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 1df20cef..8b78d202 100644 --- a/Makefile +++ b/Makefile @@ -226,7 +226,6 @@ $(KUSTOMIZE) build internal/images/custom-scorecard-tests/rbac/ | $(CLUSTER_CLIE --docker-username="$(SCORECARD_REGISTRY_USERNAME)" --docker-password="$(SCORECARD_REGISTRY_PASSWORD)"; \ $(CLUSTER_CLIENT) patch sa cryostat-scorecard -n $(SCORECARD_NAMESPACE) -p '{"imagePullSecrets": [{"name": "registry-key"}]}'; \ fi -$(CLUSTER_CLIENT) create -n $(SCORECARD_NAMESPACE) configmap scorecard-jfr-cm --from-file=internal/test/scorecard/testdata/scorecard_sample.jfr $(OPERATOR_SDK) run bundle -n $(SCORECARD_NAMESPACE) --timeout 20m $(BUNDLE_IMG) --security-context-config=restricted $(SCORECARD_ARGS) endef @@ -244,7 +243,10 @@ endef define scorecard-local for test in $${SCORECARD_TEST_SELECTION//,/ }; do \ echo "Running scorecard test \"$${test}\""; \ - SCORECARD_NAMESPACE=$(SCORECARD_NAMESPACE) BUNDLE_DIR=./bundle go run internal/images/custom-scorecard-tests/main.go $${test} | sed 's/\\n/\n/g'; \ + SCORECARD_NAMESPACE=$(SCORECARD_NAMESPACE) \ + BUNDLE_DIR=./bundle \ + TESTDATA_DIR=./internal/test/scorecard/testdata \ + go run internal/images/custom-scorecard-tests/main.go $${test} | sed 's/\\n/\n/g'; \ done endef diff --git a/internal/test/scorecard/clients.go b/internal/test/scorecard/clients.go index 6d786fa0..6dcb7425 100644 --- a/internal/test/scorecard/clients.go +++ b/internal/test/scorecard/clients.go @@ -24,6 +24,7 @@ import ( "mime/multipart" "net/http" "net/url" + "os" "strings" "time" @@ -520,7 +521,7 @@ func (client *RecordingClient) ListArchives(ctx context.Context, target *Target) return graphQLResponse.Data.TargetNodes[0].Target.ArchivedRecordings.Data, nil } -func (client *RecordingClient) UploadArchive(ctx context.Context, archiveContent []byte) error { +func (client *RecordingClient) UploadArchive(ctx context.Context, filePath string) error { url := client.Base.JoinPath("/api/v1/recordings") body := &bytes.Buffer{} @@ -531,7 +532,13 @@ func (client *RecordingClient) UploadArchive(ctx context.Context, archiveContent return err } - if _, err = part.Write(archiveContent); err != nil { + file, err := os.Open(filePath) + if err != nil { + return err + } + defer file.Close() + + if _, err = io.Copy(part, file); err != nil { return err } diff --git a/internal/test/scorecard/common_utils.go b/internal/test/scorecard/common_utils.go index 7f3f7352..aac24844 100644 --- a/internal/test/scorecard/common_utils.go +++ b/internal/test/scorecard/common_utils.go @@ -42,8 +42,8 @@ import ( const ( operatorDeploymentName string = "cryostat-operator-controller-manager" - jfrConfigMapName string = "scorecard-jfr-cm" jfrFilename string = "scorecard_sample.jfr" + podTestDataRoot string = "/testdata" testTimeout time.Duration = time.Minute * 10 ) diff --git a/internal/test/scorecard/tests.go b/internal/test/scorecard/tests.go index cf0e2687..ac957a65 100644 --- a/internal/test/scorecard/tests.go +++ b/internal/test/scorecard/tests.go @@ -18,12 +18,13 @@ import ( "context" "fmt" "net/url" + "os" + "path" "time" operatorv1beta2 "github.com/cryostatio/cryostat-operator/api/v1beta2" scapiv1alpha3 "github.com/operator-framework/api/pkg/apis/scorecard/v1alpha3" apimanifests "github.com/operator-framework/api/pkg/manifests" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) const ( @@ -340,13 +341,14 @@ func CryostatGrafanaTest(bundle *apimanifests.Bundle, namespace string, openShif apiClient := NewCryostatRESTClientset(base) - // Get JFR data - cm, err := r.Client.CoreV1().ConfigMaps(namespace).Get(context.Background(), jfrConfigMapName, metav1.GetOptions{}) - if err != nil { - return r.fail(fmt.Sprintf("failed to get ConfigMap containing JFR data: %s", err.Error())) + // Get the path to the testdata directory from fromTESTDATA_DIR environment variable + // If empty, assume running within a pod and use "/testdata" + testDataDir := os.Getenv("TESTDATA_DIR") + if len(testDataDir) == 0 { + testDataDir = podTestDataRoot } - err = apiClient.Recordings().UploadArchive(context.Background(), cm.BinaryData[jfrFilename]) + err = apiClient.Recordings().UploadArchive(context.Background(), path.Join(testDataDir, jfrFilename)) if err != nil { return r.fail(fmt.Sprintf("failed to upload archive %s: %s", jfrFilename, err.Error())) } @@ -362,7 +364,7 @@ func CryostatGrafanaTest(bundle *apimanifests.Bundle, namespace string, openShif return r.fail(fmt.Sprintf("failed to get datasource %s: %s", GRAFANA_DATASOURCE_NAME, err.Error())) } - if err = datasource.Valid(); err != nil { + if err = datasource.IsValid(); err != nil { return r.fail(fmt.Sprintf("datasource %s is invalid: %s", GRAFANA_DATASOURCE_NAME, err.Error())) } @@ -371,7 +373,7 @@ func CryostatGrafanaTest(bundle *apimanifests.Bundle, namespace string, openShif return r.fail(fmt.Sprintf("failed to get dashboard %s: %s", GRAFANA_DASHBOARD_UID, err.Error())) } - if err = dashboard.Valid(); err != nil { + if err = dashboard.IsValid(); err != nil { return r.fail(fmt.Sprintf("dashboard %s is invalid: %s", GRAFANA_DASHBOARD_UID, err.Error())) } diff --git a/internal/test/scorecard/types.go b/internal/test/scorecard/types.go index fc34e2d7..cbbc89b6 100644 --- a/internal/test/scorecard/types.go +++ b/internal/test/scorecard/types.go @@ -177,7 +177,7 @@ type DataSource struct { BasicAuth bool `json:"basicAuth"` } -func (datasource *DataSource) Valid() error { +func (datasource *DataSource) IsValid() error { if datasource.Name != GRAFANA_DATASOURCE_NAME { return fmt.Errorf("expected datasource name %s, but got %s", GRAFANA_DATASOURCE_NAME, datasource.Name) } @@ -246,7 +246,7 @@ type PanelQueryOptions struct { Data string `json:"data"` } -func (dashboard *DashBoard) Valid() error { +func (dashboard *DashBoard) IsValid() error { if dashboard.UID != GRAFANA_DASHBOARD_UID { return fmt.Errorf("expected dashboard uid %s, but got %s", GRAFANA_DASHBOARD_UID, dashboard.UID) }