From 0272df305fd636b6fd30005cbe9cff0eb51c2cf2 Mon Sep 17 00:00:00 2001 From: Gavin Williams Date: Wed, 16 Aug 2017 17:37:04 +0100 Subject: [PATCH] Increase test coverage --- .../rabbitmq_binding/rabbitmqadmin.rb | 9 +- spec/acceptance/queue_spec.rb | 4 + .../rabbitmq_binding/rabbitmqadmin_spec.rb | 142 ++++++++++++++++-- 3 files changed, 137 insertions(+), 18 deletions(-) diff --git a/lib/puppet/provider/rabbitmq_binding/rabbitmqadmin.rb b/lib/puppet/provider/rabbitmq_binding/rabbitmqadmin.rb index 927cbb0ee..6013a11c0 100644 --- a/lib/puppet/provider/rabbitmq_binding/rabbitmqadmin.rb +++ b/lib/puppet/provider/rabbitmq_binding/rabbitmqadmin.rb @@ -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 @@ -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 diff --git a/spec/acceptance/queue_spec.rb b/spec/acceptance/queue_spec.rb index abf946bbd..781c31a81 100644 --- a/spec/acceptance/queue_spec.rb +++ b/spec/acceptance/queue_spec.rb @@ -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', @@ -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', diff --git a/spec/unit/puppet/provider/rabbitmq_binding/rabbitmqadmin_spec.rb b/spec/unit/puppet/provider/rabbitmq_binding/rabbitmqadmin_spec.rb index e165d557c..519325962 100644 --- a/spec/unit/puppet/provider/rabbitmq_binding/rabbitmqadmin_spec.rb +++ b/spec/unit/puppet/provider/rabbitmq_binding/rabbitmqadmin_spec.rb @@ -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 @@ -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) @@ -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