Commit f9774acc by Arve Knudsen Committed by GitHub

Enable errcheck for golangci-lint (#19976)

* Enable `errcheck` for golangci-lint
* Fix linting errors
* pkg/plugins: Fix tests
* pkg/plugins: Add test
parent 9e004b92
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util/errutil"
datasourceV1 "github.com/grafana/grafana-plugin-model/go/datasource" datasourceV1 "github.com/grafana/grafana-plugin-model/go/datasource"
sdk "github.com/grafana/grafana-plugin-sdk-go" sdk "github.com/grafana/grafana-plugin-sdk-go"
...@@ -46,16 +47,16 @@ type DataSourcePlugin struct { ...@@ -46,16 +47,16 @@ type DataSourcePlugin struct {
} }
func (p *DataSourcePlugin) Load(decoder *json.Decoder, pluginDir string) error { func (p *DataSourcePlugin) Load(decoder *json.Decoder, pluginDir string) error {
if err := decoder.Decode(&p); err != nil { if err := decoder.Decode(p); err != nil {
return err return errutil.Wrapf(err, "Failed to decode datasource plugin")
} }
if !p.isVersionOne() && !setting.IsExpressionsEnabled() { if !p.isVersionOne() && !setting.IsExpressionsEnabled() {
return errors.New("A plugin version 2 was found but expressions feature toggle is not enabled") return errors.New("A plugin version 2 was found, but expressions feature toggle is not enabled")
} }
if err := p.registerPlugin(pluginDir); err != nil { if err := p.registerPlugin(pluginDir); err != nil {
return err return errutil.Wrapf(err, "Failed to register plugin")
} }
DataSources[p.Id] = p DataSources[p.Id] = p
......
...@@ -5,30 +5,68 @@ import ( ...@@ -5,30 +5,68 @@ import (
"encoding/json" "encoding/json"
"testing" "testing"
"github.com/grafana/grafana/pkg/setting"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestLoadDatasourceVersion(t *testing.T) { func TestLoadDatasourceVersion(t *testing.T) {
t.Run("If plugin version is not set, it should be treated as plugin version one", func(t *testing.T) { t.Run("If plugin version is not set, it should be treated as plugin version one", func(t *testing.T) {
pluginJSON, _ := json.Marshal(DataSourcePlugin{}) refPlug := DataSourcePlugin{}
pluginJSON, err := json.Marshal(refPlug)
require.NoError(t, err)
datasourcePlugin := DataSourcePlugin{} datasourcePlugin := DataSourcePlugin{}
(&datasourcePlugin).Load(json.NewDecoder(bytes.NewReader(pluginJSON)), "/tmp") err = datasourcePlugin.Load(json.NewDecoder(bytes.NewReader(pluginJSON)), "/tmp")
require.NoError(t, err)
delete(Plugins, refPlug.Id)
delete(DataSources, refPlug.Id)
assert.True(t, datasourcePlugin.isVersionOne()) assert.True(t, datasourcePlugin.isVersionOne())
}) })
t.Run("If plugin version is set to one, it should be treated as plugin version one", func(t *testing.T) { t.Run("If plugin version is set to one, it should be treated as plugin version one", func(t *testing.T) {
pluginJSON, _ := json.Marshal(DataSourcePlugin{SDK: false}) refPlug := DataSourcePlugin{SDK: false}
pluginJSON, err := json.Marshal(refPlug)
require.NoError(t, err)
datasourcePlugin := DataSourcePlugin{} datasourcePlugin := DataSourcePlugin{}
(&datasourcePlugin).Load(json.NewDecoder(bytes.NewReader(pluginJSON)), "/tmp") err = datasourcePlugin.Load(json.NewDecoder(bytes.NewReader(pluginJSON)), "/tmp")
require.NoError(t, err)
delete(Plugins, refPlug.Id)
delete(DataSources, refPlug.Id)
assert.True(t, datasourcePlugin.isVersionOne()) assert.True(t, datasourcePlugin.isVersionOne())
assert.False(t, datasourcePlugin.SDK) assert.False(t, datasourcePlugin.SDK)
}) })
t.Run("If plugin version is set to two, it should not be treated as plugin version one", func(t *testing.T) { t.Run("If plugin version is set to two, it should not be treated as plugin version one", func(t *testing.T) {
pluginJSON, _ := json.Marshal(DataSourcePlugin{SDK: true}) refPlug := DataSourcePlugin{SDK: true}
pluginJSON, err := json.Marshal(refPlug)
require.NoError(t, err)
origToggles := setting.FeatureToggles
setting.FeatureToggles = map[string]bool{"expressions": true}
datasourcePlugin := DataSourcePlugin{} datasourcePlugin := DataSourcePlugin{}
(&datasourcePlugin).Load(json.NewDecoder(bytes.NewReader(pluginJSON)), "/tmp") err = datasourcePlugin.Load(json.NewDecoder(bytes.NewReader(pluginJSON)), "/tmp")
setting.FeatureToggles = origToggles
require.NoError(t, err)
delete(Plugins, refPlug.Id)
delete(DataSources, refPlug.Id)
assert.False(t, datasourcePlugin.isVersionOne()) assert.False(t, datasourcePlugin.isVersionOne())
assert.True(t, datasourcePlugin.SDK) assert.True(t, datasourcePlugin.SDK)
}) })
t.Run("Plugin version two requires expressions feature to be toggled", func(t *testing.T) {
refPlug := DataSourcePlugin{SDK: true}
pluginJSON, err := json.Marshal(refPlug)
require.NoError(t, err)
require.Nil(t, setting.FeatureToggles, "setting.FeatureToggles shouldn't be set")
datasourcePlugin := DataSourcePlugin{}
err = datasourcePlugin.Load(json.NewDecoder(bytes.NewReader(pluginJSON)), "/tmp")
require.EqualError(t, err, "A plugin version 2 was found, but expressions feature toggle is not enabled")
})
} }
...@@ -2,7 +2,6 @@ package plugins ...@@ -2,7 +2,6 @@ package plugins
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"strings" "strings"
...@@ -61,7 +60,7 @@ type PluginBase struct { ...@@ -61,7 +60,7 @@ type PluginBase struct {
func (pb *PluginBase) registerPlugin(pluginDir string) error { func (pb *PluginBase) registerPlugin(pluginDir string) error {
if _, exists := Plugins[pb.Id]; exists { if _, exists := Plugins[pb.Id]; exists {
return errors.New("Plugin with same id already exists") return fmt.Errorf("Plugin with ID %q already exists", pb.Id)
} }
if !strings.HasPrefix(pluginDir, setting.StaticRootPath) { if !strings.HasPrefix(pluginDir, setting.StaticRootPath) {
......
enable: enable:
run: run:
deadline: 10m deadline: 10m
linters: linters:
disable-all: true disable-all: true
...@@ -15,11 +15,11 @@ linters: ...@@ -15,11 +15,11 @@ linters:
- typecheck - typecheck
- unconvert - unconvert
- unused - unused
- varcheck - varcheck
- goconst - goconst
- staticcheck - staticcheck
- errcheck
linter-settings: linter-settings:
goconst: goconst:
ignore-tests: true ignore-tests: true
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment