Skip to content

Commit add9652

Browse files
committed
Add initial tests
1 parent 07bd5c3 commit add9652

File tree

1 file changed

+189
-0
lines changed

1 file changed

+189
-0
lines changed

grafsy/grafsy_internal_test.go

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
package grafsy
2+
3+
import (
4+
"bufio"
5+
"net"
6+
"strings"
7+
"testing"
8+
)
9+
10+
var cleanMonitoring = &Monitoring{
11+
got: source{
12+
retry: 0,
13+
dir: 0,
14+
net: 0,
15+
},
16+
dropped: 0,
17+
invalid: 0,
18+
saved: 0,
19+
sent: 0,
20+
}
21+
22+
// These variables defined to prevent reading the config multiple times
23+
// and avoid code duplication
24+
var conf, lc, configError = getConfigs()
25+
var mon, monError = generateMonitoringObject()
26+
var cli = Client{
27+
conf,
28+
lc,
29+
mon,
30+
}
31+
32+
// These metrics are used in many tests. Check before updating them
33+
var testMetrics = []string{
34+
"test.oleg.test 8 1500000000",
35+
"whoop.whoop 11 1500000000",
36+
}
37+
38+
func generateMonitoringObject() (*Monitoring, error) {
39+
s := source{
40+
net: 1,
41+
dir: 2,
42+
retry: 3,
43+
}
44+
45+
return &Monitoring{
46+
Conf: conf,
47+
Lc: lc,
48+
got: s,
49+
dropped: 1,
50+
invalid: 2,
51+
saved: 3,
52+
sent: 4,
53+
}, nil
54+
}
55+
56+
func getConfigs() (*Config, *localConfig, error) {
57+
conf := &Config{}
58+
err := conf.LoadConfig("/etc/grafsy/grafsy.toml")
59+
if err != nil {
60+
return nil, nil, err
61+
}
62+
63+
lc, err := conf.GenerateLocalConfig()
64+
return conf, lc, err
65+
}
66+
67+
func acceptAndReport(l net.Listener, ch chan string) error {
68+
conn, err := l.Accept()
69+
if err != nil {
70+
return err
71+
}
72+
73+
defer conn.Close()
74+
conBuf := bufio.NewReader(conn)
75+
for {
76+
metric, err := conBuf.ReadString('\n')
77+
ch <- strings.Replace(strings.Replace(metric, "\r", "", -1), "\n", "", -1)
78+
if err != nil {
79+
return err
80+
}
81+
}
82+
}
83+
84+
func TestConfig_GenerateLocalConfig(t *testing.T) {
85+
if configError != nil {
86+
t.Error(configError)
87+
}
88+
}
89+
90+
func TestMonitoring_generateOwnMonitoring(t *testing.T) {
91+
if monError != nil {
92+
t.Error("Can not generate monitoring object", monError)
93+
}
94+
95+
mon.generateOwnMonitoring()
96+
if len(mon.Lc.monitoringChannel) != MonitorMetrics {
97+
t.Error("Mismatch amount of the monitor metrics:", len(mon.Lc.monitoringChannel))
98+
}
99+
}
100+
101+
func TestMonitoring_clean(t *testing.T) {
102+
m, _ := generateMonitoringObject()
103+
m.Conf = nil
104+
m.Lc = nil
105+
m.clean()
106+
107+
if *cleanMonitoring != *m {
108+
t.Error("Monitoring was not cleaned up")
109+
}
110+
}
111+
112+
func TestMetricData_getSizeInLinesFromFile(t *testing.T) {
113+
if getSizeInLinesFromFile("/etc/grafsy/grafsy.toml") == 0 {
114+
t.Error("Can not be 0 lines in config file")
115+
}
116+
}
117+
118+
func TestConfg_generateRegexpsForOverwrite(t *testing.T) {
119+
if configError != nil {
120+
t.Error(configError)
121+
}
122+
123+
conf.Overwrite = []struct {
124+
ReplaceWhatRegexp, ReplaceWith string
125+
}{{"^test.*test ", "does not matter"}}
126+
127+
regexps := conf.generateRegexpsForOverwrite()
128+
if len(regexps) != 1 {
129+
t.Error("There must be only 1 regexp")
130+
}
131+
132+
if !regexps[0].MatchString(testMetrics[0]) {
133+
t.Error("Test regexp does not match")
134+
}
135+
}
136+
137+
func TestClient_retry(t *testing.T) {
138+
var metricsFound int
139+
140+
err := cli.saveSliceToRetry(testMetrics)
141+
if err != nil {
142+
t.Error(err)
143+
}
144+
145+
metrics, err := readMetricsFromFile(conf.RetryFile)
146+
if err != nil {
147+
t.Error(err)
148+
}
149+
150+
for _, m := range metrics {
151+
for _, tm := range testMetrics {
152+
if tm == m {
153+
metricsFound++
154+
}
155+
}
156+
}
157+
158+
if metricsFound != len(testMetrics) {
159+
t.Error("Not all metrics were saved/read")
160+
}
161+
}
162+
163+
func TestClient_tryToSendToGraphite(t *testing.T) {
164+
// Pretend to be a server
165+
l, err := net.Listen("tcp", "127.0.0.1:0")
166+
if err != nil {
167+
t.Error(err)
168+
}
169+
ch := make(chan string, len(testMetrics))
170+
go acceptAndReport(l, ch)
171+
172+
// Send as client
173+
conn, err := net.Dial("tcp", l.Addr().String())
174+
if err != nil {
175+
t.Error("Unable to connect to", l.Addr().String())
176+
}
177+
178+
for _, metric := range testMetrics {
179+
cli.tryToSendToGraphite(metric, conn)
180+
}
181+
182+
// Read from channel
183+
for i := 0; i < len(testMetrics); i++ {
184+
received := <-ch
185+
if received != testMetrics[i] {
186+
t.Error("Received something different than sent")
187+
}
188+
}
189+
}

0 commit comments

Comments
 (0)