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

[V2V] Remove removing_snapshots state #19374

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
44 changes: 1 addition & 43 deletions app/models/infra_conversion_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ def load_transitions
{
:initializing => {'initialize' => 'waiting_to_start'},
:start => {'waiting_to_start' => 'started'},
:remove_snapshots => {'started' => 'removing_snapshots'},
:poll_remove_snapshots_complete => {'removing_snapshots' => 'removing_snapshots'},
:wait_for_ip_address => {
'removing_snapshots' => 'waiting_for_ip_address',
'started' => 'waiting_for_ip_address',
'powering_on_vm' => 'waiting_for_ip_address',
'waiting_for_ip_address' => 'waiting_for_ip_address'
},
Expand Down Expand Up @@ -77,11 +75,6 @@ def load_transitions
# }
def state_settings
@state_settings ||= {
:removing_snapshots => {
:description => 'Remove snapshosts',
:weight => 5,
:max_retries => 4.hours / state_retry_interval
},
:waiting_for_ip_address => {
:description => 'Waiting for VM IP address',
:weight => 1,
Expand Down Expand Up @@ -272,42 +265,7 @@ def apply_right_sizing_memory(mode)
def start
migration_task.update!(:state => 'migrate')
migration_task.update_options(:migration_phase => 'pre')
queue_signal(:remove_snapshots)
end

def remove_snapshots
update_migration_task_progress(:on_entry)
if migration_task.source.supports_remove_all_snapshots?
context[:async_task_id_removing_snapshots] = migration_task.source.remove_all_snapshots_queue(migration_task.userid.to_i)
update_migration_task_progress(:on_exit)
return queue_signal(:poll_remove_snapshots_complete, :deliver_on => Time.now.utc + state_retry_interval)
end

update_migration_task_progress(:on_exit)
queue_signal(:wait_for_ip_address)
rescue StandardError => error
update_migration_task_progress(:on_error)
abort_conversion(error.message, 'error')
end

def poll_remove_snapshots_complete
update_migration_task_progress(:on_entry)
raise 'Removing snapshots timed out' if polling_timeout

async_task = MiqTask.find(context[:async_task_id_removing_snapshots])

if async_task.state == MiqTask::STATE_FINISHED
raise async_task.message unless async_task.status == MiqTask::STATUS_OK

update_migration_task_progress(:on_exit)
return queue_signal(:wait_for_ip_address)
end

update_migration_task_progress(:on_retry)
queue_signal(:poll_remove_snapshots_complete, :deliver_on => Time.now.utc + state_retry_interval)
rescue StandardError => error
update_migration_task_progress(:on_error)
abort_conversion(error.message, 'error')
end

def wait_for_ip_address
Expand Down
142 changes: 4 additions & 138 deletions spec/models/infra_conversion_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@
end

context 'state transitions' do
%w[start remove_snapshots poll_remove_snapshots_complete wait_for_ip_address run_migration_playbook poll_run_migration_playbook_complete shutdown_vm poll_shutdown_vm_complete transform_vm poll_transform_vm_complete poll_inventory_refresh_complete apply_right_sizing restore_vm_attributes power_on_vm poll_power_on_vm_complete mark_vm_migrated abort_virtv2v poll_automate_state_machine finish abort_job cancel error].each do |signal|
%w[start wait_for_ip_address run_migration_playbook poll_run_migration_playbook_complete shutdown_vm poll_shutdown_vm_complete transform_vm poll_transform_vm_complete poll_inventory_refresh_complete apply_right_sizing restore_vm_attributes power_on_vm poll_power_on_vm_complete mark_vm_migrated abort_virtv2v poll_automate_state_machine finish abort_job cancel error].each do |signal|
shared_examples_for "allows #{signal} signal" do
it signal.to_s do
expect(job).to receive(signal.to_sym)
Expand All @@ -364,7 +364,7 @@
end
end

%w[start remove_snapshots poll_remove_snapshots_complete wait_for_ip_address run_migration_playbook poll_run_migration_playbook_complete shutdown_vm poll_shutdown_vm_complete transform_vm poll_transform_vm_complete poll_inventory_refresh_complete apply_right_sizing restore_vm_attributes power_on_vm poll_power_on_vm_complete mark_vm_migrated abort_virtv2v poll_automate_state_machine].each do |signal|
%w[start wait_for_ip_address run_migration_playbook poll_run_migration_playbook_complete shutdown_vm poll_shutdown_vm_complete transform_vm poll_transform_vm_complete poll_inventory_refresh_complete apply_right_sizing restore_vm_attributes power_on_vm poll_power_on_vm_complete mark_vm_migrated abort_virtv2v poll_automate_state_machine].each do |signal|
shared_examples_for "doesn't allow #{signal} signal" do
it signal.to_s do
expect { job.signal(signal.to_sym) }.to raise_error(RuntimeError, /#{signal} is not permitted at state #{job.state}/)
Expand All @@ -383,8 +383,6 @@
it_behaves_like 'allows cancel signal'
it_behaves_like 'allows error signal'

it_behaves_like 'doesn\'t allow remove_snapshots signal'
it_behaves_like 'doesn\'t allow poll_remove_snapshots_complete signal'
it_behaves_like 'doesn\'t allow wait_for_ip_address signal'
it_behaves_like 'doesn\'t allow run_migration_playbook signal'
it_behaves_like 'doesn\'t allow poll_run_migration_playbook_complete signal'
Expand All @@ -406,44 +404,13 @@
job.state = 'started'
end

it_behaves_like 'allows remove_snapshots signal'
it_behaves_like 'allows finish signal'
it_behaves_like 'allows abort_job signal'
it_behaves_like 'allows cancel signal'
it_behaves_like 'allows error signal'

it_behaves_like 'doesn\'t allow start signal'
it_behaves_like 'doesn\'t allow poll_remove_snapshots_complete signal'
it_behaves_like 'doesn\'t allow wait_for_ip_address signal'
it_behaves_like 'doesn\'t allow run_migration_playbook signal'
it_behaves_like 'doesn\'t allow poll_run_migration_playbook_complete signal'
it_behaves_like 'doesn\'t allow shutdown_vm signal'
it_behaves_like 'doesn\'t allow poll_shutdown_vm_complete signal'
it_behaves_like 'doesn\'t allow transform_vm signal'
it_behaves_like 'doesn\'t allow poll_transform_vm_complete signal'
it_behaves_like 'doesn\'t allow poll_inventory_refresh_complete signal'
it_behaves_like 'doesn\'t allow apply_right_sizing signal'
it_behaves_like 'doesn\'t allow restore_vm_attributes signal'
it_behaves_like 'doesn\'t allow power_on_vm signal'
it_behaves_like 'doesn\'t allow poll_power_on_vm_complete signal'
it_behaves_like 'doesn\'t allow mark_vm_migrated signal'
it_behaves_like 'doesn\'t allow poll_automate_state_machine signal'
end

context 'removing_snapshots' do
before do
job.state = 'removing_snapshots'
end

it_behaves_like 'allows poll_remove_snapshots_complete signal'
it_behaves_like 'allows wait_for_ip_address signal'
it_behaves_like 'allows finish signal'
it_behaves_like 'allows abort_job signal'
it_behaves_like 'allows cancel signal'
it_behaves_like 'allows error signal'

it_behaves_like 'doesn\'t allow start signal'
it_behaves_like 'doesn\'t allow remove_snapshots signal'
it_behaves_like 'doesn\'t allow run_migration_playbook signal'
it_behaves_like 'doesn\'t allow poll_run_migration_playbook_complete signal'
it_behaves_like 'doesn\'t allow shutdown_vm signal'
Expand Down Expand Up @@ -472,8 +439,6 @@
it_behaves_like 'allows error signal'

it_behaves_like 'doesn\'t allow start signal'
it_behaves_like 'doesn\'t allow remove_snapshots signal'
it_behaves_like 'doesn\'t allow poll_remove_snapshots_complete signal'
it_behaves_like 'doesn\'t allow poll_run_migration_playbook_complete signal'
it_behaves_like 'doesn\'t allow shutdown_vm signal'
it_behaves_like 'doesn\'t allow poll_shutdown_vm_complete signal'
Expand Down Expand Up @@ -502,8 +467,6 @@
it_behaves_like 'allows error signal'

it_behaves_like 'doesn\'t allow start signal'
it_behaves_like 'doesn\'t allow remove_snapshots signal'
it_behaves_like 'doesn\'t allow poll_remove_snapshots_complete signal'
it_behaves_like 'doesn\'t allow wait_for_ip_address signal'
it_behaves_like 'doesn\'t allow run_migration_playbook signal'
it_behaves_like 'doesn\'t allow poll_shutdown_vm_complete signal'
Expand All @@ -529,8 +492,6 @@
it_behaves_like 'allows error signal'

it_behaves_like 'doesn\'t allow start signal'
it_behaves_like 'doesn\'t allow remove_snapshots signal'
it_behaves_like 'doesn\'t allow poll_remove_snapshots_complete signal'
it_behaves_like 'doesn\'t allow wait_for_ip_address signal'
it_behaves_like 'doesn\'t allow run_migration_playbook signal'
it_behaves_like 'doesn\'t allow poll_run_migration_playbook_complete signal'
Expand Down Expand Up @@ -558,8 +519,6 @@
it_behaves_like 'allows error signal'

it_behaves_like 'doesn\'t allow start signal'
it_behaves_like 'doesn\'t allow remove_snapshots signal'
it_behaves_like 'doesn\'t allow poll_remove_snapshots_complete signal'
it_behaves_like 'doesn\'t allow wait_for_ip_address signal'
it_behaves_like 'doesn\'t allow run_migration_playbook signal'
it_behaves_like 'doesn\'t allow poll_run_migration_playbook_complete signal'
Expand Down Expand Up @@ -587,8 +546,6 @@
it_behaves_like 'allows error signal'

it_behaves_like 'doesn\'t allow start signal'
it_behaves_like 'doesn\'t allow remove_snapshots signal'
it_behaves_like 'doesn\'t allow poll_remove_snapshots_complete signal'
it_behaves_like 'doesn\'t allow wait_for_ip_address signal'
it_behaves_like 'doesn\'t allow run_migration_playbook signal'
it_behaves_like 'doesn\'t allow poll_run_migration_playbook_complete signal'
Expand All @@ -615,8 +572,6 @@
it_behaves_like 'allows error signal'

it_behaves_like 'doesn\'t allow start signal'
it_behaves_like 'doesn\'t allow remove_snapshots signal'
it_behaves_like 'doesn\'t allow poll_remove_snapshots_complete signal'
it_behaves_like 'doesn\'t allow wait_for_ip_address signal'
it_behaves_like 'doesn\'t allow run_migration_playbook signal'
it_behaves_like 'doesn\'t allow poll_run_migration_playbook_complete signal'
Expand Down Expand Up @@ -644,8 +599,6 @@
it_behaves_like 'allows error signal'

it_behaves_like 'doesn\'t allow start signal'
it_behaves_like 'doesn\'t allow remove_snapshots signal'
it_behaves_like 'doesn\'t allow poll_remove_snapshots_complete signal'
it_behaves_like 'doesn\'t allow wait_for_ip_address signal'
it_behaves_like 'doesn\'t allow run_migration_playbook signal'
it_behaves_like 'doesn\'t allow poll_run_migration_playbook_complete signal'
Expand Down Expand Up @@ -675,8 +628,6 @@
it_behaves_like 'allows error signal'

it_behaves_like 'doesn\'t allow start signal'
it_behaves_like 'doesn\'t allow remove_snapshots signal'
it_behaves_like 'doesn\'t allow poll_remove_snapshots_complete signal'
it_behaves_like 'doesn\'t allow run_migration_playbook signal'
it_behaves_like 'doesn\'t allow poll_run_migration_playbook_complete signal'
it_behaves_like 'doesn\'t allow shutdown_vm signal'
Expand All @@ -702,8 +653,6 @@
it_behaves_like 'allows error signal'

it_behaves_like 'doesn\'t allow start signal'
it_behaves_like 'doesn\'t allow remove_snapshots signal'
it_behaves_like 'doesn\'t allow poll_remove_snapshots_complete signal'
it_behaves_like 'doesn\'t allow wait_for_ip_address signal'
it_behaves_like 'doesn\'t allow run_migration_playbook signal'
it_behaves_like 'doesn\'t allow poll_run_migration_playbook_complete signal'
Expand Down Expand Up @@ -731,8 +680,6 @@
it_behaves_like 'allows error signal'

it_behaves_like 'doesn\'t allow start signal'
it_behaves_like 'doesn\'t allow remove_snapshots signal'
it_behaves_like 'doesn\'t allow poll_remove_snapshots_complete signal'
it_behaves_like 'doesn\'t allow wait_for_ip_address signal'
it_behaves_like 'doesn\'t allow run_migration_playbook signal'
it_behaves_like 'doesn\'t allow poll_run_migration_playbook_complete signal'
Expand Down Expand Up @@ -762,8 +709,6 @@
it_behaves_like 'allows error signal'

it_behaves_like 'doesn\'t allow start signal'
it_behaves_like 'doesn\'t allow remove_snapshots signal'
it_behaves_like 'doesn\'t allow poll_remove_snapshots_complete signal'
it_behaves_like 'doesn\'t allow wait_for_ip_address signal'
it_behaves_like 'doesn\'t allow run_migration_playbook signal'
it_behaves_like 'doesn\'t allow poll_run_migration_playbook_complete signal'
Expand Down Expand Up @@ -791,8 +736,6 @@
it_behaves_like 'allows error signal'

it_behaves_like 'doesn\'t allow start signal'
it_behaves_like 'doesn\'t allow remove_snapshots signal'
it_behaves_like 'doesn\'t allow poll_remove_snapshots_complete signal'
it_behaves_like 'doesn\'t allow wait_for_ip_address signal'
it_behaves_like 'doesn\'t allow run_migration_playbook signal'
it_behaves_like 'doesn\'t allow poll_run_migration_playbook_complete signal'
Expand All @@ -812,93 +755,16 @@
context 'transition methods' do
context '#start' do
it 'to poll_automate_state_machine when preflight_check passes' do
expect(job).to receive(:queue_signal).with(:remove_snapshots)
expect(job).to receive(:queue_signal).with(:wait_for_ip_address)
job.signal(:start)
expect(task.reload.state).to eq('migrate')
end
end

context '#remove_snapshots' do
before { job.state = 'started' }

context 'without any snapshots' do
it 'does not queue the task' do
expect(job).to receive(:update_migration_task_progress).once.ordered.with(:on_entry)
expect(vm_vmware).not_to receive(:remove_all_snapshots)
expect(job).to receive(:update_migration_task_progress).once.ordered.with(:on_exit)
expect(job).to receive(:queue_signal).with(:wait_for_ip_address)
job.signal(:remove_snapshots)
end
end

context 'with snapshots' do
before { FactoryBot.create(:snapshot, :vm_or_template => vm_vmware) }

it 'queues the remove_all_snapshots task' do
Timecop.freeze(2019, 2, 6) do
expect(job).to receive(:update_migration_task_progress).once.ordered.with(:on_entry)
expect(job).to receive(:update_migration_task_progress).once.ordered.with(:on_exit)
expect(job).to receive(:queue_signal).with(:poll_remove_snapshots_complete, :deliver_on => Time.now.utc + job.state_retry_interval)
job.signal(:remove_snapshots)
task = MiqTask.find(job.context[:async_task_id_removing_snapshots])
expect(task).to have_attributes(
:name => "Removing all snapshots for #{vm_vmware.name}",
:state => MiqTask::STATE_QUEUED,
:status => MiqTask::STATUS_OK,
:userid => user.id.to_s
)
end
end
end
end

context '#poll_remove_snapshots_complete' do
let(:async_task) { FactoryBot.create(:miq_task, :userid => user.id) }

before do
job.state = 'removing_snapshots'
job.context[:async_task_id_removing_snapshots] = async_task.id
end

it 'abort_conversion when remove_snapshots times out' do
job.context[:retries_removing_snapshots] = 960
expect(job).to receive(:update_migration_task_progress).once.ordered.with(:on_entry)
expect(job).to receive(:update_migration_task_progress).once.ordered.with(:on_error)
expect(job).to receive(:abort_conversion).with('Removing snapshots timed out', 'error')
job.signal(:poll_remove_snapshots_complete)
end

it 'retries if async task is not finished' do
async_task.update!(:state => MiqTask::STATE_ACTIVE)
Timecop.freeze(2019, 2, 6) do
expect(job).to receive(:update_migration_task_progress).once.ordered.with(:on_entry)
expect(job).to receive(:update_migration_task_progress).once.ordered.with(:on_retry)
expect(job).to receive(:queue_signal).with(:poll_remove_snapshots_complete, :deliver_on => Time.now.utc + job.state_retry_interval)
job.signal(:poll_remove_snapshots_complete)
end
end

it 'exits if async task is finished and its status is Ok' do
async_task.update!(:state => MiqTask::STATE_FINISHED, :status => MiqTask::STATUS_OK)
expect(job).to receive(:update_migration_task_progress).once.ordered.with(:on_entry)
expect(job).to receive(:update_migration_task_progress).once.ordered.with(:on_exit)
expect(job).to receive(:queue_signal).with(:wait_for_ip_address)
job.signal(:poll_remove_snapshots_complete)
end

it 'fails if async task is finished and its status is Error' do
async_task.update!(:state => MiqTask::STATE_FINISHED, :status => MiqTask::STATUS_ERROR, :message => 'Fake error message')
expect(job).to receive(:update_migration_task_progress).once.ordered.with(:on_entry)
expect(job).to receive(:update_migration_task_progress).once.ordered.with(:on_error)
expect(job).to receive(:abort_conversion).with('Fake error message', 'error')
job.signal(:poll_remove_snapshots_complete)
end
end

context '#wait_for_ip_address' do
before do
task.update_options(:migration_phase => 'pre')
job.state = 'waiting_for_ip_address'
job.state = 'started'
end

it 'abort_conversion when waiting_on_ip_address times out' do
Expand Down