forked from mperham/inspeqtor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands_test.go
186 lines (149 loc) · 4.73 KB
/
commands_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package inspeqtor
import (
"bytes"
"fmt"
"github.com/mperham/inspeqtor/metrics"
"github.com/mperham/inspeqtor/services"
"github.com/mperham/inspeqtor/util"
"github.com/stretchr/testify/assert"
"io"
"net"
"regexp"
"testing"
"time"
)
func TestAcceptSocket(t *testing.T) {
// not parallelizable since it uses a Unix sock
i, err := New("test", "")
assert.Nil(t, err)
err = i.Parse()
assert.Nil(t, err)
err = i.openSocket("/tmp/tmp.sock")
assert.Nil(t, err)
defer i.Socket.Close()
go func() {
conn, err := net.Dial("unix", "/tmp/tmp.sock")
assert.Nil(t, err)
conn.Write([]byte("start deploy\n"))
conn.Close()
conn, err = net.Dial("unix", "/tmp/tmp.sock")
assert.Nil(t, err)
conn.Write([]byte("finish deploy\n"))
conn.Close()
conn, err = net.Dial("unix", "/tmp/tmp.sock")
assert.Nil(t, err)
conn.Write([]byte("?\n"))
buf := make([]byte, 19)
_, err = io.ReadFull(conn, buf)
assert.Nil(t, err)
conn.Close()
assert.Equal(t, "Unknown command: ?\n", string(buf))
}()
assert.False(t, i.silenced())
i.acceptCommand()
assert.True(t, i.silenced())
i.acceptCommand()
assert.False(t, i.silenced())
i.acceptCommand()
}
func TestStartDeploy(t *testing.T) {
t.Parallel()
i, err := New("_", "")
i.SilenceUntil = time.Now()
outbuf := make([]byte, 0)
resp := bytes.NewBuffer(outbuf)
assert.Nil(t, err)
proc := CommandHandlers["start"]
proc(i, []string{}, resp)
assert.True(t, i.SilenceUntil.After(time.Now()))
assert.True(t, i.silenced())
output, err := resp.ReadString('\n')
assert.Nil(t, err)
assert.Equal(t, "Starting deploy, now silenced\n", output)
}
func TestFinishDeploy(t *testing.T) {
t.Parallel()
i, err := New("_", "")
outbuf := make([]byte, 0)
resp := bytes.NewBuffer(outbuf)
assert.Nil(t, err)
proc := CommandHandlers["finish"]
proc(i, []string{}, resp)
assert.True(t, i.SilenceUntil.Before(time.Now()))
output, err := resp.ReadString('\n')
assert.Nil(t, err)
assert.Equal(t, "Finished deploy, volume turned to 11\n", output)
}
func TestTheLove(t *testing.T) {
t.Parallel()
i, err := New("_", "")
outbuf := make([]byte, 0)
resp := bytes.NewBuffer(outbuf)
assert.Nil(t, err)
proc := CommandHandlers["♡"]
proc(i, []string{}, resp)
output, err := resp.ReadString('\n')
assert.Nil(t, err)
assert.Equal(t, "Awwww, I love you too.\n", output)
}
func TestStatus(t *testing.T) {
t.Parallel()
i, err := New("_", "")
i.Services = []Checkable{
&Service{&Entity{"foo", nil, metrics.NewProcessStore("/proc", 15), nil}, nil, &services.ProcessStatus{99, services.Up}, nil},
}
var resp bytes.Buffer
assert.Nil(t, err)
proc := CommandHandlers["status"]
proc(i, []string{}, &resp)
line, err := resp.ReadString('\n')
assert.Nil(t, err)
idxs := regexp.MustCompile(fmt.Sprintf("\\AInspeqtor %s, uptime: ", VERSION)).FindStringIndex(line)
assert.NotNil(t, idxs)
assert.Equal(t, 0, idxs[0])
}
type mockDisplayable struct {
*util.RingBuffer
}
func (*mockDisplayable) Displayable(val float64) string {
return fmt.Sprintf("%.2fm", val)
}
func TestMetricParse(t *testing.T) {
t.Parallel()
var f, n string
f, n = parseMetric("memory(rss)")
assert.Equal(t, f, "memory")
assert.Equal(t, n, "rss")
f, n = parseMetric("memory:rss")
assert.Equal(t, f, "memory")
assert.Equal(t, n, "rss")
f, n = parseMetric("cpu")
assert.Equal(t, f, "cpu")
assert.Equal(t, n, "")
}
func TestSparkline(t *testing.T) {
t.Parallel()
i, err := New("_", "")
assert.Nil(t, err)
buf := util.NewRingBuffer(120)
for i := 1; i <= 100; i++ {
buf.Add(float64(i))
}
src := &mockDisplayable{buf}
output := buildSparkline(i.Host, "memory:rss", func(family, name string) displayable {
return src
})
expected := "localhost memory:rss min 1.00m max 100.00m avg 50.50m\n▁▁▁▁▁▁▁▁▁▁▁▁▁▂▂▂▂▂▂▂▂▂▂▂▂▃▃▃▃▃▃▃▃▃▃▃▃▃▄▄▄▄▄▄▄▄▄▄▄▄▅▅▅▅▅▅▅▅▅▅▅▅▆▆▆▆▆▆▆▆▆▆▆▆▆▇▇▇▇▇▇▇▇▇▇▇▇█████████████\n"
assert.Equal(t, output, expected)
// alternate, CLI friendy metric naming format
expected = "localhost memory:rss min 1.00m max 100.00m avg 50.50m\n▁▁▁▁▁▁▁▁▁▁▁▁▁▂▂▂▂▂▂▂▂▂▂▂▂▃▃▃▃▃▃▃▃▃▃▃▃▃▄▄▄▄▄▄▄▄▄▄▄▄▅▅▅▅▅▅▅▅▅▅▅▅▆▆▆▆▆▆▆▆▆▆▆▆▆▇▇▇▇▇▇▇▇▇▇▇▇█████████████\n"
output = buildSparkline(i.Host, "memory:rss", func(family, name string) displayable {
return src
})
assert.Equal(t, output, expected)
// invalid metric
output = buildSparkline(i.Host, "memory:xxx", func(family, name string) displayable {
return nil
})
assert.Equal(t, output, "Unknown metric: memory:xxx\n")
}