Skip to content

Commit

Permalink
Refactor redhat_best_fit_cluster method for VM Placement.
Browse files Browse the repository at this point in the history
Refactored method and created test.

This PR is based on the issue below.
ManageIQ/manageiq#12038

@miq-bot add_label refactoring
@miq-bot assign @tinaafitz
Fixed description
  • Loading branch information
billfitzgerald0120 committed Jan 16, 2020
1 parent 1196ecc commit 96d939f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,56 @@
#
# 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
vm_obj = request.vm_template.tap do |vm|
raise "VM not specified" if vm.nil?
end
vm_obj.ext_management_system.tap do |ems|
raise 'ext_management_system not specified' if ems.nil?
end
vm_obj
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

0 comments on commit 96d939f

Please sign in to comment.