summaryrefslogtreecommitdiff
path: root/qpid/tools/src/ruby/qpid_management/spec/broker_spec.rb
blob: 6d6e1106a4f65a0a976dcb564a473b259aa9bf28 (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

require 'spec_helper'

describe Qpid::Management::Broker do
  before(:each) do
    @broker_port = `qpidd --no-data-dir --auth=no --no-module-dir --daemon --port 0`.chop
    @connection = Qpid::Messaging::Connection.new(url:"localhost:#{@broker_port}")
    @connection.open()
    @agent = Qpid::Management::BrokerAgent.new(@connection)
    @broker = @agent.broker
  end

  after(:each) do
    @agent.close()
    @connection.close()
    `qpidd --quit --port #{@broker_port}`
  end

  def setup_queue_route
    @other_port = `qpidd --no-data-dir --auth=no --no-module-dir --daemon --port 0`.chop
    @broker.add_link('link1', 'localhost', @other_port)
    @broker.add_queue('queue')
    @broker.add_queue_route('qr1',
                            link: 'link1',
                            queue: 'queue',
                            exchange: 'amq.direct',
                            sync: 2)
  end

  %w(connection session subscription exchange queue binding link bridge).each do |type|
    describe "##{type}s" do
      before(:each) do
        setup_queue_route if %w(link bridge).include?(type)
      end

      after(:each) do
        if %w(link bridge).include?(type)
          `qpidd --quit --port #{@other_port}`
        end
      end

      let(:collection) { @broker.send("#{type}s") }

      it "returns at least 1 #{type}" do
        if type == 'subscription'
          session = @connection.create_session
          receiver = session.create_receiver("amq.direct/temp")
        end
        collection.count.should be > 0
      end
    end

    describe "##{type}" do
      before(:each) do
        setup_queue_route if %w(link bridge).include?(type)
      end

      after(:each) do
        if %w(link bridge).include?(type)
          `qpidd --quit --port #{@other_port}`
        end
      end

      let(:object) { @broker.send("#{type}s")[0] }

      it "returns a #{type} by oid" do
        if type == 'subscription'
          session = @connection.create_session
          receiver = session.create_receiver("amq.direct/temp")
        end
        @broker.send(type, object.short_id).id.should == object.id
      end
    end
  end

  describe '#add_exchange' do
    %w(fanout direct topic headers).each do |type|
      context "when adding a #{type} exchange" do
        let(:exchange_name) { "#{type}1" }
        before(:each) do
          @before_creation = Time.now
          @broker.add_exchange(type, exchange_name, {'qpid.replicate' => 'none'})
        end

        subject { @broker.exchange(exchange_name) }
        its(:short_id) { should == exchange_name }
        its(:type) { should == type }
        its(:created_at) { should be > @before_creation }
        it 'has the correct arguments' do
          subject.arguments.should == {'qpid.replicate' => 'none'}
        end
      end
    end
  end

  describe "#delete_exchange" do
    before(:each) do
      @before_creation = Time.now
      @broker.add_exchange('fanout', 'fanout_to_delete')
    end

    let(:exchange) { @broker.exchange('fanout_to_delete') }

    context "with a valid exchange name" do
      it "deletes the exchange" do
        @broker.delete_exchange(exchange.short_id)
        expect { exchange.refresh! }.to raise_error
      end
    end

    context "with an invalid exchange name" do
      it "raises a not-found exception" do
        expect { @broker.delete_exchange("badname") }.to raise_error(/not-found.*badname/)
      end
    end
  end

  describe "#add_queue" do
    before(:each) do
      @before_creation = Time.now
      @queue_name = 'myqueue'
      @broker.add_queue(@queue_name, {'qpid.replicate' => 'none'})
    end

    subject { @broker.queue(@queue_name) }
    its(:short_id) { should == @queue_name }
    its(:created_at) { should be > @before_creation }
    it 'has the correct arguments' do
      subject.arguments.should == {'qpid.replicate' => 'none'}
    end
  end

  describe "#delete_queue" do
    before(:each) do
      @before_creation = Time.now
      @broker.add_queue('queue_to_delete')
    end

    let(:queue) { @broker.queue('queue_to_delete') }

    context "with a valid queue name" do
      it "deletes the queue" do
        @broker.delete_queue(queue.short_id)
        expect { queue.refresh! }.to raise_error
      end
    end

    context "with an invalid name" do
      it "raises a not-found exception" do
        expect { @broker.delete_queue("badname") }.to raise_error(/not-found.*badname/)
      end
    end
  end

  describe "#add_binding" do
    before(:each) do
      @broker.add_queue('queue')
    end

    it "creates a binding for a fanout exchange" do
      @broker.add_exchange('fanout', 'fanout')
      @broker.add_binding('fanout', 'queue')
      expect { @broker.binding('org.apache.qpid.broker:exchange:fanout,org.apache.qpid.broker:queue:queue,') }.to_not raise_error
    end

    it "creates a binding for a direct exchange" do
      @broker.add_exchange('direct', 'direct')
      @broker.add_binding('direct', 'queue', 'mykey')
      expect { @broker.binding('org.apache.qpid.broker:exchange:direct,org.apache.qpid.broker:queue:queue,mykey') }.to_not raise_error
    end

    it "creates a binding for a topic exchange" do
      @broker.add_exchange('topic', 'topic')
      @broker.add_binding('topic', 'queue', 'us.#')
      expect { @broker.binding('org.apache.qpid.broker:exchange:topic,org.apache.qpid.broker:queue:queue,us.#') }.to_not raise_error
    end
  end

  describe "#delete_binding" do
    it "deletes an existing binding" do
      @broker.add_queue('queue')
      @broker.add_exchange('fanout', 'fanout')
      @broker.add_binding('fanout', 'queue')
      expect { @broker.delete_binding('fanout', 'queue') }.to_not raise_error
    end
  end

  describe "#add_link" do
    before(:each) do
      @other_port = `/usr/sbin/qpidd --no-data-dir --auth=no --no-module-dir --daemon --port 0`.chop
    end

    after(:each) do
      `/usr/sbin/qpidd -q --port #{@other_port}`
    end

    it "adds a link" do
      @broker.add_link('link1', 'localhost', @other_port)
      @broker.links.count.should == 1
    end
  end

  describe "#delete_link" do
    before(:each) do
      @other_port = `/usr/sbin/qpidd --no-data-dir --auth=no --no-module-dir --daemon --port 0`.chop
      @broker.add_link('link1', 'localhost', @other_port)
    end

    after(:each) do
      `/usr/sbin/qpidd -q --port #{@other_port}`
    end

    it "deletes a link" do
      @broker.delete_link('link1')
      @broker.links.count.should == 0
    end
  end

  describe "#add_queue_route" do
    context "with missing options" do
      [:link, :queue, :exchange, :sync].each do |opt|
        opts = {link: 'l', queue: 'q', exchange: 'e', sync:2}
        opts.delete(opt)
        it "raises an error when :#{opt} is missing" do
          expect { @broker.add_queue_route('name', opts) }.to raise_error(/Option :#{opt} is required/)
        end
      end
    end

    context "with all required options" do
      before(:each) do
        @other_port = `/usr/sbin/qpidd --no-data-dir --auth=no --no-module-dir --daemon --port 0`.chop
        @broker.add_link('link1', 'localhost', @other_port)
        @broker.add_queue('queue')
        @broker.add_queue_route('qr1',
                                link: 'link1',
                                queue: 'queue',
                                exchange: 'amq.direct',
                                sync: 2)
      end

      after(:each) do
        `/usr/sbin/qpidd -q --port #{@other_port}`
      end

      it "adds a queue route" do
        @broker.bridges.count.should == 1
      end

      subject { @broker.bridges[0] }
      its(:dest) { should == 'amq.direct' }
      its(:durable) { should == false }
      its(:dynamic) { should == false }
      its(:excludes) { should == "" }
      its(:key) { should == "" }
      its(:name) { should == "qr1" }
      its(:src) { should == "queue" }
      its(:srcIsLocal) { should == false }
      its(:srcIsQueue) { should == true }
      its(:sync) { should == 2 }
      its(:tag) { should == "" }
    end
  end

  describe "#add_exchange_route" do
    context "with missing options" do
      [:link, :exchange, :key, :sync].each do |opt|
        opts = {link: 'l', exchange: 'e', key:'rk', sync:2}
        opts.delete(opt)
        it "raises an error when :#{opt} is missing" do
          expect { @broker.add_exchange_route('name', opts) }.to raise_error(/Option :#{opt} is required/)
        end
      end
    end

    context "with all required options" do
      before(:each) do
        @other_port = `/usr/sbin/qpidd --no-data-dir --auth=no --no-module-dir --daemon --port 0`.chop
        @broker.add_link('link1', 'localhost', @other_port)
        @broker.add_queue('queue')
        @broker.add_exchange_route('er1',
                                link: 'link1',
                                exchange: 'amq.direct',
                                key: 'foo',
                                sync: 2)
      end

      after(:each) do
        `/usr/sbin/qpidd -q --port #{@other_port}`
      end

      it "adds an exchange route" do
        @broker.bridges.count.should == 1
      end

      subject { @broker.bridges[0] }
      its(:dest) { should == 'amq.direct' }
      its(:durable) { should == false }
      its(:dynamic) { should == false }
      its(:excludes) { should == "" }
      its(:key) { should == "foo" }
      its(:name) { should == "er1" }
      its(:src) { should == "amq.direct" }
      its(:srcIsLocal) { should == false }
      its(:srcIsQueue) { should == false }
      its(:sync) { should == 2 }
      its(:tag) { should == "" }
    end
  end

  describe "#add_dynamic_route" do
    context "with missing options" do
      [:link, :exchange, :sync].each do |opt|
        opts = {link: 'l', exchange: 'e', sync:2}
        opts.delete(opt)
        it "raises an error when :#{opt} is missing" do
          expect { @broker.add_dynamic_route('name', opts) }.to raise_error(/Option :#{opt} is required/)
        end
      end
    end

    context "with all required options" do
      before(:each) do
        @other_port = `/usr/sbin/qpidd --no-data-dir --auth=no --no-module-dir --daemon --port 0`.chop
        @broker.add_link('link1', 'localhost', @other_port)
        @broker.add_queue('queue')
        @broker.add_dynamic_route('dr1',
                                link: 'link1',
                                exchange: 'amq.direct',
                                sync: 2)
      end

      after(:each) do
        `/usr/sbin/qpidd -q --port #{@other_port}`
      end

      it "adds an exchange route" do
        @broker.bridges.count.should == 1
      end

      subject { @broker.bridges[0] }
      its(:dest) { should == 'amq.direct' }
      its(:durable) { should == false }
      its(:dynamic) { should == true }
      its(:excludes) { should == "" }
      its(:key) { should == "" }
      its(:name) { should == "dr1" }
      its(:src) { should == "amq.direct" }
      its(:srcIsLocal) { should == false }
      its(:srcIsQueue) { should == false }
      its(:sync) { should == 2 }
      its(:tag) { should == "" }
    end
  end
end