From 9c4a302b9212ce027b730e3731aeeb17ac999540 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Luka=C5=A1=C3=ADk?= Date: Wed, 19 Apr 2017 11:02:36 +0200 Subject: [PATCH 1/3] Do not queue E-mails unless there is a notifier in the region --- app/mailers/generic_mailer.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/mailers/generic_mailer.rb b/app/mailers/generic_mailer.rb index 7d1d8942fc8..c52a4891e35 100644 --- a/app/mailers/generic_mailer.rb +++ b/app/mailers/generic_mailer.rb @@ -48,6 +48,7 @@ def self.deliver(method, options = {}) end def self.deliver_queue(method, options = {}) + return unless MiqRegion.my_region.role_assigned?('notifier') _log.info("starting: method: #{method} args: #{options} ") options[:attachment] &&= attachment_to_blob(options[:attachment]) MiqQueue.put( From f9ce5228879aa9a88890e39849c2418dc27139c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Luka=C5=A1=C3=ADk?= Date: Wed, 10 May 2017 14:24:23 +0200 Subject: [PATCH 2/3] Make sure the specs have notifier within the region --- spec/mailers/generic_mailer_spec.rb | 37 ++++++++++++++++++----------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/spec/mailers/generic_mailer_spec.rb b/spec/mailers/generic_mailer_spec.rb index efcdc87f33c..17178f21cb9 100644 --- a/spec/mailers/generic_mailer_spec.rb +++ b/spec/mailers/generic_mailer_spec.rb @@ -10,21 +10,30 @@ ActionMailer::Base.deliveries.clear end - it "call deliver_queue for generic_notification" do - @args[:attachment] = [{:content_type => "text/plain", :filename => "generic_mailer_test.txt", :body => "generic_notification with text/plain attachment" * 10}] - expect(BinaryBlob.count).to eq(0) - GenericMailer.deliver_queue(:generic_notification, @args) - expect(BinaryBlob.count).to eq(1) - expect(MiqQueue.exists?(:method_name => 'deliver', - :class_name => described_class.name, - :role => 'notifier')).to be_truthy - end + context 'with a notifier within a region' do + before do + MiqRegion.seed + ServerRole.seed + @miq_server.server_roles << ServerRole.where(:name => 'notifier') + @miq_server.save! + end + + it "call deliver_queue for generic_notification" do + @args[:attachment] = [{:content_type => "text/plain", :filename => "generic_mailer_test.txt", :body => "generic_notification with text/plain attachment" * 10}] + expect(BinaryBlob.count).to eq(0) + GenericMailer.deliver_queue(:generic_notification, @args) + expect(BinaryBlob.count).to eq(1) + expect(MiqQueue.exists?(:method_name => 'deliver', + :class_name => described_class.name, + :role => 'notifier')).to be_truthy + end - it "call deliver_queue for automation_notification" do - GenericMailer.deliver_queue(:automation_notification, @args) - expect(MiqQueue.exists?(:method_name => 'deliver', - :class_name => described_class.name, - :role => 'notifier')).to be_truthy + it "call deliver_queue for automation_notification" do + GenericMailer.deliver_queue(:automation_notification, @args) + expect(MiqQueue.exists?(:method_name => 'deliver', + :class_name => described_class.name, + :role => 'notifier')).to be_truthy + end end context "delivery error" do From fee656783b301d708bd894f9af6f803ca8ce8d12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Luka=C5=A1=C3=ADk?= Date: Wed, 10 May 2017 14:33:01 +0200 Subject: [PATCH 3/3] Specs for negative case -- when there is no notifier around --- spec/mailers/generic_mailer_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/mailers/generic_mailer_spec.rb b/spec/mailers/generic_mailer_spec.rb index 17178f21cb9..a1c22b05962 100644 --- a/spec/mailers/generic_mailer_spec.rb +++ b/spec/mailers/generic_mailer_spec.rb @@ -36,6 +36,15 @@ end end + context 'without a notifier within a region' do + before { MiqRegion.seed } + + it 'does not queue any mail notifications' do + @args[:attachment] = [{:content_type => "text/plain", :filename => "generic_mailer_test.txt", :body => "generic_notification with text/plain attachment" * 10}] + expect { GenericMailer.deliver_queue(:generic_notification, @args) }.not_to change { MiqQueue.count } + end + end + context "delivery error" do it "call attempts to send message to recipients indivually" do # generate message w/ two recipients that