Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate EmsRefresh.refresh queue args to data #107

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions db/migrate/20171025122732_move_ems_refresh_args_to_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class MoveEmsRefreshArgsToData < ActiveRecord::Migration[5.0]
class MiqQueue < ActiveRecord::Base
serialize :args, Array
end

def up
say_with_time('Move MiqQueue refresh args to data') do
MiqQueue.where(:class_name => 'EmsRefresh', :method_name => 'refresh').each do |queue_item|
begin
targets = queue_item.args.first
data = Marshal.dump(targets) unless targets.nil?

queue_item.update_attributes(:msg_data => data, :args => [])
rescue
# If Marshal.load fails we want to delete the queue item
queue_item.delete
end
end
end
end

def down
say_with_time('Move MiqQueue refresh data to args') do
MiqQueue.where(:class_name => 'EmsRefresh', :method_name => 'refresh').each do |queue_item|
begin
args = queue_item.msg_data && Marshal.load(queue_item.msg_data)

queue_item.update_attributes(:args => args, :msg_data => nil)
rescue
# If Marshal.load fails we want to delete the queue item
queue_item.delete
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require_migration

describe MoveEmsRefreshArgsToData do
let(:miq_queue_stub) { migration_stub(:MiqQueue) }
let(:targets) { [['Vm', 1], ['Host', 2]] }
let(:refresh_queue_options) do
{
:class_name => 'EmsRefresh',
:method_name => 'refresh',
:role => 'ems_inventory',
:queue_name => 'ems_1',
:zone => 'default',
}
end
let(:refresh_new_target_queue_options) do
{
:class_name => 'EmsRefresh',
:method_name => 'refresh_new_target',
:role => 'ems_inventory',
:queue_name => 'ems_1',
:zone => 'default',
}
end

migration_context :up do
it "Moves EmsRefresh.refresh queue args to data" do
q_item = miq_queue_stub.create!(refresh_queue_options.merge(:args => [targets]))

migrate

expect(Marshal.load(q_item.reload.msg_data)).to match_array(targets)
end

it "Leaves EmsRefresh.refresh args empty" do
q_item = miq_queue_stub.create!(refresh_queue_options.merge(:args => [targets]))

migrate

expect(q_item.reload.args).to match_array([])
end

it "Ignores unrelated queue items" do
args = [{:ems_ref => "vm-123"}]
q_item = miq_queue_stub.create!(refresh_new_target_queue_options.merge(:args => args))

migrate

expect(q_item.reload.args).to match_array(args)
end
end

migration_context :down do
it "Moves EmsRefresh.refresh data to args" do
q_item = miq_queue_stub.create!(refresh_queue_options.merge(:msg_data => Marshal.dump(targets)))

migrate

expect(q_item.reload.args).to match_array(targets)
end

it "If there are no args" do
q_item = miq_queue_stub.create!(refresh_queue_options.merge(:msg_data => nil))

migrate

expect(q_item.reload.args).to match_array([])
end

it "Handle invalid marshal format errors" do
# "\x03\x00\x00" is an empty array in Marshal version 3.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a version of this for the up migration?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure how to test Marshal.dump failing due to a version issue, ideas?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me try putting an anonymous class in the args, that should do it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scratch that, that won't even serializing the value to args

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see...it's only the load part that fails. 👍

q_item = miq_queue_stub.create!(refresh_queue_options.merge(:msg_data => "\x03\x00\x00"))

migrate

expect { q_item.reload }.to raise_exception(ActiveRecord::RecordNotFound)
end

it "Deletes invalid queue items but migrates the rest" do
# "\x03\x00\x00" is an empty array in Marshal version 3.0
miq_queue_stub.create!(refresh_queue_options.merge(:msg_data => "\x03\x00\x00"))
q_item = miq_queue_stub.create!(refresh_queue_options.merge(:msg_data => Marshal.dump(targets)))

migrate

expect(q_item.reload.args).to match_array(targets)
end
end
end