Skip to content

Commit 43ee029

Browse files
committed
MBean stanzas
1 parent faf3c6c commit 43ee029

4 files changed

Lines changed: 209 additions & 15 deletions

File tree

manifests/plugin/genericjmx/connection.pp

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# https://collectd.org/wiki/index.php/Plugin:GenericJMX
2+
define collectd::plugin::genericjmx::mbean (
3+
$object_name,
4+
$instance_prefix = undef,
5+
$instance_from = undef,
6+
$values,
7+
) {
8+
include collectd::plugin::genericjmx
9+
validate_array($values)
10+
11+
concat::fragment {
12+
"collectd_plugin_genericjmx_conf_${name}":
13+
order => '10',
14+
content => template('collectd/plugin/genericjmx/mbean.conf.erb'),
15+
target => $collectd::plugin::genericjmx::config_file;
16+
}
17+
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
require 'spec_helper'
2+
3+
describe 'collectd::plugin::genericjmx::mbean', :type => :define do
4+
5+
let (:facts) {{
6+
:osfamily => 'Debian',
7+
:concat_basedir => tmpfilename('collectd-genericjmx-mbean'),
8+
}}
9+
10+
let (:config_filename) { '/etc/collectd/conf.d/15-genericjmx.conf' }
11+
12+
let (:default_params) {{
13+
:object_name => 'bar',
14+
:values => [],
15+
}}
16+
17+
let (:title) { 'foo' }
18+
let (:concat_fragment_name) { 'collectd_plugin_genericjmx_conf_foo' }
19+
20+
# empty values array is technically not valid, but we'll test those cases later
21+
context 'defaults' do
22+
let (:params) { default_params }
23+
it 'provides an MBean stanza concat fragment' do
24+
should contain_concat__fragment(concat_fragment_name).with({
25+
:target => config_filename,
26+
:order => '10',
27+
})
28+
end
29+
30+
it { should contain_concat__fragment(concat_fragment_name).with_content(%r{<MBean "foo">\s+ObjectName "bar".+</MBean>}m) }
31+
it { should contain_concat__fragment(concat_fragment_name).without_content(/InstancePrefix/) }
32+
it { should contain_concat__fragment(concat_fragment_name).without_content(/InstanceFrom/) }
33+
end
34+
35+
context 'instance_prefix set' do
36+
let (:params) {
37+
default_params.merge({
38+
:instance_prefix => 'baz'
39+
})
40+
}
41+
42+
it { should contain_concat__fragment(concat_fragment_name).with_content(/InstancePrefix "baz"/) }
43+
end
44+
45+
context 'instance_from array' do
46+
let (:params) {
47+
default_params.merge({
48+
:instance_from => %w{ foo bar baz }
49+
})
50+
}
51+
52+
it { should contain_concat__fragment(concat_fragment_name).with_content(/InstanceFrom "foo"\s+InstanceFrom "bar"\s+InstanceFrom "baz"/) }
53+
end
54+
55+
context 'instance_from string' do
56+
let (:params) {
57+
default_params.merge({
58+
:instance_from => 'bat'
59+
})
60+
}
61+
62+
it { should contain_concat__fragment(concat_fragment_name).with_content(/InstanceFrom "bat"/) }
63+
it { should contain_concat__fragment(concat_fragment_name).without_content(/(.*InstanceFrom.*){2,}/) }
64+
end
65+
66+
let (:default_values_args) {{
67+
'type' => 'foo',
68+
'attribute' => 'bar'
69+
}}
70+
71+
72+
# testing the Value template section is going to be messy
73+
context 'value section defaults' do
74+
let (:params) {
75+
default_params.merge({
76+
:values => [default_values_args]
77+
})
78+
}
79+
80+
it 'should have a value stanza' do
81+
should contain_concat__fragment(concat_fragment_name).with_content(%r{<Value>.*</Value>}m)
82+
end
83+
84+
it 'should have only one value stanza' do
85+
should contain_concat__fragment(concat_fragment_name).without_content(%r{(.*<Value>.*){2,}})
86+
end
87+
88+
it { should contain_concat__fragment(concat_fragment_name).with_content(/Type "foo"/) }
89+
it { should contain_concat__fragment(concat_fragment_name).with_content(/Table false/) }
90+
it { should contain_concat__fragment(concat_fragment_name).with_content(/Attribute "bar"/) }
91+
it { should contain_concat__fragment(concat_fragment_name).without_content(/InstancePrefix/) }
92+
it { should contain_concat__fragment(concat_fragment_name).without_content(/InstanceFrom/) }
93+
end
94+
95+
context 'value section instance_prefix set' do
96+
let (:params) {
97+
default_params.merge({
98+
:values => [default_values_args.merge({
99+
'instance_prefix' => 'baz',
100+
})]
101+
})
102+
}
103+
104+
it { should contain_concat__fragment(concat_fragment_name).with_content(/InstancePrefix "baz"/) }
105+
it { should contain_concat__fragment(concat_fragment_name).without_content(/InstanceFrom/) }
106+
end
107+
108+
context 'value section instance_from array' do
109+
let (:params) {
110+
default_params.merge({
111+
:values => [default_values_args.merge({
112+
'instance_from' => %w{ alice bob carol }
113+
})]
114+
})
115+
}
116+
117+
it { should contain_concat__fragment(concat_fragment_name).with_content(/InstanceFrom "alice"/) }
118+
it { should contain_concat__fragment(concat_fragment_name).with_content(/InstanceFrom "bob"/) }
119+
it { should contain_concat__fragment(concat_fragment_name).with_content(/InstanceFrom "carol"/) }
120+
it { should contain_concat__fragment(concat_fragment_name).without_content(/InstancePrefix/) }
121+
end
122+
123+
context 'value section instance_from string' do
124+
let (:params) {
125+
default_params.merge({
126+
:values => [default_values_args.merge({
127+
'instance_from' => 'dave',
128+
})]
129+
})
130+
}
131+
132+
it { should contain_concat__fragment(concat_fragment_name).with_content(/InstanceFrom "dave"/) }
133+
it { should contain_concat__fragment(concat_fragment_name).without_content(/(.*InstancePrefix.*){2,}/) }
134+
it { should contain_concat__fragment(concat_fragment_name).without_content(/InstancePrefix/) }
135+
end
136+
137+
context 'value section table true-like' do
138+
['true', true].each do |truthy|
139+
let (:params) {
140+
default_params.merge({
141+
:values => [default_values_args.merge({
142+
'table' => truthy
143+
})]
144+
})
145+
}
146+
147+
it { should contain_concat__fragment(concat_fragment_name).with_content(/Table true/) }
148+
end
149+
end
150+
151+
context 'value section table false-like' do
152+
['false', false].each do |truthy|
153+
let (:params) {
154+
default_params.merge({
155+
:values => [default_values_args.merge({
156+
'table' => truthy
157+
})]
158+
})
159+
}
160+
161+
it { should contain_concat__fragment(concat_fragment_name).with_content(/Table false/) }
162+
end
163+
end
164+
165+
context 'multiple values' do
166+
let (:params) {
167+
default_params.merge({
168+
:values => [default_values_args,default_values_args]
169+
})
170+
}
171+
172+
it { should contain_concat__fragment(concat_fragment_name).with_content(%r{(.*<Value>.*</Value>.*){2}}m) }
173+
end
174+
175+
end
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
<MBean "<%= @name %>">
2-
ObjectName "<%= @object_name %>"
3-
<% if @instance_prefix -%>
2+
ObjectName "<%= @object_name %>"
3+
<% if @instance_prefix -%>
44
InstancePrefix "<%= @instance_prefix %>"
5-
<% end -%>
6-
<% if @instance_from -%>
7-
<% Array(@instance_from).each do |instance_from_item| -%>
8-
InstanceFrom "<%= instance_from_item %>"
95
<% end -%>
10-
<% end -%>
6+
<% if @instance_from -%>
7+
<% Array(@instance_from).each do |instance_from_item| -%>
8+
InstanceFrom "<%= instance_from_item %>"
9+
<% end -%>
10+
<% end -%>
1111

12-
<% @values.each do |value| -%>
12+
<% @values.each do |value| -%>
1313
<Value>
1414
Type "<%= value['type'] %>"
15-
<% if value['instance_prefix'] -%>
15+
<% if value['instance_prefix'] -%>
1616
InstancePrefix "<%= value['instance_prefix'] %>"
17-
<% end -%>
18-
<% if value['instance_from'] -%>
19-
<% Array(value['instance_from']).each do |instance_from_item| -%>
20-
InstanceFrom "<%= instance_from_item %>"
2117
<% end -%>
22-
<% end -%>
18+
<% if value['instance_from'] -%>
19+
<% Array(value['instance_from']).each do |instance_from_item| -%>
20+
InstanceFrom "<%= instance_from_item %>"
21+
<% end -%>
22+
<% end -%>
23+
Table <%= scope.function_str2bool([value['table'] || false ]) ? 'true' : 'false' %>
24+
Attribute "<%= value['attribute'] %>"
2325
</Value>
24-
<% end -%>
26+
<% end -%>
2527
</MBean>

0 commit comments

Comments
 (0)