fix datasource tests

This commit is contained in:
Alexander Zobnin
2020-06-03 17:02:12 +03:00
parent 67ed44cf20
commit 6f95e9f7e8
2 changed files with 53 additions and 109 deletions

9
pkg/cache/cache.go vendored
View File

@@ -10,6 +10,15 @@ import (
cache "github.com/patrickmn/go-cache" cache "github.com/patrickmn/go-cache"
) )
const (
// For use with functions that take an expiration time.
NoExpiration time.Duration = -1
// For use with functions that take an expiration time. Equivalent to
// passing in the same expiration duration as was given to New() or
// NewFrom() when the cache was created (e.g. 5 minutes.)
DefaultExpiration time.Duration = 0
)
// Cache is a abstraction over go-cache. // Cache is a abstraction over go-cache.
type Cache struct { type Cache struct {
cache *cache.Cache cache *cache.Cache

View File

@@ -1,66 +1,64 @@
package main package main
import ( import (
"fmt"
"testing" "testing"
simplejson "github.com/bitly/go-simplejson" "github.com/alexanderzobnin/grafana-zabbix/pkg/cache"
"github.com/grafana/grafana_plugin_model/go/datasource" "github.com/grafana/grafana-plugin-sdk-go/backend"
hclog "github.com/hashicorp/go-hclog" "github.com/grafana/grafana-plugin-sdk-go/backend/log"
cache "github.com/patrickmn/go-cache"
"gotest.tools/assert" "gotest.tools/assert"
"gotest.tools/assert/cmp"
) )
func TestZabbixBackend_getCachedDatasource(t *testing.T) { func TestZabbixBackend_getCachedDatasource(t *testing.T) {
basicDatasourceInfo := &datasource.DatasourceInfo{ basicDsSettings := &backend.DataSourceInstanceSettings{
Id: 1, ID: 1,
Name: "TestDatasource", Name: "TestDatasource",
Url: "http://zabbix.org/zabbix", URL: "http://zabbix.org/zabbix",
JSONData: []byte("{}"),
} }
basicDatasourceInfoHash := HashDatasourceInfo(basicDatasourceInfo)
modifiedDatasource, _ := newZabbixDatasource(basicDatasourceInfo) modifiedDatasourceSettings := &backend.DataSourceInstanceSettings{
modifiedDatasource.authToken = "AB404F1234" ID: 1,
Name: "TestDatasource",
altDatasourceInfo := &datasource.DatasourceInfo{ URL: "http://another.zabbix.org/zabbix",
Id: 2, JSONData: []byte("{}"),
Name: "AnotherDatasource",
Url: "http://zabbix.org/another/zabbix",
} }
altDatasourceInfoHash := HashDatasourceInfo(altDatasourceInfo) modifiedDatasource, _ := NewZabbixDatasourceInstance(modifiedDatasourceSettings)
basicDS, _ := NewZabbixDatasourceInstance(basicDsSettings)
dsCache := cache.NewCache(cache.NoExpiration, cache.NoExpiration)
dsCache.Set("1-1", basicDS)
basicDS, _ := newZabbixDatasource(basicDatasourceInfo)
tests := []struct { tests := []struct {
name string name string
cache *cache.Cache cache *cache.Cache
request *datasource.DatasourceRequest pluginContext backend.PluginContext
want *ZabbixDatasourceInstance want *ZabbixDatasourceInstance
}{ }{
{ {
name: "Uncached Datasource (nothing in cache)", name: "Uncached Datasource (nothing in cache)",
request: &datasource.DatasourceRequest{ pluginContext: backend.PluginContext{
Datasource: basicDatasourceInfo, OrgID: 1,
DataSourceInstanceSettings: basicDsSettings,
}, },
want: basicDS, want: basicDS,
}, },
{ {
name: "Uncached Datasource (cache miss)", name: "Cached Datasource",
cache: cache.NewFrom(cache.NoExpiration, cache.NoExpiration, map[string]cache.Item{ cache: dsCache,
basicDatasourceInfoHash: {Object: modifiedDatasource}, pluginContext: backend.PluginContext{
}), OrgID: 1,
request: &datasource.DatasourceRequest{ DataSourceInstanceSettings: basicDsSettings,
Datasource: altDatasourceInfo,
}, },
want: basicDS, want: basicDS,
}, },
{ {
name: "Cached Datasource", name: "Cached then modified",
cache: cache.NewFrom(cache.NoExpiration, cache.NoExpiration, map[string]cache.Item{ cache: dsCache,
altDatasourceInfoHash: {Object: basicDS}, pluginContext: backend.PluginContext{
basicDatasourceInfoHash: {Object: modifiedDatasource}, OrgID: 1,
}), DataSourceInstanceSettings: modifiedDatasourceSettings,
request: &datasource.DatasourceRequest{
Datasource: basicDatasourceInfo,
}, },
want: modifiedDatasource, want: modifiedDatasource,
}, },
@@ -68,84 +66,21 @@ func TestZabbixBackend_getCachedDatasource(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if tt.cache == nil { if tt.cache == nil {
tt.cache = cache.New(cache.NoExpiration, cache.NoExpiration) tt.cache = cache.NewCache(cache.NoExpiration, cache.NoExpiration)
} }
b := &ZabbixPlugin{ ds := &ZabbixDatasource{
logger: hclog.New(&hclog.LoggerOptions{ datasourceCache: tt.cache,
Name: "TestZabbixBackend_getCachedDatasource", logger: log.New(),
Level: hclog.LevelFromString("DEBUG"),
}),
datasourceCache: &Cache{cache: tt.cache},
} }
got, _ := b.GetDatasource(tt.request) got, _ := ds.GetDatasource(tt.pluginContext)
// Only checking the authToken, being the easiest value to, and guarantee equality for // Only checking the URL, being the easiest value to, and guarantee equality for
assert.Equal(t, tt.want.authToken, got.authToken) assert.Equal(t, tt.want.zabbixAPI.GetUrl().String(), got.zabbixAPI.GetUrl().String())
// Ensure the datasource is in the cache // Ensure the datasource is in the cache
cacheds, ok := tt.cache.Get(HashDatasourceInfo(tt.request.GetDatasource())) cacheds, ok := tt.cache.Get(fmt.Sprint("1-", tt.pluginContext.DataSourceInstanceSettings.ID))
assert.Equal(t, true, ok) assert.Equal(t, true, ok)
assert.Equal(t, got, cacheds) assert.Equal(t, got, cacheds)
}) })
} }
} }
func TestBuildResponse(t *testing.T) {
jsonData := simplejson.New()
jsonData.Set("testing", []int{5, 12, 75})
tests := []struct {
name string
responseData interface{}
want *datasource.DatasourceResponse
wantErr string
}{
{
name: "simplejson Response",
responseData: jsonData,
want: &datasource.DatasourceResponse{
Results: []*datasource.QueryResult{
{
RefId: "zabbixAPI",
MetaJson: `{"testing":[5,12,75]}`,
},
},
},
},
{
name: "Connection Status Response",
responseData: connectionTestResponse{
ZabbixVersion: "2.4",
DbConnectorStatus: &dbConnectionStatus{
DsType: "mysql",
DsName: "MyDatabase",
},
},
want: &datasource.DatasourceResponse{
Results: []*datasource.QueryResult{
{
RefId: "zabbixAPI",
MetaJson: `{"zabbixVersion":"2.4","dbConnectorStatus":{"dsType":"mysql","dsName":"MyDatabase"}}`,
},
},
},
},
{
name: "Unmarshalable",
responseData: 2i,
wantErr: "json: unsupported type: complex128",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := BuildResponse(tt.responseData)
if tt.wantErr != "" {
assert.Error(t, err, tt.wantErr)
assert.Assert(t, cmp.Nil(got))
return
}
assert.NilError(t, err)
assert.DeepEqual(t, got, tt.want)
})
}
}