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
|
require 'spec_helper'
describe Puppet::Type.type(:rabbitmq_policy).provider(:rabbitmqctl) do
let(:resource) do
Puppet::Type.type(:rabbitmq_policy).new(
name: 'ha-all@/',
pattern: '.*',
definition: {
'ha-mode' => 'all'
}
)
end
let(:provider) { described_class.new(resource) }
after do
described_class.instance_variable_set(:@policies, nil)
end
context 'has "@" in policy name' do
let(:resource) do
Puppet::Type.type(:rabbitmq_policy).new(
name: 'ha@home@/',
pattern: '.*',
definition: {
'ha-mode' => 'all'
},
provider: described_class.name
)
end
let(:provider) { described_class.new(resource) }
it do
expect(provider.should_policy).to eq('ha@home')
end
it do
expect(provider.should_vhost).to eq('/')
end
end
it 'fails with invalid output from list' do
provider.class.expects(:rabbitmqctl_list).with('policies', '-p', '/').returns 'foobar'
provider.class.expects(:rabbitmq_version).returns '3.1.5'
expect { provider.exists? }.to raise_error(Puppet::Error, %r{cannot parse line from list_policies})
end
context 'with RabbitMQ version >=3.7.0' do
it 'matches policies from list' do
provider.class.expects(:rabbitmq_version).returns '3.7.0'
provider.class.expects(:rabbitmqctl_list).with('policies', '-p', '/').returns <<-EOT
/ ha-all .* all {"ha-mode":"all","ha-sync-mode":"automatic"} 0
/ test .* exchanges {"ha-mode":"all"} 0
EOT
expect(provider.exists?).to eq(applyto: 'all',
pattern: '.*',
priority: '0',
definition: {
'ha-mode' => 'all',
'ha-sync-mode' => 'automatic'
})
end
end
context 'with RabbitMQ version >=3.2.0 and < 3.7.0' do
it 'matches policies from list' do
provider.class.expects(:rabbitmq_version).returns '3.6.9'
provider.class.expects(:rabbitmqctl_list).with('policies', '-p', '/').returns <<-EOT
/ ha-all all .* {"ha-mode":"all","ha-sync-mode":"automatic"} 0
/ test exchanges .* {"ha-mode":"all"} 0
EOT
expect(provider.exists?).to eq(applyto: 'all',
pattern: '.*',
priority: '0',
definition: {
'ha-mode' => 'all',
'ha-sync-mode' => 'automatic'
})
end
end
context 'with RabbitMQ version <3.2.0' do
it 'matches policies from list (<3.2.0)' do
provider.class.expects(:rabbitmq_version).returns '3.1.5'
provider.class.expects(:rabbitmqctl_list).with('policies', '-p', '/').returns <<-EOT
/ ha-all .* {"ha-mode":"all","ha-sync-mode":"automatic"} 0
/ test .* {"ha-mode":"all"} 0
EOT
expect(provider.exists?).to eq(applyto: 'all',
pattern: '.*',
priority: '0',
definition: {
'ha-mode' => 'all',
'ha-sync-mode' => 'automatic'
})
end
end
it 'does not match an empty list' do
provider.class.expects(:rabbitmqctl_list).with('policies', '-p', '/').returns ''
provider.class.expects(:rabbitmq_version).returns '3.1.5'
expect(provider.exists?).to eq(nil)
end
it 'destroys policy' do
provider.expects(:rabbitmqctl).with('clear_policy', '-p', '/', 'ha-all')
provider.destroy
end
it 'onlies call set_policy once (<3.2.0)' do
provider.class.expects(:rabbitmq_version).returns '3.1.0'
provider.resource[:priority] = '10'
provider.resource[:applyto] = 'exchanges'
provider.expects(:rabbitmqctl).with('set_policy',
'-p', '/',
'ha-all',
'.*',
'{"ha-mode":"all"}',
'10').once
provider.priority = '10'
provider.applyto = 'exchanges'
end
it 'onlies call set_policy once (>=3.2.0)' do
provider.class.expects(:rabbitmq_version).returns '3.2.0'
provider.resource[:priority] = '10'
provider.resource[:applyto] = 'exchanges'
provider.expects(:rabbitmqctl).with('set_policy',
'-p', '/',
'--priority', '10',
'--apply-to', 'exchanges',
'ha-all',
'.*',
'{"ha-mode":"all"}').once
provider.priority = '10'
provider.applyto = 'exchanges'
end
end
|