diff --git a/application_test.go b/application_test.go index 69232fb..1228b8f 100644 --- a/application_test.go +++ b/application_test.go @@ -26,6 +26,8 @@ func testDeleteApplication(app *zapi.Application, t *testing.T) { } func TestApplications(t *testing.T) { + skipTestIfVersionGreaterThanOrEqual(t, "5.4", "dropped support for Application API") + api := testGetAPI(t) group := testCreateHostGroup(t) diff --git a/base_test.go b/base_test.go index a1bf46f..791d701 100644 --- a/base_test.go +++ b/base_test.go @@ -10,6 +10,7 @@ import ( "time" zapi "github.com/claranet/go-zabbix-api" + "github.com/hashicorp/go-version" ) var ( @@ -62,6 +63,19 @@ func testGetAPI(t *testing.T) *zapi.API { return _api } +func skipTestIfVersionGreaterThanOrEqual(t *testing.T, comparedVersion, msg string) { + api := testGetAPI(t) + serverVersion, err := api.Version() + if err != nil { + t.Fatal(err) + } + sVersion, _ := version.NewVersion(serverVersion) + cVersion, _ := version.NewVersion(comparedVersion) + if sVersion.GreaterThanOrEqual(cVersion) { + t.Skipf("Zabbix version %s is greater than or equal to %s which %s, skipping test.", serverVersion, comparedVersion, msg) + } +} + func TestBadCalls(t *testing.T) { api := testGetAPI(t) res, err := api.Call("", nil) diff --git a/go.mod b/go.mod index 0a0a8c1..ec726bf 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/claranet/go-zabbix-api go 1.18 + +require github.com/hashicorp/go-version v1.6.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..805e323 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek= +github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= diff --git a/item_test.go b/item_test.go index b62a41c..a6a2828 100644 --- a/item_test.go +++ b/item_test.go @@ -6,7 +6,22 @@ import ( zapi "github.com/claranet/go-zabbix-api" ) -func testCreateItem(app *zapi.Application, t *testing.T) *zapi.Item { +func testCreateItem(host *zapi.Host, t *testing.T) *zapi.Item { + items := zapi.Items{{ + HostID: host.HostID, + Key: "key.lala.laa", + Name: "name for key", + Type: zapi.ZabbixTrapper, + Delay: "0", + }} + err := testGetAPI(t).ItemsCreate(items) + if err != nil { + t.Fatal(err) + } + return &items[0] +} + +func testCreateItemWithApplication(app *zapi.Application, t *testing.T) *zapi.Item { items := zapi.Items{{ HostID: app.HostID, Key: "key.lala.laa", @@ -38,6 +53,33 @@ func TestItems(t *testing.T) { host := testCreateHost(group, t) defer testDeleteHost(host, t) + item := testCreateItem(host, t) + + _, err := api.ItemGetByID(item.ItemID) + if err != nil { + t.Fatal(err) + } + + item.Name = "another name" + err = api.ItemsUpdate(zapi.Items{*item}) + if err != nil { + t.Error(err) + } + + testDeleteItem(item, t) +} + +func TestItemsWithApplication(t *testing.T) { + skipTestIfVersionGreaterThanOrEqual(t, "5.4", "dropped support for Application API") + + api := testGetAPI(t) + + group := testCreateHostGroup(t) + defer testDeleteHostGroup(group, t) + + host := testCreateHost(group, t) + defer testDeleteHost(host, t) + app := testCreateApplication(host, t) defer testDeleteApplication(app, t) @@ -49,7 +91,7 @@ func TestItems(t *testing.T) { t.Fatal("Found items") } - item := testCreateItem(app, t) + item := testCreateItemWithApplication(app, t) _, err = api.ItemGetByID(item.ItemID) if err != nil { diff --git a/trigger_test.go b/trigger_test.go index 27546a7..48cc873 100644 --- a/trigger_test.go +++ b/trigger_test.go @@ -36,10 +36,44 @@ func TestTrigger(t *testing.T) { host := testCreateHost(group, t) defer testDeleteHost(host, t) + item := testCreateItem(host, t) + defer testDeleteItem(item, t) + + triggerParam := zapi.Params{"hostids": host.HostID} + res, err := api.TriggersGet(triggerParam) + if err != nil { + t.Fatal(err) + } + if len(res) != 0 { + t.Fatal("Found items") + } + + trigger := testCreateTrigger(item, host, t) + + trigger.Description = "new trigger name" + err = api.TriggersUpdate(zapi.Triggers{*trigger}) + if err != nil { + t.Error(err) + } + + testDeleteTrigger(trigger, t) +} + +func TestTriggerWithApplication(t *testing.T) { + skipTestIfVersionGreaterThanOrEqual(t, "5.4", "dropped support for Application API") + + api := testGetAPI(t) + + group := testCreateHostGroup(t) + defer testDeleteHostGroup(group, t) + + host := testCreateHost(group, t) + defer testDeleteHost(host, t) + app := testCreateApplication(host, t) defer testDeleteApplication(app, t) - item := testCreateItem(app, t) + item := testCreateItemWithApplication(app, t) defer testDeleteItem(item, t) triggerParam := zapi.Params{"hostids": host.HostID}