forked from SumoLogic/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activemq_test.go
180 lines (143 loc) · 4.47 KB
/
activemq_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
package activemq
import (
"encoding/xml"
"net/http"
"net/http/httptest"
"testing"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)
func TestGatherQueuesMetrics(t *testing.T) {
s := `<queues>
<queue name="sandra">
<stats size="0" consumerCount="0" enqueueCount="0" dequeueCount="0"/>
<feed>
<atom>queueBrowse/sandra?view=rss&feedType=atom_1.0</atom>
<rss>queueBrowse/sandra?view=rss&feedType=rss_2.0</rss>
</feed>
</queue>
<queue name="Test">
<stats size="0" consumerCount="0" enqueueCount="0" dequeueCount="0"/>
<feed>
<atom>queueBrowse/Test?view=rss&feedType=atom_1.0</atom>
<rss>queueBrowse/Test?view=rss&feedType=rss_2.0</rss>
</feed>
</queue>
</queues>`
queues := Queues{}
xml.Unmarshal([]byte(s), &queues)
records := make(map[string]interface{})
tags := make(map[string]string)
tags["name"] = "Test"
tags["source"] = "localhost"
tags["port"] = "8161"
records["size"] = 0
records["consumer_count"] = 0
records["enqueue_count"] = 0
records["dequeue_count"] = 0
var acc testutil.Accumulator
activeMQ := new(ActiveMQ)
activeMQ.Server = "localhost"
activeMQ.Port = 8161
activeMQ.Init()
activeMQ.GatherQueuesMetrics(&acc, queues)
acc.AssertContainsTaggedFields(t, "activemq_queues", records, tags)
}
func TestGatherTopicsMetrics(t *testing.T) {
s := `<topics>
<topic name="ActiveMQ.Advisory.MasterBroker ">
<stats size="0" consumerCount="0" enqueueCount="1" dequeueCount="0"/>
</topic>
<topic name="AAA ">
<stats size="0" consumerCount="1" enqueueCount="0" dequeueCount="0"/>
</topic>
<topic name="ActiveMQ.Advisory.Topic ">
<stats size="0" consumerCount="0" enqueueCount="1" dequeueCount="0"/>
</topic>
<topic name="ActiveMQ.Advisory.Queue ">
<stats size="0" consumerCount="0" enqueueCount="2" dequeueCount="0"/>
</topic>
<topic name="AAAA ">
<stats size="0" consumerCount="0" enqueueCount="0" dequeueCount="0"/>
</topic>
</topics>`
topics := Topics{}
xml.Unmarshal([]byte(s), &topics)
records := make(map[string]interface{})
tags := make(map[string]string)
tags["name"] = "ActiveMQ.Advisory.MasterBroker "
tags["source"] = "localhost"
tags["port"] = "8161"
records["size"] = 0
records["consumer_count"] = 0
records["enqueue_count"] = 1
records["dequeue_count"] = 0
var acc testutil.Accumulator
activeMQ := new(ActiveMQ)
activeMQ.Server = "localhost"
activeMQ.Port = 8161
activeMQ.Init()
activeMQ.GatherTopicsMetrics(&acc, topics)
acc.AssertContainsTaggedFields(t, "activemq_topics", records, tags)
}
func TestGatherSubscribersMetrics(t *testing.T) {
s := `<subscribers>
<subscriber clientId="AAA" subscriptionName="AAA" connectionId="NOTSET" destinationName="AAA" selector="AA" active="no">
<stats pendingQueueSize="0" dispatchedQueueSize="0" dispatchedCounter="0" enqueueCounter="0" dequeueCounter="0"/>
</subscriber>
</subscribers>`
subscribers := Subscribers{}
xml.Unmarshal([]byte(s), &subscribers)
records := make(map[string]interface{})
tags := make(map[string]string)
tags["client_id"] = "AAA"
tags["subscription_name"] = "AAA"
tags["connection_id"] = "NOTSET"
tags["destination_name"] = "AAA"
tags["selector"] = "AA"
tags["active"] = "no"
tags["source"] = "localhost"
tags["port"] = "8161"
records["pending_queue_size"] = 0
records["dispatched_queue_size"] = 0
records["dispatched_counter"] = 0
records["enqueue_counter"] = 0
records["dequeue_counter"] = 0
var acc testutil.Accumulator
activeMQ := new(ActiveMQ)
activeMQ.Server = "localhost"
activeMQ.Port = 8161
activeMQ.Init()
activeMQ.GatherSubscribersMetrics(&acc, subscribers)
acc.AssertContainsTaggedFields(t, "activemq_subscribers", records, tags)
}
func TestURLs(t *testing.T) {
ts := httptest.NewServer(http.NotFoundHandler())
defer ts.Close()
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/admin/xml/queues.jsp":
w.WriteHeader(http.StatusOK)
w.Write([]byte("<queues></queues>"))
case "/admin/xml/topics.jsp":
w.WriteHeader(http.StatusOK)
w.Write([]byte("<topics></topics>"))
case "/admin/xml/subscribers.jsp":
w.WriteHeader(http.StatusOK)
w.Write([]byte("<subscribers></subscribers>"))
default:
w.WriteHeader(http.StatusNotFound)
t.Fatalf("unexpected path: " + r.URL.Path)
}
})
plugin := ActiveMQ{
URL: "http://" + ts.Listener.Addr().String(),
Webadmin: "admin",
}
err := plugin.Init()
require.NoError(t, err)
var acc testutil.Accumulator
err = plugin.Gather(&acc)
require.NoError(t, err)
require.Len(t, acc.GetTelegrafMetrics(), 0)
}