Files
grafana-zabbix/pkg/zabbix/zabbix_test.go
Jocelyn Collado-Kuri 89ae290942 Move health check to the backend (#2120)
This PR moves the health check to backend only leaving in the frontend
the functionality to test the dbconnector datasource.

Leaving the `dbconnector.testDataSource` should be fine since the
datasource types we allow for db connection with Zabbix already are
backend datasources, and so their health requests would go through the
backend.

Verified:
Clicking test and seeing a `health` request go out.

IMPORTANT: While testing this in the UI, I found a bug with the config
editor - whenever a change is made in the UI and tested, the changes
don't take effect (i.e. disabling trends, keeps `trends` set to `true`,
enabling db connection keep `dbConnectionEnabled` set to `false` and so
on.). Created a separate
[issue](https://github.com/orgs/grafana/projects/457/views/40?pane=issue&itemId=3627315751&issue=grafana%7Coss-big-tent-squad%7C132)
to fix this

Fixes https://github.com/grafana/oss-big-tent-squad/issues/124
Fixes https://github.com/grafana/grafana-zabbix/issues/2004
2025-11-25 14:54:18 -08:00

136 lines
3.6 KiB
Go

package zabbix
import (
"context"
"testing"
"github.com/alexanderzobnin/grafana-zabbix/pkg/settings"
"github.com/stretchr/testify/assert"
)
var emptyParams = map[string]interface{}{}
func TestLogin(t *testing.T) {
zabbixClient, _ := MockZabbixClient(BasicDatasourceInfo, `{"result":"secretauth"}`, 200)
err := zabbixClient.Authenticate(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.Authenticate(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)
}
func TestItemTagCache(t *testing.T) {
zabbixClient, _ := MockZabbixClient(
BasicDatasourceInfo,
`{"result":[{"itemid":"1","name":"test1"}]}`,
200,
)
// tag filtering is on >= 54 version
zabbixClient.version = 64
zabbixClient.settings.AuthType = settings.AuthTypeToken
zabbixClient.api.SetAuth("test")
items, err := zabbixClient.GetAllItems(
context.Background(),
nil,
nil,
"num",
false,
"Application: test, interface: test",
)
assert.NoError(t, err)
if assert.Len(t, items, 1) {
item := items[0]
assert.Equal(t, "1", item.ID)
assert.Equal(t, "test1", item.Name)
}
zabbixClient, _ = MockZabbixClientResponse(
zabbixClient,
// intentionally different response to test if the cache hits
`{"result":[{"itemid":"2","name":"test2"}]}`,
200,
)
zabbixClient.api.SetAuth("test")
items, err = zabbixClient.GetAllItems(
context.Background(),
nil,
nil,
"num",
false,
// change tag order
"interface: test, Application: test",
)
assert.NoError(t, err)
if assert.Len(t, items, 1) {
item := items[0]
// test if it still uses cached response
assert.Equal(t, "1", item.ID)
assert.Equal(t, "test1", item.Name)
}
}