Files
grafana-zabbix/pkg/zabbix/zabbix_test.go
Jocelyn Collado-Kuri e073382983 Fix always fetch Zabbix version before issuing new requests (#2133)
Previously we were only fetching the version when the version was `0`.
This generally worked, but posed some problems when customers were
updating their Zabbix version, specifically when upgrading from a
version < `7.2.x` to `7.2.x` or above.

Before `7.2.x`, an `auth` parameter was still supported when issuing a
zabbix request, this was deprecated in `6.4.x` and later removed in
`7.2.x`. When a user was on a version < `7.2.x` all the outgoing
requests would add this `auth` parameter. When upgrading to `7.2.x` this
was a problem, because the version was not `0`, hence, not requiring
getting the version again, but also because we were still building the
request considering an older version and adding the `auth` parameter,
when this was no longer supported.

This PR removes the check for `version == 0`, though this now means that
every request that goes out will check the version before hand, I think
this will give us a more accurate representation of the version that
needs to be used.

fixes
https://github.com/orgs/grafana/projects/457/views/40?pane=issue&itemId=3683181283&issue=grafana%7Coss-big-tent-squad%7C135
2025-12-05 17:34:20 -08:00

144 lines
3.9 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) {
callCount := 0
zabbixClient := NewZabbixClientWithHandler(t, func(payload ApiRequestPayload) string {
switch payload.Method {
case "apiinfo.version":
return `{"result":"6.4.0"}`
case "item.get":
callCount++
if callCount == 1 {
return `{"result":[{"itemid":"1","name":"test1"}]}`
}
return `{"result":[{"itemid":"2","name":"test2"}]}`
default:
return `{"result":null}`
}
})
// tag filtering is on >= 54 version
zabbixClient.settings.AuthType = settings.AuthTypeToken
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)
}
}