Skip to content

Commit 8d85b79

Browse files
committed
test(scorecard): cryostat dashboard validations
1 parent e9b0e63 commit 8d85b79

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

internal/test/scorecard/clients.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,29 @@ type GrafanaClient struct {
602602
*commonCryostatRESTClient
603603
}
604604

605+
func (client *GrafanaClient) GetDashboardByUID(ctx context.Context, uid string) (*DashBoard, error) {
606+
url := client.Base.JoinPath(client.BasePath, "api/dashboards/uid", uid)
607+
header := make(http.Header)
608+
header.Add("Accept", "*/*")
609+
610+
resp, err := SendRequest(ctx, client.Client, http.MethodGet, url.String(), nil, header)
611+
if err != nil {
612+
return nil, err
613+
}
614+
defer resp.Body.Close()
615+
616+
if !StatusOK(resp.StatusCode) {
617+
return nil, fmt.Errorf("API request failed with status code: %d, response body: %s, and headers:\n%s", resp.StatusCode, ReadError(resp), ReadHeader(resp))
618+
}
619+
620+
dashboard := &DashBoard{}
621+
if err = ReadJSON(resp, dashboard); err != nil {
622+
return nil, fmt.Errorf("failed to read response body: %s", err.Error())
623+
}
624+
625+
return dashboard, nil
626+
}
627+
605628
func (client *GrafanaClient) GetDatasourceByName(ctx context.Context, name string) (*DataSource, error) {
606629
url := client.Base.JoinPath(client.BasePath, "api/datasources/name", GRAFANA_DATASOURCE_NAME)
607630
header := make(http.Header)

internal/test/scorecard/tests.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,5 +366,14 @@ func CryostatGrafanaTest(bundle *apimanifests.Bundle, namespace string, openShif
366366
return r.fail(fmt.Sprintf("datasource %s is invalid: %s", GRAFANA_DATASOURCE_NAME, err.Error()))
367367
}
368368

369+
dashboard, err := apiClient.Grafana().GetDashboardByUID(context.Background(), GRAFANA_DASHBOARD_UID)
370+
if err != nil {
371+
return r.fail(fmt.Sprintf("failed to get dashboard %s: %s", GRAFANA_DASHBOARD_UID, err.Error()))
372+
}
373+
374+
if err = dashboard.Valid(); err != nil {
375+
return r.fail(fmt.Sprintf("dashboard %s is invalid: %s", GRAFANA_DASHBOARD_UID, err.Error()))
376+
}
377+
369378
return r.TestResult
370379
}

internal/test/scorecard/types.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ const (
164164

165165
// DataSource represents a Grafana data source
166166
type DataSource struct {
167-
ID int64 `json:"id"`
167+
ID uint32 `json:"id"`
168168
UID string `json:"uid"`
169169
Name string `json:"name"`
170170

@@ -199,3 +199,59 @@ func (ds *DataSource) Valid() error {
199199

200200
return nil
201201
}
202+
203+
// DashBoard represents a Grafana dashboard
204+
type DashBoard struct {
205+
DashBoardMeta `json:"meta"`
206+
DashBoardInfo `json:"dashboard"`
207+
}
208+
209+
type DashBoardMeta struct {
210+
Slug string `json:"slug"`
211+
URL string `json:"url"`
212+
Provisioned bool `json:"provisioned"`
213+
}
214+
215+
type DashBoardInfo struct {
216+
UID string `json:"uid"`
217+
Title string `json:"title"`
218+
Annotations map[string]interface{} `json:"annotations"`
219+
Panels []Panel `json:"panels"`
220+
}
221+
222+
// Panel represents a Grafana panel.
223+
// A panel can be used either for displaying data or separating groups
224+
type Panel struct {
225+
ID uint32 `json:"id"`
226+
Title string `json:"title"`
227+
Type string `json:"type"`
228+
Targets []PanelQuery `json:"targets"`
229+
Panels []Panel `json:"panels"`
230+
}
231+
232+
type PanelQuery struct {
233+
RawQuery bool `json:"rawQuery"`
234+
RefID string `json:"refId"`
235+
Target string `json:"target"`
236+
Type string `json:"table"`
237+
}
238+
239+
func (db *DashBoard) Valid() error {
240+
if db.UID != GRAFANA_DASHBOARD_UID {
241+
return fmt.Errorf("expected dashboard uid %s, but got %s", GRAFANA_DASHBOARD_UID, db.UID)
242+
}
243+
244+
if db.Title != GRAFANA_DASHBOARD_TITLE {
245+
return fmt.Errorf("expected dashboard title %s, but got %s", GRAFANA_DASHBOARD_TITLE, db.Title)
246+
}
247+
248+
if !db.Provisioned {
249+
return errors.New("expected dashboard to be provisioned, but got unprovisioned")
250+
}
251+
252+
if len(db.Panels) == 0 {
253+
return errors.New("expected dashboard to have panels, but got 0")
254+
}
255+
256+
return nil
257+
}

0 commit comments

Comments
 (0)