From b300689a645e3741ccd5468e09acf20b5b9fb4d2 Mon Sep 17 00:00:00 2001 From: Thuan Vo Date: Wed, 15 May 2024 07:22:51 -0700 Subject: [PATCH] feat(scorecard): allow running scorecard test locally (#821) * feat(scorecard): allow running scorecard test locally Signed-off-by: Thuan Vo * feat(make): make target to run scorecard locally --------- Signed-off-by: Thuan Vo --- Makefile | 25 +++++++++++++++++++ .../images/custom-scorecard-tests/main.go | 11 ++++++-- internal/test/scorecard/clients.go | 5 ++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 536066819..655faca4a 100644 --- a/Makefile +++ b/Makefile @@ -158,6 +158,9 @@ ifneq ($(SCORECARD_TEST_SUITE),) SCORECARD_TEST_SELECTOR := --selector=suite=$(SCORECARD_TEST_SUITE) endif +# Specify whether to run scorecard tests only (without setup) +SCORECARD_TEST_ONLY ?= false + ##@ General .PHONY: all @@ -187,6 +190,21 @@ ifneq ($(SKIP_TESTS), true) $(OPERATOR_SDK) scorecard -n $(SCORECARD_NAMESPACE) -s cryostat-scorecard -w 20m $(BUNDLE_IMG) --pod-security=restricted $(SCORECARD_TEST_SELECTOR) endif +.PHONY: test-scorecard-local +test-scorecard-local: check_cert_manager kustomize operator-sdk ## Run scorecard test locally without rebuilding bundle. +ifneq ($(SKIP_TESTS), true) +ifeq ($(SCORECARD_TEST_SELECTION),) + @echo "No test selected. Use SCORECARD_TEST_SELECTION to specify tests. For example: SCORECARD_TEST_SELECTION=cryostat-recording make test-scorecard-local" +else ifeq ($(SCORECARD_TEST_ONLY), true) + @$(call scorecard-local) +else + @$(call scorecard-setup) + $(call scorecard-cleanup) ; \ + trap cleanup EXIT ; \ + $(call scorecard-local) +endif +endif + .PHONY: clean-scorecard clean-scorecard: operator-sdk ## Clean up scorecard resources. - $(call scorecard-cleanup); cleanup @@ -222,6 +240,13 @@ function cleanup { \ } 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}; \ +done +endef + ##@ Build .PHONY: manager diff --git a/internal/images/custom-scorecard-tests/main.go b/internal/images/custom-scorecard-tests/main.go index 5592e766f..3ef4d98e4 100644 --- a/internal/images/custom-scorecard-tests/main.go +++ b/internal/images/custom-scorecard-tests/main.go @@ -51,8 +51,15 @@ func main() { log.Fatal("SCORECARD_NAMESPACE environment variable not set") } - // Read the pod's untar'd bundle from a well-known path. - bundle, err := apimanifests.GetBundleFromDir(podBundleRoot) + // Get the path to the bundle from BUNDLE_DIR environment variable + // If empty, assume running within a pod and use a well-known path to the pod's untar'd bundle + bundleDir := os.Getenv("BUNDLE_DIR") + if len(bundleDir) == 0 { + bundleDir = podBundleRoot + } + + // Read the bundle from the specified path + bundle, err := apimanifests.GetBundleFromDir(bundleDir) if err != nil { log.Fatalf("failed to read bundle manifest: %s", err.Error()) } diff --git a/internal/test/scorecard/clients.go b/internal/test/scorecard/clients.go index d1adb198a..781afd8ce 100644 --- a/internal/test/scorecard/clients.go +++ b/internal/test/scorecard/clients.go @@ -30,6 +30,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/serializer" "k8s.io/apimachinery/pkg/util/wait" + "sigs.k8s.io/controller-runtime/pkg/client/config" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes" @@ -51,7 +52,7 @@ func (c *CryostatClientset) OperatorCRDs() *OperatorCRDClient { // NewClientset creates a CryostatClientset func NewClientset() (*CryostatClientset, error) { // Get in-cluster REST config from pod - config, err := rest.InClusterConfig() + config, err := config.GetConfig() if err != nil { return nil, err } @@ -583,7 +584,7 @@ func NewHttpRequest(ctx context.Context, method string, url string, body *string } // Authentication for OpenShift SSO - config, err := rest.InClusterConfig() + config, err := config.GetConfig() if err != nil { return nil, fmt.Errorf("failed to get in-cluster configurations: %s", err.Error()) }