reorganize packages and add tests for zabbix datasource instance methods
This commit is contained in:
44
pkg/zabbixapi/testing.go
Normal file
44
pkg/zabbixapi/testing.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package zabbixapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
|
||||
)
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
func MockZabbixAPI(body string, statusCode int) (*ZabbixAPI, error) {
|
||||
apiLogger := log.New()
|
||||
zabbixURL, err := url.Parse("http://zabbix.org/zabbix")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ZabbixAPI{
|
||||
url: zabbixURL,
|
||||
logger: apiLogger,
|
||||
|
||||
httpClient: NewTestClient(func(req *http.Request) *http.Response {
|
||||
return &http.Response{
|
||||
StatusCode: statusCode,
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
|
||||
Header: make(http.Header),
|
||||
}
|
||||
}),
|
||||
}, nil
|
||||
}
|
||||
@@ -81,6 +81,11 @@ func (api *ZabbixAPI) SetUrl(api_url string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAuth returns API authentication token
|
||||
func (api *ZabbixAPI) GetAuth() string {
|
||||
return api.auth
|
||||
}
|
||||
|
||||
// SetAuth sets API authentication token
|
||||
func (api *ZabbixAPI) SetAuth(auth string) {
|
||||
api.auth = auth
|
||||
|
||||
@@ -1,53 +1,14 @@
|
||||
package zabbixapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
func mockZabbixAPI(body string, statusCode int) (*ZabbixAPI, error) {
|
||||
apiLogger := log.New()
|
||||
zabbixURL, err := url.Parse("http://zabbix.org/zabbix")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ZabbixAPI{
|
||||
url: zabbixURL,
|
||||
logger: apiLogger,
|
||||
|
||||
httpClient: NewTestClient(func(req *http.Request) *http.Response {
|
||||
return &http.Response{
|
||||
StatusCode: statusCode,
|
||||
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
|
||||
Header: make(http.Header),
|
||||
}
|
||||
}),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func TestZabbixAPIUnauthenticatedQuery(t *testing.T) {
|
||||
zabbixApi, _ := mockZabbixAPI(`{"result":"sampleResult"}`, 200)
|
||||
zabbixApi, _ := MockZabbixAPI(`{"result":"sampleResult"}`, 200)
|
||||
resp, err := zabbixApi.RequestUnauthenticated(context.Background(), "test.get", map[string]interface{}{})
|
||||
|
||||
assert.Equal(t, "sampleResult", resp.MustString())
|
||||
@@ -55,7 +16,7 @@ func TestZabbixAPIUnauthenticatedQuery(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLogin(t *testing.T) {
|
||||
zabbixApi, _ := mockZabbixAPI(`{"result":"secretauth"}`, 200)
|
||||
zabbixApi, _ := MockZabbixAPI(`{"result":"secretauth"}`, 200)
|
||||
err := zabbixApi.Authenticate(context.Background(), "user", "password")
|
||||
|
||||
assert.Nil(t, err)
|
||||
@@ -91,7 +52,7 @@ func TestZabbixAPI(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
zabbixApi, _ := mockZabbixAPI(tt.mockApiResponse, tt.mockApiResponseCode)
|
||||
zabbixApi, _ := MockZabbixAPI(tt.mockApiResponse, tt.mockApiResponseCode)
|
||||
zabbixApi.auth = tt.auth
|
||||
resp, err := zabbixApi.Request(context.Background(), "test.get", map[string]interface{}{})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user