Skip to content

Commit

Permalink
test(scorecard): use testdata from local filesystem instead of cm.
Browse files Browse the repository at this point in the history
Notes: Dockerfile needs updating to include the testdata directory.
  • Loading branch information
tthvo committed Jul 24, 2024
1 parent be8c351 commit 61ae9cc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
11 changes: 9 additions & 2 deletions internal/test/scorecard/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"mime/multipart"
"net/http"
"net/url"
"os"
"strings"
"time"

Expand Down Expand Up @@ -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{}
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion internal/test/scorecard/common_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
18 changes: 10 additions & 8 deletions internal/test/scorecard/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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()))
}
Expand All @@ -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()))
}

Expand All @@ -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()))
}

Expand Down
4 changes: 2 additions & 2 deletions internal/test/scorecard/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 61ae9cc

Please sign in to comment.