Skip to content

Commit

Permalink
Leverage task / conversion host relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiendupont committed Oct 3, 2018
1 parent 289c53f commit 02b51cc
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 36 deletions.
5 changes: 2 additions & 3 deletions app/models/conversion_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ class ConversionHost < ApplicationRecord
acts_as_miq_taggable

belongs_to :resource, :polymorphic => true
has_many :service_template_transformation_plan_tasks, :dependent => :nullify

def active_tasks
ServiceTemplateTransformationPlanTask.where(:state => 'active').select do |task|
task.conversion_host == self
end
service_template_transformation_plan_tasks { active }
end

def eligible?
Expand Down
10 changes: 4 additions & 6 deletions app/models/service_template_transformation_plan_task.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ServiceTemplateTransformationPlanTask < ServiceTemplateProvisionTask
belongs_to :conversion_host

def self.base_model
ServiceTemplateTransformationPlanTask
end
Expand Down Expand Up @@ -46,10 +48,6 @@ def task_active
vm_resource.update_attributes(:status => ServiceResource::STATUS_ACTIVE)
end

def conversion_host
ConversionHost.find_by(:id => options[:transformation_host_id])
end

def source_ems
options[:source_ems_id] ||= source.ext_management_system.id
source.ext_management_system
Expand Down Expand Up @@ -110,7 +108,7 @@ def destination_security_group
def transformation_log
host = conversion_host
if host.nil?
msg = "Conversion host was not found: ID [#{options[:transformation_host_id]}]. Download of transformation log aborted."
msg = "Conversion host was not found. Download of transformation log aborted."
_log.error(msg)
raise MiqException::Error, msg
end
Expand Down Expand Up @@ -144,7 +142,7 @@ def transformation_log_queue(userid = nil)
userid ||= User.current_userid || 'system'
host = conversion_host
if host.nil?
msg = "Conversion host was not found: ID [#{options[:transformation_host_id]}]. Cannot queue the download of transformation log."
msg = "Conversion host was not found. Cannot queue the download of transformation log."
return create_error_status_task(userid, msg).id
end

Expand Down
27 changes: 12 additions & 15 deletions spec/models/conversion_host_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
describe ConversionHost do
let(:apst) { FactoryGirl.create(:service_template_ansible_playbook) }
let(:conversion_host) { FactoryGirl.create(:conversion_host) }

context "provider independent methods" do
let(:task_1) { FactoryGirl.create(:service_template_transformation_plan_task) }
let(:task_2) { FactoryGirl.create(:service_template_transformation_plan_task) }
let(:task_3) { FactoryGirl.create(:service_template_transformation_plan_task) }

let(:conversion_host_1) { FactoryGirl.create(:conversion_host) }
let(:conversion_host_2) { FactoryGirl.create(:conversion_host) }
let(:host) { FactoryGirl.create(:host) }
let(:vm) { FactoryGirl.create(:vm_or_template) }
let(:conversion_host_1) { FactoryGirl.create(:conversion_host, :resource => host) }
let(:conversion_host_2) { FactoryGirl.create(:conversion_host, :resource => vm) }
let(:task_1) { FactoryGirl.create(:service_template_transformation_plan_task, :state => 'active', :conversion_host => conversion_host_1) }
let(:task_2) { FactoryGirl.create(:service_template_transformation_plan_task, :conversion_host => conversion_host_1) }
let(:task_3) { FactoryGirl.create(:service_template_transformation_plan_task, :state => 'active', :conversion_host => conversion_host_2) }

before do
conversion_host_1.concurrent_transformation_limit = "2"
conversion_host_2.concurrent_transformation_limit = "1"

task_1.options[:transformation_host_id] = conversion_host_1.id
task_2.options[:transformation_host_id] = conversion_host_1.id
task_3.options[:transformation_host_id] = conversion_host_2.id

allow(ServiceTemplateTransformationPlanTask).to receive(:where).with(:state => 'active').and_return([task_1, task_3])
end

Expand Down Expand Up @@ -85,6 +81,7 @@
let(:dst_storage) { FactoryGirl.create(:storage) }
let(:dst_lan_1) { FactoryGirl.create(:lan) }
let(:dst_lan_2) { FactoryGirl.create(:lan) }
let(:conversion_host) { FactoryGirl.create(:conversion_host, :resource => FactoryGirl.create(:host, :ext_management_system => dst_ems)) }

let(:mapping) do
FactoryGirl.create(
Expand Down Expand Up @@ -115,11 +112,11 @@

let(:plan) { ServiceTemplateTransformationPlan.create_catalog_item(catalog_item_options) }
let(:request) { FactoryGirl.create(:service_template_transformation_plan_request, :source => plan) }
let(:task) { FactoryGirl.create(:service_template_transformation_plan_task, :miq_request => request, :request_type => 'transformation_plan', :source => src_vm) }
let(:task) { FactoryGirl.create(:service_template_transformation_plan_task, :miq_request => request, :request_type => 'transformation_plan', :source => src_vm, :conversion_host => conversion_host) }

before do
task.options[:transformation_host_id] = conversion_host.id
allow(task).to receive(:source_disks).and_return(source_disks)
allow(conversion_host).to receive(:ipaddress).and_return('10.0.1.1')
end

context "transport method is vddk" do
Expand Down Expand Up @@ -177,6 +174,7 @@
let(:dst_cloud_network_2) { FactoryGirl.create(:cloud_network) }
let(:dst_flavor) { FactoryGirl.create(:flavor) }
let(:dst_security_group) { FactoryGirl.create(:security_group) }
let(:conversion_host) { FactoryGirl.create(:conversion_host, :resource => FactoryGirl.create(:vm_or_template, :ext_management_system => dst_ems)) }

let(:mapping) do
FactoryGirl.create(
Expand Down Expand Up @@ -209,10 +207,9 @@

let(:plan) { ServiceTemplateTransformationPlan.create_catalog_item(catalog_item_options) }
let(:request) { FactoryGirl.create(:service_template_transformation_plan_request, :source => plan) }
let(:task) { FactoryGirl.create(:service_template_transformation_plan_task, :miq_request => request, :request_type => 'transformation_plan', :source => src_vm) }
let(:task) { FactoryGirl.create(:service_template_transformation_plan_task, :miq_request => request, :request_type => 'transformation_plan', :source => src_vm, :conversion_host => conversion_host) }

before do
task.options[:transformation_host_id] = conversion_host.id
allow(task).to receive(:source_disks).and_return(source_disks)
end

Expand Down
17 changes: 5 additions & 12 deletions spec/models/service_template_transformation_plan_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
let(:vm) { FactoryGirl.create(:vm_or_template) }
let(:vm2) { FactoryGirl.create(:vm_or_template) }
let(:apst) { FactoryGirl.create(:service_template_ansible_playbook) }
let(:conversion_host) { FactoryGirl.create(:conversion_host, :resource => host) }

let(:mapping) do
FactoryGirl.create(
:transformation_mapping,
Expand Down Expand Up @@ -103,16 +105,9 @@
end

describe '#transformation_log_queue' do
let(:conversion_host_id) { 22 }

before do
task.options[:transformation_host_id] = conversion_host_id
task.save!
end

context 'when conversion host exists' do
before do
FactoryGirl.create(:conversion_host, :id => conversion_host_id, :resource => host)
task.conversion_host = conversion_host

allow(described_class).to receive(:find).and_return(task)

Expand Down Expand Up @@ -147,19 +142,17 @@
it 'returns an error message' do
taskid = task.transformation_log_queue('user')
expect(MiqTask.find(taskid)).to have_attributes(
:message => "Conversion host was not found: ID [#{conversion_host_id}]. Cannot queue the download of transformation log.",
:message => "Conversion host was not found. Cannot queue the download of transformation log.",
:status => 'Error'
)
end
end
end

describe '#transformation_log' do
let(:conversion_host) { FactoryGirl.create(:conversion_host, :id => 9, :resource => host) }

before do
EvmSpecHelper.create_guid_miq_server_zone
task.options[:transformation_host_id] = conversion_host.id
task.conversion_host = conversion_host
task.options.store_path(:virtv2v_wrapper, "v2v_log", "/path/to/log.file")
task.save!

Expand Down

0 comments on commit 02b51cc

Please sign in to comment.