forked from canonical/nrpe_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nrpe_exporter_test.go
66 lines (55 loc) · 1.45 KB
/
nrpe_exporter_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
package main
import (
"net"
"testing"
"github.com/aperum/nrpe"
"github.com/go-kit/kit/log"
)
func TestClientServer(t *testing.T) {
sock := testCreateSocketPair(t)
defer sock.Close()
c := make(chan float64)
go func() {
err := nrpe.ServeOne(sock.server, func(command nrpe.Command) (*nrpe.CommandResult, error) {
return &nrpe.CommandResult{
StatusLine: (""),
StatusCode: nrpe.StatusOK,
}, nil
}, false, 0)
if err != nil {
t.Fatal(err)
}
c <- 1.0
}()
cr, err := collectCommandMetrics("check_load", sock.client, log.NewNopLogger())
if err != nil {
t.Fatal(err)
}
nrpeResult := nrpe.CommandResult{StatusLine: "", StatusCode: 0.0}
expectedResults := CommandResult{0.0, 1.0, &nrpeResult}
if cr.commandDuration < expectedResults.commandDuration {
t.Fatalf("Expected commandDuration greater than 0s, got: %v", cr.commandDuration)
}
if cr.statusOk != expectedResults.statusOk {
t.Fatalf("Expected %v, got: %v", expectedResults.statusOk, cr.statusOk)
}
if cr.result.StatusCode != expectedResults.result.StatusCode {
t.Fatalf("Expected %v, got: %v", expectedResults.result.StatusCode, cr.result.StatusCode)
}
}
type testSocketPair struct {
client net.Conn
server net.Conn
}
func (s *testSocketPair) Close() {
s.client.Close()
s.server.Close()
}
func testCreateSocketPair(t *testing.T) *testSocketPair {
serverConn, clientConn := net.Pipe()
s := &testSocketPair{
client: clientConn,
server: serverConn,
}
return s
}