Skip to content

Commit

Permalink
fix: Ensure to populate client opts even when JSONData is nil
Browse files Browse the repository at this point in the history
* Fix data races in unit tests

Signed-off-by: Mahendra Paipuri <[email protected]>
  • Loading branch information
mahendrapaipuri committed Dec 24, 2024
1 parent 3482e92 commit 57466cf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
7 changes: 0 additions & 7 deletions pkg/plugin/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 6 additions & 8 deletions pkg/plugin/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions pkg/plugin/dashboard/panels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"path/filepath"
"sync"
"testing"

"github.com/grafana/grafana-plugin-sdk-go/backend"
Expand All @@ -20,6 +21,8 @@ import (
. "github.com/smartystreets/goconvey/convey"
)

var muLock sync.RWMutex

func TestDashboardFetchWithLocalChrome(t *testing.T) {
var execPath string

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 57466cf

Please sign in to comment.