Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fatmcgav authored and William Yardley committed Aug 16, 2017
1 parent 8b5cbcc commit 0272df3
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 18 deletions.
9 changes: 7 additions & 2 deletions lib/puppet/provider/rabbitmq_binding/rabbitmqadmin.rb
Expand Up @@ -15,6 +15,9 @@
end
defaultfor :feature => :posix

# Without this, the composite namevar stuff doesn't work properly.
mk_resource_methods

def should_vhost
if @should_vhost
@should_vhost
Expand Down Expand Up @@ -67,10 +70,12 @@ def self.instances
resources
end

# see
# https://github.com/puppetlabs/puppetlabs-netapp/blob/d0a655665463c69c932f835ba8756be32417a4e9/lib/puppet/provider/netapp_qtree/sevenmode.rb#L66-L73
def self.prefetch(resources)
bindings = instances
resources.keys.each do |name|
if provider = bindings.find{ |route| route.source == source && route.dest == dest && route.vhost == vhost && route.routing_key == routing_key }
resources.each do |name, res|
if provider = bindings.find{ |binding| binding.source == res[:source] && binding.dest == res[:dest] && binding.vhost == res[:vhost] && binding.routing_key == res[:routing_key] }
resources[name].provider = provider
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/acceptance/queue_spec.rb
Expand Up @@ -125,7 +125,9 @@ class { '::rabbitmq':
rabbitmq_binding { 'binding 1':
source => 'exchange1',
dest => 'queue1',
user => 'dan',
vhost => 'host1',
password => 'bar',
destination_type => 'queue',
routing_key => 'test1',
Expand All @@ -134,7 +136,9 @@ class { '::rabbitmq':
rabbitmq_binding { 'binding 2':
source => 'exchange1',
dest => 'queue1',
user => 'dan',
vhost => 'host1',
password => 'bar',
destination_type => 'queue',
routing_key => 'test2',
Expand Down
142 changes: 126 additions & 16 deletions spec/unit/puppet/provider/rabbitmq_binding/rabbitmqadmin_spec.rb
Expand Up @@ -7,24 +7,111 @@
describe provider_class do
before :each do
@resource = Puppet::Type::Rabbitmq_binding.new(
{:name => 'source@target@/',
:destination_type => :queue,
:routing_key => 'blablub',
:arguments => {}
{
:name => 'source@target@/',
:destination_type => :queue,
:routing_key => 'blablub',
:arguments => {}
}
)
@provider = provider_class.new(@resource)
end

it 'should return instances' do
provider_class.expects(:rabbitmqctl).with('list_vhosts', '-q').returns <<-EOT
describe "#instances" do
it 'should return instances' do
provider_class.expects(:rabbitmqctl).with('list_vhosts', '-q').returns <<-EOT
/
EOT
provider_class.expects(:rabbitmqctl).with('list_bindings', '-q', '-p', '/', 'source_name', 'destination_name', 'destination_kind', 'routing_key', 'arguments').returns <<-EOT
queue queue queue []
provider_class.expects(:rabbitmqctl).with('list_bindings', '-q', '-p', '/', 'source_name', 'destination_name', 'destination_kind', 'routing_key', 'arguments').returns <<-EOT
exchange\tdst_queue\tqueue\t*\t[]
EOT
instances = provider_class.instances
instances.size.should == 1
instances = provider_class.instances
instances.size.should == 1
instances.map do |prov|
{
:source => prov.get(:source),
:dest => prov.get(:dest),
:vhost => prov.get(:vhost),
:routing_key => prov.get(:routing_key)
}
end.should == [
{
:source => 'exchange',
:dest => 'dst_queue',
:vhost => '/',
:routing_key => '*'
}
]
end

it 'should return multiple instances' do
provider_class.expects(:rabbitmqctl).with('list_vhosts', '-q').returns <<-EOT
/
EOT
provider_class.expects(:rabbitmqctl).with('list_bindings', '-q', '-p', '/', 'source_name', 'destination_name', 'destination_kind', 'routing_key', 'arguments').returns <<-EOT
exchange\tdst_queue\tqueue\trouting_one\t[]
exchange\tdst_queue\tqueue\trouting_two\t[]
EOT
instances = provider_class.instances
instances.size.should == 2
instances.map do |prov|
{
:source => prov.get(:source),
:dest => prov.get(:dest),
:vhost => prov.get(:vhost),
:routing_key => prov.get(:routing_key)
}
end.should == [
{
:source => 'exchange',
:dest => 'dst_queue',
:vhost => '/',
:routing_key => 'routing_one'
},
{
:source => 'exchange',
:dest => 'dst_queue',
:vhost => '/',
:routing_key => 'routing_two'
}
]
end
end

describe "Test for prefetch error" do
it "exists" do
provider_class.expects(:rabbitmqctl).with('list_vhosts', '-q').returns <<-EOT
/
EOT
provider_class.expects(:rabbitmqctl).with('list_bindings', '-q', '-p', '/', 'source_name', 'destination_name', 'destination_kind', 'routing_key', 'arguments').returns <<-EOT
exchange\tdst_queue\tqueue\t*\t[]
EOT

provider_class.prefetch({})
end

it "matches" do
# Test resource to match against
@resource = Puppet::Type::Rabbitmq_binding.new(
{
:name => 'binding1',
:source => 'exchange1',
:dest => 'destqueue',
:destination_type => :queue,
:routing_key => 'blablubd',
:arguments => {}
}
)

provider_class.expects(:rabbitmqctl).with('list_vhosts', '-q').returns <<-EOT
/
EOT
provider_class.expects(:rabbitmqctl).with('list_bindings', '-q', '-p', '/', 'source_name', 'destination_name', 'destination_kind', 'routing_key', 'arguments').returns <<-EOT
exchange\tdst_queue\tqueue\t*\t[]
EOT

provider_class.prefetch({ "binding1" => @resource})
end
end

it 'should call rabbitmqadmin to create' do
Expand All @@ -40,12 +127,13 @@
context 'specifying credentials' do
before :each do
@resource = Puppet::Type::Rabbitmq_binding.new(
{:name => 'source@test2@/',
:destination_type => :queue,
:routing_key => 'blablubd',
:arguments => {},
:user => 'colin',
:password => 'secret'
{
:name => 'source@test2@/',
:destination_type => :queue,
:routing_key => 'blablubd',
:arguments => {},
:user => 'colin',
:password => 'secret'
}
)
@provider = provider_class.new(@resource)
Expand All @@ -56,4 +144,26 @@
@provider.create
end
end

context 'new queue_bindings' do
before :each do
@resource = Puppet::Type::Rabbitmq_binding.new(
{
:name => 'binding1',
:source => 'exchange1',
:dest => 'destqueue',
:destination_type => :queue,
:routing_key => 'blablubd',
:arguments => {}
}
)
@provider = provider_class.new(@resource)
end

it 'should call rabbitmqadmin to create' do
@provider.expects(:rabbitmqadmin).with('declare', 'binding', '--vhost=/', '--user=guest', '--password=guest', '-c', '/etc/rabbitmq/rabbitmqadmin.conf', 'source=exchange1', 'destination=destqueue', 'arguments={}', 'routing_key=blablubd', 'destination_type=queue')
@provider.create
end
end

end

0 comments on commit 0272df3

Please sign in to comment.