forked from mperham/inspeqtor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands_test.go
232 lines (184 loc) · 5.89 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package inspeqtor
import (
"bytes"
"fmt"
"io"
"net"
"regexp"
"strings"
"testing"
"time"
"github.com/mperham/inspeqtor/metrics"
"github.com/mperham/inspeqtor/services"
"github.com/mperham/inspeqtor/util"
"github.com/stretchr/testify/assert"
)
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())
x := i.SilenceUntil
i.acceptCommand()
// Inspeqtor will be silenced for one more cycle, verify the
// silence period shrunk
assert.True(t, i.silenced())
assert.True(t, x.After(i.SilenceUntil))
i.acceptCommand()
}
func TestStartDeploy(t *testing.T) {
t.Parallel()
i, err := New("_", "")
i.SilenceUntil = time.Now()
var outbuf []byte
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("_", "")
var outbuf []byte
resp := bytes.NewBuffer(outbuf)
assert.Nil(t, err)
proc := CommandHandlers["finish"]
proc(i, []string{}, resp)
oneCycle := time.Duration(i.GlobalConfig.CycleTime) * time.Second
assert.True(t, i.SilenceUntil.Before(time.Now().Add(oneCycle)))
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("_", "")
var outbuf []byte
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.WithStatus(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])
}
func TestStatusSilenced(t *testing.T) {
i, err := New("_", "")
assert.Nil(t, err)
proc := CommandHandlers["status"]
containsSilenced := func() bool {
var resp bytes.Buffer
proc(i, []string{}, &resp)
return strings.Contains(resp.String(), "Silenced until: ")
}
assert.False(t, containsSilenced())
i.SilenceUntil = time.Now().Add(10 * time.Second)
assert.True(t, containsSilenced())
}
func TestExport(t *testing.T) {
t.Parallel()
i, err := New("_", "")
i.Services = []Checkable{
&Service{&Entity{"foo", nil, metrics.NewProcessStore("/proc", 15), nil}, nil, services.WithStatus(99, services.Up), nil},
}
var resp bytes.Buffer
assert.Nil(t, err)
proc := CommandHandlers["export"]
proc(i, []string{}, &resp)
line, err := resp.ReadString('\n')
assert.Nil(t, err)
assert.True(t, strings.Contains(line, "\"pid\":99"))
assert.True(t, strings.Contains(line, "\"name\":\"foo\""))
assert.True(t, strings.Contains(line, "\"memory\":{\"rss\":-1}"))
}
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")
}