From 26d6cb28d943861f6caa7526a212b5f4f1fe5dff Mon Sep 17 00:00:00 2001 From: Samuel Beaulieu Date: Wed, 7 Jun 2017 17:04:21 +0100 Subject: [PATCH 1/2] Refactor the get_cluster_host_utilization method The same method logic was being used in two places but only one was calling the method get_cluster_host_utilization, so this refactors it to use the method for both. The method could also return an empty array and the subsequent line would try to .sort[0][1] which would return undefined method [] for nil:NilClass in that case. The return value is now checked and an exception raised --- lib/vmpooler/providers/vsphere.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/vmpooler/providers/vsphere.rb b/lib/vmpooler/providers/vsphere.rb index ee6b3bc..6b559d4 100644 --- a/lib/vmpooler/providers/vsphere.rb +++ b/lib/vmpooler/providers/vsphere.rb @@ -513,6 +513,11 @@ module Vmpooler # Params: # +model+:: CPU arch version to match on # +limit+:: Hard limit for CPU or memory utilization beyond which a host is excluded for deployments + # returns nil if one on these conditions is true: + # the model param is defined and cannot be found + # the host is in maintenance mode + # the host status is not 'green' + # the cpu or memory utilization is bigger than the limit param def get_host_utilization(host, model = nil, limit = 90) if model return nil unless host_has_cpu_model?(host, model) @@ -555,6 +560,7 @@ module Vmpooler def find_least_used_host(cluster, connection) cluster_object = find_cluster(cluster, connection) target_hosts = get_cluster_host_utilization(cluster_object) + raise("There is no host candidate in vcenter that meets all the required conditions, check that the cluster has available hosts in a 'green' status, not in maintenance mode and not overloaded CPU and memory'") if target_hosts.empty? least_used_host = target_hosts.sort[0][1] least_used_host end @@ -564,10 +570,10 @@ module Vmpooler datacenter.hostFolder.children.find { |cluster_object| cluster_object.name == cluster } end - def get_cluster_host_utilization(cluster) + def get_cluster_host_utilization(cluster, model=nil) cluster_hosts = [] cluster.host.each do |host| - host_usage = get_host_utilization(host) + host_usage = get_host_utilization(host, model) cluster_hosts << host_usage if host_usage end cluster_hosts @@ -577,11 +583,8 @@ module Vmpooler source_host = vm.summary.runtime.host model = get_host_cpu_arch_version(source_host) cluster = source_host.parent - target_hosts = [] - cluster.host.each do |host| - host_usage = get_host_utilization(host, model) - target_hosts << host_usage if host_usage - end + target_hosts = get_cluster_host_utilization(cluster, model) + raise("There is no host candidate in vcenter that meets all the required conditions, check that the cluster has available hosts in a 'green' status, not in maintenance mode and not overloaded CPU and memory'") if target_hosts.empty? target_host = target_hosts.sort[0][1] [target_host, target_host.name] end From 87056a731c6287ec569132770eae9e461942e211 Mon Sep 17 00:00:00 2001 From: Samuel Beaulieu Date: Wed, 7 Jun 2017 17:20:11 +0100 Subject: [PATCH 2/2] Fix test to raise expected error --- spec/unit/providers/vsphere_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/unit/providers/vsphere_spec.rb b/spec/unit/providers/vsphere_spec.rb index 74ccc66..f27ea70 100644 --- a/spec/unit/providers/vsphere_spec.rb +++ b/spec/unit/providers/vsphere_spec.rb @@ -2198,7 +2198,7 @@ EOT end it 'should raise error' do - expect{subject.find_least_used_vpshere_compatible_host(vm)}.to raise_error(NoMethodError,/undefined method/) + expect{subject.find_least_used_vpshere_compatible_host(vm)}.to raise_error(/There is no host candidate in vcenter that meets all the required conditions/) end end @@ -2238,7 +2238,7 @@ EOT end it 'should raise error' do - expect{subject.find_least_used_vpshere_compatible_host(vm)}.to raise_error(NoMethodError,/undefined method/) + expect{subject.find_least_used_vpshere_compatible_host(vm)}.to raise_error(/There is no host candidate in vcenter that meets all the required conditions/) end end