Blame


1 191337d0 2022-01-18 o package icinga_test
2 191337d0 2022-01-18 o
3 191337d0 2022-01-18 o import (
4 191337d0 2022-01-18 o "crypto/tls"
5 191337d0 2022-01-18 o "errors"
6 191337d0 2022-01-18 o "math/rand"
7 191337d0 2022-01-18 o "net/http"
8 191337d0 2022-01-18 o "reflect"
9 191337d0 2022-01-18 o "sort"
10 191337d0 2022-01-18 o "testing"
11 191337d0 2022-01-18 o
12 191337d0 2022-01-18 o "olowe.co/icinga"
13 191337d0 2022-01-18 o )
14 191337d0 2022-01-18 o
15 191337d0 2022-01-18 o func randomHostname() string {
16 191337d0 2022-01-18 o var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
17 191337d0 2022-01-18 o b := make([]rune, 8)
18 191337d0 2022-01-18 o for i := range b {
19 191337d0 2022-01-18 o b[i] = letters[rand.Intn(len(letters))]
20 191337d0 2022-01-18 o }
21 191337d0 2022-01-18 o return string(b) + ".example.org"
22 191337d0 2022-01-18 o }
23 191337d0 2022-01-18 o
24 7cb145ba 2022-01-18 o func createTestHosts(c *icinga.Client) ([]icinga.Host, error) {
25 7cb145ba 2022-01-18 o hostgroup := icinga.HostGroup{Name: "test", DisplayName: "Test Group"}
26 7cb145ba 2022-01-18 o if err := c.CreateHostGroup(hostgroup); err != nil && !errors.Is(err, icinga.ErrExist) {
27 7cb145ba 2022-01-18 o return nil, err
28 7cb145ba 2022-01-18 o }
29 7cb145ba 2022-01-18 o
30 7cb145ba 2022-01-18 o var hosts []icinga.Host
31 7cb145ba 2022-01-18 o for i := 0; i < 5; i++ {
32 7cb145ba 2022-01-18 o h := icinga.Host{
33 7cb145ba 2022-01-18 o Name: randomHostname(),
34 7cb145ba 2022-01-18 o CheckCommand: "random",
35 7cb145ba 2022-01-18 o Groups: []string{hostgroup.Name},
36 7cb145ba 2022-01-18 o }
37 7cb145ba 2022-01-18 o hosts = append(hosts, h)
38 7cb145ba 2022-01-18 o if err := c.CreateHost(h); err != nil && !errors.Is(err, icinga.ErrExist) {
39 7cb145ba 2022-01-18 o return nil, err
40 7cb145ba 2022-01-18 o }
41 7cb145ba 2022-01-18 o }
42 7cb145ba 2022-01-18 o return hosts, nil
43 7cb145ba 2022-01-18 o }
44 7cb145ba 2022-01-18 o
45 191337d0 2022-01-18 o func newTestClient() (*icinga.Client, error) {
46 191337d0 2022-01-18 o tp := http.DefaultTransport.(*http.Transport)
47 191337d0 2022-01-18 o tp.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
48 191337d0 2022-01-18 o c := http.DefaultClient
49 191337d0 2022-01-18 o c.Transport = tp
50 191337d0 2022-01-18 o return icinga.Dial("127.0.0.1:5665", "root", "icinga", c)
51 191337d0 2022-01-18 o }
52 191337d0 2022-01-18 o
53 191337d0 2022-01-18 o func compareStringSlice(a, b []string) bool {
54 191337d0 2022-01-18 o if len(a) != len(b) {
55 191337d0 2022-01-18 o return false
56 191337d0 2022-01-18 o }
57 191337d0 2022-01-18 o for i, v := range a {
58 191337d0 2022-01-18 o if v != b[i] {
59 191337d0 2022-01-18 o return false
60 191337d0 2022-01-18 o }
61 191337d0 2022-01-18 o }
62 191337d0 2022-01-18 o return true
63 191337d0 2022-01-18 o }
64 191337d0 2022-01-18 o
65 191337d0 2022-01-18 o func TestFilter(t *testing.T) {
66 191337d0 2022-01-18 o client, err := newTestClient()
67 191337d0 2022-01-18 o if err != nil {
68 191337d0 2022-01-18 o t.Skipf("no local test icinga? got: %v", err)
69 191337d0 2022-01-18 o }
70 191337d0 2022-01-18 o
71 191337d0 2022-01-18 o var want, got []string
72 7cb145ba 2022-01-18 o hosts, err := createTestHosts(client)
73 7cb145ba 2022-01-18 o if err != nil {
74 7cb145ba 2022-01-18 o t.Fatal(err)
75 7cb145ba 2022-01-18 o }
76 7cb145ba 2022-01-18 o for _, h := range hosts {
77 191337d0 2022-01-18 o want = append(want, h.Name)
78 191337d0 2022-01-18 o }
79 191337d0 2022-01-18 o defer func() {
80 7cb145ba 2022-01-18 o for _, h := range hosts {
81 7cb145ba 2022-01-18 o if err := client.DeleteHost(h.Name, false); err != nil {
82 191337d0 2022-01-18 o t.Log(err)
83 191337d0 2022-01-18 o }
84 191337d0 2022-01-18 o }
85 191337d0 2022-01-18 o }()
86 7cb145ba 2022-01-18 o hosts, err = client.Hosts("match(\"*example.org\", host.name)")
87 191337d0 2022-01-18 o if err != nil {
88 191337d0 2022-01-18 o t.Fatal(err)
89 191337d0 2022-01-18 o }
90 191337d0 2022-01-18 o for _, h := range hosts {
91 191337d0 2022-01-18 o got = append(got, h.Name)
92 191337d0 2022-01-18 o }
93 191337d0 2022-01-18 o sort.Strings(want)
94 191337d0 2022-01-18 o sort.Strings(got)
95 191337d0 2022-01-18 o if !compareStringSlice(want, got) {
96 191337d0 2022-01-18 o t.Fail()
97 191337d0 2022-01-18 o }
98 191337d0 2022-01-18 o t.Logf("want %+v got %+v", want, got)
99 191337d0 2022-01-18 o }
100 191337d0 2022-01-18 o
101 191337d0 2022-01-18 o func TestUserRoundTrip(t *testing.T) {
102 191337d0 2022-01-18 o client, err := newTestClient()
103 191337d0 2022-01-18 o if err != nil {
104 191337d0 2022-01-18 o t.Skipf("no local test icinga? got: %v", err)
105 191337d0 2022-01-18 o }
106 191337d0 2022-01-18 o want := icinga.User{Name: "olly", Email: "olly@example.com", Groups: []string{}}
107 191337d0 2022-01-18 o if err := client.CreateUser(want); err != nil && !errors.Is(err, icinga.ErrExist) {
108 191337d0 2022-01-18 o t.Fatal(err)
109 191337d0 2022-01-18 o }
110 191337d0 2022-01-18 o defer func() {
111 02a1a100 2022-01-18 o if err := client.DeleteUser(want.Name, false); err != nil {
112 191337d0 2022-01-18 o t.Error(err)
113 191337d0 2022-01-18 o }
114 191337d0 2022-01-18 o }()
115 191337d0 2022-01-18 o got, err := client.LookupUser(want.Name)
116 191337d0 2022-01-18 o if err != nil {
117 191337d0 2022-01-18 o t.Fatal(err)
118 191337d0 2022-01-18 o }
119 191337d0 2022-01-18 o if !reflect.DeepEqual(want, got) {
120 191337d0 2022-01-18 o t.Errorf("want %+v, got %+v", want, got)
121 191337d0 2022-01-18 o }
122 191337d0 2022-01-18 o }
123 191337d0 2022-01-18 o
124 191337d0 2022-01-18 o func TestChecker(t *testing.T) {
125 191337d0 2022-01-18 o client, err := newTestClient()
126 191337d0 2022-01-18 o if err != nil {
127 191337d0 2022-01-18 o t.Skipf("no local test icinga? got: %v", err)
128 191337d0 2022-01-18 o }
129 191337d0 2022-01-18 o
130 191337d0 2022-01-18 o s := icinga.Service{Name: "9p.io!http"}
131 191337d0 2022-01-18 o if err := s.Check(client); err != nil {
132 191337d0 2022-01-18 o t.Fatal(err)
133 191337d0 2022-01-18 o }
134 191337d0 2022-01-18 o s, err = client.LookupService("9p.io!http")
135 191337d0 2022-01-18 o if err != nil {
136 191337d0 2022-01-18 o t.Fatal(err)
137 191337d0 2022-01-18 o }
138 191337d0 2022-01-18 o t.Logf("%+v\n", s)
139 191337d0 2022-01-18 o }
140 02a1a100 2022-01-18 o
141 7cb145ba 2022-01-18 o func TestCheckHostGroup(t *testing.T) {
142 7cb145ba 2022-01-18 o client, err := newTestClient()
143 7cb145ba 2022-01-18 o if err != nil {
144 7cb145ba 2022-01-18 o t.Skipf("no local test icinga? got: %v", err)
145 7cb145ba 2022-01-18 o }
146 7cb145ba 2022-01-18 o hosts, err := createTestHosts(client)
147 7cb145ba 2022-01-18 o if err != nil {
148 7cb145ba 2022-01-18 o t.Fatal(err)
149 7cb145ba 2022-01-18 o }
150 7cb145ba 2022-01-18 o defer func() {
151 7cb145ba 2022-01-18 o for _, h := range hosts {
152 7cb145ba 2022-01-18 o if err := client.DeleteHost(h.Name, true); err != nil {
153 7cb145ba 2022-01-18 o t.Error(err)
154 7cb145ba 2022-01-18 o }
155 7cb145ba 2022-01-18 o }
156 7cb145ba 2022-01-18 o }()
157 7cb145ba 2022-01-18 o hostgroup, err := client.LookupHostGroup("test")
158 7cb145ba 2022-01-18 o if err != nil {
159 7cb145ba 2022-01-18 o t.Fatal(err)
160 7cb145ba 2022-01-18 o }
161 7cb145ba 2022-01-18 o if err := hostgroup.Check(client); err != nil {
162 7cb145ba 2022-01-18 o t.Fatal(err)
163 7cb145ba 2022-01-18 o }
164 7cb145ba 2022-01-18 o }
165 7cb145ba 2022-01-18 o
166 02a1a100 2022-01-18 o func TestCreateService(t *testing.T) {
167 02a1a100 2022-01-18 o client, err := newTestClient()
168 02a1a100 2022-01-18 o if err != nil {
169 02a1a100 2022-01-18 o t.Skipf("no local test icinga? got: %v", err)
170 02a1a100 2022-01-18 o }
171 02a1a100 2022-01-18 o
172 02a1a100 2022-01-18 o h := icinga.Host{
173 02a1a100 2022-01-18 o Name: "example.com",
174 02a1a100 2022-01-18 o Address: "example.com",
175 02a1a100 2022-01-18 o CheckCommand: "dummy",
176 02a1a100 2022-01-18 o DisplayName: "RFC 2606 example host",
177 02a1a100 2022-01-18 o }
178 02a1a100 2022-01-18 o if err := client.CreateHost(h); err != nil {
179 02a1a100 2022-01-18 o t.Error(err)
180 02a1a100 2022-01-18 o }
181 02a1a100 2022-01-18 o defer client.DeleteHost(h.Name, true)
182 02a1a100 2022-01-18 o s := icinga.Service{
183 02a1a100 2022-01-18 o Name: h.Name + "!http",
184 02a1a100 2022-01-18 o CheckCommand: "http",
185 02a1a100 2022-01-18 o DisplayName: "RFC 2606 example website",
186 02a1a100 2022-01-18 o }
187 02a1a100 2022-01-18 o if err := client.CreateService(s); err != nil {
188 02a1a100 2022-01-18 o t.Error(err)
189 02a1a100 2022-01-18 o }
190 02a1a100 2022-01-18 o }