From 57466cfd5c920ea7922f7411748b6e3c4ebe0945 Mon Sep 17 00:00:00 2001 From: Mahendra Paipuri Date: Tue, 24 Dec 2024 11:54:27 +0100 Subject: [PATCH] fix: Ensure to populate client opts even when JSONData is nil * Fix data races in unit tests Signed-off-by: Mahendra Paipuri --- pkg/plugin/app.go | 7 ------- pkg/plugin/config/settings.go | 14 ++++++-------- pkg/plugin/dashboard/panels_test.go | 7 +++++++ 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/pkg/plugin/app.go b/pkg/plugin/app.go index 55f987f..9fb668d 100644 --- a/pkg/plugin/app.go +++ b/pkg/plugin/app.go @@ -72,13 +72,6 @@ func NewDashboardReporterApp(ctx context.Context, settings backend.AppInstanceSe return nil, fmt.Errorf("error loading config: %w", err) } - // Validate plugin config - if err := app.conf.Validate(); err != nil { - app.ctxLogger.Error("error config validation", "err", err) - - return nil, fmt.Errorf("error config validation: %w", err) - } - app.ctxLogger.Info("starting plugin with initial config: " + app.conf.String()) // Make a new HTTP client diff --git a/pkg/plugin/config/settings.go b/pkg/plugin/config/settings.go index 3da3c28..a195fdb 100644 --- a/pkg/plugin/config/settings.go +++ b/pkg/plugin/config/settings.go @@ -171,6 +171,8 @@ func Load(ctx context.Context, settings backend.AppInstanceSettings) (Config, er }, } + var err error + // Fetch token, if configured in SecureJSONData if settings.DecryptedSecureJSONData != nil { if saToken, ok := settings.DecryptedSecureJSONData[SaToken]; ok && saToken != "" { @@ -179,14 +181,10 @@ func Load(ctx context.Context, settings backend.AppInstanceSettings) (Config, er } // Update plugin settings defaults - if settings.JSONData == nil || string(settings.JSONData) == "null" { - return config, nil - } - - var err error - - if err = json.Unmarshal(settings.JSONData, &config); err != nil { //nolint:musttag - return Config{}, err + if settings.JSONData != nil && string(settings.JSONData) != "null" { + if err = json.Unmarshal(settings.JSONData, &config); err != nil { //nolint:musttag + return Config{}, err + } } // Override provisioned config from env vars, if set diff --git a/pkg/plugin/dashboard/panels_test.go b/pkg/plugin/dashboard/panels_test.go index 699abee..6cd5427 100644 --- a/pkg/plugin/dashboard/panels_test.go +++ b/pkg/plugin/dashboard/panels_test.go @@ -9,6 +9,7 @@ import ( "os" "os/exec" "path/filepath" + "sync" "testing" "github.com/grafana/grafana-plugin-sdk-go/backend" @@ -20,6 +21,8 @@ import ( . "github.com/smartystreets/goconvey/convey" ) +var muLock sync.RWMutex + func TestDashboardFetchWithLocalChrome(t *testing.T) { var execPath string @@ -74,8 +77,10 @@ func TestDashboardFetchWithLocalChrome(t *testing.T) { requestCookie := "" ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + muLock.Lock() requestURI = append(requestURI, r.RequestURI) requestCookie = r.Header.Get(backend.CookiesHeaderName) + muLock.Unlock() if _, err := w.Write(data); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -175,8 +180,10 @@ func TestDashboardFetchWithRemoteChrome(t *testing.T) { requestCookie := "" ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + muLock.Lock() requestURI = append(requestURI, r.RequestURI) requestCookie = r.Header.Get(backend.CookiesHeaderName) + muLock.Unlock() if _, err := w.Write(data); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError)