From 377f42225ef7a50a2ed00af6a4e6097b4c3742bf Mon Sep 17 00:00:00 2001 From: Bill Wei Date: Wed, 20 Dec 2017 15:43:32 -0500 Subject: [PATCH] Rescue migration error and update status https://bugzilla.redhat.com/show_bug.cgi?id=1448023 --- app/models/vm_migrate_task.rb | 14 ++++++++++---- spec/models/vm_migrate_task_spec.rb | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 spec/models/vm_migrate_task_spec.rb diff --git a/app/models/vm_migrate_task.rb b/app/models/vm_migrate_task.rb index e202a2ef5dd..247c93b41ad 100644 --- a/app/models/vm_migrate_task.rb +++ b/app/models/vm_migrate_task.rb @@ -62,10 +62,16 @@ def do_request end _log.warn("Calling VM #{vc_method} for #{vm.id}:#{vm.name}") - if vc_method == :migrate - vm.migrate(host, respool) - else - vm.relocate(host, respool, datastore, nil, disk_transform) + + begin + if vc_method == :migrate + vm.migrate(host, respool) + else + vm.relocate(host, respool, datastore, nil, disk_transform) + end + rescue => err + update_and_notify_parent(:state => 'finished', :status => 'error', :message => "Failed. Reason[#{err.message}]") + return end if AUTOMATE_DRIVES diff --git a/spec/models/vm_migrate_task_spec.rb b/spec/models/vm_migrate_task_spec.rb new file mode 100644 index 00000000000..b2721888d8b --- /dev/null +++ b/spec/models/vm_migrate_task_spec.rb @@ -0,0 +1,18 @@ +describe VmMigrateTask do + describe '.do_request' do + let(:vm) { Vm.new } + before { subject.vm = vm } + + it 'migrates the vm and updates the status' do + expect(vm).to receive(:migrate) + expect(subject).to receive(:update_and_notify_parent).with(hash_including(:state => 'migrated')) + subject.do_request + end + + it 'catches migrate error and update the status' do + expect(vm).to receive(:migrate).and_raise("Bad things happened") + expect(subject).to receive(:update_and_notify_parent).with(hash_including(:state => 'finished', :status => 'error', :message => 'Failed. Reason[Bad things happened]')) + subject.do_request + end + end +end