Refactor tests
This commit is contained in:
@@ -1,33 +1,16 @@
|
||||
package datasource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/alexanderzobnin/grafana-zabbix/pkg/cache"
|
||||
"github.com/alexanderzobnin/grafana-zabbix/pkg/zabbix"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var emptyParams = map[string]interface{}{}
|
||||
|
||||
type RoundTripFunc func(req *http.Request) *http.Response
|
||||
|
||||
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return f(req), nil
|
||||
}
|
||||
|
||||
//NewTestClient returns *http.Client with Transport replaced to avoid making real calls
|
||||
func NewTestClient(fn RoundTripFunc) *http.Client {
|
||||
return &http.Client{
|
||||
Transport: RoundTripFunc(fn),
|
||||
}
|
||||
}
|
||||
|
||||
var basicDatasourceInfo = &backend.DataSourceInstanceSettings{
|
||||
ID: 1,
|
||||
Name: "TestDatasource",
|
||||
@@ -56,79 +39,8 @@ func MockZabbixDataSource(body string, statusCode int) *ZabbixDatasourceInstance
|
||||
}
|
||||
|
||||
func MockZabbixDataSourceResponse(dsInstance *ZabbixDatasourceInstance, body string, statusCode int) *ZabbixDatasourceInstance {
|
||||
zabbixClient, _ := zabbix.MockZabbixAPI(dsInstance.zabbix, body, statusCode)
|
||||
zabbixClient, _ := zabbix.MockZabbixClientResponse(dsInstance.zabbix, body, statusCode)
|
||||
dsInstance.zabbix = zabbixClient
|
||||
|
||||
return dsInstance
|
||||
}
|
||||
|
||||
func TestLogin(t *testing.T) {
|
||||
dsInstance := MockZabbixDataSource(`{"result":"secretauth"}`, 200)
|
||||
err := dsInstance.zabbix.Login(context.Background())
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "secretauth", dsInstance.zabbix.GetAPI().GetAuth())
|
||||
}
|
||||
|
||||
func TestLoginError(t *testing.T) {
|
||||
dsInstance := MockZabbixDataSource(`{"result":""}`, 500)
|
||||
err := dsInstance.zabbix.Login(context.Background())
|
||||
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, "", dsInstance.zabbix.GetAPI().GetAuth())
|
||||
}
|
||||
|
||||
func TestZabbixAPIQuery(t *testing.T) {
|
||||
dsInstance := MockZabbixDataSource(`{"result":"test"}`, 200)
|
||||
resp, err := dsInstance.ZabbixAPIQuery(context.Background(), mockZabbixQuery("test.get", emptyParams))
|
||||
|
||||
assert.Nil(t, err)
|
||||
|
||||
result, ok := resp.Result.(string)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, "test", result)
|
||||
}
|
||||
|
||||
func TestCachedQuery(t *testing.T) {
|
||||
// Using methods with caching enabled
|
||||
query := mockZabbixQuery("host.get", emptyParams)
|
||||
dsInstance := MockZabbixDataSource(`{"result":"testOld"}`, 200)
|
||||
|
||||
// Run query first time
|
||||
resp, err := dsInstance.ZabbixAPIQuery(context.Background(), query)
|
||||
|
||||
assert.Nil(t, err)
|
||||
result, _ := resp.Result.(string)
|
||||
assert.Equal(t, "testOld", result)
|
||||
|
||||
// Mock request with new value
|
||||
dsInstance = MockZabbixDataSourceResponse(dsInstance, `{"result":"testNew"}`, 200)
|
||||
// Should not run actual API query and return first result
|
||||
resp, err = dsInstance.ZabbixAPIQuery(context.Background(), query)
|
||||
|
||||
assert.Nil(t, err)
|
||||
result, _ = resp.Result.(string)
|
||||
assert.Equal(t, "testOld", result)
|
||||
}
|
||||
|
||||
func TestNonCachedQuery(t *testing.T) {
|
||||
// Using methods with caching disabled
|
||||
query := mockZabbixQuery("history.get", emptyParams)
|
||||
dsInstance := MockZabbixDataSource(`{"result":"testOld"}`, 200)
|
||||
|
||||
// Run query first time
|
||||
resp, err := dsInstance.ZabbixAPIQuery(context.Background(), query)
|
||||
|
||||
assert.Nil(t, err)
|
||||
result, _ := resp.Result.(string)
|
||||
assert.Equal(t, "testOld", result)
|
||||
|
||||
// Mock request with new value
|
||||
dsInstance = MockZabbixDataSourceResponse(dsInstance, `{"result":"testNew"}`, 200)
|
||||
// Should not run actual API query and return first result
|
||||
resp, err = dsInstance.ZabbixAPIQuery(context.Background(), query)
|
||||
|
||||
assert.Nil(t, err)
|
||||
result, _ = resp.Result.(string)
|
||||
assert.Equal(t, "testNew", result)
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ func MockZabbixClient(dsInfo *backend.DataSourceInstanceSettings, body string, s
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func MockZabbixAPI(client *Zabbix, body string, statusCode int) (*Zabbix, error) {
|
||||
func MockZabbixClientResponse(client *Zabbix, body string, statusCode int) (*Zabbix, error) {
|
||||
zabbixAPI, err := zabbixapi.MockZabbixAPI(body, statusCode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
89
pkg/zabbix/zabbix_test.go
Normal file
89
pkg/zabbix/zabbix_test.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package zabbix
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var basicDatasourceInfo = &backend.DataSourceInstanceSettings{
|
||||
ID: 1,
|
||||
Name: "TestDatasource",
|
||||
URL: "http://zabbix.org/zabbix",
|
||||
JSONData: []byte(`{"username":"username", "password":"password", "cacheTTL":"10m"}`),
|
||||
}
|
||||
|
||||
var emptyParams = map[string]interface{}{}
|
||||
|
||||
func TestLogin(t *testing.T) {
|
||||
zabbixClient, _ := MockZabbixClient(basicDatasourceInfo, `{"result":"secretauth"}`, 200)
|
||||
err := zabbixClient.Login(context.Background())
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "secretauth", zabbixClient.api.GetAuth())
|
||||
}
|
||||
|
||||
func TestLoginError(t *testing.T) {
|
||||
zabbixClient, _ := MockZabbixClient(basicDatasourceInfo, `{"result":""}`, 500)
|
||||
err := zabbixClient.Login(context.Background())
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "", zabbixClient.api.GetAuth())
|
||||
}
|
||||
|
||||
func TestZabbixAPIQuery(t *testing.T) {
|
||||
zabbixClient, _ := MockZabbixClient(basicDatasourceInfo, `{"result":"test"}`, 200)
|
||||
resp, err := zabbixClient.Request(context.Background(), &ZabbixAPIRequest{Method: "test.get", Params: emptyParams})
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
||||
result, err := resp.String()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "test", result)
|
||||
}
|
||||
|
||||
func TestCachedQuery(t *testing.T) {
|
||||
// Using methods with caching enabled
|
||||
query := &ZabbixAPIRequest{Method: "host.get", Params: emptyParams}
|
||||
zabbixClient, _ := MockZabbixClient(basicDatasourceInfo, `{"result":"testOld"}`, 200)
|
||||
|
||||
// Run query first time
|
||||
resp, err := zabbixClient.Request(context.Background(), query)
|
||||
|
||||
assert.NoError(t, err)
|
||||
result, _ := resp.String()
|
||||
assert.Equal(t, "testOld", result)
|
||||
|
||||
// Mock request with new value
|
||||
zabbixClient, _ = MockZabbixClientResponse(zabbixClient, `{"result":"testNew"}`, 200)
|
||||
// Should not run actual API query and return first result
|
||||
resp, err = zabbixClient.Request(context.Background(), query)
|
||||
|
||||
assert.NoError(t, err)
|
||||
result, _ = resp.String()
|
||||
assert.Equal(t, "testOld", result)
|
||||
}
|
||||
|
||||
func TestNonCachedQuery(t *testing.T) {
|
||||
// Using methods with caching disabled
|
||||
query := &ZabbixAPIRequest{Method: "history.get", Params: emptyParams}
|
||||
zabbixClient, _ := MockZabbixClient(basicDatasourceInfo, `{"result":"testOld"}`, 200)
|
||||
|
||||
// Run query first time
|
||||
resp, err := zabbixClient.Request(context.Background(), query)
|
||||
|
||||
assert.NoError(t, err)
|
||||
result, _ := resp.String()
|
||||
assert.Equal(t, "testOld", result)
|
||||
|
||||
// Mock request with new value
|
||||
zabbixClient, _ = MockZabbixClientResponse(zabbixClient, `{"result":"testNew"}`, 200)
|
||||
// Should not run actual API query and return first result
|
||||
resp, err = zabbixClient.Request(context.Background(), query)
|
||||
|
||||
assert.NoError(t, err)
|
||||
result, _ = resp.String()
|
||||
assert.Equal(t, "testNew", result)
|
||||
}
|
||||
Reference in New Issue
Block a user