-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I was mildly annoyed that I couldn't cover the new events to be sent to rabbitmq, so I did a little research on how to test bunny. It's not so easy, but good enough to mock
- Loading branch information
Showing
7 changed files
with
130 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe RabbitmqBus, rabbitmq: '#' do | ||
it 'publishes messages' do | ||
RabbitmqBus.send_to_bus('metrics', 'hallo') | ||
expect_message('opensuse.obs.metrics', 'hallo') | ||
end | ||
|
||
context 'with exceptions' do | ||
before do | ||
allow_any_instance_of(BunnyMock::Queue).to receive(:publish).and_raise(Net::ReadTimeout) | ||
end | ||
|
||
it 'disconnects on errors' do | ||
RabbitmqBus.send_to_bus('metrics', 'hallo') | ||
expect_no_message | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe UpdateNotificationEvents, type: :model do | ||
let!(:admin) { create(:admin_user, login: 'Admin') } | ||
let(:notifications) do | ||
<<-RESPONSE | ||
<notifications next="3"> | ||
<notification type="SRCSRV_CREATE_PROJECT" time="1539445101"> | ||
<data key="project">project_1</data> | ||
<data key="sender">_nobody_</data> | ||
</notification> | ||
<notification type="SRCSRV_CREATE_PACKAGE" time="1539445101"> | ||
<data key="package">multibuild</data> | ||
<data key="project">project_1</data> | ||
<data key="sender">_nobody_</data> | ||
</notification> | ||
</notifications> | ||
RESPONSE | ||
end | ||
|
||
before do | ||
url = CONFIG['source_url'] + '/lastnotifications?block=1&start=1' | ||
stub_request(:get, url).and_return(body: notifications) | ||
end | ||
|
||
it 'fetches events' do | ||
UpdateNotificationEvents.new.perform | ||
expect(BackendInfo.lastnotification_nr).to eq(3) | ||
end | ||
|
||
context 'with configured amqp', rabbitmq: '#' do | ||
it 'sends events' do | ||
# not interested in setup messages | ||
empty_message_queue | ||
|
||
UpdateNotificationEvents.new.perform | ||
|
||
# SRCSRV_CREATE_PROJECT | ||
expect_message('opensuse.obs.project.create', '{"project":"project_1","sender":"_nobody_"}') | ||
expect_message('opensuse.obs.metrics', 'project.create,home=false value=1') | ||
|
||
# SRCSRV_CREATE_PACKAGE | ||
expect_message('opensuse.obs.package.create', '{"project":"project_1","package":"multibuild","sender":"_nobody_"}') | ||
expect_message('opensuse.obs.metrics', 'package.create,home=false value=1') | ||
|
||
expect_no_message | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,3 +80,6 @@ | |
|
||
# silence migration tests | ||
require 'support/migration' | ||
|
||
# support rabbitmq | ||
require 'support/rabbitmq' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module RabbitMQHelpers | ||
cattr_accessor :test_queue | ||
|
||
def empty_message_queue | ||
test_queue.purge | ||
end | ||
|
||
def expect_no_message | ||
expect(test_queue.message_count).to eq(0) | ||
end | ||
|
||
def expect_message(routing_key, message) | ||
expect(test_queue.all.shift).to include(message: message, options: { routing_key: routing_key, exchange: 'pubsub' }) | ||
end | ||
end | ||
|
||
RSpec.configure do |config| | ||
config.before do |example| | ||
if example.metadata[:rabbitmq] | ||
config.include RabbitMQHelpers | ||
# define config - not taken into account though due to mocking | ||
stub_const('CONFIG', CONFIG.merge('amqp_options' => { dummy: 1 }, 'amqp_exchange_name' => 'pubsub', 'amqp_exchange_options' => { type: :topic })) | ||
|
||
connection = BunnyMock.new | ||
RabbitmqBus.connection = connection | ||
# setup exchange | ||
RabbitmqBus.exchange = nil | ||
RabbitmqBus.wrapped_exchange | ||
|
||
RabbitMQHelpers.test_queue = connection.channel.queue('test') | ||
RabbitMQHelpers.test_queue.bind('pubsub', routing_key: example.metadata[:rabbitmq]) | ||
end | ||
end | ||
end |