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 b799846
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 29 deletions.
28 changes: 28 additions & 0 deletions README.md
Expand Up @@ -595,6 +595,34 @@ rabbitmq_binding { 'myexchange@myqueue@myvhost':
}
```

```puppet
rabbitmq_binding { 'binding 1':
source => 'myexchange',
destination => 'myqueue',
vhost => 'myvhost',
user => 'dan',
password => 'bar',
destination_type => 'queue',
routing_key => 'key1',
arguments => {},
ensure => present,
}
rabbitmq_binding { 'binding 2':
source => 'myexchange',
destination => 'myqueue',
vhost => 'myvhost',
user => 'dan',
password => 'bar',
destination_type => 'queue',
routing_key => 'key2',
arguments => {},
ensure => present,
}
```


### rabbitmq\_user\_permissions

```puppet
Expand Down
15 changes: 10 additions & 5 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 @@ -52,7 +55,7 @@ def self.instances
unless(source_name.empty?)
binding = {
:source => source_name,
:dest => destination_name,
:destination => destination_name,
:vhost => vhost,
:destination_type => destination_type,
:routing_key => routing_key,
Expand All @@ -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[:destination] && binding.vhost == res[:vhost] && binding.routing_key == res[:routing_key] }
resources[name].provider = provider
end
end
Expand All @@ -94,7 +99,7 @@ def create
'-c',
'/etc/rabbitmq/rabbitmqadmin.conf',
"source=#{resource[:source]}",
"destination=#{resource[:dest]}",
"destination=#{resource[:destination]}",
"arguments=#{arguments.to_json}",
"routing_key=#{resource[:routing_key]}",
"destination_type=#{resource[:destination_type]}"
Expand All @@ -104,7 +109,7 @@ def create

def destroy
vhost_opt = should_vhost ? "--vhost=#{should_vhost}" : ''
rabbitmqadmin('delete', 'binding', vhost_opt, "--user=#{resource[:user]}", "--password=#{resource[:password]}", '-c', '/etc/rabbitmq/rabbitmqadmin.conf', "source=#{resource[:source]}", "destination_type=#{resource[:destination_type]}", "destination=#{resource[:dest]}", "properties_key=#{resource[:routing_key]}")
rabbitmqadmin('delete', 'binding', vhost_opt, "--user=#{resource[:user]}", "--password=#{resource[:password]}", '-c', '/etc/rabbitmq/rabbitmqadmin.conf', "source=#{resource[:source]}", "destination_type=#{resource[:destination_type]}", "destination=#{resource[:destination]}", "properties_key=#{resource[:routing_key]}")
@property_hash[:ensure] = :absent
end

Expand Down
14 changes: 7 additions & 7 deletions lib/puppet/type/rabbitmq_binding.rb
Expand Up @@ -12,7 +12,7 @@
end

# Match patterns without '@' as arbitrary names; match patterns with
# src@dst@vhost to their named params for backwards compatibility.
# src@destination@vhost to their named params for backwards compatibility.
def self.title_patterns
[
[
Expand All @@ -25,15 +25,15 @@ def self.title_patterns
/^(\S+)@(\S+)@(\S+)$/m,
[
[ :source ],
[ :dest ],
[ :destination],
[ :vhost ]
]
]
]
end

newparam(:name) do
desc 'resource name, either source@dest@vhost or arbitrary name with params'
desc 'resource name, either source@destination@vhost or arbitrary name with params'

isnamevar
end
Expand All @@ -45,7 +45,7 @@ def self.title_patterns
isnamevar
end

newparam(:dest, :namevar => true) do
newparam(:destination, :namevar => true) do
desc 'destination of binding'

newvalues(/^\S+$/)
Expand Down Expand Up @@ -113,7 +113,7 @@ def self.title_patterns
autorequire(:rabbitmq_user_permissions) do
[
"#{self[:user]}@#{self[:source]}",
"#{self[:user]}@#{self[:dest]}"
"#{self[:user]}@#{self[:destination]}"
]
end

Expand All @@ -122,11 +122,11 @@ def setup_autorequire(type)
if type == 'exchange'
rval = ["#{self[:source]}@#{self[:vhost]}"]
if destination_type == type
rval.push("#{self[:dest]}@#{self[:vhost]}")
rval.push("#{self[:destination]}@#{self[:vhost]}")
end
else
if destination_type == type
rval = ["#{self[:dest]}@#{self[:vhost]}"]
rval = ["#{self[:destination]}@#{self[:vhost]}"]
else
rval = []
end
Expand Down
6 changes: 5 additions & 1 deletion spec/acceptance/queue_spec.rb
Expand Up @@ -78,7 +78,7 @@ class { '::rabbitmq':
end


context "create multiple bindings when same source / dest / vhost but different routing keys" do
context "create multiple bindings when same source / destination / vhost but different routing keys" do
it 'should run successfully' do
pp = <<-EOS
if $::osfamily == 'RedHat' {
Expand Down Expand Up @@ -125,7 +125,9 @@ class { '::rabbitmq':
rabbitmq_binding { 'binding 1':
source => 'exchange1',
destination => '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',
destination => '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),
:destination => prov.get(:destination),
:vhost => prov.get(:vhost),
:routing_key => prov.get(:routing_key)
}
end.should == [
{
:source => 'exchange',
:destination => '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),
:destination => prov.get(:destination),
:vhost => prov.get(:vhost),
:routing_key => prov.get(:routing_key)
}
end.should == [
{
:source => 'exchange',
:destination => 'dst_queue',
:vhost => '/',
:routing_key => 'routing_one'
},
{
:source => 'exchange',
:destination => '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',
:destination => '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',
:destination => '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 b799846

Please sign in to comment.