forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
129 lines (105 loc) · 3.2 KB
/
client_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
package jolokia2
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/influxdata/telegraf/testutil"
)
func TestJolokia2_ClientAuthRequest(t *testing.T) {
var username string
var password string
var requests []map[string]interface{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, _ = r.BasicAuth()
body, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(body, &requests)
if err != nil {
t.Error(err)
}
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
plugin := setupPlugin(t, fmt.Sprintf(`
[jolokia2_agent]
urls = ["%s/jolokia"]
username = "sally"
password = "seashore"
[[jolokia2_agent.metric]]
name = "hello"
mbean = "hello:foo=bar"
`, server.URL))
var acc testutil.Accumulator
plugin.Gather(&acc)
if username != "sally" {
t.Errorf("Expected to post with username %s, but was %s", "sally", username)
}
if password != "seashore" {
t.Errorf("Expected to post with password %s, but was %s", "seashore", password)
}
if len(requests) == 0 {
t.Fatal("Expected to post a request body, but was empty.")
}
request := requests[0]
if expect := "hello:foo=bar"; request["mbean"] != expect {
t.Errorf("Expected to query mbean %s, but was %s", expect, request["mbean"])
}
}
func TestJolokia2_ClientProxyAuthRequest(t *testing.T) {
var requests []map[string]interface{}
var username string
var password string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, _ = r.BasicAuth()
body, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(body, &requests)
if err != nil {
t.Error(err)
}
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
plugin := setupPlugin(t, fmt.Sprintf(`
[jolokia2_proxy]
url = "%s/jolokia"
username = "sally"
password = "seashore"
[[jolokia2_proxy.target]]
url = "service:jmx:rmi:///jndi/rmi://target:9010/jmxrmi"
username = "jack"
password = "benimble"
[[jolokia2_proxy.metric]]
name = "hello"
mbean = "hello:foo=bar"
`, server.URL))
var acc testutil.Accumulator
plugin.Gather(&acc)
if username != "sally" {
t.Errorf("Expected to post with username %s, but was %s", "sally", username)
}
if password != "seashore" {
t.Errorf("Expected to post with password %s, but was %s", "seashore", password)
}
if len(requests) == 0 {
t.Fatal("Expected to post a request body, but was empty.")
}
request := requests[0]
if expect := "hello:foo=bar"; request["mbean"] != expect {
t.Errorf("Expected to query mbean %s, but was %s", expect, request["mbean"])
}
target, ok := request["target"].(map[string]interface{})
if !ok {
t.Fatal("Expected a proxy target, but was empty.")
}
if expect := "service:jmx:rmi:///jndi/rmi://target:9010/jmxrmi"; target["url"] != expect {
t.Errorf("Expected proxy target url %s, but was %s", expect, target["url"])
}
if expect := "jack"; target["user"] != expect {
t.Errorf("Expected proxy target username %s, but was %s", expect, target["user"])
}
if expect := "benimble"; target["password"] != expect {
t.Errorf("Expected proxy target password %s, but was %s", expect, target["password"])
}
}