Angular deprecation: Disable dynamic angular inspector if CheckForPluginUpdates is false (#91194)

* Angular deprecation: Disable dynamic angular inspector if CheckForPluginUpdates is false

* Add tests

* add type checks for dynamic service
This commit is contained in:
Giuseppe Guerra 2024-07-31 10:10:12 +02:00 committed by GitHub
parent 160fe2a3a4
commit 95f340738c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 0 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/grafana/grafana/pkg/plugins/log"
"github.com/grafana/grafana/pkg/plugins/manager/loader/angular/angulardetector"
"github.com/grafana/grafana/pkg/registry"
"github.com/grafana/grafana/pkg/services/pluginsintegration/angularpatternsstore"
"github.com/grafana/grafana/pkg/setting"
)
@ -35,6 +36,7 @@ type Dynamic struct {
httpClient http.Client
baseURL string
disabled bool
// store is the underlying angular patterns store used as a cache.
store angularpatternsstore.Service
@ -64,6 +66,9 @@ func ProvideDynamic(cfg *setting.Cfg, store angularpatternsstore.Service) (*Dyna
httpClient: makeHttpClient(),
baseURL: cfg.GrafanaComAPIURL,
backgroundJobInterval: backgroundJobInterval,
// Disable the background service if the user has opted out of plugin updates.
// (useful for air-gapped installations)
disabled: !cfg.CheckForPluginUpdates,
}
d.log.Debug("Providing dynamic angular detection patterns", "baseURL", d.baseURL, "interval", d.backgroundJobInterval)
@ -296,6 +301,11 @@ func (d *Dynamic) Run(ctx context.Context) error {
}
}
// IsDisabled returns whether the dynamic detectors provider background service is disabled.
func (d *Dynamic) IsDisabled() bool {
return d.disabled
}
// ProvideDetectors returns the cached detectors. It returns an empty slice if there's no value.
func (d *Dynamic) ProvideDetectors(_ context.Context) []angulardetector.AngularDetector {
d.mux.RLock()
@ -323,3 +333,10 @@ func makeHttpClient() http.Client {
Transport: tr,
}
}
// static checks
var (
_ registry.BackgroundService = (*Dynamic)(nil)
_ registry.CanBeDisabled = (*Dynamic)(nil)
)

View File

@ -421,6 +421,24 @@ func TestDynamicAngularDetectorsProviderBackgroundService(t *testing.T) {
require.True(t, jobCalls.calledX(tcRuns), "should have the correct number of job calls")
require.True(t, gcom.httpCalls.calledX(tcRuns), "should have the correct number of gcom api calls")
})
t.Run("IsDisabled", func(t *testing.T) {
for _, tc := range []struct {
name string
checkForPluginUpdates bool
expIsDisabled bool
}{
{name: "true", checkForPluginUpdates: true, expIsDisabled: false},
{name: "false", checkForPluginUpdates: false, expIsDisabled: true},
} {
t.Run(tc.name, func(t *testing.T) {
cfg := setting.NewCfg()
cfg.CheckForPluginUpdates = tc.checkForPluginUpdates
svc := provideDynamic(t, srv.URL, provideDynamicOpts{cfg: cfg})
require.Equal(t, tc.expIsDisabled, svc.IsDisabled(), "IsDisabled should return correct value")
})
}
})
})
}