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] Lan validation in Transformation Mapping #19220

Merged
merged 4 commits into from
Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 31 additions & 0 deletions app/models/transformation_mapping_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class TransformationMappingItem < ApplicationRecord
validate :validate_source_datastore, :if => -> { source.kind_of?(Storage) }
validate :validate_destination_datastore, :if => -> { destination.kind_of?(Storage) || destination.kind_of?(CloudVolume) }

validate :validate_source_network, :if => -> { source.kind_of?(Lan) }
validate :validate_destination_network, :if => -> { destination.kind_of?(Lan) || destination.kind_of?(CloudNetwork) }

VALID_SOURCE_CLUSTER_PROVIDERS = %w[vmwarews].freeze
VALID_DESTINATION_CLUSTER_PROVIDERS = %w[rhevm openstack].freeze

Expand Down Expand Up @@ -55,4 +58,32 @@ def validate_destination_datastore
errors.add(:destination, "Destination cluster storages must include destination storage: #{destination_storage}")
end
end

def validate_source_network
tm = transformation_mapping
tmin = tm.transformation_mapping_items.where(:source_type => "EmsCluster")
src_cluster_lans = tmin.collect(&:source).flat_map(&:lans)
source_lan = source

unless src_cluster_lans.include?(source_lan)
errors.add(:source, "Source cluster lans must include source lan: #{source_lan}")
end
end

def validate_destination_network
tm = transformation_mapping
destination_lan = destination

if destination.kind_of?(Lan) # red hat
tmin = tm.transformation_mapping_items.where(:destination_type=> "EmsCluster")
dst_cluster_lans = tmin.collect(&:destination).flat_map(&:lans)
elsif destination.kind_of?(CloudNetwork) # Openstack, lans are of 'CloudNetwork' type
tmin = tm.transformation_mapping_items.where(:destination_type => "CloudTenant")
dst_cluster_lans = tmin.collect(&:destination).flat_map(&:cloud_networks)
end

unless dst_cluster_lans.include?(destination_lan)
errors.add(:destination, "Destination cluster lans must include destination lan: #{destination_lan}")
end
end
end
84 changes: 84 additions & 0 deletions spec/models/transformation_mapping_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
let(:ems_openstack) { FactoryBot.create(:ems_openstack) }
let(:openstack_cluster) { FactoryBot.create(:ems_cluster_openstack, :ext_management_system => ems_openstack) }

# ---------------------------------------------------------------------------
# Cluster Validation
# ---------------------------------------------------------------------------
context "source cluster validation" do
let(:valid_mapping_item) do
FactoryBot.build(:transformation_mapping_item, :source => vmware_cluster, :destination => openstack_cluster)
Expand Down Expand Up @@ -46,6 +49,9 @@
end
end

# ---------------------------------------------------------------------------
# Datastore Validation
# ---------------------------------------------------------------------------
context "datastore validation" do
let(:ems_vmware) { FactoryBot.create(:ems_vmware) }
let(:vmware_cluster) { FactoryBot.create(:ems_cluster, :ext_management_system => ems_vmware) }
Expand Down Expand Up @@ -100,4 +106,82 @@
end
end
end

# ---------------------------------------------------------------------------
# Network Validation
# ---------------------------------------------------------------------------
context "Network validation" do
let(:ems_vmware) { FactoryBot.create(:ems_vmware) }
Copy link
Member

Choose a reason for hiding this comment

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

This is a duplicate

let(:ems_vmware) { FactoryBot.create(:ems_vmware) }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

let(:vmware_cluster) { FactoryBot.create(:ems_cluster, :ext_management_system => ems_vmware) }
thearifismail marked this conversation as resolved.
Show resolved Hide resolved

let(:ems_redhat) { FactoryBot.create(:ems_redhat) }
thearifismail marked this conversation as resolved.
Show resolved Hide resolved
let(:redhat_cluster) { FactoryBot.create(:ems_cluster, :ext_management_system => ems_redhat) }
Copy link
Member

Choose a reason for hiding this comment

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

Same

let(:redhat_cluster) { FactoryBot.create(:ems_cluster, :ext_management_system => ems_redhat) }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


let(:ems_ops) { FactoryBot.create(:ems_openstack) }
Copy link
Member

Choose a reason for hiding this comment

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

This could just be

let(:ems_openstack) { FactoryBot.create(:ems_openstack) }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

let(:cloud_tenant) { FactoryBot.create(:cloud_tenant_openstack, :ext_management_system => ems_ops) }
Copy link
Member

Choose a reason for hiding this comment

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

This isn't used by the "destination red hat" context so it could be moved under the "source vmware network" context


# source network
context "source vmware network" do
let(:src_vmware_host) { FactoryBot.create(:host_vmware, :ems_cluster => vmware_cluster) }
let(:src_switch) { FactoryBot.create(:switch, :host => src_vmware_host) }
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you can actually set :host on a switch this way since there is a host_switches join table not a host_id on the record. You can do FactoryBot.create(:switch, :hosts => [src_vmware_host]) though.

let(:src_lan) { FactoryBot.create(:lan, :switch => src_switch) }

context "destination openstack" do
let(:dst_cloud_network) { FactoryBot.create(:cloud_network, :cloud_tenant => cloud_tenant) }

let(:tmi_ops_cluster) { FactoryBot.create(:transformation_mapping_item, :source => vmware_cluster, :destination => cloud_tenant) }
let(:ops_mapping) { FactoryBot.create(:transformation_mapping, :transformation_mapping_items => [tmi_ops_cluster]) }

let(:valid_source) { FactoryBot.create(:transformation_mapping_item, :source => src_lan, :destination => dst_cloud_network, :transformation_mapping_id => ops_mapping.id) }
let(:invalid_source) { FactoryBot.build(:transformation_mapping_item, :source => dst_cloud_network, :destination => src_lan, :transformation_mapping_id => ops_mapping.id) }

before do
Copy link
Member

Choose a reason for hiding this comment

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

Looks like there's an extra indentation here

src_switch.lans << [src_lan]
src_vmware_host.switches << [src_switch]
vmware_cluster.hosts << [src_vmware_host]

cloud_tenant.cloud_networks << [dst_cloud_network]
Copy link
Member

Choose a reason for hiding this comment

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

Is this any different between contexts? If not you can just do this when you define these up here

Copy link
Member

Choose a reason for hiding this comment

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

Actually the way you have those set up e.g. setting the switch for the lan (FactoryBot.create(:lan, :switch => src_switch)) then I doubt you need to do anything extra.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it is a different context.

end
Copy link
Contributor

Choose a reason for hiding this comment

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

I would say instead of doing it this way, you should use FactoryBot.create_list with regular assignment instead of << [stuff].


it "valid source" do
expect(valid_source.valid?).to be(true)
end
it "invalid source" do
expect(invalid_source.valid?).to be(false)
end
end

context "destination red hat" do
let(:dst_rh_host) { FactoryBot.create(:host_redhat, :ems_cluster => redhat_cluster) }
let(:dst_rh_switch) { FactoryBot.create(:switch, :host => dst_rh_host) }
Copy link
Member

Choose a reason for hiding this comment

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

This has to be :hosts => [dst_rh_host]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

let(:dst_rh_lan) { FactoryBot.create(:lan, :switch=> dst_rh_switch) }

let(:tmi_cluster) { FactoryBot.create(:transformation_mapping_item, :source => vmware_cluster, :destination => redhat_cluster) }

let(:rh_mapping) { FactoryBot.create(:transformation_mapping, :transformation_mapping_items => [tmi_cluster]) }

context "source validation" do
let(:valid_lan) { FactoryBot.create(:transformation_mapping_item, :source => src_lan, :destination => dst_rh_lan, :transformation_mapping_id => rh_mapping.id) }
let(:invalid_lan) { FactoryBot.build(:transformation_mapping_item, :source => dst_rh_lan, :destination => src_lan, :transformation_mapping_id => rh_mapping.id) }

before do
src_switch.lans << [src_lan]
Copy link
Member

Choose a reason for hiding this comment

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

Same, not needed

src_vmware_host.switches << [src_switch]
Copy link
Member

Choose a reason for hiding this comment

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

Similarly if you fix the switch.hosts then you don't need this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

vmware_cluster.hosts << [src_vmware_host]
Copy link
Member

Choose a reason for hiding this comment

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

Not needed


dst_rh_switch.lans << [dst_rh_lan]
Copy link
Member

Choose a reason for hiding this comment

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

Not needed

dst_rh_host.switches << [dst_rh_switch]
Copy link
Member

Choose a reason for hiding this comment

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

Not needed if you set switch.hosts properly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

redhat_cluster.hosts << [dst_rh_host]
Copy link
Member

Choose a reason for hiding this comment

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

Not needed

end

it "valid rhev lan" do
expect(valid_lan.valid?).to be(true)
end
it "invalid rhev lan" do
expect(invalid_lan.valid?).to be(false)
end
end
end
end
end
end
13 changes: 8 additions & 5 deletions spec/models/transformation_mapping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
let(:src_storages_vmware) { FactoryBot.create_list(:storage, 1, :hosts => src_hosts_vmware) }
let(:dst_storages_redhat) { FactoryBot.create_list(:storage, 1, :hosts => dst_hosts_redhat) }

let(:src_lan_vmware) { FactoryBot.create(:lan) }
let(:dst_lan_redhat) { FactoryBot.create(:lan) }
let(:src_switches_vmware) { FactoryBot.create_list(:switch, 1, :hosts => src_hosts_vmware) }
let(:dst_switches_redhat) { FactoryBot.create_list(:switch, 1, :hosts => dst_hosts_redhat) }

let(:src_lans_vmware) { FactoryBot.create_list(:lan, 1, :switch => src_switches_vmware.first) }
let(:dst_lans_redhat) { FactoryBot.create_list(:lan, 1, :switch => dst_switches_redhat.first) }

let(:dst_cloud_tenant_openstack) { FactoryBot.create(:cloud_tenant, :ext_management_system => dst_ems_openstack) }

Expand All @@ -30,8 +33,8 @@
:transformation_mapping => tm
)
FactoryBot.create(:transformation_mapping_item,
:source => src_lan_vmware,
:destination => dst_lan_redhat,
:source => src_lans_vmware.first,
:destination => dst_lans_redhat.first,
:transformation_mapping => tm
)
end
Expand Down Expand Up @@ -69,7 +72,7 @@
end

context '#search_vms_and_validate' do
let(:nics) { FactoryBot.create_list(:guest_device_nic, 1, :lan => src_lan_vmware) }
let(:nics) { FactoryBot.create_list(:guest_device_nic, 1, :lan => src_lans_vmware.first) }
let(:hardware) { FactoryBot.create(:hardware, :guest_devices => nics) }

let!(:vm) do
Expand Down