Skip to content

Commit

Permalink
test(scorecard): fix broken scorecard tests for 3.0 (#820)
Browse files Browse the repository at this point in the history
  • Loading branch information
tthvo authored May 13, 2024
1 parent efec98e commit 95fa911
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ metadata:
capabilities: Seamless Upgrades
categories: Monitoring, Developer Tools
containerImage: quay.io/cryostat/cryostat-operator:3.0.0-dev
createdAt: "2024-05-10T19:40:13Z"
createdAt: "2024-05-11T06:47:54Z"
description: JVM monitoring and profiling tool
operatorframework.io/initialization-resource: |-
{
Expand Down
12 changes: 6 additions & 6 deletions bundle/tests/scorecard/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ stages:
- entrypoint:
- cryostat-scorecard-tests
- operator-install
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416210645
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240511064726
labels:
suite: cryostat
test: operator-install
Expand All @@ -80,7 +80,7 @@ stages:
- entrypoint:
- cryostat-scorecard-tests
- cryostat-cr
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416210645
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240511064726
labels:
suite: cryostat
test: cryostat-cr
Expand All @@ -90,7 +90,7 @@ stages:
- entrypoint:
- cryostat-scorecard-tests
- cryostat-multi-namespace
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416210645
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240511064726
labels:
suite: cryostat
test: cryostat-multi-namespace
Expand All @@ -100,7 +100,7 @@ stages:
- entrypoint:
- cryostat-scorecard-tests
- cryostat-recording
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416210645
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240511064726
labels:
suite: cryostat
test: cryostat-recording
Expand All @@ -110,7 +110,7 @@ stages:
- entrypoint:
- cryostat-scorecard-tests
- cryostat-config-change
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416210645
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240511064726
labels:
suite: cryostat
test: cryostat-config-change
Expand All @@ -120,7 +120,7 @@ stages:
- entrypoint:
- cryostat-scorecard-tests
- cryostat-report
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240416210645
image: quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240511064726
labels:
suite: cryostat
test: cryostat-report
Expand Down
12 changes: 6 additions & 6 deletions config/scorecard/patches/custom.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
entrypoint:
- cryostat-scorecard-tests
- operator-install
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240510194331"
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240511064726"
labels:
suite: cryostat
test: operator-install
Expand All @@ -18,7 +18,7 @@
entrypoint:
- cryostat-scorecard-tests
- cryostat-cr
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240510194331"
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240511064726"
labels:
suite: cryostat
test: cryostat-cr
Expand All @@ -28,7 +28,7 @@
entrypoint:
- cryostat-scorecard-tests
- cryostat-multi-namespace
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240510194331"
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240511064726"
labels:
suite: cryostat
test: cryostat-multi-namespace
Expand All @@ -38,7 +38,7 @@
entrypoint:
- cryostat-scorecard-tests
- cryostat-recording
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240510194331"
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240511064726"
labels:
suite: cryostat
test: cryostat-recording
Expand All @@ -48,7 +48,7 @@
entrypoint:
- cryostat-scorecard-tests
- cryostat-config-change
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240510194331"
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240511064726"
labels:
suite: cryostat
test: cryostat-config-change
Expand All @@ -58,7 +58,7 @@
entrypoint:
- cryostat-scorecard-tests
- cryostat-report
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240510194331"
image: "quay.io/cryostat/cryostat-operator-scorecard:3.0.0-20240511064726"
labels:
suite: cryostat
test: cryostat-report
86 changes: 48 additions & 38 deletions internal/test/scorecard/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package scorecard

import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -276,8 +275,8 @@ type RecordingClient struct {
*commonCryostatRESTClient
}

func (client *RecordingClient) List(ctx context.Context, connectUrl string) ([]Recording, error) {
url := client.Base.JoinPath(fmt.Sprintf("/api/v1/targets/%s/recordings", url.PathEscape(connectUrl)))
func (client *RecordingClient) List(ctx context.Context, target *Target) ([]Recording, error) {
url := client.Base.JoinPath(fmt.Sprintf("/api/v3/targets/%d/recordings", target.Id))
header := make(http.Header)
header.Add("Accept", "*/*")

Expand All @@ -300,8 +299,8 @@ func (client *RecordingClient) List(ctx context.Context, connectUrl string) ([]R
return recordings, nil
}

func (client *RecordingClient) Get(ctx context.Context, connectUrl string, recordingName string) (*Recording, error) {
recordings, err := client.List(ctx, connectUrl)
func (client *RecordingClient) Get(ctx context.Context, target *Target, recordingName string) (*Recording, error) {
recordings, err := client.List(ctx, target)
if err != nil {
return nil, err
}
Expand All @@ -312,11 +311,11 @@ func (client *RecordingClient) Get(ctx context.Context, connectUrl string, recor
}
}

return nil, fmt.Errorf("recording %s does not exist for target %s", recordingName, connectUrl)
return nil, fmt.Errorf("recording %s does not exist for target %s", recordingName, target.ConnectUrl)
}

func (client *RecordingClient) Create(ctx context.Context, connectUrl string, options *RecordingCreateOptions) (*Recording, error) {
url := client.Base.JoinPath(fmt.Sprintf("/api/v1/targets/%s/recordings", url.PathEscape(connectUrl)))
func (client *RecordingClient) Create(ctx context.Context, target *Target, options *RecordingCreateOptions) (*Recording, error) {
url := client.Base.JoinPath(fmt.Sprintf("/api/v3/targets/%d/recordings", target.Id))
body := options.ToFormData()
header := make(http.Header)
header.Add("Content-Type", "application/x-www-form-urlencoded")
Expand All @@ -341,8 +340,8 @@ func (client *RecordingClient) Create(ctx context.Context, connectUrl string, op
return recording, err
}

func (client *RecordingClient) Archive(ctx context.Context, connectUrl string, recordingName string) (string, error) {
url := client.Base.JoinPath(fmt.Sprintf("/api/v1/targets/%s/recordings/%s", url.PathEscape(connectUrl), url.PathEscape(recordingName)))
func (client *RecordingClient) Archive(ctx context.Context, target *Target, recordingId uint32) (string, error) {
url := client.Base.JoinPath(fmt.Sprintf("/api/v3/targets/%d/recordings/%d", target.Id, recordingId))
body := "SAVE"
header := make(http.Header)
header.Add("Content-Type", "text/plain")
Expand All @@ -366,8 +365,8 @@ func (client *RecordingClient) Archive(ctx context.Context, connectUrl string, r
return bodyAsString, nil
}

func (client *RecordingClient) Stop(ctx context.Context, connectUrl string, recordingName string) error {
url := client.Base.JoinPath(fmt.Sprintf("/api/v1/targets/%s/recordings/%s", url.PathEscape(connectUrl), url.PathEscape(recordingName)))
func (client *RecordingClient) Stop(ctx context.Context, target *Target, recordingId uint32) error {
url := client.Base.JoinPath(fmt.Sprintf("/api/v3/targets/%d/recordings/%d", target.Id, recordingId))
body := "STOP"
header := make(http.Header)
header.Add("Content-Type", "text/plain")
Expand All @@ -386,8 +385,8 @@ func (client *RecordingClient) Stop(ctx context.Context, connectUrl string, reco
return nil
}

func (client *RecordingClient) Delete(ctx context.Context, connectUrl string, recordingName string) error {
url := client.Base.JoinPath(fmt.Sprintf("/api/v1/targets/%s/recordings/%s", url.PathEscape(connectUrl), url.PathEscape(recordingName)))
func (client *RecordingClient) Delete(ctx context.Context, target *Target, recordingId uint32) error {
url := client.Base.JoinPath(fmt.Sprintf("/api/v3/targets/%d/recordings/%d", target.Id, recordingId))
header := make(http.Header)

resp, err := SendRequest(ctx, client.Client, http.MethodDelete, url.String(), nil, header)
Expand All @@ -403,17 +402,17 @@ func (client *RecordingClient) Delete(ctx context.Context, connectUrl string, re
return nil
}

func (client *RecordingClient) GenerateReport(ctx context.Context, connectUrl string, recordingName *Recording) (map[string]interface{}, error) {
reportURL := recordingName.ReportURL

if len(reportURL) < 1 {
func (client *RecordingClient) GenerateReport(ctx context.Context, target *Target, recording *Recording) (map[string]interface{}, error) {
if len(recording.ReportURL) < 1 {
return nil, fmt.Errorf("report URL is not available")
}

reportURL := client.Base.JoinPath(recording.ReportURL)

header := make(http.Header)
header.Add("Accept", "application/json")

resp, err := SendRequest(ctx, client.Client, http.MethodGet, reportURL, nil, header)
resp, err := SendRequest(ctx, client.Client, http.MethodGet, reportURL.String(), nil, header)
if err != nil {
return nil, err
}
Expand All @@ -432,27 +431,34 @@ func (client *RecordingClient) GenerateReport(ctx context.Context, connectUrl st
return report, nil
}

func (client *RecordingClient) ListArchives(ctx context.Context, connectUrl string) ([]Archive, error) {
func (client *RecordingClient) ListArchives(ctx context.Context, target *Target) ([]Archive, error) {
url := client.Base.JoinPath("/api/v2.2/graphql")

query := &GraphQLQuery{
Query: `
query ArchivedRecordingsForTarget($connectUrl: String) {
archivedRecordings(filter: { sourceTarget: $connectUrl }) {
data {
name
downloadUrl
reportUrl
metadata {
labels
query ArchivedRecordingsForTarget($id: BigInteger!) {
targetNodes(filter: { targetIds: [$id] }) {
target {
archivedRecordings {
data {
name
downloadUrl
reportUrl
metadata {
labels {
key
value
}
}
size
}
}
size
}
}
}
`,
Variables: map[string]string{
connectUrl: connectUrl,
Variables: map[string]any{
"id": target.Id,
},
}
queryJSON, err := query.ToJSON()
Expand Down Expand Up @@ -481,7 +487,7 @@ func (client *RecordingClient) ListArchives(ctx context.Context, connectUrl stri
return nil, fmt.Errorf("failed to read response body: %s", err.Error())
}

return graphQLResponse.Data.ArchivedRecordings.Data, nil
return graphQLResponse.Data.TargetNodes[0].Target.ArchivedRecordings.Data, nil
}

type CredentialClient struct {
Expand Down Expand Up @@ -515,7 +521,7 @@ func ReadJSON(resp *http.Response, result interface{}) error {

err = json.Unmarshal(body, result)
if err != nil {
return err
return fmt.Errorf("failed to unmarshal JSON: %s, response body: %s ", err.Error(), body)
}
return nil
}
Expand Down Expand Up @@ -565,15 +571,19 @@ func NewHttpRequest(ctx context.Context, method string, url string, body *string
if err != nil {
return nil, err
}
if header != nil {
req.Header = header

req.Header = header
if req.Header == nil {
req.Header = make(http.Header)
}
// Authentication is only enabled on OCP. Ignored on k8s.

// Authentication for OpenShift SSO
config, err := rest.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("failed to get in-cluster configurations: %s", err.Error())
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", base64.StdEncoding.EncodeToString([]byte(config.BearerToken))))

req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", config.BearerToken))
return req, nil
}

Expand All @@ -583,7 +593,7 @@ func StatusOK(statusCode int) bool {

func SendRequest(ctx context.Context, httpClient *http.Client, method string, url string, body *string, header http.Header) (*http.Response, error) {
var response *http.Response
err := wait.PollImmediateUntilWithContext(ctx, time.Second, func(ctx context.Context) (done bool, err error) {
err := wait.PollUntilContextCancel(ctx, time.Second, true, func(ctx context.Context) (done bool, err error) {
// Create a new request
req, err := NewHttpRequest(ctx, method, url, body, header)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/test/scorecard/common_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type TestResources struct {
}

func (r *TestResources) waitForDeploymentAvailability(ctx context.Context, name string, namespace string) error {
err := wait.PollImmediateUntilWithContext(ctx, time.Second, func(ctx context.Context) (done bool, err error) {
err := wait.PollUntilContextCancel(ctx, time.Second, true, func(ctx context.Context) (done bool, err error) {
deploy, err := r.Client.AppsV1().Deployments(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
if kerrors.IsNotFound(err) {
Expand Down Expand Up @@ -344,7 +344,7 @@ func (r *TestResources) createAndWaitTillCryostatAvailable(cr *operatorv1beta2.C
return nil, err
}

err = wait.PollImmediateUntilWithContext(ctx, time.Second, func(ctx context.Context) (done bool, err error) {
err = wait.PollUntilContextCancel(ctx, time.Second, true, func(ctx context.Context) (done bool, err error) {
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())
Expand Down Expand Up @@ -421,7 +421,7 @@ func (r *TestResources) sendHealthRequest(base *url.URL, healthCheck func(resp *
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
defer cancel()

err := wait.PollImmediateUntilWithContext(ctx, time.Second, func(ctx context.Context) (done bool, err error) {
err := wait.PollUntilContextCancel(ctx, time.Second, true, func(ctx context.Context) (done bool, err error) {
url := base.JoinPath("/health")
req, err := NewHttpRequest(ctx, http.MethodGet, url.String(), nil, make(http.Header))
if err != nil {
Expand Down Expand Up @@ -483,7 +483,7 @@ func (r *TestResources) updateAndWaitTillCryostatAvailable(cr *operatorv1beta2.C
// Poll the deployment until it becomes available or we timeout
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
defer cancel()
err = wait.PollImmediateUntilWithContext(ctx, time.Second, func(ctx context.Context) (done bool, err error) {
err = wait.PollUntilContextCancel(ctx, time.Second, true, func(ctx context.Context) (done bool, err error) {
deploy, err := r.Client.AppsV1().Deployments(cr.Namespace).Get(ctx, cr.Name, metav1.GetOptions{})
if err != nil {
if kerrors.IsNotFound(err) {
Expand Down
2 changes: 1 addition & 1 deletion internal/test/scorecard/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (r *TestResources) installOpenShiftCertManager() error {
// Check CSV status until we know cert-manager installed successfully
ctx, cancel := context.WithTimeout(ctx, testTimeout)
defer cancel()
return wait.PollImmediateUntilWithContext(ctx, time.Second, func(ctx context.Context) (bool, error) {
return wait.PollUntilContextCancel(ctx, time.Second, true, func(ctx context.Context) (bool, error) {
err := olmClient.Get().Resource("subscriptions").Namespace(sub.Namespace).Name(sub.Name).Do(ctx).Into(sub)
if err != nil {
return false, fmt.Errorf("failed to get Subscription: %s", err.Error())
Expand Down
Loading

0 comments on commit 95fa911

Please sign in to comment.