forked from claranet/go-zabbix-api
-
Notifications
You must be signed in to change notification settings - Fork 2
/
trigger_prototype_test.go
96 lines (81 loc) · 2.71 KB
/
trigger_prototype_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package zabbix_test
import (
"fmt"
"testing"
"github.com/claranet/go-zabbix-api"
dd "github.com/claranet/go-zabbix-api"
zapi "github.com/claranet/go-zabbix-api"
)
func testCreateTriggerPrototype(template *dd.Template, item *dd.ItemPrototype, t *testing.T) *dd.TriggerPrototype {
var expression string
if compGreaterThanOrEqual, _ := isVersionGreaterThanOrEqual(t, "5.4"); compGreaterThanOrEqual {
expression = fmt.Sprintf("last(/%s/%s)=0", template.Host, item.Key)
} else {
expression = fmt.Sprintf("{%s:%s.last()}=0", template.Host, item.Key)
}
triggers := dd.TriggerPrototypes{{
Description: "test trigger prototype",
Expression: expression,
Priority: zabbix.Warning,
}}
err := testGetAPI(t).TriggerPrototypesCreate(triggers)
if err != nil {
t.Fatal(err)
}
return &triggers[0]
}
func testDeleteTriggerPrototype(trigger *dd.TriggerPrototype, t *testing.T) {
err := testGetAPI(t).TriggerPrototypesDelete(dd.TriggerPrototypes{*trigger})
if err != nil {
t.Fatal(err)
}
}
func TestTriggerPrototype(t *testing.T) {
api := testGetAPI(t)
// Zabbix v6.2 introduced Template Groups and requires them for Templates
var groupIds zapi.HostGroupIDs
if compLessThan, _ := isVersionLessThan(t, "6.2"); compLessThan {
hostGroup := testCreateHostGroup(t)
defer testDeleteHostGroup(hostGroup, t)
groupIds = zapi.HostGroupIDs{
{
GroupID: hostGroup.GroupID,
},
}
} else {
templateGroup := testCreateTemplateGroup(t)
defer testDeleteTemplateGroup(templateGroup, t)
groupIds = zapi.HostGroupIDs{
{
GroupID: templateGroup.GroupID,
},
}
}
template := testCreateTemplate(&groupIds, t)
defer testDeleteTemplate(template, t)
lldRule := testCreateLLDRule(template, t)
defer testDeleteLLDRule(lldRule, t)
itemPrototype := testCreateItemPrototype(template, lldRule, t)
defer testDeleteItemPrototype(itemPrototype, t)
triggerPrototype := testCreateTriggerPrototype(template, itemPrototype, t)
getTriggerPrototype, err := api.TriggerPrototypesGet(dd.Params{"triggerids": triggerPrototype.TriggerID})
if err != nil {
t.Error(err)
}
if len(getTriggerPrototype) != 1 {
t.Errorf("Expecting one result and got : %d", len(getTriggerPrototype))
}
triggerPrototype.Description = "update_trigger_prototype_name"
err = api.TriggerPrototypesUpdate(dd.TriggerPrototypes{*triggerPrototype})
if err != nil {
t.Error(err)
}
getByIdTriggerPrototype, err := api.TriggerPrototypeGetByID(triggerPrototype.TriggerID)
if err != nil {
t.Error(err)
}
if getByIdTriggerPrototype.Description != triggerPrototype.Description {
t.Errorf("Trigger prototype description is %q and should be %q", getByIdTriggerPrototype.Description, triggerPrototype.Description)
}
testDeleteTriggerPrototype(triggerPrototype, t)
}