-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverstat_test.go
More file actions
141 lines (110 loc) · 3.45 KB
/
Copy pathserverstat_test.go
File metadata and controls
141 lines (110 loc) · 3.45 KB
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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/vikpe/udphelper"
)
type AppRunner struct {
app string
}
func (ar AppRunner) run(appArgs string) string {
argsStr := strings.TrimSpace(fmt.Sprintf("%s %s", ar.app, appArgs))
argsArr := strings.Split(argsStr, " ")
captureOutput := func(f func()) string {
rescueStderr := os.Stderr
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
os.Stderr = w
f()
w.Close()
out, _ := ioutil.ReadAll(r)
os.Stderr = rescueStderr
os.Stdout = rescueStdout
return string(out)
}
return captureOutput(func() { run(argsArr) })
}
var app = AppRunner{
app: "masterstat",
}
func TestHelp(t *testing.T) {
helpText := `serverstat [__VERSION__]
Get info from QuakeWorld servers.
Usage: serverstat <address>
Example: serverstat quake.se:28501
`
t.Run("No args", func(t *testing.T) {
assert.Equal(t, app.run(""), helpText)
})
t.Run("Help", func(t *testing.T) {
assert.Equal(t, app.run("help"), helpText)
assert.Equal(t, app.run("--help"), helpText)
assert.Equal(t, app.run("-h"), helpText)
})
}
func TestGetInfo(t *testing.T) {
t.Run("UDP request error", func(t *testing.T) {
output := app.run("foo:666")
assert.Contains(t, output, "ERROR:")
})
t.Run("MVDSV", func(t *testing.T) {
go func() {
responseHeader := string([]byte{0xff, 0xff, 0xff, 0xff, 'n', '\\'})
responseBody := `\status\Standby\*gamedir\qw\map\dm2\maxfps\77\pm_ktjump\1\*version\MVDSV 0.35-dev\maxclients\8\maxspectators\4
65 -9999 16 -666 "\s\[ServeMe]" "" 12 11 "lqwc" ""`
udphelper.New(":8000").Respond([]byte((responseHeader + responseBody)))
}()
time.Sleep(10 * time.Millisecond)
outputAsJson := app.run(":8000")
expectedOutput := jsonFileToString("./test_files/mvdsv.json")
assert.JSONEq(t, expectedOutput, outputAsJson)
})
t.Run("QTV", func(t *testing.T) {
go func() {
responseHeader := string([]byte{0xff, 0xff, 0xff, 0xff, 'n', '\\'})
responseBody := `\*version\QTV 1.12-rc1\hostname\qw.foppa.dk - qtv\maxclients\100`
udphelper.New(":8001").Respond([]byte((responseHeader + responseBody)))
}()
time.Sleep(10 * time.Millisecond)
outputAsJson := app.run(":8001")
expectedOutput := jsonFileToString("./test_files/qtv.json")
assert.JSONEq(t, expectedOutput, outputAsJson)
})
t.Run("QWFWD", func(t *testing.T) {
go func() {
responseHeader := string([]byte{0xff, 0xff, 0xff, 0xff, 'n', '\\'})
responseBody := `\*version\qwfwd 1.2\maxclients\128\hostname\qw.foppa.dk - qwfwd`
udphelper.New(":8002").Respond([]byte((responseHeader + responseBody)))
}()
time.Sleep(10 * time.Millisecond)
outputAsJson := app.run(":8002")
expectedOutput := jsonFileToString("./test_files/qwfwd.json")
assert.JSONEq(t, expectedOutput, outputAsJson)
})
t.Run("Unknown", func(t *testing.T) {
go func() {
responseHeader := string([]byte{0xff, 0xff, 0xff, 0xff, 'n', '\\'})
responseBody := `\*version\foo v1.2\hostname\foo.bar`
udphelper.New(":8003").Respond([]byte((responseHeader + responseBody)))
}()
time.Sleep(10 * time.Millisecond)
outputAsJson := app.run(":8003")
expectedOutput := jsonFileToString("./test_files/unknown.json")
assert.JSONEq(t, expectedOutput, outputAsJson)
})
}
func jsonFileToString(filePath string) string {
jsonFile, err := os.Open(filePath)
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
return string(byteValue)
}