Skip to content

Commit

Permalink
Prevent queueing things for a zone that doesn't exist in the region
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunne committed Sep 13, 2018
1 parent 9f3c401 commit 0d1d1b0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/models/miq_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def self.lower_priority?(p1, p2)
serialize :args, Array
serialize :miq_callback, Hash

validates :zone, :inclusion => {:in => proc { Zone.in_my_region.pluck(:name) }}, :allow_nil => true

STATE_READY = 'ready'.freeze
STATE_DEQUEUE = 'dequeue'.freeze
STATE_WARN = 'warn'.freeze
Expand Down
16 changes: 14 additions & 2 deletions spec/models/miq_queue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@
]

message_parms.each do |mparms|
msg = FactoryGirl.create(:miq_queue, mparms)
msg = FactoryGirl.build(:miq_queue, mparms)
expect(MiqQueue.format_short_log_msg(msg)).to eq("Message id: [#{msg.id}]")
expect(MiqQueue.format_full_log_msg(msg)).to eq("Message id: [#{msg.id}], #{msg.handler_type} id: [#{msg.handler_id}], Zone: [#{msg.zone}], Role: [#{msg.role}], Server: [#{msg.server_guid}], MiqTask id: [#{msg.miq_task_id}], Ident: [#{msg.queue_name}], Target id: [#{msg.target_id}], Instance id: [#{msg.instance_id}], Task id: [#{msg.task_id}], Command: [#{msg.class_name}.#{msg.method_name}], Timeout: [#{msg.msg_timeout}], Priority: [#{msg.priority}], State: [#{msg.state}], Deliver On: [#{msg.deliver_on}], Data: [#{msg.data.nil? ? "" : "#{msg.data.length} bytes"}], Args: #{args_cleaned_password.inspect}")
end
Expand Down Expand Up @@ -771,10 +771,11 @@ def self.some_method(single_arg)
end

it "should not unqueue a message from a different zone" do
zone = FactoryGirl.create(:zone)
MiqQueue.put(
:class_name => 'MyClass',
:method_name => 'method1',
:zone => 'other_zone'
:zone => zone.name
)

expect(MiqQueue.unqueue(
Expand Down Expand Up @@ -840,4 +841,15 @@ def self.some_method(single_arg)
expect(MiqQueue.where(:id => q.id).count).to eq(0)
end
end

context "validates that the zone exists in the current region" do
it "with a matching region" do
zone = FactoryGirl.create(:zone)
expect(MiqQueue.create!(:state => "ready", :zone => zone.name)).to be_kind_of(MiqQueue)
end

it "without a matching region" do
expect { MiqQueue.create!(:state => "ready", :zone => "Missing Zone") }.to raise_error(ActiveRecord::RecordInvalid)
end
end
end

0 comments on commit 0d1d1b0

Please sign in to comment.