forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nginx_upstream_check_test.go
135 lines (116 loc) · 3.29 KB
/
nginx_upstream_check_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
package nginx_upstream_check
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)
const sampleStatusResponse = `
{
"servers": {
"total": 2,
"generation": 1,
"server": [
{
"index": 0,
"upstream": "upstream-1",
"name": "127.0.0.1:8081",
"status": "up",
"rise": 1000,
"fall": 0,
"type": "http",
"port": 0
},
{
"index": 1,
"upstream": "upstream-2",
"name": "127.0.0.1:8082",
"status": "down",
"rise": 0,
"fall": 2000,
"type": "tcp",
"port": 8080
}
]
}
}
`
func TestNginxUpstreamCheckData(test *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(responseWriter http.ResponseWriter, request *http.Request) {
var response string
if request.URL.Path == "/status" {
response = sampleStatusResponse
responseWriter.Header()["Content-Type"] = []string{"application/json"}
} else {
panic("Cannot handle request")
}
fmt.Fprintln(responseWriter, response)
}))
defer testServer.Close()
check := NewNginxUpstreamCheck()
check.URL = fmt.Sprintf("%s/status", testServer.URL)
var accumulator testutil.Accumulator
checkError := check.Gather(&accumulator)
require.NoError(test, checkError)
accumulator.AssertContainsTaggedFields(
test,
"nginx_upstream_check",
map[string]interface{}{
"status": string("up"),
"status_code": uint8(1),
"rise": uint64(1000),
"fall": uint64(0),
},
map[string]string{
"upstream": string("upstream-1"),
"type": string("http"),
"name": string("127.0.0.1:8081"),
"port": string("0"),
"url": fmt.Sprintf("%s/status", testServer.URL),
})
accumulator.AssertContainsTaggedFields(
test,
"nginx_upstream_check",
map[string]interface{}{
"status": string("down"),
"status_code": uint8(2),
"rise": uint64(0),
"fall": uint64(2000),
},
map[string]string{
"upstream": string("upstream-2"),
"type": string("tcp"),
"name": string("127.0.0.1:8082"),
"port": string("8080"),
"url": fmt.Sprintf("%s/status", testServer.URL),
})
}
func TestNginxUpstreamCheckRequest(test *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(responseWriter http.ResponseWriter, request *http.Request) {
var response string
if request.URL.Path == "/status" {
response = sampleStatusResponse
responseWriter.Header()["Content-Type"] = []string{"application/json"}
} else {
panic("Cannot handle request")
}
fmt.Fprintln(responseWriter, response)
require.Equal(test, request.Method, "POST")
require.Equal(test, request.Header.Get("X-Test"), "test-value")
require.Equal(test, request.Header.Get("Authorization"), "Basic dXNlcjpwYXNzd29yZA==")
require.Equal(test, request.Host, "status.local")
}))
defer testServer.Close()
check := NewNginxUpstreamCheck()
check.URL = fmt.Sprintf("%s/status", testServer.URL)
check.Headers["X-test"] = "test-value"
check.HostHeader = "status.local"
check.Username = "user"
check.Password = "password"
check.Method = "POST"
var accumulator testutil.Accumulator
checkError := check.Gather(&accumulator)
require.NoError(test, checkError)
}