Skip to content

Commit

Permalink
feat(scorecard): allow running scorecard test locally (#821)
Browse files Browse the repository at this point in the history
* feat(scorecard): allow running scorecard test locally

Signed-off-by: Thuan Vo <[email protected]>

* feat(make): make target to run scorecard locally

---------

Signed-off-by: Thuan Vo <[email protected]>
  • Loading branch information
tthvo authored May 15, 2024
1 parent 9fa1007 commit b300689
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions internal/images/custom-scorecard-tests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
5 changes: 3 additions & 2 deletions internal/test/scorecard/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down Expand Up @@ -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())
}
Expand Down

0 comments on commit b300689

Please sign in to comment.