/
client_test.go
119 lines (108 loc) · 2.57 KB
/
client_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
package munin
import (
"bufio"
"io"
"os"
"strings"
"testing"
"time"
)
// Thanks rsc (Russ Cox, Google Inc.) for this nice fake code
type fakePipes struct {
io.ReadCloser
io.WriteCloser
}
func (p *fakePipes) Close() error {
p.ReadCloser.Close()
p.WriteCloser.Close()
return nil
}
func fakeConnect(t *testing.T, fakeReply fakeResponseMap) (io.ReadWriteCloser, error) {
r1, w1 := io.Pipe()
r2, w2 := io.Pipe()
go fakeServer(t, &fakePipes{r1, w2}, fakeReply)
return &fakePipes{r2, w1}, nil
}
func fakeServer(t *testing.T, rw io.ReadWriteCloser, fakeReply fakeResponseMap) {
b := bufio.NewReader(rw)
_, err := rw.Write([]byte(fakeReply[""]))
if err != nil {
t.Errorf("fakeServer: Banner write causes %s", err)
}
for {
line, err := b.ReadString('\n')
if err != nil {
if err != io.EOF {
t.Errorf("fakeServer: Reading causes %s", err)
}
break
}
reply := fakeReply[strings.TrimSpace(line)]
if reply == "" {
break
}
_, err = rw.Write([]byte(reply))
if err != nil {
t.Errorf("fakeServer: Writing causes %s", err)
break
}
}
rw.Close()
}
// End of rsc (Russ Cox, Google Inc.) code
type fakeResponseMap map[string]string
var fakeReply = fakeResponseMap{
"": "# munin node at localhost\n",
"list": "cpu\n",
"quit": "",
"fetch cpu": "user.value 234600\n" +
"nice.value 1931\n" +
"system.value 80354\n" +
"idle.value 11153645\n" +
"iowait.value 98142\n" +
"irq.value 1\n" +
"softirq.value 706\n" +
"steal.value 0\n" +
".\n",
}
var expectedReply = map[string]string{
"cpu.user": "234600",
"cpu.nice": "1931",
"cpu.system": "80354",
"cpu.idle": "11153645",
"cpu.iowait": "98142",
"cpu.irq": "1",
"cpu.softirq": "706",
"cpu.steal": "0",
}
func TestConnect(t *testing.T) {
t.Logf("Seting up fake server\n")
conn, _ := fakeConnect(t, fakeReply)
t.Logf("Seting up fake server done\n")
interval := time.Millisecond * 200
done := make(chan os.Signal, 32)
go func(die chan<- os.Signal) {
time.Sleep(interval * 2)
die <- os.Interrupt
}(done)
valChan := NewMuninClient(conn, interval, done)
reply := make(map[string]string)
for values := range valChan {
for key, value := range values {
reply[key] = value
}
}
for key, value := range expectedReply {
if _, exists := reply[key]; !exists {
t.Errorf("missing key %s in reply", key)
}
if reply[key] != value {
t.Errorf("bad value for key %s in reply: got = %s, want = %s", key, reply[key], value)
}
}
for key, value := range reply {
if _, exists := expectedReply[key]; !exists {
t.Errorf("extra key %s in reply, value %s", key, value)
}
}
}