-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_test.go
121 lines (110 loc) · 3.12 KB
/
output_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
package distributed_statsd
import (
"encoding/json"
"fmt"
"strings"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/guregu/null.v3"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/lib/types"
"go.k6.io/k6/metrics"
"go.k6.io/k6/output"
)
func getOutput(
logger logrus.FieldLogger, addr, namespace null.String, bufferSize null.Int, pushInterval types.NullDuration,
) (*Output, error) {
return newOutput(
output.Params{
Logger: logger,
JSONConfig: json.RawMessage(fmt.Sprintf(`{
"addr": "%s",
"namespace": "%s",
"bufferSize": %d,
"pushInterval": "%s"
}`, addr.String, namespace.String, bufferSize.Int64, pushInterval.Duration.String())),
})
}
func TestStatsdOutput(t *testing.T) {
t.Parallel()
baseTest(t, getOutput,
func(t *testing.T, _ []metrics.SampleContainer, expectedOutput, output string) {
assert.Equal(t, expectedOutput, output)
})
}
func TestStatsdEnabledTags(t *testing.T) {
t.Parallel()
tagMap := metrics.EnabledTags{"tag1": true, "tag2": true}
baseTest(t, func(
logger logrus.FieldLogger, addr, namespace null.String, bufferSize null.Int, pushInterval types.NullDuration,
) (*Output, error) {
return newOutput(
output.Params{
Logger: logger,
JSONConfig: json.RawMessage(fmt.Sprintf(`{
"addr": "%s",
"namespace": "%s",
"bufferSize": %d,
"pushInterval": "%s",
"tagBlocklist": ["tag1", "tag2"],
"enableTags": true
}`, addr.String, namespace.String, bufferSize.Int64, pushInterval.Duration.String())),
})
}, func(t *testing.T, containers []metrics.SampleContainer, expectedOutput, output string) {
outputLines := strings.Split(output, "\n")
expectedOutputLines := strings.Split(expectedOutput, "\n")
var lines int
for i, container := range containers {
for j, sample := range container.GetSamples() {
lines++
var (
expectedTagList = processTags(tagMap, sample.GetTags().Map())
expectedOutputLine = expectedOutputLines[i*j+i]
outputLine = outputLines[i*j+i]
outputWithoutTags = outputLine
outputTagList = []string{}
tagSplit = strings.LastIndex(outputLine, "|#")
)
if tagSplit != -1 {
outputWithoutTags = outputLine[:tagSplit]
outputTagList = strings.Split(outputLine[tagSplit+len("|#"):], ",")
}
require.Equal(t, expectedOutputLine, outputWithoutTags)
require.ElementsMatch(t, expectedTagList, outputTagList)
}
}
require.Equal(t, lines, len(outputLines))
})
}
func TestInitWithoutAddressErrors(t *testing.T) {
t.Parallel()
c := &Output{
config: config{},
logger: testutils.NewLogger(t),
}
err := c.Start()
require.Error(t, err)
}
func TestInitWithBogusAddressErrors(t *testing.T) {
t.Parallel()
c := &Output{
config: config{
Addr: null.StringFrom("localhost:90000"),
},
logger: testutils.NewLogger(t),
}
err := c.Start()
require.Error(t, err)
}
func TestLinkReturnAddress(t *testing.T) {
t.Parallel()
bogusValue := "bogus value"
c := &Output{
config: config{
Addr: null.StringFrom(bogusValue),
},
}
require.Equal(t, fmt.Sprintf("statsd (%s)", bogusValue), c.Description())
}