-
Notifications
You must be signed in to change notification settings - Fork 8
/
grafsy_internal_test.go
258 lines (226 loc) · 6.04 KB
/
grafsy_internal_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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package grafsy
import (
"bufio"
"net"
"path"
"reflect"
"strings"
"testing"
)
var cleanMonitoring = &Monitoring{
Conf: conf,
Lc: lc,
serverStat: serverStat{
dir: 0,
invalid: 0,
net: 0,
},
clientStat: map[string]*clientStat{
"localhost:2003": &clientStat{
saved: 0,
sent: 0,
dropped: 0,
aggregated: 0,
},
"localhost:2004": &clientStat{
saved: 0,
sent: 0,
dropped: 0,
aggregated: 0,
},
},
}
// These variables defined to prevent reading the config multiple times
// and avoid code duplication
var conf, lc, configError = getConfigs()
// There are 3 metrics per backend
var mon, monError = generateMonitoringObject()
var serverStatMetrics = reflect.ValueOf(mon.serverStat).NumField()
var clientStatMetrics = reflect.TypeOf(mon.clientStat).Elem().Elem().NumField()
var MonitorMetrics = serverStatMetrics + len(conf.CarbonAddrs)*clientStatMetrics
var cli = Client{
Conf: conf,
Lc: lc,
Mon: mon,
monChannels: map[string]chan string{
"localhost:2003": make(chan string, len(testMetrics)),
"localhost:2004": make(chan string, len(testMetrics)),
},
}
// These metrics are used in many tests. Check before updating them
var testMetrics = []string{
"test.oleg.test 8 1500000000",
"whoop.whoop 11 1500000000",
}
func generateMonitoringObject() (*Monitoring, error) {
return &Monitoring{
Conf: conf,
Lc: lc,
serverStat: serverStat{
net: 1,
invalid: 4,
dir: 2,
},
clientStat: map[string]*clientStat{
"localhost:2003": &clientStat{
1,
3,
2,
4,
5,
},
"localhost:2004": &clientStat{
1,
3,
2,
4,
5,
},
},
}, nil
}
func getConfigs() (*Config, *LocalConfig, error) {
conf := &Config{}
err := conf.LoadConfig("grafsy.toml")
if err != nil {
return nil, nil, err
}
lc, err := conf.GenerateLocalConfig()
return conf, lc, err
}
func acceptAndReport(l net.Listener, ch chan string) error {
conn, err := l.Accept()
if err != nil {
return err
}
defer conn.Close()
conBuf := bufio.NewReader(conn)
for {
metric, err := conBuf.ReadString('\n')
ch <- strings.Replace(strings.Replace(metric, "\r", "", -1), "\n", "", -1)
if err != nil {
return err
}
}
}
func TestConfig_MonitoringChannelCapacity(t *testing.T) {
testConf := &Config{}
err := testConf.LoadConfig("grafsy.toml")
if err != nil {
t.Error("Fail to load config")
}
backendSets := make([][]string, 5)
backendSets = append(backendSets, []string{"localhost:2003"})
backendSets = append(backendSets, []string{"localhost:2003", "localhost:2003"})
backendSets = append(backendSets, []string{"localhost:2003", "localhost:2003", "localhost:2003"})
backendSets = append(backendSets, []string{"localhost:2003", "localhost:2003", "localhost:2003", "localhost:2003"})
backendSets = append(backendSets, []string{"localhost:2003", "localhost:2003", "localhost:2003", "localhost:2003", "localhost:2003"})
for _, backends := range backendSets {
testConf.CarbonAddrs = backends
testLc, err := testConf.GenerateLocalConfig()
if err != nil {
t.Error("Fail to generate local config")
}
metricsLen := serverStatMetrics + len(backends)*clientStatMetrics
monCap := cap(testLc.monitoringChannel)
if monCap != metricsLen {
t.Errorf("The formula for monitoring channel capasity is wrong, got %v, should be %v", monCap, metricsLen)
}
}
}
func TestConfig_GenerateLocalConfig(t *testing.T) {
if configError != nil {
t.Error(configError)
}
}
func TestMonitoring_generateOwnMonitoring(t *testing.T) {
if monError != nil {
t.Error("Can not generate monitoring object", monError)
}
mon.generateOwnMonitoring()
if len(mon.Lc.monitoringChannel) != MonitorMetrics {
t.Logf("Fix the amount of metrics for server stats to %v and client stats to %v\n", serverStatMetrics, clientStatMetrics)
t.Errorf("Mismatch amount of the monitor metrics: expected=%v, gotten=%v", MonitorMetrics, len(mon.Lc.monitoringChannel))
}
}
func TestMonitoring_clean(t *testing.T) {
m, _ := generateMonitoringObject()
m.Conf = conf
m.Lc = lc
m.clean()
if !reflect.DeepEqual(*cleanMonitoring, *m) {
t.Errorf("Monitoring was not cleaned up:\n Sample: %+v\n Gotten: %+v", cleanMonitoring, m)
}
}
func TestMetricData_getSizeInLinesFromFile(t *testing.T) {
if getSizeInLinesFromFile("grafsy.toml") == 0 {
t.Error("Can not be 0 lines in config file")
}
}
func TestConfg_generateRegexpsForOverwrite(t *testing.T) {
if configError != nil {
t.Error(configError)
}
conf.Overwrite = []struct {
ReplaceWhatRegexp, ReplaceWith string
}{{"^test.*test ", "does not matter"}}
regexps := conf.generateRegexpsForOverwrite()
if len(regexps) != 1 {
t.Error("There must be only 1 regexp")
}
if !regexps[0].MatchString(testMetrics[0]) {
t.Error("Test regexp does not match")
}
}
func TestClient_retry(t *testing.T) {
var metricsFound int
err := cli.createRetryDir()
if err != nil {
t.Error(err)
}
err = cli.saveSliceToRetry(testMetrics, conf.CarbonAddrs[0])
if err != nil {
t.Error(err)
}
metrics, err := readMetricsFromFile(path.Join(conf.RetryDir, conf.CarbonAddrs[0]))
if err != nil {
t.Error(err)
}
for _, m := range metrics {
for _, tm := range testMetrics {
if tm == m {
metricsFound++
}
}
}
if metricsFound != len(testMetrics) {
t.Error("Not all metrics were saved/read")
}
}
func TestClient_tryToSendToGraphite(t *testing.T) {
// Pretend to be a server with random port
carbonServer := "localhost:0"
l, err := net.Listen("tcp", carbonServer)
if err != nil {
t.Error(err)
}
ch := make(chan string, len(testMetrics))
go acceptAndReport(l, ch)
// Send as client
conn, err := net.Dial("tcp", l.Addr().String())
if err != nil {
t.Error("Unable to connect to", l.Addr().String())
}
// Create monitoring structure for statistic
cli.Mon.clientStat[carbonServer] = &clientStat{0, 0, 0, 0, 0}
for _, metric := range testMetrics {
cli.tryToSendToGraphite(metric, carbonServer, conn)
}
// Read from channel
for i := 0; i < len(testMetrics); i++ {
received := <-ch
if received != testMetrics[i] {
t.Error("Received something different than sent")
}
}
}