Fix cache tests

This commit is contained in:
Alexander Zobnin
2020-06-03 16:21:30 +03:00
parent 1e04c310df
commit 7872d55dbc

View File

@@ -3,38 +3,72 @@ package cache
import ( import (
"testing" "testing"
"github.com/grafana/grafana_plugin_model/go/datasource" "github.com/grafana/grafana-plugin-sdk-go/backend"
"gotest.tools/assert" "gotest.tools/assert"
) )
func TestHashDatasourceInfo(t *testing.T) { func TestHashDatasourceInfo(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
dsInfo *datasource.DatasourceInfo dsInfoBefore *backend.DataSourceInstanceSettings
want string dsInfoAfter *backend.DataSourceInstanceSettings
equal bool
}{ }{
{ {
name: "Normal Datasource Info", name: "Same datasource settings",
dsInfo: &datasource.DatasourceInfo{ dsInfoBefore: &backend.DataSourceInstanceSettings{
Id: 1, ID: 1,
OrgId: 1,
Name: "Zabbix", Name: "Zabbix",
Type: "alexanderzobnin-zabbix-datasource", URL: "https://localhost:3306/zabbix/api_jsonrpc.php",
Url: "https://localhost:3306/zabbix/api_jsonrpc.php", JSONData: []byte("{}"),
JsonData: "{}", DecryptedSecureJSONData: map[string]string{
DecryptedSecureJsonData: map[string]string{
"username": "grafanaZabbixUser", "username": "grafanaZabbixUser",
"password": "$uper$ecr3t!!!", "password": "$uper$ecr3t!!!",
}, },
}, },
want: "ed161f89179c46d9a578e4d7e92ff95444222e0a", dsInfoAfter: &backend.DataSourceInstanceSettings{
ID: 1,
Name: "Zabbix",
URL: "https://localhost:3306/zabbix/api_jsonrpc.php",
JSONData: []byte("{}"),
DecryptedSecureJSONData: map[string]string{
"username": "grafanaZabbixUser",
"password": "$uper$ecr3t!!!",
},
},
equal: true,
},
{
name: "Password changed",
dsInfoBefore: &backend.DataSourceInstanceSettings{
ID: 1,
Name: "Zabbix",
URL: "https://localhost:3306/zabbix/api_jsonrpc.php",
JSONData: []byte("{}"),
DecryptedSecureJSONData: map[string]string{
"username": "grafanaZabbixUser",
"password": "$uper$ecr3t!!!",
},
},
dsInfoAfter: &backend.DataSourceInstanceSettings{
ID: 1,
Name: "Zabbix",
URL: "https://localhost:3306/zabbix/api_jsonrpc.php",
JSONData: []byte("{}"),
DecryptedSecureJSONData: map[string]string{
"username": "grafanaZabbixUser",
"password": "new$uper$ecr3t!!!",
},
},
equal: false,
}, },
// Can't find a case where the input causes the encoder to fail
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got := HashDatasourceInfo(tt.dsInfo) before := HashDatasourceInfo(tt.dsInfoBefore)
assert.Equal(t, tt.want, got) after := HashDatasourceInfo(tt.dsInfoAfter)
got := before == after
assert.Equal(t, tt.equal, got)
}) })
} }
} }
@@ -42,18 +76,16 @@ func TestHashDatasourceInfo(t *testing.T) {
func BenchmarkHashDatasourceInfo(b *testing.B) { func BenchmarkHashDatasourceInfo(b *testing.B) {
benches := []struct { benches := []struct {
name string name string
dsInfo *datasource.DatasourceInfo dsInfo *backend.DataSourceInstanceSettings
}{ }{
{ {
"Normal Datasource Info", "Normal Datasource Info",
&datasource.DatasourceInfo{ &backend.DataSourceInstanceSettings{
Id: 4, ID: 4,
OrgId: 6,
Name: "MyZabbixDatasource", Name: "MyZabbixDatasource",
Type: "alexanderzobnin-zabbix-datasource", URL: "https://localhost:3306/zabbix/api_jsonrpc.php",
Url: "https://localhost:3306/zabbix/api_jsonrpc.php", JSONData: []byte(`{ "addThresholds": true, "disableReadOnlyUsersAck": true }`),
JsonData: `{ "addThresholds": true, "disableReadOnlyUsersAck": true }`, DecryptedSecureJSONData: map[string]string{
DecryptedSecureJsonData: map[string]string{
"username": "grafanaZabbixUser", "username": "grafanaZabbixUser",
"password": "$uper$ecr3t!!!", "password": "$uper$ecr3t!!!",
}, },