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

Refactor redhat_best_fit_cluster method for VM Placement. #629

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
Original file line number Diff line number Diff line change
@@ -1,22 +1,53 @@
#
# Description: This method sets the cluster based on source template
#
module ManageIQ
module Automate
module Infrastructure
module VM
module Provisioning
module Placement
class RedhatBestFitCluster
def initialize(handle = $evm)
@handle = handle
end

# Get variables
prov = $evm.root["miq_provision"]
vm = prov.vm_template
raise "VM not specified" if vm.nil?
user = prov.miq_request.requester
raise "User not specified" if user.nil?
ems = vm.ext_management_system
raise "EMS not found for VM [#{vm.name}" if ems.nil?
def main
best_fit_cluster
end

$evm.log("info", "vm=[#{vm.name}]")
private

cluster = vm.ems_cluster
$evm.log("info", "Selected Cluster: [#{cluster.nil? ? "nil" : cluster.name}]")
def request
@handle.root["miq_provision"].tap do |req|
raise "miq_provision not specified" if req.nil?
end
end

# Set cluster
prov.set_cluster(cluster) if cluster
def vm
request.vm_template.tap do |vm|
raise 'VM not specified' if vm.nil?
raise "EMS not found for VM [#{vm.name}]" if vm.ext_management_system.nil?
end
end

$evm.log("info", "vm=[#{vm.name}] cluster=[#{cluster}]")
def best_fit_cluster
@handle.log("info", "vm=[#{vm.name}]")

cluster = vm.ems_cluster
@handle.log("info", "Selected Cluster: [#{cluster.nil? ? "nil" : cluster.name}]")

# Set cluster
request.set_cluster(cluster) if cluster

@handle.log("info", "vm=[#{vm.name}] cluster=[#{cluster}]")
end
end
end
end
end
end
end
end

ManageIQ::Automate::Infrastructure::VM::Provisioning::Placement::RedhatBestFitCluster.new.main
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require_domain_file

describe ManageIQ::Automate::Infrastructure::VM::Provisioning::Placement::RedhatBestFitCluster do
let(:datacenter) { FactoryBot.create(:datacenter, :ext_management_system => ems) }
let(:ems) { FactoryBot.create(:ems_redhat_with_authentication) }
let(:ems_cluster) { FactoryBot.create(:ems_cluster, :ext_management_system => ems) }
let(:miq_provision) do
FactoryBot.create(:miq_provision_redhat,
:options => {:src_vm_id => vm_template.id, :placement_auto => [true, 1]},
:userid => user.userid,
:source => vm_template,
:request_type => 'clone_to_vm',
:state => 'active',
:status => 'Ok')
end
let(:user) { FactoryBot.create(:user_with_group, :settings => {:display => {:timezone => 'UTC'}}) }
let(:vm_template) { FactoryBot.create(:template_redhat, :ext_management_system => ems) }

let(:svc_miq_provision) { MiqAeMethodService::MiqAeServiceMiqProvision.find(miq_provision.id) }
let(:root_object) { Spec::Support::MiqAeMockObject.new(:miq_provision => svc_miq_provision) }
let(:ae_service) { Spec::Support::MiqAeMockService.new(root_object) }

it 'Check log messages with No errors' do
expect(ae_service).to receive(:log).with('info', "vm=[#{vm_template.name}]")
expect(ae_service).to receive(:log).with('info', "Selected Cluster: [nil]")
expect(ae_service).to receive(:log).with('info', "vm=[#{vm_template.name}] cluster=[]")
described_class.new(ae_service).main
end

it 'Raise error when source vm is not specified' do
miq_provision.update(:source => nil)
expect { described_class.new(ae_service).main }.to raise_error('VM not specified')
end

context "Raise error" do
let(:svc_miq_provision) { nil }
it 'when miq_provision is not specified' do
expect { described_class.new(ae_service).main }.to raise_error('miq_provision not specified')
end
end
end