|
| 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 |
0 commit comments