-
Notifications
You must be signed in to change notification settings - Fork 29
/
maintenances_test.go
173 lines (146 loc) · 3.79 KB
/
maintenances_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
const (
// https://www.zabbix.com/documentation/3.4/manual/api/reference/maintenance/get
maintenanceGet = `
{
"jsonrpc": "2.0",
"result": [
{
"maintenanceid": "3",
"name": "Sunday maintenance",
"maintenance_type": "0",
"description": "",
"active_since": "1358844540",
"active_till": "1390466940",
"groups": [
{
"groupid": "4",
"name": "Zabbix servers",
"internal": "0"
}
],
"timeperiods": [
{
"timeperiodid": "4",
"timeperiod_type": "3",
"every": "1",
"month": "0",
"dayofweek": "1",
"day": "0",
"start_time": "64800",
"period": "3600",
"start_date": "2147483647"
}
]
}
],
"id": 1
}`
// https://www.zabbix.com/documentation/3.4/manual/api/reference/maintenance/delete
maintenancesRemove = `
{
"jsonrpc": "2.0",
"result": {
"maintenanceids": [
"3",
"1"
]
},
"id": 1
}
`
// https://www.zabbix.com/documentation/3.4/manual/api/reference/maintenance/create
maintenanceCreate = `
{
"jsonrpc": "2.0",
"result": {
"maintenanceids": [
"3"
]
},
"id": 1
}
`
)
func TestMaintenanceGet(t *testing.T) {
test := assert.New(t)
testserver := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, maintenanceGet)
},
))
defer testserver.Close()
zabbix := &Zabbix{}
zabbix.client = testserver.Client()
zabbix.apiURL = testserver.URL
maintenances, err := zabbix.GetMaintenances(Params{
"search": Params{
"name": "Sunday maintenance",
},
})
test.NoError(err)
test.Len(maintenances, 1)
test.Equal("3", maintenances[0].ID)
test.Equal("Sunday maintenance", maintenances[0].Name)
test.Equal("Zabbix servers", maintenances[0].Groups[0].Name)
}
func TestMaintenanceRemove(t *testing.T) {
test := assert.New(t)
testserver := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, maintenancesRemove)
},
))
defer testserver.Close()
zabbix := &Zabbix{}
zabbix.client = testserver.Client()
zabbix.apiURL = testserver.URL
payload := []string{"3", "1"}
var maintenances Maintenances
maintenances, err := zabbix.RemoveMaintenance(payload)
test.NoError(err)
test.Len(maintenances.ID, 2)
test.Equal("3", maintenances.ID[0])
test.Equal("1", maintenances.ID[1])
}
func TestMaintenanceCreate(t *testing.T) {
test := assert.New(t)
testserver := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, maintenanceCreate)
},
))
defer testserver.Close()
zabbix := &Zabbix{}
zabbix.client = testserver.Client()
zabbix.apiURL = testserver.URL
var timeperiod Timeperiod
timeperiod.TypeID = "0"
timeperiod.Every = "1"
timeperiod.Month = "0"
timeperiod.DayOfWeek = "0"
timeperiod.Day = "1"
timeperiod.StartTime = "0"
timeperiod.StartDate = strconv.FormatInt(int64(1551092132), 10)
timeperiod.Period = strconv.FormatInt(int64(3600), 10)
params := Params{
"name": "test maintenance",
"active_since": "1551092132",
"active_till": "1551178532",
"hostids": []string{"2"},
"timeperiods": []Timeperiod{timeperiod},
}
var maintenances Maintenances
maintenances, err := zabbix.RemoveMaintenance(params)
test.NoError(err)
test.Len(maintenances.ID, 1)
test.Equal("3", maintenances.ID[0])
}