Skip to content

Commit

Permalink
Simplify method. Add test for VM conversion host.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiendupont committed Oct 31, 2018
1 parent 8342dbb commit 597c2fc
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 39 deletions.
7 changes: 4 additions & 3 deletions app/models/service_template_transformation_plan_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ def source_vms
end

def validate_conversion_hosts
transformation_mapping.transformation_mapping_items.select { |item| %w(EmsCluster CloudTenant).include?(item.source_type) }.each do |item|
return false if item.destination.ext_management_system.conversion_hosts.empty?
transformation_mapping.transformation_mapping_items.select do |item|
%w(EmsCluster CloudTenant).include?(item.source_type)
end.all? do |item|
item.destination.ext_management_system.conversion_hosts.present?
end
true
end

def validate_vm(_vm_id)
Expand Down
140 changes: 104 additions & 36 deletions spec/models/service_template_transformation_plan_request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'byebug'

describe ServiceTemplateTransformationPlanRequest do
let(:vms) { Array.new(3) { FactoryGirl.create(:vm_or_template) } }
let(:vm_requests) do
Expand All @@ -6,31 +8,6 @@
end
end

let(:dst_ems) { FactoryGirl.create(:ext_management_system) }
let(:src_cluster) { FactoryGirl.create(:ems_cluster) }
let(:dst_cluster) { FactoryGirl.create(:ems_cluster, :ext_management_system => dst_ems) }

let(:mapping) do
FactoryGirl.create(
:transformation_mapping,
:transformation_mapping_items => [TransformationMappingItem.new(:source => src_cluster, :destination => dst_cluster)]
)
end

let(:catalog_item_options) do
{
:name => 'Transformation Plan',
:description => 'a description',
:config_info => {
:transformation_mapping_id => mapping.id,
:actions => [
{:vm_id => vms.first.id.to_s, :pre_service => false, :post_service => false},
{:vm_id => vms.last.id.to_s, :pre_service => false, :post_service => false},
],
}
}
end

let(:plan) { FactoryGirl.create(:service_template_transformation_plan, :service_resources => vm_requests) }
let(:request) { FactoryGirl.create(:service_template_transformation_plan_request, :source => plan) }

Expand All @@ -55,21 +32,112 @@
end

describe '#validate_conversion_hosts' do
let(:plan) { ServiceTemplateTransformationPlan.create_catalog_item(catalog_item_options) }
let(:request) { FactoryGirl.create(:service_template_transformation_plan_request, :source => plan) }

context 'no conversion host exists in EMS' do
let(:host) { FactoryGirl.create(:host, :ext_management_system => FactoryGirl.create(:ext_management_system, :zone => FactoryGirl.create(:zone))) }
let(:conversion_host) { FactoryGirl.create(:conversion_host, :resource => host) }

it { expect(request.validate_conversion_hosts).to eq(false) }
let(:dst_ems) { FactoryGirl.create(:ext_management_system) }
let(:src_cluster) { FactoryGirl.create(:ems_cluster) }
let(:dst_cluster) { FactoryGirl.create(:ems_cluster, :ext_management_system => dst_ems) }

let(:mapping) do
FactoryGirl.create(
:transformation_mapping,
:transformation_mapping_items => [TransformationMappingItem.new(:source => src_cluster, :destination => dst_cluster)]
)
end

let(:catalog_item_options) do
{
:name => 'Transformation Plan',
:description => 'a description',
:config_info => {
:transformation_mapping_id => mapping.id,
:actions => [
{:vm_id => vms.first.id.to_s, :pre_service => false, :post_service => false},
{:vm_id => vms.last.id.to_s, :pre_service => false, :post_service => false},
],
}
}
end

let(:plan) { ServiceTemplateTransformationPlan.create_catalog_item(catalog_item_options) }
let(:request) { FactoryGirl.create(:service_template_transformation_plan_request, :source => plan) }

it 'returns false' do
host = FactoryGirl.create(:host, :ext_management_system => FactoryGirl.create(:ext_management_system, :zone => FactoryGirl.create(:zone)))
conversion_host = FactoryGirl.create(:conversion_host, :resource => host)
expect(request.validate_conversion_hosts).to be false
end
end

context 'conversion host exists in EMS' do
let(:host) { FactoryGirl.create(:host, :ext_management_system => dst_ems) }
let(:conversion_host) { FactoryGirl.create(:conversion_host, :resource => host) }
context 'conversion host exists in EMS and resource is a Host' do
let(:dst_ems) { FactoryGirl.create(:ems_redhat) }
let(:src_cluster) { FactoryGirl.create(:ems_cluster) }
let(:dst_cluster) { FactoryGirl.create(:ems_cluster, :ext_management_system => dst_ems) }

let(:mapping) do
FactoryGirl.create(
:transformation_mapping,
:transformation_mapping_items => [TransformationMappingItem.new(:source => src_cluster, :destination => dst_cluster)]
)
end

let(:catalog_item_options) do
{
:name => 'Transformation Plan',
:description => 'a description',
:config_info => {
:transformation_mapping_id => mapping.id,
:actions => [
{:vm_id => vms.first.id.to_s, :pre_service => false, :post_service => false},
{:vm_id => vms.last.id.to_s, :pre_service => false, :post_service => false},
],
}
}
end

let(:plan) { ServiceTemplateTransformationPlan.create_catalog_item(catalog_item_options) }
let(:request) { FactoryGirl.create(:service_template_transformation_plan_request, :source => plan) }

it 'returns true' do
host = FactoryGirl.create(:host, :ext_management_system => dst_ems, :ems_cluster => dst_cluster)
conversion_host = FactoryGirl.create(:conversion_host, :resource => host)
expect(request.validate_conversion_hosts).to be true
end
end

it { expect(request.validate_conversion_hosts).to eq(false) }
context 'conversion host exists in EMS and resource is a Vm' do
let(:dst_ems) { FactoryGirl.create(:ems_openstack) }
let(:src_cluster) { FactoryGirl.create(:ems_cluster) }
let(:dst_cloud_tenant) { FactoryGirl.create(:cloud_tenant, :ext_management_system => dst_ems) }

let(:mapping) do
FactoryGirl.create(
:transformation_mapping,
:transformation_mapping_items => [TransformationMappingItem.new(:source => src_cluster, :destination => dst_cloud_tenant)]
)
end

let(:catalog_item_options) do
{
:name => 'Transformation Plan',
:description => 'a description',
:config_info => {
:transformation_mapping_id => mapping.id,
:actions => [
{:vm_id => vms.first.id.to_s, :pre_service => false, :post_service => false},
{:vm_id => vms.last.id.to_s, :pre_service => false, :post_service => false},
],
}
}
end

let(:plan) { ServiceTemplateTransformationPlan.create_catalog_item(catalog_item_options) }
let(:request) { FactoryGirl.create(:service_template_transformation_plan_request, :source => plan) }

it 'returns true' do
vm = FactoryGirl.create(:vm_openstack, :ext_management_system => dst_ems, :cloud_tenant => dst_cloud_tenant)
conversion_host = FactoryGirl.create(:conversion_host, :resource => vm)
expect(request.validate_conversion_hosts).to be true
end
end
end

Expand Down

0 comments on commit 597c2fc

Please sign in to comment.