|
|
|
|
@ -2,9 +2,12 @@ package librarypanels |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"encoding/json" |
|
|
|
|
"fmt" |
|
|
|
|
"testing" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson" |
|
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp" |
|
|
|
|
"github.com/stretchr/testify/require" |
|
|
|
|
"gopkg.in/macaron.v1" |
|
|
|
|
@ -30,7 +33,7 @@ func TestCreateLibraryPanel(t *testing.T) { |
|
|
|
|
func TestConnectLibraryPanel(t *testing.T) { |
|
|
|
|
testScenario(t, "When an admin tries to create a connection for a library panel that does not exist, it should fail", |
|
|
|
|
func(t *testing.T, sc scenarioContext) { |
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": "unknown", "dashboardId": "1"}) |
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": "unknown", ":dashboardId": "1"}) |
|
|
|
|
response := sc.service.connectHandler(sc.reqContext) |
|
|
|
|
require.Equal(t, 404, response.Status()) |
|
|
|
|
}) |
|
|
|
|
@ -45,7 +48,7 @@ func TestConnectLibraryPanel(t *testing.T) { |
|
|
|
|
err := json.Unmarshal(response.Body(), &result) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID, "dashboardId": "1"}) |
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID, ":dashboardId": "1"}) |
|
|
|
|
response = sc.service.connectHandler(sc.reqContext) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
|
|
|
|
|
@ -97,7 +100,7 @@ func TestDeleteLibraryPanel(t *testing.T) { |
|
|
|
|
func TestDisconnectLibraryPanel(t *testing.T) { |
|
|
|
|
testScenario(t, "When an admin tries to remove a connection with a library panel that does not exist, it should fail", |
|
|
|
|
func(t *testing.T, sc scenarioContext) { |
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": "unknown", "dashboardId": "1"}) |
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": "unknown", ":dashboardId": "1"}) |
|
|
|
|
response := sc.service.disconnectHandler(sc.reqContext) |
|
|
|
|
require.Equal(t, 404, response.Status()) |
|
|
|
|
}) |
|
|
|
|
@ -112,7 +115,7 @@ func TestDisconnectLibraryPanel(t *testing.T) { |
|
|
|
|
err := json.Unmarshal(response.Body(), &result) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID, "dashboardId": "1"}) |
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID, ":dashboardId": "1"}) |
|
|
|
|
response = sc.service.disconnectHandler(sc.reqContext) |
|
|
|
|
require.Equal(t, 404, response.Status()) |
|
|
|
|
}) |
|
|
|
|
@ -127,7 +130,7 @@ func TestDisconnectLibraryPanel(t *testing.T) { |
|
|
|
|
err := json.Unmarshal(response.Body(), &result) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID, "dashboardId": "1"}) |
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": result.Result.UID, ":dashboardId": "1"}) |
|
|
|
|
response = sc.service.connectHandler(sc.reqContext) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
response = sc.service.disconnectHandler(sc.reqContext) |
|
|
|
|
@ -330,7 +333,7 @@ func TestPatchLibraryPanel(t *testing.T) { |
|
|
|
|
{ |
|
|
|
|
"datasource": "${DS_GDEV-TESTDATA}", |
|
|
|
|
"id": 1, |
|
|
|
|
"name": "Model - New name", |
|
|
|
|
"title": "Model - New name", |
|
|
|
|
"type": "text" |
|
|
|
|
} |
|
|
|
|
`), |
|
|
|
|
@ -344,7 +347,7 @@ func TestPatchLibraryPanel(t *testing.T) { |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
existing.Result.FolderID = int64(2) |
|
|
|
|
existing.Result.Name = "Panel - New name" |
|
|
|
|
existing.Result.Model["name"] = "Model - New name" |
|
|
|
|
existing.Result.Model["title"] = "Model - New name" |
|
|
|
|
if diff := cmp.Diff(existing.Result, result.Result, getCompareOptions()...); diff != "" { |
|
|
|
|
t.Fatalf("Result mismatch (-want +got):\n%s", diff) |
|
|
|
|
} |
|
|
|
|
@ -516,6 +519,578 @@ func TestPatchLibraryPanel(t *testing.T) { |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestLoadLibraryPanelsForDashboard(t *testing.T) { |
|
|
|
|
testScenario(t, "When an admin tries to load a dashboard with a library panel, it should copy JSON properties from library panel", |
|
|
|
|
func(t *testing.T, sc scenarioContext) { |
|
|
|
|
command := getCreateCommand(1, "Text - Library Panel1") |
|
|
|
|
response := sc.service.createHandler(sc.reqContext, command) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
|
|
|
|
|
var existing libraryPanelResult |
|
|
|
|
err := json.Unmarshal(response.Body(), &existing) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID, ":dashboardId": "1"}) |
|
|
|
|
response = sc.service.connectHandler(sc.reqContext) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
|
|
|
|
|
dashJSON := map[string]interface{}{ |
|
|
|
|
"panels": []interface{}{ |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(1), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 0, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(2), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 6, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
"libraryPanel": map[string]interface{}{ |
|
|
|
|
"uid": existing.Result.UID, |
|
|
|
|
"name": existing.Result.Name, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
dash := models.Dashboard{ |
|
|
|
|
Id: 1, |
|
|
|
|
Data: simplejson.NewFromAny(dashJSON), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = sc.service.LoadLibraryPanelsForDashboard(&dash) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
expectedJSON := map[string]interface{}{ |
|
|
|
|
"panels": []interface{}{ |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(1), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 0, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(2), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 6, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
"datasource": "${DS_GDEV-TESTDATA}", |
|
|
|
|
"libraryPanel": map[string]interface{}{ |
|
|
|
|
"uid": existing.Result.UID, |
|
|
|
|
"name": existing.Result.Name, |
|
|
|
|
}, |
|
|
|
|
"title": "Text - Library Panel", |
|
|
|
|
"type": "text", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
expected := simplejson.NewFromAny(expectedJSON) |
|
|
|
|
if diff := cmp.Diff(expected.Interface(), dash.Data.Interface(), getCompareOptions()...); diff != "" { |
|
|
|
|
t.Fatalf("Result mismatch (-want +got):\n%s", diff) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
testScenario(t, "When an admin tries to load a dashboard with a library panel without uid, it should fail", |
|
|
|
|
func(t *testing.T, sc scenarioContext) { |
|
|
|
|
command := getCreateCommand(1, "Text - Library Panel1") |
|
|
|
|
response := sc.service.createHandler(sc.reqContext, command) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
|
|
|
|
|
var existing libraryPanelResult |
|
|
|
|
err := json.Unmarshal(response.Body(), &existing) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID, ":dashboardId": "1"}) |
|
|
|
|
response = sc.service.connectHandler(sc.reqContext) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
|
|
|
|
|
dashJSON := map[string]interface{}{ |
|
|
|
|
"panels": []interface{}{ |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(1), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 0, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(2), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 6, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
"libraryPanel": map[string]interface{}{ |
|
|
|
|
"name": existing.Result.Name, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
dash := models.Dashboard{ |
|
|
|
|
Id: 1, |
|
|
|
|
Data: simplejson.NewFromAny(dashJSON), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = sc.service.LoadLibraryPanelsForDashboard(&dash) |
|
|
|
|
require.EqualError(t, err, errLibraryPanelHeaderUIDMissing.Error()) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
testScenario(t, "When an admin tries to load a dashboard with a library panel that is not connected, it should fail", |
|
|
|
|
func(t *testing.T, sc scenarioContext) { |
|
|
|
|
command := getCreateCommand(1, "Text - Library Panel1") |
|
|
|
|
response := sc.service.createHandler(sc.reqContext, command) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
|
|
|
|
|
var existing libraryPanelResult |
|
|
|
|
err := json.Unmarshal(response.Body(), &existing) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
dashJSON := map[string]interface{}{ |
|
|
|
|
"panels": []interface{}{ |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(1), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 0, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(2), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 6, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
"libraryPanel": map[string]interface{}{ |
|
|
|
|
"uid": existing.Result.UID, |
|
|
|
|
"name": existing.Result.Name, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
dash := models.Dashboard{ |
|
|
|
|
Id: 1, |
|
|
|
|
Data: simplejson.NewFromAny(dashJSON), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = sc.service.LoadLibraryPanelsForDashboard(&dash) |
|
|
|
|
require.EqualError(t, err, fmt.Errorf("found connection to library panel %q that isn't in database", existing.Result.UID).Error()) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestCleanLibraryPanelsForDashboard(t *testing.T) { |
|
|
|
|
testScenario(t, "When an admin tries to store a dashboard with a library panel, it should just keep the correct JSON properties in library panel", |
|
|
|
|
func(t *testing.T, sc scenarioContext) { |
|
|
|
|
command := getCreateCommand(1, "Text - Library Panel1") |
|
|
|
|
response := sc.service.createHandler(sc.reqContext, command) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
|
|
|
|
|
var existing libraryPanelResult |
|
|
|
|
err := json.Unmarshal(response.Body(), &existing) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
dashJSON := map[string]interface{}{ |
|
|
|
|
"panels": []interface{}{ |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(1), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 0, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(2), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 6, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
"datasource": "${DS_GDEV-TESTDATA}", |
|
|
|
|
"libraryPanel": map[string]interface{}{ |
|
|
|
|
"uid": existing.Result.UID, |
|
|
|
|
"name": existing.Result.Name, |
|
|
|
|
}, |
|
|
|
|
"title": "Text - Library Panel", |
|
|
|
|
"type": "text", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
dash := models.Dashboard{ |
|
|
|
|
Id: 1, |
|
|
|
|
Data: simplejson.NewFromAny(dashJSON), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = sc.service.CleanLibraryPanelsForDashboard(&dash) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
expectedJSON := map[string]interface{}{ |
|
|
|
|
"panels": []interface{}{ |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(1), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 0, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(2), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 6, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
"libraryPanel": map[string]interface{}{ |
|
|
|
|
"uid": existing.Result.UID, |
|
|
|
|
"name": existing.Result.Name, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
expected := simplejson.NewFromAny(expectedJSON) |
|
|
|
|
if diff := cmp.Diff(expected.Interface(), dash.Data.Interface(), getCompareOptions()...); diff != "" { |
|
|
|
|
t.Fatalf("Result mismatch (-want +got):\n%s", diff) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
testScenario(t, "When an admin tries to store a dashboard with a library panel without uid, it should fail", |
|
|
|
|
func(t *testing.T, sc scenarioContext) { |
|
|
|
|
command := getCreateCommand(1, "Text - Library Panel1") |
|
|
|
|
response := sc.service.createHandler(sc.reqContext, command) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
|
|
|
|
|
var existing libraryPanelResult |
|
|
|
|
err := json.Unmarshal(response.Body(), &existing) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
dashJSON := map[string]interface{}{ |
|
|
|
|
"panels": []interface{}{ |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(1), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 0, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(2), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 6, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
"datasource": "${DS_GDEV-TESTDATA}", |
|
|
|
|
"libraryPanel": map[string]interface{}{ |
|
|
|
|
"name": existing.Result.Name, |
|
|
|
|
}, |
|
|
|
|
"title": "Text - Library Panel", |
|
|
|
|
"type": "text", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
dash := models.Dashboard{ |
|
|
|
|
Id: 1, |
|
|
|
|
Data: simplejson.NewFromAny(dashJSON), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = sc.service.CleanLibraryPanelsForDashboard(&dash) |
|
|
|
|
require.EqualError(t, err, errLibraryPanelHeaderUIDMissing.Error()) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
testScenario(t, "When an admin tries to store a dashboard with a library panel without name, it should fail", |
|
|
|
|
func(t *testing.T, sc scenarioContext) { |
|
|
|
|
command := getCreateCommand(1, "Text - Library Panel1") |
|
|
|
|
response := sc.service.createHandler(sc.reqContext, command) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
|
|
|
|
|
var existing libraryPanelResult |
|
|
|
|
err := json.Unmarshal(response.Body(), &existing) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
dashJSON := map[string]interface{}{ |
|
|
|
|
"panels": []interface{}{ |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(1), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 0, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(2), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 6, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
"datasource": "${DS_GDEV-TESTDATA}", |
|
|
|
|
"libraryPanel": map[string]interface{}{ |
|
|
|
|
"uid": existing.Result.UID, |
|
|
|
|
}, |
|
|
|
|
"title": "Text - Library Panel", |
|
|
|
|
"type": "text", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
dash := models.Dashboard{ |
|
|
|
|
Id: 1, |
|
|
|
|
Data: simplejson.NewFromAny(dashJSON), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = sc.service.CleanLibraryPanelsForDashboard(&dash) |
|
|
|
|
require.EqualError(t, err, errLibraryPanelHeaderNameMissing.Error()) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestConnectLibraryPanelsForDashboard(t *testing.T) { |
|
|
|
|
testScenario(t, "When an admin tries to store a dashboard with a library panel, it should connect the two", |
|
|
|
|
func(t *testing.T, sc scenarioContext) { |
|
|
|
|
command := getCreateCommand(1, "Text - Library Panel1") |
|
|
|
|
response := sc.service.createHandler(sc.reqContext, command) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
|
|
|
|
|
var existing libraryPanelResult |
|
|
|
|
err := json.Unmarshal(response.Body(), &existing) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
dashJSON := map[string]interface{}{ |
|
|
|
|
"panels": []interface{}{ |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(1), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 0, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(2), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 6, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
"datasource": "${DS_GDEV-TESTDATA}", |
|
|
|
|
"libraryPanel": map[string]interface{}{ |
|
|
|
|
"uid": existing.Result.UID, |
|
|
|
|
"name": existing.Result.Name, |
|
|
|
|
}, |
|
|
|
|
"title": "Text - Library Panel", |
|
|
|
|
"type": "text", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
dash := models.Dashboard{ |
|
|
|
|
Id: int64(1), |
|
|
|
|
Data: simplejson.NewFromAny(dashJSON), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = sc.service.ConnectLibraryPanelsForDashboard(sc.reqContext, &dash) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID}) |
|
|
|
|
response = sc.service.getConnectedDashboardsHandler(sc.reqContext) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
|
|
|
|
|
var dashResult libraryPanelDashboardsResult |
|
|
|
|
err = json.Unmarshal(response.Body(), &dashResult) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Len(t, dashResult.Result, 1) |
|
|
|
|
require.Equal(t, int64(1), dashResult.Result[0]) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
testScenario(t, "When an admin tries to store a dashboard with a library panel without uid, it should fail", |
|
|
|
|
func(t *testing.T, sc scenarioContext) { |
|
|
|
|
command := getCreateCommand(1, "Text - Library Panel1") |
|
|
|
|
response := sc.service.createHandler(sc.reqContext, command) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
|
|
|
|
|
var existing libraryPanelResult |
|
|
|
|
err := json.Unmarshal(response.Body(), &existing) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
dashJSON := map[string]interface{}{ |
|
|
|
|
"panels": []interface{}{ |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(1), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 0, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(2), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 6, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
"datasource": "${DS_GDEV-TESTDATA}", |
|
|
|
|
"libraryPanel": map[string]interface{}{ |
|
|
|
|
"name": existing.Result.Name, |
|
|
|
|
}, |
|
|
|
|
"title": "Text - Library Panel", |
|
|
|
|
"type": "text", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
dash := models.Dashboard{ |
|
|
|
|
Id: int64(1), |
|
|
|
|
Data: simplejson.NewFromAny(dashJSON), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = sc.service.ConnectLibraryPanelsForDashboard(sc.reqContext, &dash) |
|
|
|
|
require.EqualError(t, err, errLibraryPanelHeaderUIDMissing.Error()) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestDisconnectLibraryPanelsForDashboard(t *testing.T) { |
|
|
|
|
testScenario(t, "When an admin tries to delete a dashboard with a library panel, it should disconnect the two", |
|
|
|
|
func(t *testing.T, sc scenarioContext) { |
|
|
|
|
command := getCreateCommand(1, "Text - Library Panel1") |
|
|
|
|
response := sc.service.createHandler(sc.reqContext, command) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
|
|
|
|
|
var existing libraryPanelResult |
|
|
|
|
err := json.Unmarshal(response.Body(), &existing) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID, ":dashboardId": "1"}) |
|
|
|
|
response = sc.service.connectHandler(sc.reqContext) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
|
|
|
|
|
dashJSON := map[string]interface{}{ |
|
|
|
|
"panels": []interface{}{ |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(1), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 0, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(2), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 6, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
"datasource": "${DS_GDEV-TESTDATA}", |
|
|
|
|
"libraryPanel": map[string]interface{}{ |
|
|
|
|
"uid": existing.Result.UID, |
|
|
|
|
"name": existing.Result.Name, |
|
|
|
|
}, |
|
|
|
|
"title": "Text - Library Panel", |
|
|
|
|
"type": "text", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
dash := models.Dashboard{ |
|
|
|
|
Id: int64(1), |
|
|
|
|
Data: simplejson.NewFromAny(dashJSON), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = sc.service.DisconnectLibraryPanelsForDashboard(&dash) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID}) |
|
|
|
|
response = sc.service.getConnectedDashboardsHandler(sc.reqContext) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
|
|
|
|
|
var dashResult libraryPanelDashboardsResult |
|
|
|
|
err = json.Unmarshal(response.Body(), &dashResult) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
require.Empty(t, dashResult.Result) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
testScenario(t, "When an admin tries to delete a dashboard with a library panel without uid, it should fail", |
|
|
|
|
func(t *testing.T, sc scenarioContext) { |
|
|
|
|
command := getCreateCommand(1, "Text - Library Panel1") |
|
|
|
|
response := sc.service.createHandler(sc.reqContext, command) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
|
|
|
|
|
var existing libraryPanelResult |
|
|
|
|
err := json.Unmarshal(response.Body(), &existing) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
sc.reqContext.ReplaceAllParams(map[string]string{":uid": existing.Result.UID, ":dashboardId": "1"}) |
|
|
|
|
response = sc.service.connectHandler(sc.reqContext) |
|
|
|
|
require.Equal(t, 200, response.Status()) |
|
|
|
|
|
|
|
|
|
dashJSON := map[string]interface{}{ |
|
|
|
|
"panels": []interface{}{ |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(1), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 0, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
map[string]interface{}{ |
|
|
|
|
"id": int64(2), |
|
|
|
|
"gridPos": map[string]interface{}{ |
|
|
|
|
"h": 6, |
|
|
|
|
"w": 6, |
|
|
|
|
"x": 6, |
|
|
|
|
"y": 0, |
|
|
|
|
}, |
|
|
|
|
"datasource": "${DS_GDEV-TESTDATA}", |
|
|
|
|
"libraryPanel": map[string]interface{}{ |
|
|
|
|
"name": existing.Result.Name, |
|
|
|
|
}, |
|
|
|
|
"title": "Text - Library Panel", |
|
|
|
|
"type": "text", |
|
|
|
|
}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
dash := models.Dashboard{ |
|
|
|
|
Id: int64(1), |
|
|
|
|
Data: simplejson.NewFromAny(dashJSON), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = sc.service.DisconnectLibraryPanelsForDashboard(&dash) |
|
|
|
|
require.EqualError(t, err, errLibraryPanelHeaderUIDMissing.Error()) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type libraryPanel struct { |
|
|
|
|
ID int64 `json:"id"` |
|
|
|
|
OrgID int64 `json:"orgId"` |
|
|
|
|
@ -570,7 +1145,7 @@ func getCreateCommand(folderID int64, name string) createLibraryPanelCommand { |
|
|
|
|
{ |
|
|
|
|
"datasource": "${DS_GDEV-TESTDATA}", |
|
|
|
|
"id": 1, |
|
|
|
|
"name": "Text - Library Panel", |
|
|
|
|
"title": "Text - Library Panel", |
|
|
|
|
"type": "text" |
|
|
|
|
} |
|
|
|
|
`), |
|
|
|
|
|