This repository was archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnagios_test.go
171 lines (153 loc) · 4.58 KB
/
nagios_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
package main
import (
// "strconv"
"fmt"
"testing"
"os"
"time"
"bytes"
proto "goprotobuf.googlecode.com/hg/proto"
)
var hostname = "test-host"
var servicename = "test-service"
var status = CheckStatus(CheckStatus_OK)
const co = "OK:\tcount to potato | potato=1\n hoooraaaay\n | potatoe=2"
const conp = "OK:\tcount to potato\n hoorraaaay\n "
var servicecheck = CheckResult{
Hostname: proto.String(hostname),
ServiceName: proto.String(servicename),
Status: &status,
CheckOutput: proto.String(co),
StartTimestamp: proto.Int64(time.Nanoseconds() - 11111390),
EndTimestamp: proto.Int64(time.Nanoseconds()),
}
var hostcheck = CheckResult{
Hostname: proto.String(hostname),
Status: &status,
CheckOutput: proto.String(co),
StartTimestamp: proto.Int64(time.Nanoseconds() - 11111390),
EndTimestamp: proto.Int64(time.Nanoseconds()),
}
type PerfElementCheck struct{ in, out string }
type PerfDataCheck struct{ in, outstr, outpd string }
var perfdatacheck = CheckResult{
Hostname: proto.String(hostname),
ServiceName: proto.String(servicename),
Status: &status,
CheckOutput: proto.String(conp),
StartTimestamp: proto.Int64(time.Nanoseconds() - 11111390),
EndTimestamp: proto.Int64(time.Nanoseconds()),
Perfdata: []*PerfData{
&PerfData{Name: proto.String("potato"), Value: proto.Float32(1)},
&PerfData{Name: proto.String("potatoe"), Value: proto.Float32(2)},
},
}
var pdtests = [...]PerfDataCheck{
{"OK: matched | time=2sec", "OK: matched ", "[name:\"time\" value:2 units:\"sec\" ]"},
{"OK:\tcount to potato | potato=1\n hoooraaaay\n | potatoe=2", "OK:\tcount to potato \n hoooraaaay\n ", "[name:\"potato\" value:1 name:\"potatoe\" value:2 ]"},
{"OK:\tcount to potato | potato=1\n hoooraaaay\n | potatoe=2%", "OK:\tcount to potato \n hoooraaaay\n ", "[name:\"potato\" value:1 name:\"potatoe\" value:2 units:\"%\" ]"},
}
var petests = [...]PerfElementCheck{
{"data=1elem;5;10;1;4", "name:\"data\" value:1 units:\"elem\" warning:5 critical:10 minimum:1 maximum:4 "},
{"bananas=3", "name:\"bananas\" value:3 "},
}
func TestParsePerfDataElement(t *testing.T) {
for i, v := range petests {
pd, err := parsePerfDataElement(v.in)
if err != nil {
t.Errorf("test %d: parsePerfDataElement returned error: %v", i, err)
}
out := proto.CompactTextString(pd)
if v.out != out {
t.Errorf("test %d: mismatch:\noutput: %v\nexpected: %v", i, out, v.out)
}
}
}
func TestParsePerfData(t *testing.T) {
for i, v := range pdtests {
outstr, outpd, err := ParseRawPluginOutput(v.in)
if err != nil {
t.Errorf("test %d: can't parse plugin output: %v", i, err)
}
if outstr != v.outstr {
t.Errorf("test %d: checkoutput doesn't match:\noutput: %#v\n --- \nexpected: %#v", i, outstr, v.outstr)
}
t.Log("outpd:", outpd)
outpdstr := fmt.Sprint(outpd)
if v.outpd != outpdstr {
t.Errorf("test %d: perfdata doesn't match:\noutput: %#v\n --- \nexpected: %#v", i, outpdstr, v.outpd)
t.Logf("%#v\n", outpdstr)
}
}
}
func TestGetIntFromEnum(t *testing.T) {
blarg := NewCheckStatus(CheckStatus_WARNING)
t.Logf("%#v\n", blarg)
t.Logf("%#v\n", *blarg)
t.Logf("%s\n", *blarg)
}
func TestStringMap(t *testing.T) {
/* fmt.Printf("check = %#v\n", servicecheck)*/
}
func TestStringEscaping(t *testing.T) {
buf := new(bytes.Buffer)
co := "OK:\tcount to potato | potato=1\n hoooraaaay\n | potatoe=2"
escapedStringFormatter(buf, "", co)
for i := 0; i < buf.Len(); i++ {
c, err := buf.ReadByte()
if err != nil && err == os.EOF {
break
}
switch c {
case '\n', '\t':
t.Error("failed escaping")
}
}
}
func TestCreateSpoolFile(t *testing.T) {
f, err := spoolFile(".", "test")
if err != nil {
t.Error("spoolFile:", err)
}
t.Log("wrote check to", f.Name())
fi, err := f.Stat()
if err != nil {
t.Error("spoolFile: couldn't stat file:", err)
}
_ = fi
err = os.Remove(f.Name())
if err != nil {
err := os.Remove(f.Name())
if err != nil {
t.Log("couldn't remove", f.Name(), ":", err)
}
}
}
func TestWriteServiceCheckFile(t *testing.T) {
_test_writeCheckFile(t, &servicecheck)
}
func TestWriteHostFile(t *testing.T) {
_test_writeCheckFile(t, &hostcheck)
}
func _test_writeCheckFile(t *testing.T, check *CheckResult) {
fn, err := WriteCheck(check, ".")
t.Log("wrote check to", fn)
if err != nil {
t.Error("WriteCheck:", err)
}
for _, v := range [...]string{fn, fn + ".ok"} {
err := os.Remove(v)
if err != nil {
err := os.Remove(v)
if err != nil {
t.Log("couldn't remove", v, ":", err)
}
}
}
return
}
func BenchmarkStringEscaping(b *testing.B) {
for i := 0; i < b.N; i++ {
fmt.Printf("hello world\n")
}
}