File: rabbitmqadmin_spec.rb

package info (click to toggle)
puppet-module-puppetlabs-rabbitmq 8.5.0-10
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,192 kB
  • sloc: ruby: 5,227; sh: 10; makefile: 4
file content (187 lines) | stat: -rw-r--r-- 6,048 bytes parent folder | download | duplicates (4)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
require 'spec_helper'

provider_class = Puppet::Type.type(:rabbitmq_binding).provider(:rabbitmqadmin)
describe provider_class do
  let(:resource) do
    Puppet::Type::Rabbitmq_binding.new(
      name: 'source@target@/',
      destination_type: :queue,
      routing_key: 'blablub',
      arguments: {}
    )
  end
  let(:provider) { provider_class.new(resource) }

  # rubocop:disable RSpec/MultipleExpectations
  describe '#instances' do
    it 'returns instances' do
      provider_class.expects(:rabbitmqctl_list).with('vhosts').returns <<-EOT
/
EOT
      provider_class.expects(:rabbitmqctl_list).with(
        'bindings', '-p', '/', 'source_name', 'destination_name', 'destination_kind', 'routing_key', 'arguments'
      ).returns <<-EOT
exchange\tdst_queue\tqueue\t*\t[]
EOT
      instances = provider_class.instances
      expect(instances.size).to eq(1)
      expect(instances.map do |prov|
        {
          source: prov.get(:source),
          destination: prov.get(:destination),
          vhost: prov.get(:vhost),
          routing_key: prov.get(:routing_key)
        }
      end).to eq([
                   {
                     source: 'exchange',
                     destination: 'dst_queue',
                     vhost: '/',
                     routing_key: '*'
                   }
                 ])
    end
    # rubocop:enable RSpec/MultipleExpectations

    # rubocop:disable RSpec/MultipleExpectations
    it 'returns multiple instances' do
      provider_class.expects(:rabbitmqctl_list).with('vhosts').returns <<-EOT
/
EOT
      provider_class.expects(:rabbitmqctl_list).with(
        'bindings', '-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
      expect(instances.size).to eq(2)
      expect(instances.map do |prov|
        {
          source: prov.get(:source),
          destination: prov.get(:destination),
          vhost: prov.get(:vhost),
          routing_key: prov.get(:routing_key)
        }
      end).to eq([
                   {
                     source: 'exchange',
                     destination: 'dst_queue',
                     vhost: '/',
                     routing_key: 'routing_one'
                   },
                   {
                     source: 'exchange',
                     destination: 'dst_queue',
                     vhost: '/',
                     routing_key: 'routing_two'
                   }
                 ])
    end
  end
  # rubocop:enable RSpec/MultipleExpectations

  describe 'Test for prefetch error' do
    let(:resource) do
      Puppet::Type::Rabbitmq_binding.new(
        name: 'binding1',
        source: 'exchange1',
        destination: 'destqueue',
        destination_type: :queue,
        routing_key: 'blablubd',
        arguments: {}
      )
    end

    it 'exists' do
      provider_class.expects(:rabbitmqctl_list).with('vhosts').returns <<-EOT
/
EOT
      provider_class.expects(:rabbitmqctl_list).with(
        'bindings', '-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
      provider_class.expects(:rabbitmqctl_list).with('vhosts').returns <<-EOT
/
EOT
      provider_class.expects(:rabbitmqctl_list).with(
        'bindings', '-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

  describe '#create' do
    it 'calls rabbitmqadmin to create' do
      provider.expects(:rabbitmqadmin).with(
        'declare', 'binding', '--vhost=/', '--user=guest', '--password=guest', '-c', '/etc/rabbitmq/rabbitmqadmin.conf',
        'source=source', 'destination=target', 'arguments={}', 'routing_key=blablub', 'destination_type=queue'
      )
      provider.create
    end

    context 'specifying credentials' do
      let(:resource) do
        Puppet::Type::Rabbitmq_binding.new(
          name: 'source@test2@/',
          destination_type: :queue,
          routing_key: 'blablubd',
          arguments: {},
          user: 'colin',
          password: 'secret'
        )
      end
      let(:provider) { provider_class.new(resource) }

      it 'calls rabbitmqadmin to create' do
        provider.expects(:rabbitmqadmin).with(
          'declare', 'binding', '--vhost=/', '--user=colin', '--password=secret', '-c', '/etc/rabbitmq/rabbitmqadmin.conf',
          'source=source', 'destination=test2', 'arguments={}', 'routing_key=blablubd', 'destination_type=queue'
        )
        provider.create
      end
    end

    context 'new queue_bindings' do
      let(:resource) do
        Puppet::Type::Rabbitmq_binding.new(
          name: 'binding1',
          source: 'exchange1',
          destination: 'destqueue',
          destination_type: :queue,
          routing_key: 'blablubd',
          arguments: {}
        )
      end
      let(:provider) { provider_class.new(resource) }

      it 'calls 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

  describe '#destroy' do
    it 'calls rabbitmqadmin to destroy' do
      provider.expects(:rabbitmqadmin).with(
        'delete', 'binding', '--vhost=/', '--user=guest', '--password=guest', '-c', '/etc/rabbitmq/rabbitmqadmin.conf',
        'source=source', 'destination_type=queue', 'destination=target', 'properties_key=blablub'
      )
      provider.destroy
    end
  end
end