mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
fix and add test (#44286)
This commit is contained in:
parent
1ae155d2b1
commit
3cb0fb3ddc
@ -135,7 +135,6 @@ func (l *Loader) loadPlugins(ctx context.Context, class plugins.Class, pluginJSO
|
||||
PluginDir: pluginDir,
|
||||
Class: class,
|
||||
}
|
||||
l.setDefaults(plugin)
|
||||
plugin.SetLogger(l.log.New("pluginID", plugin.ID))
|
||||
|
||||
sig, err := signature.Calculate(l.log, plugin)
|
||||
@ -203,6 +202,7 @@ func (l *Loader) loadPlugins(ctx context.Context, class plugins.Class, pluginJSO
|
||||
}
|
||||
|
||||
for _, p := range verifiedPlugins {
|
||||
l.setDefaults(p)
|
||||
err := l.pluginInitializer.Initialize(ctx, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -273,7 +273,7 @@ func (l *Loader) setDefaults(p *plugins.Plugin) {
|
||||
|
||||
if p.IsApp() {
|
||||
for _, child := range p.Children {
|
||||
setChildModule(p, child)
|
||||
setPathsBasedOnApp(p, child)
|
||||
}
|
||||
|
||||
// slugify pages
|
||||
@ -310,7 +310,7 @@ func setModule(p *plugins.Plugin) {
|
||||
p.BaseURL = path.Join("public/plugins", p.ID)
|
||||
}
|
||||
|
||||
func setChildModule(parent *plugins.Plugin, child *plugins.Plugin) {
|
||||
func setPathsBasedOnApp(parent *plugins.Plugin, child *plugins.Plugin) {
|
||||
appSubPath := strings.ReplaceAll(strings.Replace(child.PluginDir, parent.PluginDir, "", 1), "\\", "/")
|
||||
child.IncludedInAppID = parent.ID
|
||||
child.BaseURL = parent.BaseURL
|
||||
|
@ -753,6 +753,146 @@ func TestLoader_loadNestedPlugins(t *testing.T) {
|
||||
t.Fatalf("Result mismatch (-want +got):\n%s", cmp.Diff(got, expected, compareOpts))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Plugin child field `IncludedInAppID` is set to parent app's plugin ID", func(t *testing.T) {
|
||||
parent := &plugins.Plugin{
|
||||
JSONData: plugins.JSONData{
|
||||
ID: "myorgid-simple-app",
|
||||
Type: "app",
|
||||
Name: "Simple App",
|
||||
Info: plugins.Info{
|
||||
Author: plugins.InfoLink{
|
||||
Name: "Your Name",
|
||||
},
|
||||
Links: []plugins.InfoLink{
|
||||
{Name: "Website", URL: "https://github.com/grafana/grafana-starter-app"},
|
||||
{Name: "License", URL: "https://github.com/grafana/grafana-starter-app/blob/master/LICENSE"},
|
||||
},
|
||||
Logos: plugins.Logos{
|
||||
Small: "public/plugins/myorgid-simple-app/img/logo.svg",
|
||||
Large: "public/plugins/myorgid-simple-app/img/logo.svg",
|
||||
},
|
||||
Screenshots: []plugins.Screenshots{},
|
||||
Description: "Grafana App Plugin Template",
|
||||
Version: "%VERSION%",
|
||||
Updated: "%TODAY%",
|
||||
},
|
||||
Dependencies: plugins.Dependencies{
|
||||
GrafanaVersion: "7.0.0",
|
||||
GrafanaDependency: ">=7.0.0",
|
||||
Plugins: []plugins.Dependency{},
|
||||
},
|
||||
Includes: []*plugins.Includes{
|
||||
{
|
||||
Name: "Root Page (react)",
|
||||
Path: "/a/myorgid-simple-app",
|
||||
Type: "page",
|
||||
Role: "Viewer",
|
||||
AddToNav: true,
|
||||
DefaultNav: true,
|
||||
Slug: "root-page-react",
|
||||
},
|
||||
{
|
||||
Name: "Root Page (Tab B)",
|
||||
Path: "/a/myorgid-simple-app/?tab=b",
|
||||
Type: "page",
|
||||
Role: "Viewer",
|
||||
AddToNav: true,
|
||||
Slug: "root-page-tab-b",
|
||||
},
|
||||
{
|
||||
Name: "React Config",
|
||||
Path: "/plugins/myorgid-simple-app/?page=page2",
|
||||
Type: "page",
|
||||
Role: "Admin",
|
||||
AddToNav: true,
|
||||
Slug: "react-config",
|
||||
},
|
||||
{
|
||||
Name: "Streaming Example",
|
||||
Path: "dashboards/streaming.json",
|
||||
Type: "dashboard",
|
||||
Role: "Viewer",
|
||||
Slug: "streaming-example",
|
||||
},
|
||||
{
|
||||
Name: "Lots of Stats",
|
||||
Path: "dashboards/stats.json",
|
||||
Type: "dashboard",
|
||||
Role: "Viewer",
|
||||
Slug: "lots-of-stats",
|
||||
},
|
||||
},
|
||||
Backend: false,
|
||||
},
|
||||
Module: "plugins/myorgid-simple-app/module",
|
||||
BaseURL: "public/plugins/myorgid-simple-app",
|
||||
PluginDir: filepath.Join(parentDir, "testdata/app-with-child/dist"),
|
||||
DefaultNavURL: "/plugins/myorgid-simple-app/page/root-page-react",
|
||||
Signature: plugins.SignatureValid,
|
||||
SignatureType: plugins.GrafanaSignature,
|
||||
SignatureOrg: "Grafana Labs",
|
||||
Class: plugins.External,
|
||||
}
|
||||
|
||||
child := &plugins.Plugin{
|
||||
JSONData: plugins.JSONData{
|
||||
ID: "myorgid-simple-panel",
|
||||
Type: "panel",
|
||||
Name: "Grafana Panel Plugin Template",
|
||||
Info: plugins.Info{
|
||||
Author: plugins.InfoLink{
|
||||
Name: "Your Name",
|
||||
},
|
||||
Links: []plugins.InfoLink{
|
||||
{Name: "Website", URL: "https://github.com/grafana/grafana-starter-panel"},
|
||||
{Name: "License", URL: "https://github.com/grafana/grafana-starter-panel/blob/master/LICENSE"},
|
||||
},
|
||||
Logos: plugins.Logos{
|
||||
Small: "public/plugins/myorgid-simple-panel/img/logo.svg",
|
||||
Large: "public/plugins/myorgid-simple-panel/img/logo.svg",
|
||||
},
|
||||
Screenshots: []plugins.Screenshots{},
|
||||
Description: "Grafana Panel Plugin Template",
|
||||
Version: "%VERSION%",
|
||||
Updated: "%TODAY%",
|
||||
},
|
||||
Dependencies: plugins.Dependencies{
|
||||
GrafanaDependency: ">=7.0.0",
|
||||
GrafanaVersion: "*",
|
||||
Plugins: []plugins.Dependency{},
|
||||
},
|
||||
},
|
||||
Module: "plugins/myorgid-simple-app/child/module",
|
||||
BaseURL: "public/plugins/myorgid-simple-app",
|
||||
PluginDir: filepath.Join(parentDir, "testdata/app-with-child/dist/child"),
|
||||
IncludedInAppID: parent.ID,
|
||||
Signature: plugins.SignatureValid,
|
||||
SignatureType: plugins.GrafanaSignature,
|
||||
SignatureOrg: "Grafana Labs",
|
||||
Class: plugins.External,
|
||||
}
|
||||
|
||||
parent.Children = []*plugins.Plugin{child}
|
||||
child.Parent = parent
|
||||
|
||||
expected := []*plugins.Plugin{parent, child}
|
||||
l := newLoader(&plugins.Cfg{
|
||||
PluginsPath: parentDir,
|
||||
})
|
||||
|
||||
got, err := l.Load(context.Background(), plugins.External, []string{"../testdata/app-with-child"}, map[string]struct{}{})
|
||||
assert.NoError(t, err)
|
||||
|
||||
// to ensure we can compare with expected
|
||||
sort.SliceStable(got, func(i, j int) bool {
|
||||
return got[i].ID < got[j].ID
|
||||
})
|
||||
|
||||
if !cmp.Equal(got, expected, compareOpts) {
|
||||
t.Fatalf("Result mismatch (-want +got):\n%s", cmp.Diff(got, expected, compareOpts))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestLoader_readPluginJSON(t *testing.T) {
|
||||
@ -894,7 +1034,7 @@ func Test_setPathsBasedOnApp(t *testing.T) {
|
||||
BaseURL: "public/app/plugins/app/testdata",
|
||||
}
|
||||
|
||||
setChildModule(parent, child)
|
||||
setPathsBasedOnApp(parent, child)
|
||||
|
||||
assert.Equal(t, "app/plugins/app/testdata/datasources/datasource/module", child.Module)
|
||||
assert.Equal(t, "testdata", child.IncludedInAppID)
|
||||
|
29
pkg/plugins/manager/testdata/app-with-child/dist/MANIFEST.txt
vendored
Normal file
29
pkg/plugins/manager/testdata/app-with-child/dist/MANIFEST.txt
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
|
||||
-----BEGIN PGP SIGNED MESSAGE-----
|
||||
Hash: SHA512
|
||||
|
||||
{
|
||||
"manifestVersion": "2.0.0",
|
||||
"signatureType": "grafana",
|
||||
"signedByOrg": "grafana",
|
||||
"signedByOrgName": "Grafana Labs",
|
||||
"rootUrls": [],
|
||||
"plugin": "myorgid-simple-app",
|
||||
"version": "%VERSION%",
|
||||
"time": 1642614241713,
|
||||
"keyId": "7e4d0c6a708866e7",
|
||||
"files": {
|
||||
"plugin.json": "1abecfd0229814f6c284ff3c8dd744548f8d676ab3250cd7902c99dabf11480e",
|
||||
"child/plugin.json": "66ba0dffaf3b1bfa17eb9a8672918fc66d1001f465b1061f4fc19c2f2c100f51"
|
||||
}
|
||||
}
|
||||
-----BEGIN PGP SIGNATURE-----
|
||||
Version: OpenPGP.js v4.10.1
|
||||
Comment: https://openpgpjs.org
|
||||
|
||||
wqEEARMKAAYFAmHoTeEACgkQfk0ManCIZudvLwIJAQcy8NwEZZb58SuD9fhS
|
||||
kaWb9mxvhdAL6Zau+AZKQ3w2ZKzynwgKK/xwDSnX8BNI5UjcCh+0WxkpiAHL
|
||||
bm6/dhzdAgi7Mo/W5IyXqMt9/0AyyFNA1wVlLCj47C+4op4hIw054ZJ9u2j9
|
||||
KCVjajZ9bGaV6ucBnG5/l4MLFhUK1M0slI2qvw==
|
||||
=atro
|
||||
-----END PGP SIGNATURE-----
|
30
pkg/plugins/manager/testdata/app-with-child/dist/child/plugin.json
vendored
Normal file
30
pkg/plugins/manager/testdata/app-with-child/dist/child/plugin.json
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
{
|
||||
"$schema": "https://raw.githubusercontent.com/grafana/grafana/master/docs/sources/developers/plugins/plugin.schema.json",
|
||||
"type": "panel",
|
||||
"name": "Grafana Panel Plugin Template",
|
||||
"id": "myorgid-simple-panel",
|
||||
|
||||
"info": {
|
||||
"description": "Grafana Panel Plugin Template",
|
||||
"author": {
|
||||
"name": "Your Name"
|
||||
},
|
||||
"keywords": ["panel", "template"],
|
||||
"logos": {
|
||||
"small": "img/logo.svg",
|
||||
"large": "img/logo.svg"
|
||||
},
|
||||
"links": [
|
||||
{"name": "Website", "url": "https://github.com/grafana/grafana-starter-panel"},
|
||||
{"name": "License", "url": "https://github.com/grafana/grafana-starter-panel/blob/master/LICENSE"}
|
||||
],
|
||||
"screenshots": [],
|
||||
"version": "%VERSION%",
|
||||
"updated": "%TODAY%"
|
||||
},
|
||||
|
||||
"dependencies": {
|
||||
"grafanaDependency": ">=7.0.0",
|
||||
"plugins": []
|
||||
}
|
||||
}
|
66
pkg/plugins/manager/testdata/app-with-child/dist/plugin.json
vendored
Normal file
66
pkg/plugins/manager/testdata/app-with-child/dist/plugin.json
vendored
Normal file
@ -0,0 +1,66 @@
|
||||
{
|
||||
"$schema": "https://raw.githubusercontent.com/grafana/grafana/master/docs/sources/developers/plugins/plugin.schema.json",
|
||||
"type": "app",
|
||||
"name": "Simple App",
|
||||
"id": "myorgid-simple-app",
|
||||
|
||||
"info": {
|
||||
"description": "Grafana App Plugin Template",
|
||||
"author": {
|
||||
"name": "Your Name"
|
||||
},
|
||||
"keywords": ["panel", "template"],
|
||||
"logos": {
|
||||
"small": "img/logo.svg",
|
||||
"large": "img/logo.svg"
|
||||
},
|
||||
"links": [
|
||||
{ "name": "Website", "url": "https://github.com/grafana/grafana-starter-app" },
|
||||
{ "name": "License", "url": "https://github.com/grafana/grafana-starter-app/blob/master/LICENSE" }
|
||||
],
|
||||
"screenshots": [],
|
||||
"version": "%VERSION%",
|
||||
"updated": "%TODAY%"
|
||||
},
|
||||
|
||||
"includes": [
|
||||
{
|
||||
"type": "page",
|
||||
"name": "Root Page (react)",
|
||||
"path": "/a/myorgid-simple-app",
|
||||
"role": "Viewer",
|
||||
"addToNav": true,
|
||||
"defaultNav": true
|
||||
},
|
||||
{
|
||||
"type": "page",
|
||||
"name": "Root Page (Tab B)",
|
||||
"path": "/a/myorgid-simple-app/?tab=b",
|
||||
"role": "Viewer",
|
||||
"addToNav": true
|
||||
},
|
||||
{
|
||||
"type": "page",
|
||||
"name": "React Config",
|
||||
"path": "/plugins/myorgid-simple-app/?page=page2",
|
||||
"role": "Admin",
|
||||
"addToNav": true
|
||||
},
|
||||
{
|
||||
"type": "dashboard",
|
||||
"name": "Streaming Example",
|
||||
"path": "dashboards/streaming.json"
|
||||
},
|
||||
{
|
||||
"type": "dashboard",
|
||||
"name": "Lots of Stats",
|
||||
"path": "dashboards/stats.json"
|
||||
}
|
||||
],
|
||||
|
||||
"dependencies": {
|
||||
"grafanaDependency": ">=7.0.0",
|
||||
"grafanaVersion": "7.0.0",
|
||||
"plugins": []
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user