-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagent_test.go
165 lines (143 loc) · 3.75 KB
/
agent_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
// Copyright © 2020 Circonus, Inc. <[email protected]>
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package main
import (
"fmt"
"log"
"net"
"strconv"
"testing"
"time"
statsd "gopkg.in/alexcesaro/statsd.v2"
)
func TestParseTags(t *testing.T) {
// Test setup
goodTags := "key1:value1,key2:value2,key3:value3"
badTags1 := "key1:value1,key2:value2,key3"
badTags2 := "key1:value1,key2:value2,key3:"
// Run the tests and verify output
_, err := parseTags(goodTags)
if err != nil {
t.Errorf("Expected no error but got %s", err)
}
_, err = parseTags(badTags1)
if err == nil {
t.Error("Expected an error but one wasn't returned")
}
_, err = parseTags(badTags2)
if err == nil {
t.Error("Expected an error but one wasn't returned")
}
}
func TestGenMetricsNames(t *testing.T) {
// Test Setup
id := 0
metricType := "counter"
n := 3
// Run tests and verify output
metricNames := genMetricsNames(metricType, id, n)
if len(metricNames) != 3 {
t.Errorf("Expected length of slice to be %d, got %d", n, len(metricNames))
}
for i, name := range metricNames {
if name != "agent"+strconv.Itoa(id)+"-"+metricType+strconv.Itoa(i) {
t.Errorf("Expected: %s, got %s", fmt.Sprintf("agent%d-%s%d", id, metricType, i), name)
}
}
}
func TestCreateAgent(t *testing.T) {
srv, err := net.ListenPacket("udp", ":8125")
if err != nil {
t.Fatalf("creating server %s", err)
}
defer srv.Close()
go func() {
buf := make([]byte, 1024)
_, _, err = srv.ReadFrom(buf)
if err != nil {
return
}
}()
// Test Setup
id := 0
num := 1
flush := time.Second * 0
timerMin := 0
timerMax := 10
addr := ":8125"
prefix := "test"
goodTags := "key1:value1,key2:value2"
badTags := "key1:value1,key2:"
tagFormat1 := "datadog"
tagFormat2 := "influx"
badTagFormat := "not a tag format"
client, err := statsd.New(
statsd.Address(addr),
statsd.Network("udp"),
statsd.FlushPeriod(flush),
statsd.Prefix(prefix),
statsd.ErrorHandler(func(err error) {
log.Printf("error sending metrics to target %s: %s\n", addr, err)
}),
)
if err != nil {
t.Fatalf("error creating client for target %s: %s", addr, err)
}
clients := []*statsd.Client{client}
// Run tests and verify output
_, err = CreateAgent(id, num, num, num, timerMax, timerMin, flush, clients, goodTags, tagFormat1, true)
if err != nil {
t.Errorf("expected no error, got: %s", err)
}
_, err = CreateAgent(id, num, num, num, timerMax, timerMin, flush, clients, badTags, tagFormat1, true)
if err == nil {
t.Errorf("expected error, got: %s", err)
}
_, err = CreateAgent(id, num, num, num, timerMax, timerMin, flush, clients, goodTags, tagFormat2, true)
if err != nil {
t.Errorf("expected no error, got: %s", err)
}
_, err = CreateAgent(id, num, num, num, timerMax, timerMin, flush, clients, goodTags, badTagFormat, true)
if err == nil {
t.Errorf("expected error, got: %s", err)
}
}
func TestNewAgentController(t *testing.T) {
ac := NewAgentController()
if ac == nil {
t.Error("expected agent controller got nil")
}
}
// func TestAgentControllerStart(t *testing.T) {
// ac := NewAgentController()
// conf := config{
// statsdHost: ":8125",
// network: "udp",
// flushInterval: time.Second * 1,
// counters: 1,
// gauges: 0,
// timers: 0,
// agents: 1,
// spawnDrift: 10,
// }
// buf := make([]byte, 1024)
// go udpListen(buf)
// ac.Start(conf)
// time.Sleep(time.Second * 2)
// ac.ctx.Done()
// fmt.Print(string(buf))
// }
// func udpListen(buf []byte) (int, error) {
// srv, err := net.ListenPacket("udp", ":8125")
// if err != nil {
// return 0, err
// }
// defer srv.Close()
// n, _, err := srv.ReadFrom(buf)
// if err != nil {
// return 0, err
// }
// return n, nil
// }