2024-09-09 05:38:35 -04:00
package pluginassets
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/config"
"github.com/grafana/grafana/pkg/plugins/pluginscdn"
)
2026-01-15 09:06:16 -05:00
// cdnFS is a simple mock FS that returns CDN type
type cdnFS struct {
plugins . FS
}
func ( f * cdnFS ) Type ( ) plugins . FSType {
return plugins . FSTypeCDN
}
func TestCalculateLoadingStrategy ( t * testing . T ) {
2024-09-09 05:38:35 -04:00
const pluginID = "grafana-test-datasource"
const (
incompatVersion = "4.14.0"
compatVersion = CreatePluginVersionScriptSupportEnabled
futureVersion = "5.0.0"
)
tcs := [ ] struct {
name string
2026-01-05 06:12:31 -05:00
pluginSettings config . PluginSettings
2026-01-15 09:06:16 -05:00
plugin * plugins . Plugin
2024-09-09 05:38:35 -04:00
expected plugins . LoadingStrategy
} {
{
name : "Expected LoadingStrategyScript when create-plugin version is compatible and plugin is not angular" ,
pluginSettings : newPluginSettings ( pluginID , map [ string ] string {
CreatePluginVersionCfgKey : compatVersion ,
} ) ,
2026-01-15 09:06:16 -05:00
plugin : newPluginForLoadingStrategy ( pluginID , withAngularForLoadingStrategy ( false ) ) ,
2024-09-09 05:38:35 -04:00
expected : plugins . LoadingStrategyScript ,
} ,
{
name : "Expected LoadingStrategyScript when parent create-plugin version is compatible and plugin is not angular" ,
pluginSettings : newPluginSettings ( "parent-datasource" , map [ string ] string {
CreatePluginVersionCfgKey : compatVersion ,
} ) ,
2026-01-15 09:06:16 -05:00
plugin : newPluginForLoadingStrategy ( pluginID , withAngularForLoadingStrategy ( false ) , func ( p * plugins . Plugin ) {
p . Parent = & plugins . Plugin {
JSONData : plugins . JSONData { ID : "parent-datasource" } ,
FS : plugins . NewFakeFS ( ) ,
}
2024-09-09 05:38:35 -04:00
} ) ,
expected : plugins . LoadingStrategyScript ,
} ,
{
name : "Expected LoadingStrategyScript when create-plugin version is future compatible and plugin is not angular" ,
pluginSettings : newPluginSettings ( pluginID , map [ string ] string {
CreatePluginVersionCfgKey : futureVersion ,
} ) ,
2026-01-15 09:06:16 -05:00
plugin : newPluginForLoadingStrategy ( pluginID , withAngularForLoadingStrategy ( false ) , withFSForLoadingStrategy ( plugins . NewFakeFS ( ) ) ) ,
2024-09-09 05:38:35 -04:00
expected : plugins . LoadingStrategyScript ,
} ,
{
name : "Expected LoadingStrategyScript when create-plugin version is not provided, plugin is not angular and is not configured as CDN enabled" ,
2026-01-15 09:06:16 -05:00
pluginSettings : newPluginSettings ( pluginID , map [ string ] string { } ) ,
plugin : newPluginForLoadingStrategy ( pluginID , withAngularForLoadingStrategy ( false ) , withFSForLoadingStrategy ( plugins . NewFakeFS ( ) ) ) ,
expected : plugins . LoadingStrategyScript ,
2024-09-09 05:38:35 -04:00
} ,
{
2025-11-20 09:03:24 -05:00
name : "Expected LoadingStrategyScript when create-plugin version is not compatible, plugin is not angular, is not configured as CDN enabled and does not have a CDN fs" ,
2024-09-09 05:38:35 -04:00
pluginSettings : newPluginSettings ( pluginID , map [ string ] string {
CreatePluginVersionCfgKey : incompatVersion ,
} ) ,
2026-01-15 09:06:16 -05:00
plugin : newPluginForLoadingStrategy ( pluginID , withAngularForLoadingStrategy ( false ) , withClassForLoadingStrategy ( plugins . ClassExternal ) , withFSForLoadingStrategy ( plugins . NewFakeFS ( ) ) ) ,
2024-09-09 05:38:35 -04:00
expected : plugins . LoadingStrategyScript ,
} ,
2024-09-11 09:35:13 -04:00
{
name : "Expected LoadingStrategyFetch when parent create-plugin version is not set, is configured as CDN enabled and plugin is not angular" ,
2026-01-05 06:12:31 -05:00
pluginSettings : config . PluginSettings {
2024-09-11 09:35:13 -04:00
"parent-datasource" : {
"cdn" : "true" ,
} ,
} ,
2026-01-15 09:06:16 -05:00
plugin : newPluginForLoadingStrategy ( pluginID , withAngularForLoadingStrategy ( false ) , func ( p * plugins . Plugin ) {
p . Parent = & plugins . Plugin {
JSONData : plugins . JSONData { ID : "parent-datasource" } ,
FS : plugins . NewFakeFS ( ) ,
}
2024-09-11 09:35:13 -04:00
} ) ,
expected : plugins . LoadingStrategyFetch ,
} ,
{
name : "Expected LoadingStrategyFetch when parent create-plugin version is not set, is configured as CDN enabled and plugin is angular" ,
2026-01-05 06:12:31 -05:00
pluginSettings : config . PluginSettings {
2024-09-11 09:35:13 -04:00
"parent-datasource" : {
"cdn" : "true" ,
} ,
} ,
2026-01-15 09:06:16 -05:00
plugin : newPluginForLoadingStrategy ( pluginID , withAngularForLoadingStrategy ( true ) , func ( p * plugins . Plugin ) {
p . Parent = & plugins . Plugin {
JSONData : plugins . JSONData { ID : "parent-datasource" } ,
FS : plugins . NewFakeFS ( ) ,
}
2024-09-11 09:35:13 -04:00
} ) ,
expected : plugins . LoadingStrategyFetch ,
} ,
{
name : "Expected LoadingStrategyFetch when parent create-plugin version is not set, is not configured as CDN enabled and plugin is angular" ,
2026-01-05 06:12:31 -05:00
pluginSettings : config . PluginSettings { } ,
2026-01-15 09:06:16 -05:00
plugin : newPluginForLoadingStrategy ( pluginID , withAngularForLoadingStrategy ( true ) , withFSForLoadingStrategy ( plugins . NewFakeFS ( ) ) , func ( p * plugins . Plugin ) {
p . Parent = & plugins . Plugin {
JSONData : plugins . JSONData { ID : "parent-datasource" } ,
FS : plugins . NewFakeFS ( ) ,
}
2024-09-11 09:35:13 -04:00
} ) ,
expected : plugins . LoadingStrategyFetch ,
} ,
2024-09-09 05:38:35 -04:00
{
2025-11-20 09:03:24 -05:00
name : "Expected LoadingStrategyFetch when create-plugin version is not compatible, plugin is not angular, and plugin is configured as CDN enabled" ,
2024-09-09 05:38:35 -04:00
pluginSettings : newPluginSettings ( pluginID , map [ string ] string {
"cdn" : "true" ,
CreatePluginVersionCfgKey : incompatVersion ,
} ) ,
2026-01-15 09:06:16 -05:00
plugin : newPluginForLoadingStrategy ( pluginID , withAngularForLoadingStrategy ( false ) , withClassForLoadingStrategy ( plugins . ClassExternal ) , withFSForLoadingStrategy ( plugins . NewFakeFS ( ) ) ) ,
2024-09-09 05:38:35 -04:00
expected : plugins . LoadingStrategyFetch ,
} ,
{
name : "Expected LoadingStrategyFetch when create-plugin version is not compatible and plugin is angular" ,
pluginSettings : newPluginSettings ( pluginID , map [ string ] string {
CreatePluginVersionCfgKey : incompatVersion ,
} ) ,
2026-01-15 09:06:16 -05:00
plugin : newPluginForLoadingStrategy ( pluginID , withAngularForLoadingStrategy ( true ) , withFSForLoadingStrategy ( plugins . NewFakeFS ( ) ) ) ,
2024-09-09 05:38:35 -04:00
expected : plugins . LoadingStrategyFetch ,
} ,
{
name : "Expected LoadingStrategyFetch when create-plugin version is not compatible, plugin is not angular and plugin is configured as CDN enabled" ,
pluginSettings : newPluginSettings ( pluginID , map [ string ] string {
"cdn" : "true" ,
CreatePluginVersionCfgKey : incompatVersion ,
} ) ,
2026-01-15 09:06:16 -05:00
plugin : newPluginForLoadingStrategy ( pluginID , withAngularForLoadingStrategy ( false ) , withFSForLoadingStrategy ( plugins . NewFakeFS ( ) ) ) ,
2024-09-09 05:38:35 -04:00
expected : plugins . LoadingStrategyFetch ,
} ,
{
2025-11-20 09:03:24 -05:00
name : "Expected LoadingStrategyFetch when create-plugin version is not compatible, plugin is not angular and has a CDN fs" ,
2024-09-09 05:38:35 -04:00
pluginSettings : newPluginSettings ( pluginID , map [ string ] string {
CreatePluginVersionCfgKey : incompatVersion ,
} ) ,
2026-01-15 09:06:16 -05:00
plugin : newPluginForLoadingStrategy ( pluginID , withAngularForLoadingStrategy ( false ) , withFSForLoadingStrategy (
& cdnFS { FS : plugins . NewFakeFS ( ) } ,
2025-11-20 09:03:24 -05:00
) ) ,
2024-09-09 05:38:35 -04:00
expected : plugins . LoadingStrategyFetch ,
} ,
{
2025-11-20 09:03:24 -05:00
name : "Expected LoadingStrategyScript when plugin setting create-plugin version is badly formatted, plugin is not configured as CDN enabled and does not have a CDN fs" ,
2024-09-09 05:38:35 -04:00
pluginSettings : newPluginSettings ( pluginID , map [ string ] string {
CreatePluginVersionCfgKey : "invalidSemver" ,
} ) ,
2026-01-15 09:06:16 -05:00
plugin : newPluginForLoadingStrategy ( pluginID , withAngularForLoadingStrategy ( false ) , withFSForLoadingStrategy ( plugins . NewFakeFS ( ) ) ) ,
2024-09-09 05:38:35 -04:00
expected : plugins . LoadingStrategyScript ,
} ,
}
for _ , tc := range tcs {
t . Run ( tc . name , func ( t * testing . T ) {
2026-01-15 09:06:16 -05:00
cfg := & config . PluginManagementCfg {
PluginSettings : tc . pluginSettings ,
2024-09-09 05:38:35 -04:00
}
2026-01-15 09:06:16 -05:00
cdn := pluginscdn . ProvideService ( & config . PluginManagementCfg {
PluginsCDNURLTemplate : "http://cdn.example.com" , // required for cdn.PluginSupported check
PluginSettings : tc . pluginSettings ,
} )
2024-09-09 05:38:35 -04:00
2026-01-15 09:06:16 -05:00
got := CalculateLoadingStrategy ( tc . plugin , cfg , cdn )
2024-09-09 05:38:35 -04:00
assert . Equal ( t , tc . expected , got , "unexpected loading strategy" )
} )
}
}
2026-01-15 09:06:16 -05:00
func newPluginForLoadingStrategy ( pluginID string , cbs ... func ( * plugins . Plugin ) ) * plugins . Plugin {
p := & plugins . Plugin {
2024-09-09 05:38:35 -04:00
JSONData : plugins . JSONData {
ID : pluginID ,
} ,
}
for _ , cb := range cbs {
2026-01-15 09:06:16 -05:00
cb ( p )
2024-09-09 05:38:35 -04:00
}
return p
}
2026-01-15 09:06:16 -05:00
func withAngularForLoadingStrategy ( angular bool ) func ( * plugins . Plugin ) {
return func ( p * plugins . Plugin ) {
2024-10-04 08:55:09 -04:00
p . Angular = plugins . AngularMeta { Detected : angular }
}
}
2026-01-15 09:06:16 -05:00
func withFSForLoadingStrategy ( fs plugins . FS ) func ( * plugins . Plugin ) {
return func ( p * plugins . Plugin ) {
p . FS = fs
2025-01-14 03:51:01 -05:00
}
}
2026-01-15 09:06:16 -05:00
func withClassForLoadingStrategy ( class plugins . Class ) func ( * plugins . Plugin ) {
return func ( p * plugins . Plugin ) {
p . Class = class
2024-09-09 05:38:35 -04:00
}
}
2026-01-05 06:12:31 -05:00
func newPluginSettings ( pluginID string , kv map [ string ] string ) config . PluginSettings {
return config . PluginSettings {
2024-09-09 05:38:35 -04:00
pluginID : kv ,
}
}