forked from dainis/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 18
/
template_test.go
76 lines (65 loc) · 1.63 KB
/
template_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
package zabbix_test
import (
"testing"
zapi "github.com/claranet/go-zabbix-api"
)
func testCreateTemplate(hostGroups *zapi.HostGroupIDs, t *testing.T) *zapi.Template {
template := zapi.Templates{zapi.Template{
Host: "template name",
Groups: *hostGroups,
}}
err := testGetAPI(t).TemplatesCreate(template)
if err != nil {
t.Fatal(err)
}
return &template[0]
}
func testDeleteTemplate(template *zapi.Template, t *testing.T) {
err := testGetAPI(t).TemplatesDelete(zapi.Templates{*template})
if err != nil {
t.Fatal(err)
}
}
func TestTemplates(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)
if template.TemplateID == "" {
t.Errorf("Template id is empty %#v", template)
}
templates, err := api.TemplatesGet(zapi.Params{})
if err != nil {
t.Fatal(err)
}
if len(templates) == 0 {
t.Fatal("No templates were obtained")
}
_, err = api.TemplateGetByID(template.TemplateID)
if err != nil {
t.Error(err)
}
template.Name = "new template name"
err = api.TemplatesUpdate(zapi.Templates{*template})
if err != nil {
t.Error(err)
}
testDeleteTemplate(template, t)
}