Skip to content

Commit

Permalink
Fix templating init with options field and add integration test (graf…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuzminykh Aleksandr Olegovich committed Oct 19, 2021
1 parent 56cdea6 commit d7a8666
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 1 deletion.
2 changes: 1 addition & 1 deletion board.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type (
AutoCount *int `json:"auto_count,omitempty"`
Datasource *string `json:"datasource"`
Refresh BoolInt `json:"refresh"`
Options []Option `json:"options"`
Options []Option `json:"options,omitempty"`
IncludeAll bool `json:"includeAll"`
AllFormat string `json:"allFormat"`
AllValue string `json:"allValue"`
Expand Down
79 changes: 79 additions & 0 deletions rest-dashboard_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,82 @@ func Test_Dashboard_CRUD_By_UID(t *testing.T) {
}

}

func Test_Dashboard_CRUD_Templating(t *testing.T) {
var (
err error
boardResult sdk.Board
properties sdk.BoardProperties
)

shouldSkip(t)
ctx := context.Background()
client := getClient(t)

var board sdk.Board

raw, _ := ioutil.ReadFile("testdata/empty-dashboard-with-uid-templating-8.2.json")

if err = json.Unmarshal(raw, &board); err != nil {
t.Fatal(err)
}

//Cleanup if Already exists
if _, err = client.DeleteDashboardByUID(ctx, board.UID); err != nil {
t.Fatal(err)
}

if _, err = client.SetRawDashboard(ctx, raw); err != nil {
t.Fatal(err)
}

if boardResult, properties, err = client.GetDashboardByUID(ctx, board.UID); err != nil {
t.Fatal(err)
}

params := sdk.SetDashboardParams{
FolderID: properties.FolderID,
Overwrite: true,
}
if _, err = client.SetDashboard(ctx, boardResult, params); err != nil {
t.Fatal(err)
}

if raw, _, err = client.GetRawDashboardByUID(ctx, board.UID); err != nil {
t.Fatal(err)
}

{
var boardMap map[string]interface{}

err = json.Unmarshal(raw, &boardMap)
if err != nil {
t.Fatal(err)
}

templatingMap, found := boardMap["templating"].(map[string]interface{})
if !found {
t.Fatal()
}

list, found := templatingMap["list"].([]interface{})
if !found {
t.Fatal()
}

_, found = list[0].(map[string]interface{})["options"]
if found {
t.Fatal()
}
}

//Remove the dashboard that was created.
if _, err = client.DeleteDashboardByUID(ctx, board.UID); err != nil {
t.Fatal(err)
}

//Verify that it has been deleted
if boardResult, _, err = client.GetDashboardByUID(ctx, board.UID); err == nil {
t.Fatal("Failed to delete dashboard, it can still be retrieved")
}
}
56 changes: 56 additions & 0 deletions testdata/empty-dashboard-with-uid-templating-8.2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": "-- Grafana --",
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"target": {
"limit": 100,
"matchAny": false,
"tags": [],
"type": "dashboard"
},
"type": "dashboard"
}
]
},
"editable": true,
"fiscalYearStartMonth": 0,
"gnetId": null,
"graphTooltip": 0,
"id": 15,
"iteration": 1634566582776,
"links": [],
"liveNow": false,
"panels": [],
"schemaVersion": 31,
"style": "dark",
"tags": [],
"templating": {
"list": [
{
"description": null,
"error": null,
"hide": 2,
"label": null,
"name": "period",
"query": "1d",
"skipUrlSync": false,
"type": "constant"
}
]
},
"time": {
"from": "now-6h",
"to": "now"
},
"timepicker": {},
"timezone": "",
"title": "broken",
"uid": "ytYpBcOnz",
"version": 1
}

0 comments on commit d7a8666

Please sign in to comment.