forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minecraft_test.go
234 lines (203 loc) · 5.31 KB
/
minecraft_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
package minecraft
import (
"fmt"
"reflect"
"testing"
"github.com/influxdata/telegraf/testutil"
)
// TestParsePlayerName tests different Minecraft RCON inputs for players
func TestParsePlayerName(t *testing.T) {
// Test a valid input string to ensure player is extracted
input := "1 tracked objective(s) for divislight:- jumps: 178 (jumps)"
got, err := ParsePlayerName(input)
want := "divislight"
if err != nil {
t.Fatalf("player returned error. Error: %s\n", err)
}
if got != want {
t.Errorf("got %s\nwant %s\n", got, want)
}
// Test an invalid input string to ensure error is returned
input = ""
got, err = ParsePlayerName(input)
want = ""
if err == nil {
t.Fatal("Expected error when player not present. No error found.")
}
if got != want {
t.Errorf("got %s\n want %s\n", got, want)
}
// Test an invalid input string to ensure error is returned
input = "1 tracked objective(s) for 😂:- jumps: 178 (jumps)"
got, err = ParsePlayerName(input)
want = "😂"
if err != nil {
t.Fatalf("player returned error. Error: %s\n", err)
}
if got != want {
t.Errorf("got %s\n want %s\n", got, want)
}
}
// TestParseScoreboard tests different Minecraft RCON inputs for scoreboard stats.
func TestParseScoreboard(t *testing.T) {
// test a valid input string to ensure stats are parsed correctly.
input := `1 tracked objective(s) for divislight:- jumps: 178 (jumps)- sword: 5 (sword)`
got, err := ParseScoreboard(input)
if err != nil {
t.Fatal("Unexpected error")
}
want := []Score{
{
Name: "jumps",
Value: 178,
},
{
Name: "sword",
Value: 5,
},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got: \n%#v\nWant: %#v", got, want)
}
// Tests a partial input string.
input = `1 tracked objective(s) for divislight:- jumps: (jumps)- sword: 5 (sword)`
got, err = ParseScoreboard(input)
if err != nil {
t.Fatal("Unexpected error")
}
want = []Score{
{
Name: "sword",
Value: 5,
},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got: \n%#v\nWant:\n%#v", got, want)
}
// Tests an empty string.
input = ``
_, err = ParseScoreboard(input)
if err == nil {
t.Fatal("Expected input error, but error was nil")
}
// Tests when a number isn't an integer.
input = `1 tracked objective(s) for divislight:- jumps: 178.5 (jumps)- sword: 5 (sword)`
got, err = ParseScoreboard(input)
if err != nil {
t.Fatal("Unexpected error")
}
want = []Score{
{
Name: "sword",
Value: 5,
},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got: \n%#v\nWant: %#v", got, want)
}
//Testing a real life data scenario with unicode characters
input = `7 tracked objective(s) for mauxlaim:- total_kills: 39 (total_kills)- "howdy doody": 37 (dalevel)- howdy: 37 (lvl)- jumps: 1290 (jumps)- iron_pickaxe: 284 (iron_pickaxe)- cow_kills: 1 (cow_kills)- "asdf": 37 (😂)`
got, err = ParseScoreboard(input)
if err != nil {
t.Fatal("Unexpected error")
}
want = []Score{
{
Name: "total_kills",
Value: 39,
},
{
Name: "dalevel",
Value: 37,
},
{
Name: "lvl",
Value: 37,
},
{
Name: "jumps",
Value: 1290,
},
{
Name: "iron_pickaxe",
Value: 284,
},
{
Name: "cow_kills",
Value: 1,
},
{
Name: "😂",
Value: 37,
},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Got: \n%#v\nWant: %#v", got, want)
}
}
type MockClient struct {
Result []string
Err error
}
func (m *MockClient) Gather(d RCONClientProducer) ([]string, error) {
return m.Result, m.Err
}
func TestGather(t *testing.T) {
var acc testutil.Accumulator
testConfig := Minecraft{
Server: "biffsgang.net",
Port: "25575",
client: &MockClient{
Result: []string{
`1 tracked objective(s) for divislight:- jumps: 178 (jumps)`,
`7 tracked objective(s) for mauxlaim:- total_kills: 39 (total_kills)- "howdy doody": 37 (dalevel)- howdy: 37 (lvl)- jumps: 1290 (jumps)- iron_pickaxe: 284 (iron_pickaxe)- cow_kills: 1 (cow_kills)- "asdf": 37 (😂)`,
`5 tracked objective(s) for torham:- total_kills: 29 (total_kills)- "howdy doody": 33 (dalevel)- howdy: 33 (lvl)- jumps: 263 (jumps)- "asdf": 33 (😂)`,
},
Err: nil,
},
clientSet: true,
}
err := testConfig.Gather(&acc)
if err != nil {
t.Fatalf("gather returned error. Error: %s\n", err)
}
if !testConfig.clientSet {
t.Fatalf("clientSet should be true, client should be set")
}
tags := map[string]string{
"player": "divislight",
"server": "biffsgang.net:25575",
}
assertContainsTaggedStat(t, &acc, "minecraft", "jumps", 178, tags)
tags["player"] = "mauxlaim"
assertContainsTaggedStat(t, &acc, "minecraft", "cow_kills", 1, tags)
tags["player"] = "torham"
assertContainsTaggedStat(t, &acc, "minecraft", "total_kills", 29, tags)
}
func assertContainsTaggedStat(
t *testing.T,
acc *testutil.Accumulator,
measurement string,
field string,
expectedValue int,
tags map[string]string,
) {
var actualValue int
for _, pt := range acc.Metrics {
if pt.Measurement == measurement && reflect.DeepEqual(pt.Tags, tags) {
for fieldname, value := range pt.Fields {
if fieldname == field {
actualValue = value.(int)
if value == expectedValue {
return
}
t.Errorf("Expected value %d\n got value %d\n", expectedValue, value)
}
}
}
}
msg := fmt.Sprintf(
"Could not find measurement \"%s\" with requested tags within %s, Actual: %d",
measurement, field, actualValue)
t.Fatal(msg)
}