Update 3rdparty rabbitmq module
[mirror/dsa-puppet.git] / 3rdparty / modules / rabbitmq / spec / unit / puppet / provider / rabbitmq_binding / rabbitmqadmin_spec.rb
1 require 'spec_helper'
2
3 provider_class = Puppet::Type.type(:rabbitmq_binding).provider(:rabbitmqadmin)
4 describe provider_class do
5   let(:resource) do
6     Puppet::Type::Rabbitmq_binding.new(
7       name: 'source@target@/',
8       destination_type: :queue,
9       routing_key: 'blablub',
10       arguments: {}
11     )
12   end
13   let(:provider) { provider_class.new(resource) }
14
15   # rubocop:disable RSpec/MultipleExpectations
16   describe '#instances' do
17     it 'returns instances' do
18       provider_class.expects(:rabbitmqctl).with('list_vhosts', '-q').returns <<-EOT
19 /
20 EOT
21       provider_class.expects(:rabbitmqctl).with(
22         'list_bindings', '-q', '-p', '/', 'source_name', 'destination_name', 'destination_kind', 'routing_key', 'arguments'
23       ).returns <<-EOT
24 exchange\tdst_queue\tqueue\t*\t[]
25 EOT
26       instances = provider_class.instances
27       expect(instances.size).to eq(1)
28       expect(instances.map do |prov|
29         {
30           source: prov.get(:source),
31           destination: prov.get(:destination),
32           vhost: prov.get(:vhost),
33           routing_key: prov.get(:routing_key)
34         }
35       end).to eq([
36                    {
37                      source: 'exchange',
38                      destination: 'dst_queue',
39                      vhost: '/',
40                      routing_key: '*'
41                    }
42                  ])
43     end
44     # rubocop:enable RSpec/MultipleExpectations
45
46     # rubocop:disable RSpec/MultipleExpectations
47     it 'returns multiple instances' do
48       provider_class.expects(:rabbitmqctl).with('list_vhosts', '-q').returns <<-EOT
49 /
50 EOT
51       provider_class.expects(:rabbitmqctl).with(
52         'list_bindings', '-q', '-p', '/', 'source_name', 'destination_name', 'destination_kind', 'routing_key', 'arguments'
53       ).returns <<-EOT
54 exchange\tdst_queue\tqueue\trouting_one\t[]
55 exchange\tdst_queue\tqueue\trouting_two\t[]
56 EOT
57       instances = provider_class.instances
58       expect(instances.size).to eq(2)
59       expect(instances.map do |prov|
60         {
61           source: prov.get(:source),
62           destination: prov.get(:destination),
63           vhost: prov.get(:vhost),
64           routing_key: prov.get(:routing_key)
65         }
66       end).to eq([
67                    {
68                      source: 'exchange',
69                      destination: 'dst_queue',
70                      vhost: '/',
71                      routing_key: 'routing_one'
72                    },
73                    {
74                      source: 'exchange',
75                      destination: 'dst_queue',
76                      vhost: '/',
77                      routing_key: 'routing_two'
78                    }
79                  ])
80     end
81   end
82   # rubocop:enable RSpec/MultipleExpectations
83
84   describe 'Test for prefetch error' do
85     let(:resource) do
86       Puppet::Type::Rabbitmq_binding.new(
87         name: 'binding1',
88         source: 'exchange1',
89         destination: 'destqueue',
90         destination_type: :queue,
91         routing_key: 'blablubd',
92         arguments: {}
93       )
94     end
95
96     it 'exists' do
97       provider_class.expects(:rabbitmqctl).with('list_vhosts', '-q').returns <<-EOT
98 /
99 EOT
100       provider_class.expects(:rabbitmqctl).with(
101         'list_bindings', '-q', '-p', '/', 'source_name', 'destination_name', 'destination_kind', 'routing_key', 'arguments'
102       ).returns <<-EOT
103 exchange\tdst_queue\tqueue\t*\t[]
104 EOT
105
106       provider_class.prefetch({})
107     end
108
109     it 'matches' do
110       # Test resource to match against
111       provider_class.expects(:rabbitmqctl).with('list_vhosts', '-q').returns <<-EOT
112 /
113 EOT
114       provider_class.expects(:rabbitmqctl).with(
115         'list_bindings', '-q', '-p', '/', 'source_name', 'destination_name', 'destination_kind', 'routing_key', 'arguments'
116       ).returns <<-EOT
117 exchange\tdst_queue\tqueue\t*\t[]
118 EOT
119
120       provider_class.prefetch('binding1' => resource)
121     end
122   end
123
124   describe '#create' do
125     it 'calls rabbitmqadmin to create' do
126       provider.expects(:rabbitmqadmin).with(
127         'declare', 'binding', '--vhost=/', '--user=guest', '--password=guest', '-c', '/etc/rabbitmq/rabbitmqadmin.conf',
128         'source=source', 'destination=target', 'arguments={}', 'routing_key=blablub', 'destination_type=queue'
129       )
130       provider.create
131     end
132
133     context 'specifying credentials' do
134       let(:resource) do
135         Puppet::Type::Rabbitmq_binding.new(
136           name: 'source@test2@/',
137           destination_type: :queue,
138           routing_key: 'blablubd',
139           arguments: {},
140           user: 'colin',
141           password: 'secret'
142         )
143       end
144       let(:provider) { provider_class.new(resource) }
145
146       it 'calls rabbitmqadmin to create' do
147         provider.expects(:rabbitmqadmin).with(
148           'declare', 'binding', '--vhost=/', '--user=colin', '--password=secret', '-c', '/etc/rabbitmq/rabbitmqadmin.conf',
149           'source=source', 'destination=test2', 'arguments={}', 'routing_key=blablubd', 'destination_type=queue'
150         )
151         provider.create
152       end
153     end
154
155     context 'new queue_bindings' do
156       let(:resource) do
157         Puppet::Type::Rabbitmq_binding.new(
158           name: 'binding1',
159           source: 'exchange1',
160           destination: 'destqueue',
161           destination_type: :queue,
162           routing_key: 'blablubd',
163           arguments: {}
164         )
165       end
166       let(:provider) { provider_class.new(resource) }
167
168       it 'calls rabbitmqadmin to create' do
169         provider.expects(:rabbitmqadmin).with(
170           'declare', 'binding', '--vhost=/', '--user=guest', '--password=guest', '-c', '/etc/rabbitmq/rabbitmqadmin.conf',
171           'source=exchange1', 'destination=destqueue', 'arguments={}', 'routing_key=blablubd', 'destination_type=queue'
172         )
173         provider.create
174       end
175     end
176   end
177
178   describe '#destroy' do
179     it 'calls rabbitmqadmin to destroy' do
180       provider.expects(:rabbitmqadmin).with(
181         'delete', 'binding', '--vhost=/', '--user=guest', '--password=guest', '-c', '/etc/rabbitmq/rabbitmqadmin.conf',
182         'source=source', 'destination_type=queue', 'destination=target', 'properties_key=blablub'
183       )
184       provider.destroy
185     end
186   end
187 end