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

Add a validation for conversion hosts #18135

Merged
merged 4 commits into from
Oct 31, 2018
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
7 changes: 7 additions & 0 deletions app/models/ext_management_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ def self.api_allowed_attributes
has_many :service_offerings, :foreign_key => :ems_id, :dependent => :destroy, :inverse_of => :ext_management_system
has_many :service_parameters_sets, :foreign_key => :ems_id, :dependent => :destroy, :inverse_of => :ext_management_system

has_many :host_conversion_hosts, :through => :hosts, :source => :conversion_host
has_many :vm_conversion_hosts, :through => :vms, :source => :conversion_host

validates :name, :presence => true, :uniqueness => {:scope => [:tenant_id]}
validates :hostname, :presence => true, :if => :hostname_required?
validate :hostname_uniqueness_valid?, :hostname_format_valid?, :if => :hostname_required?
Expand Down Expand Up @@ -611,6 +614,10 @@ def vm_log_user_event(_vm, user_event)
$log.warn("User event logging is not available on [#{self.class.name}] Name:[#{name}]")
end

def conversion_hosts
host_conversion_hosts + vm_conversion_hosts
end

#
# Metric methods
#
Expand Down
8 changes: 8 additions & 0 deletions app/models/service_template_transformation_plan_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ def source_vms
vm_resources.where(:status => [ServiceResource::STATUS_QUEUED, ServiceResource::STATUS_FAILED]).pluck(:resource_id)
end

def validate_conversion_hosts
Copy link
Contributor

Choose a reason for hiding this comment

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

Will valid_conversion_hosts? be a more obvious method name?

Copy link
Author

Choose a reason for hiding this comment

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

Name already validated in ManageIQ/manageiq-automation_engine#263. It would mean changing it again.

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
end

def validate_vm(_vm_id)
# TODO: enhance the logic to determine whether this VM can be included in this request
true
Expand Down
111 changes: 111 additions & 0 deletions spec/models/service_template_transformation_plan_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
ServiceResource.new(:resource => vm, :status => status)
end
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 @@ -28,6 +29,116 @@
end
end

describe '#validate_conversion_hosts' do
context 'no conversion host exists in EMS' do
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
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: how about it 'fails' 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 and resource is a Host' do
let(:dst_ems) { FactoryGirl.create(:ems_redhat) }
Copy link
Contributor

Choose a reason for hiding this comment

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

Can these lets be shared with the previous context? You can still keep the :dst_ems local

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
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: how about it 'passes' 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

context 'conversion host exists in EMS and resource is a Vm' do
let(:dst_ems) { FactoryGirl.create(:ems_openstack) }
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly with these lets, some of them can be shared with previous contexts

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
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a test for using vm as conversion host?

Copy link
Author

Choose a reason for hiding this comment

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

Done.


describe '#validate_vm' do
it { expect(request.validate_vm(vms[0].id)).to be_truthy }
end
Expand Down