forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 15
/
uwsgi_test.go
185 lines (171 loc) · 3.21 KB
/
uwsgi_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
package uwsgi_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/influxdata/telegraf/plugins/inputs/uwsgi"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)
func TestBasic(t *testing.T) {
js := `
{
"version":"2.0.12",
"listen_queue":0,
"listen_queue_errors":0,
"signal_queue":0,
"load":0,
"pid":28372,
"uid":1000,
"gid":1000,
"cwd":"/opt/uwsgi",
"locks":[
{
"user 0":0
},
{
"signal":0
},
{
"filemon":0
},
{
"timer":0
},
{
"rbtimer":0
},
{
"cron":0
},
{
"rpc":0
},
{
"snmp":0
}
],
"sockets":[
{
"name":"127.0.0.1:47430",
"proto":"uwsgi",
"queue":0,
"max_queue":100,
"shared":0,
"can_offload":0
}
],
"workers":[
{
"id":1,
"pid":28375,
"accepting":1,
"requests":0,
"delta_requests":0,
"exceptions":0,
"harakiri_count":0,
"signals":0,
"signal_queue":0,
"status":"idle",
"rss":0,
"vsz":0,
"running_time":0,
"last_spawn":1459942782,
"respawn_count":1,
"tx":0,
"avg_rt":0,
"apps":[
{
"id":0,
"modifier1":0,
"mountpoint":"",
"startup_time":0,
"requests":0,
"exceptions":0,
"chdir":""
}
],
"cores":[
{
"id":0,
"requests":0,
"static_requests":0,
"routed_requests":0,
"offloaded_requests":0,
"write_errors":0,
"read_errors":0,
"in_request":0,
"vars":[
]
}
]
}
]
}
`
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
_, _ = w.Write([]byte(js))
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer fakeServer.Close()
plugin := &uwsgi.Uwsgi{
Servers: []string{fakeServer.URL + "/"},
}
var acc testutil.Accumulator
require.NoError(t, plugin.Gather(&acc))
require.Equal(t, 0, len(acc.Errors))
}
func TestInvalidJSON(t *testing.T) {
js := `
{
"version":"2.0.12",
"listen_queue":0,
"listen_queue_errors":0,
"signal_queue":0,
"load":0,
"pid:28372
"uid":10
}
`
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
_, _ = w.Write([]byte(js))
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer fakeServer.Close()
plugin := &uwsgi.Uwsgi{
Servers: []string{fakeServer.URL + "/"},
}
var acc testutil.Accumulator
require.NoError(t, plugin.Gather(&acc))
require.Equal(t, 1, len(acc.Errors))
}
func TestHttpError(t *testing.T) {
plugin := &uwsgi.Uwsgi{
Servers: []string{"http://novalidurladress/"},
}
var acc testutil.Accumulator
require.NoError(t, plugin.Gather(&acc))
require.Equal(t, 1, len(acc.Errors))
}
func TestTcpError(t *testing.T) {
plugin := &uwsgi.Uwsgi{
Servers: []string{"tcp://novalidtcpadress/"},
}
var acc testutil.Accumulator
require.NoError(t, plugin.Gather(&acc))
require.Equal(t, 1, len(acc.Errors))
}
func TestUnixSocketError(t *testing.T) {
plugin := &uwsgi.Uwsgi{
Servers: []string{"unix:///novalidunixsocket"},
}
var acc testutil.Accumulator
require.NoError(t, plugin.Gather(&acc))
require.Equal(t, 1, len(acc.Errors))
}