-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost_test.go
121 lines (89 loc) · 2.72 KB
/
host_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
package gonagios
import (
"encoding/json"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func createHostObject() *Host {
hostName := "host1"
hostAlias := "host1"
hostAddress := "127.0.0.2"
hostMaxCheckAttempts := "5"
hostCheckPeriod := "24x7"
hostNotificationInterval := "10"
hostNotificationPeriod := "24x7"
contact := "nagiosadmin"
hostContacts := make([]interface{}, 1)
hostContacts[0] = contact
template := "generic-host"
hostTemplates := make([]interface{}, 1)
hostTemplates[0] = template
host := &Host{
HostName: hostName,
Alias: hostAlias,
Address: hostAddress,
MaxCheckAttempts: hostMaxCheckAttempts,
CheckPeriod: hostCheckPeriod,
NotificationInterval: hostNotificationInterval,
NotificationPeriod: hostNotificationPeriod,
Contacts: hostContacts,
Templates: hostTemplates,
}
return host
}
func TestHost_newHost(t *testing.T) {
if errList := envVarCheck(); errList != nil {
t.Fatal(errList)
}
client := NewClient(os.Getenv("NAGIOS_URL"), os.Getenv("API_TOKEN"))
host := createHostObject()
body, err := client.NewHost(host)
assert.NoError(t, err)
responseCode := &ResponseCode{}
err = json.Unmarshal(body, &responseCode)
assert.NoError(t, err)
assert.NotEmpty(t, responseCode.ResponseSuccess)
}
func TestHost_getHost(t *testing.T) {
if errList := envVarCheck(); errList != nil {
t.Fatal(errList)
}
hostName := "host1"
client := NewClient(os.Getenv("NAGIOS_URL"), os.Getenv("API_TOKEN"))
host, err := client.GetHost(hostName)
assert.NoError(t, err)
assert.NotEmpty(t, host)
assert.Equal(t, "host1", host.HostName)
}
func TestHost_updateHost(t *testing.T) {
if errList := envVarCheck(); errList != nil {
t.Fatal(errList)
}
client := NewClient(os.Getenv("NAGIOS_URL"), os.Getenv("API_TOKEN"))
// time.Sleep(10 * time.Second)
host := createHostObject()
oldName := "host1"
host.HostName = "updatedHost1"
// Update the host in Nagios
err := client.UpdateHost(host, oldName)
assert.NoError(t, err)
// Get the new host from Nagios to ensure the update was successful
updatedHost, err := client.GetHost(host.HostName)
assert.Equal(t, "updatedHost1", updatedHost.HostName)
}
func TestHost_deleteHost(t *testing.T) {
if errList := envVarCheck(); errList != nil {
t.Fatal(errList)
}
hostName := "updatedHost1"
client := NewClient(os.Getenv("NAGIOS_URL"), os.Getenv("API_TOKEN"))
// Delete host from Nagios
body, err := client.DeleteHost(hostName)
assert.NoError(t, err)
// Lets make sure that the response code that is returned is 'success'
responseCode := &ResponseCode{}
err = json.Unmarshal(body, &responseCode)
assert.NoError(t, err)
assert.NotEmpty(t, responseCode.ResponseSuccess)
}