Skip to content

Commit

Permalink
test(scorecard): jfr-datasource validations
Browse files Browse the repository at this point in the history
  • Loading branch information
tthvo committed Jul 4, 2024
1 parent 204fcb6 commit ca1e7a0
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
38 changes: 38 additions & 0 deletions internal/test/scorecard/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ type CryostatRESTClientset struct {
TargetClient *TargetClient
RecordingClient *RecordingClient
CredentialClient *CredentialClient
GrafanaClient *GrafanaClient
}

func (c *CryostatRESTClientset) Targets() *TargetClient {
Expand All @@ -213,6 +214,10 @@ func (c *CryostatRESTClientset) Credential() *CredentialClient {
return c.CredentialClient
}

func (c *CryostatRESTClientset) Grafana() *GrafanaClient {
return c.GrafanaClient
}

func NewCryostatRESTClientset(base *url.URL) *CryostatRESTClientset {
commonClient := &commonCryostatRESTClient{
Base: base,
Expand All @@ -229,6 +234,10 @@ func NewCryostatRESTClientset(base *url.URL) *CryostatRESTClientset {
CredentialClient: &CredentialClient{
commonCryostatRESTClient: commonClient,
},
GrafanaClient: &GrafanaClient{
commonCryostatRESTClient: commonClient,
Prefix: "grafana",
},
}
}

Expand Down Expand Up @@ -587,6 +596,35 @@ func (client *CredentialClient) Create(ctx context.Context, credential *Credenti
return nil
}

// Client for Grafana API
type GrafanaClient struct {
Prefix string
*commonCryostatRESTClient
}

func (client *GrafanaClient) GetDatasourceByName(ctx context.Context, name string) (*DataSource, error) {
url := client.Base.JoinPath(client.Prefix, "api/datasources/name", GRAFANA_DATASOURCE_NAME)
header := make(http.Header)
header.Add("Accept", "*/*")

resp, err := SendRequest(ctx, client.Client, http.MethodGet, url.String(), nil, header)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if !StatusOK(resp.StatusCode) {
return nil, fmt.Errorf("API request failed with status code: %d, response body: %s, and headers:\n%s", resp.StatusCode, ReadError(resp), ReadHeader(resp))
}

datasource := &DataSource{}
if err = ReadJSON(resp, datasource); err != nil {
return nil, fmt.Errorf("failed to read response body: %s", err.Error())
}

return datasource, nil
}

func ReadJSON(resp *http.Response, result interface{}) error {
body, err := io.ReadAll(resp.Body)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions internal/test/scorecard/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,5 +356,15 @@ func CryostatGrafanaTest(bundle *apimanifests.Bundle, namespace string, openShif
return r.fail(fmt.Sprintf("failed to load archive %s to grafana: %s", jfrFilename, err.Error()))
}

// Validate datasource
datasource, err := apiClient.Grafana().GetDatasourceByName(context.Background(), GRAFANA_DATASOURCE_NAME)
if err != nil {
return r.fail(fmt.Sprintf("failed to get datasource %s: %s", GRAFANA_DATASOURCE_NAME, err.Error()))
}

if err = datasource.Valid(); err != nil {
return r.fail(fmt.Sprintf("datasource %s is invalid: %s", GRAFANA_DATASOURCE_NAME, err.Error()))
}

return r.TestResult
}
48 changes: 48 additions & 0 deletions internal/test/scorecard/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package scorecard
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
)
Expand Down Expand Up @@ -152,3 +153,50 @@ type ArchiveGraphQLResponse struct {
} `json:"targetNodes"`
} `json:"data"`
}

const (
GRAFANA_DATASOURCE_NAME = "jfr-datasource"
GRAFANA_DATASOURCE_URL = "http://127.0.0.1:8989"
GRAFANA_DATASOURCE_TYPE = "grafana-simple-json-datasource"
GRAFANA_DATASOURCE_ACCESS = "proxy"
)

// Grafana types
type DataSource struct {
ID int64 `json:"id"`
UID string `json:"uid"`
Name string `json:"name"`

Type string `json:"type"`

URL string `json:"url"`
Access string `json:"access"`

ReadOnly bool `json:"readOnly"`
IsDefault bool `json:"isDefault"`
BasicAuth bool `json:"basicAuth"`
}

func (ds *DataSource) Valid() error {
if ds.Name != GRAFANA_DATASOURCE_NAME {
return fmt.Errorf("expected datasource name %s, but got %s", GRAFANA_DATASOURCE_NAME, ds.Name)
}

if ds.Type != GRAFANA_DATASOURCE_TYPE {
return fmt.Errorf("expected datasource type %s, but got %s", GRAFANA_DATASOURCE_TYPE, ds.Type)
}

if ds.URL != GRAFANA_DATASOURCE_URL {
return fmt.Errorf("expected datasource url %s, but got %s", GRAFANA_DATASOURCE_URL, ds.URL)
}

if ds.Access != GRAFANA_DATASOURCE_ACCESS {
return fmt.Errorf("expected datasource access %s, but got %s", GRAFANA_DATASOURCE_ACCESS, ds.Access)
}

if ds.BasicAuth {
return errors.New("expected basicAuth to be disabled, but got enabled")
}

return nil
}

0 comments on commit ca1e7a0

Please sign in to comment.