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 191337d0 2022-01-18 o func newTestClient() (*icinga.Client, error) {
25 191337d0 2022-01-18 o tp := http.DefaultTransport.(*http.Transport)
26 191337d0 2022-01-18 o tp.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
27 191337d0 2022-01-18 o c := http.DefaultClient
28 191337d0 2022-01-18 o c.Transport = tp
29 191337d0 2022-01-18 o return icinga.Dial("127.0.0.1:5665", "root", "icinga", c)
30 191337d0 2022-01-18 o }
31 191337d0 2022-01-18 o
32 191337d0 2022-01-18 o func compareStringSlice(a, b []string) bool {
33 191337d0 2022-01-18 o if len(a) != len(b) {
34 191337d0 2022-01-18 o return false
35 191337d0 2022-01-18 o }
36 191337d0 2022-01-18 o for i, v := range a {
37 191337d0 2022-01-18 o if v != b[i] {
38 191337d0 2022-01-18 o return false
39 191337d0 2022-01-18 o }
40 191337d0 2022-01-18 o }
41 191337d0 2022-01-18 o return true
42 191337d0 2022-01-18 o }
43 191337d0 2022-01-18 o
44 191337d0 2022-01-18 o func TestFilter(t *testing.T) {
45 191337d0 2022-01-18 o client, err := newTestClient()
46 191337d0 2022-01-18 o if err != nil {
47 191337d0 2022-01-18 o t.Skipf("no local test icinga? got: %v", err)
48 191337d0 2022-01-18 o }
49 191337d0 2022-01-18 o
50 191337d0 2022-01-18 o hostgroup := icinga.HostGroup{Name: "examples", DisplayName: "Test Group"}
51 191337d0 2022-01-18 o if err := client.CreateHostGroup(hostgroup); err != nil {
52 191337d0 2022-01-18 o t.Error(err)
53 191337d0 2022-01-18 o }
54 191337d0 2022-01-18 o hostgroup, err = client.LookupHostGroup(hostgroup.Name)
55 191337d0 2022-01-18 o if err != nil {
56 191337d0 2022-01-18 o t.Error(err)
57 191337d0 2022-01-18 o }
58 191337d0 2022-01-18 o defer client.DeleteHostGroup(hostgroup.Name)
59 191337d0 2022-01-18 o
60 191337d0 2022-01-18 o var want, got []string
61 191337d0 2022-01-18 o for i := 0; i < 5; i++ {
62 191337d0 2022-01-18 o h := icinga.Host{
63 191337d0 2022-01-18 o Name: randomHostname(),
64 191337d0 2022-01-18 o CheckCommand: "hostalive",
65 191337d0 2022-01-18 o Groups: []string{hostgroup.Name},
66 191337d0 2022-01-18 o }
67 191337d0 2022-01-18 o want = append(want, h.Name)
68 191337d0 2022-01-18 o if err := client.CreateHost(h); err != nil {
69 191337d0 2022-01-18 o if !errors.Is(err, icinga.ErrExist) {
70 191337d0 2022-01-18 o t.Error(err)
71 191337d0 2022-01-18 o }
72 191337d0 2022-01-18 o continue
73 191337d0 2022-01-18 o }
74 191337d0 2022-01-18 o t.Logf("created host %s", h.Name)
75 191337d0 2022-01-18 o }
76 191337d0 2022-01-18 o defer func() {
77 191337d0 2022-01-18 o for _, name := range want {
78 191337d0 2022-01-18 o if err := client.DeleteHost(name); err != nil {
79 191337d0 2022-01-18 o t.Log(err)
80 191337d0 2022-01-18 o }
81 191337d0 2022-01-18 o }
82 191337d0 2022-01-18 o }()
83 191337d0 2022-01-18 o hosts, err := client.Hosts("match(\"*example.org\", host.name)")
84 191337d0 2022-01-18 o if err != nil {
85 191337d0 2022-01-18 o t.Fatal(err)
86 191337d0 2022-01-18 o }
87 191337d0 2022-01-18 o for _, h := range hosts {
88 191337d0 2022-01-18 o got = append(got, h.Name)
89 191337d0 2022-01-18 o }
90 191337d0 2022-01-18 o sort.Strings(want)
91 191337d0 2022-01-18 o sort.Strings(got)
92 191337d0 2022-01-18 o if !compareStringSlice(want, got) {
93 191337d0 2022-01-18 o t.Fail()
94 191337d0 2022-01-18 o }
95 191337d0 2022-01-18 o t.Logf("want %+v got %+v", want, got)
96 191337d0 2022-01-18 o }
97 191337d0 2022-01-18 o
98 191337d0 2022-01-18 o func TestUserRoundTrip(t *testing.T) {
99 191337d0 2022-01-18 o client, err := newTestClient()
100 191337d0 2022-01-18 o if err != nil {
101 191337d0 2022-01-18 o t.Skipf("no local test icinga? got: %v", err)
102 191337d0 2022-01-18 o }
103 191337d0 2022-01-18 o want := icinga.User{Name: "olly", Email: "olly@example.com", Groups: []string{}}
104 191337d0 2022-01-18 o if err := client.CreateUser(want); err != nil && !errors.Is(err, icinga.ErrExist) {
105 191337d0 2022-01-18 o t.Fatal(err)
106 191337d0 2022-01-18 o }
107 191337d0 2022-01-18 o defer func() {
108 191337d0 2022-01-18 o if err := client.DeleteUser(want.Name); err != nil {
109 191337d0 2022-01-18 o t.Error(err)
110 191337d0 2022-01-18 o }
111 191337d0 2022-01-18 o }()
112 191337d0 2022-01-18 o got, err := client.LookupUser(want.Name)
113 191337d0 2022-01-18 o if err != nil {
114 191337d0 2022-01-18 o t.Fatal(err)
115 191337d0 2022-01-18 o }
116 191337d0 2022-01-18 o if !reflect.DeepEqual(want, got) {
117 191337d0 2022-01-18 o t.Errorf("want %+v, got %+v", want, got)
118 191337d0 2022-01-18 o }
119 191337d0 2022-01-18 o }
120 191337d0 2022-01-18 o
121 191337d0 2022-01-18 o func TestChecker(t *testing.T) {
122 191337d0 2022-01-18 o client, err := newTestClient()
123 191337d0 2022-01-18 o if err != nil {
124 191337d0 2022-01-18 o t.Skipf("no local test icinga? got: %v", err)
125 191337d0 2022-01-18 o }
126 191337d0 2022-01-18 o
127 191337d0 2022-01-18 o s := icinga.Service{Name: "9p.io!http"}
128 191337d0 2022-01-18 o if err := s.Check(client); err != nil {
129 191337d0 2022-01-18 o t.Fatal(err)
130 191337d0 2022-01-18 o }
131 191337d0 2022-01-18 o s, err = client.LookupService("9p.io!http")
132 191337d0 2022-01-18 o if err != nil {
133 191337d0 2022-01-18 o t.Fatal(err)
134 191337d0 2022-01-18 o }
135 191337d0 2022-01-18 o t.Logf("%+v\n", s)
136 191337d0 2022-01-18 o }