From 354b7ee7692cf4b73cc56d67ac3b7f9e4248d5a0 Mon Sep 17 00:00:00 2001 From: d-m-u Date: Wed, 13 May 2020 22:16:28 -0400 Subject: [PATCH] use virtual_aggregate for hosts and emses and clusters --- app/models/container_project.rb | 11 +- app/models/ems_cluster.rb | 8 +- app/models/ext_management_system.rb | 2 + app/models/host.rb | 9 +- .../manageiq/providers/container_manager.rb | 13 +- app/models/miq_enterprise.rb | 2 - app/models/miq_region.rb | 1 - app/models/miq_server.rb | 4 +- app/models/mixins/aggregation_mixin.rb | 25 +--- .../mixins/aggregation_mixin/methods.rb | 55 ------- .../mixins/relationships_aggregation_mixin.rb | 2 - app/models/zone.rb | 2 + spec/models/ems_cluster_spec.rb | 85 ++++++----- spec/models/ext_management_system_spec.rb | 1 + spec/models/host_spec.rb | 1 + spec/models/mixins/aggregation_mixin_spec.rb | 138 ------------------ spec/models/zone_spec.rb | 2 + .../shared_examples_for_aggregation_mixin.rb | 69 +++++++++ 18 files changed, 149 insertions(+), 281 deletions(-) delete mode 100644 app/models/mixins/aggregation_mixin/methods.rb delete mode 100644 spec/models/mixins/aggregation_mixin_spec.rb create mode 100644 spec/support/examples_group/shared_examples_for_aggregation_mixin.rb diff --git a/app/models/container_project.rb b/app/models/container_project.rb index 9a8140701f2..87f5e9e999c 100644 --- a/app/models/container_project.rb +++ b/app/models/container_project.rb @@ -44,7 +44,6 @@ class ContainerProject < ApplicationRecord include EventMixin include Metric::CiMixin - include AggregationMixin::Methods PERF_ROLLUP_CHILDREN = :all_container_groups @@ -71,20 +70,16 @@ def perf_rollup_parents(interval_name = nil) [] end - # required by aggregate_hardware - alias all_computer_system_ids computer_system_ids - alias all_computer_systems computer_systems - def aggregate_memory(targets = nil) - aggregate_hardware(:computer_systems, :memory_mb, targets) + Hardware.where(:computer_system => computer_systems).sum(:memory_mb) end def aggregate_cpu_speed(targets = nil) - aggregate_hardware(:computer_systems, :cpu_speed, targets) + Hardware.where(:computer_system => computer_systems).sum(:cpu_speed) end def aggregate_cpu_total_cores(targets = nil) - aggregate_hardware(:computer_systems, :cpu_total_cores, targets) + Hardware.where(:computer_system => computer_systems).sum(:cpu_total_cores) end def disconnect_inv diff --git a/app/models/ems_cluster.rb b/app/models/ems_cluster.rb index e67d6297296..b923aa7d8d9 100644 --- a/app/models/ems_cluster.rb +++ b/app/models/ems_cluster.rb @@ -20,6 +20,10 @@ class EmsCluster < ApplicationRecord has_many :policy_events, -> { order("timestamp") } has_many :miq_events, :as => :target, :dependent => :destroy has_many :miq_alert_statuses, :as => :resource, :dependent => :destroy + has_many :host_hardwares, :class_name => 'Hardware', :through => :hosts, :source => :hardware + has_many :vm_hardwares, :class_name => 'Hardware', :through => :vms_and_templates, :source => :hardware + has_many :storages, -> { distinct }, :through => :hosts + has_many :lans, -> { distinct }, :through => :hosts has_many :switches, -> { distinct }, :through => :hosts @@ -51,7 +55,6 @@ class EmsCluster < ApplicationRecord self.default_relationship_type = "ems_metadata" include AggregationMixin - include Metric::CiMixin include MiqPolicyMixin include AsyncDeleteMixin @@ -107,9 +110,6 @@ def total_vcpus # Relationship methods # - alias_method :storages, :all_storages - alias_method :datastores, :all_storages # Used by web-services to return datastores as the property name - # Direct Vm relationship methods def direct_vm_rels # Look for only the Vms at the second depth (default RP + 1) diff --git a/app/models/ext_management_system.rb b/app/models/ext_management_system.rb index 815b55110e5..d8d68215f0e 100644 --- a/app/models/ext_management_system.rb +++ b/app/models/ext_management_system.rb @@ -206,6 +206,8 @@ def validate_zone_not_maintenance_when_ems_enabled? include RelationshipMixin self.default_relationship_type = "ems_metadata" + has_many :host_hardwares, :class_name => 'Hardware', :through => :hosts, :source => :hardware + has_many :vm_hardwares, :class_name => 'Hardware', :through => :vms_and_templates, :source => :hardware include AggregationMixin include AuthenticationMixin diff --git a/app/models/host.rb b/app/models/host.rb index a68f9bdb8f0..8545a2b77df 100644 --- a/app/models/host.rb +++ b/app/models/host.rb @@ -86,6 +86,8 @@ class Host < ApplicationRecord :inverse_of => :host has_many :host_aggregate_hosts, :dependent => :destroy has_many :host_aggregates, :through => :host_aggregate_hosts + has_many :host_hardwares, :class_name => 'Hardware', :source => :hardware, :dependent => :nullify + has_many :vm_hardwares, :class_name => 'Hardware', :through => :vms_and_templates, :source => :hardware has_one :conversion_host, :as => :resource, :dependent => :destroy, :inverse_of => :resource # Physical server reference @@ -93,9 +95,9 @@ class Host < ApplicationRecord serialize :settings, Hash - deprecate_attribute :address, :hostname - alias_attribute :state, :power_state - alias_attribute :to_s, :name + deprecate_attribute :address, :hostname + alias_attribute :state, :power_state + alias_attribute :to_s, :name include ProviderObjectMixin include EventMixin @@ -170,6 +172,7 @@ class Host < ApplicationRecord include AsyncDeleteMixin include ComplianceMixin include AvailabilityMixin + include AggregationMixin before_create :make_smart after_save :process_events diff --git a/app/models/manageiq/providers/container_manager.rb b/app/models/manageiq/providers/container_manager.rb index d71124d41b0..eb80cef063b 100644 --- a/app/models/manageiq/providers/container_manager.rb +++ b/app/models/manageiq/providers/container_manager.rb @@ -39,6 +39,7 @@ class ContainerManager < BaseManager has_many :container_quota_items, :through => :container_quotas has_many :container_limit_items, :through => :container_limits has_many :container_template_parameters, :through => :container_templates + has_many :computer_system_hardwares, :class_name => 'Hardware', :through => :computer_systems, :source => :hardware # Archived and active entities to destroy when the container manager is deleted has_many :all_containers, :foreign_key => :ems_id, :dependent => :destroy, :class_name => "Container" @@ -67,18 +68,6 @@ def supports_metrics? true end - # required by aggregate_hardware - alias :all_computer_systems :computer_systems - alias :all_computer_system_ids :computer_system_ids - - def aggregate_cpu_total_cores(targets = nil) - aggregate_hardware(:computer_systems, :cpu_total_cores, targets) - end - - def aggregate_memory(targets = nil) - aggregate_hardware(:computer_systems, :memory_mb, targets) - end - class << model_name define_method(:route_key) { "ems_containers" } define_method(:singular_route_key) { "ems_container" } diff --git a/app/models/miq_enterprise.rb b/app/models/miq_enterprise.rb index 9c8eb618967..27178f1e084 100644 --- a/app/models/miq_enterprise.rb +++ b/app/models/miq_enterprise.rb @@ -17,8 +17,6 @@ class MiqEnterprise < ApplicationRecord acts_as_miq_taggable include SupportsFeatureMixin - include AggregationMixin - include MiqPolicyMixin include Metric::CiMixin diff --git a/app/models/miq_region.rb b/app/models/miq_region.rb index c836460c680..f652d74b024 100644 --- a/app/models/miq_region.rb +++ b/app/models/miq_region.rb @@ -24,7 +24,6 @@ class MiqRegion < ApplicationRecord acts_as_miq_taggable include UuidMixin include NamingSequenceMixin - include AggregationMixin include ConfigurationManagementMixin include MiqPolicyMixin diff --git a/app/models/miq_server.rb b/app/models/miq_server.rb index f0069998226..9b7b9bcd81e 100644 --- a/app/models/miq_server.rb +++ b/app/models/miq_server.rb @@ -491,7 +491,7 @@ def self.managed_resources { :vms => Vm.active.count, :hosts => Host.active.count, - :aggregate_physical_cpus => MiqRegion.my_region.aggregate_physical_cpus(Host.active), + :aggregate_physical_cpus => Host.active.in_my_region.sum(:aggregate_physical_cpus), } end @@ -499,7 +499,7 @@ def self.unmanaged_resources { :vms => Vm.not_active.count, :hosts => Host.archived.count, - :aggregate_physical_cpus => MiqRegion.my_region.aggregate_physical_cpus(Host.archived), + :aggregate_physical_cpus => Host.archived.in_my_region.sum(:aggregate_physical_cpus), } end diff --git a/app/models/mixins/aggregation_mixin.rb b/app/models/mixins/aggregation_mixin.rb index e5e5749578d..77e0d4ec5e7 100644 --- a/app/models/mixins/aggregation_mixin.rb +++ b/app/models/mixins/aggregation_mixin.rb @@ -1,23 +1,12 @@ module AggregationMixin extend ActiveSupport::Concern included do - virtual_column :aggregate_cpu_speed, :type => :integer, :uses => :hosts - virtual_column :aggregate_cpu_total_cores, :type => :integer, :uses => :hosts - virtual_column :aggregate_physical_cpus, :type => :integer, :uses => :hosts - virtual_column :aggregate_memory, :type => :integer, :uses => :hosts - virtual_column :aggregate_vm_cpus, :type => :integer, :uses => :vms_and_templates - virtual_column :aggregate_vm_memory, :type => :integer, :uses => :vms_and_templates - virtual_column :aggregate_disk_capacity, :type => :integer, :uses => :hosts - - alias_method :all_hosts, :hosts - alias_method :all_host_ids, :host_ids - alias_method :all_vms_and_templates, :vms_and_templates - alias_method :all_vm_or_template_ids, :vm_or_template_ids - alias_method :all_vms, :vms - alias_method :all_vm_ids, :vm_ids - alias_method :all_miq_templates, :miq_templates - alias_method :all_miq_template_ids, :miq_template_ids - - include AggregationMixin::Methods + virtual_aggregate :aggregate_cpu_speed, :host_hardwares, :sum, :aggregate_cpu_speed + virtual_aggregate :aggregate_cpu_total_cores, :host_hardwares, :sum, :cpu_total_cores + virtual_aggregate :aggregate_disk_capacity, :host_hardwares, :sum, :disk_capacity + virtual_aggregate :aggregate_memory, :host_hardwares, :sum, :memory_mb + virtual_aggregate :aggregate_physical_cpus, :host_hardwares, :sum, :cpu_sockets + virtual_aggregate :aggregate_vm_cpus, :vm_hardwares, :sum, :cpu_sockets + virtual_aggregate :aggregate_vm_memory, :vm_hardwares, :sum, :memory_mb end end diff --git a/app/models/mixins/aggregation_mixin/methods.rb b/app/models/mixins/aggregation_mixin/methods.rb deleted file mode 100644 index 195f32f916f..00000000000 --- a/app/models/mixins/aggregation_mixin/methods.rb +++ /dev/null @@ -1,55 +0,0 @@ -module AggregationMixin - module Methods - extend ActiveSupport::Concern - - def aggregate_cpu_speed(targets = nil) - aggregate_hardware(:hosts, :aggregate_cpu_speed, targets) - end - - def aggregate_cpu_total_cores(targets = nil) - aggregate_hardware(:hosts, :cpu_total_cores, targets) - end - - def aggregate_physical_cpus(targets = nil) - aggregate_hardware(:hosts, :cpu_sockets, targets) - end - - def aggregate_memory(targets = nil) - aggregate_hardware(:hosts, :memory_mb, targets) - end - - def aggregate_vm_cpus(targets = nil) - aggregate_hardware(:vms_and_templates, :cpu_sockets, targets) - end - - def aggregate_vm_memory(targets = nil) - aggregate_hardware(:vms_and_templates, :memory_mb, targets) - end - - def aggregate_disk_capacity(targets = nil) - aggregate_hardware(:hosts, :disk_capacity, targets) - end - - # Default implementations which can be overridden with something more optimized - - def all_storages - hosts = all_hosts - MiqPreloader.preload(hosts, :storages) - hosts.collect(&:storages).flatten.compact.uniq - end - - def aggregate_hardware(from, field, targets = nil) - from = from.to_s.singularize - select = field == :aggregate_cpu_speed ? "cpu_total_cores, cpu_speed" : field - targets ||= send("all_#{from.pluralize}") - hdws = Hardware.where(from.singularize => targets).select(select) - hdws.inject(0) { |t, hdw| t + hdw.send(field).to_i } - end - - def lans - hosts = all_hosts - MiqPreloader.preload(hosts, :lans) - hosts.flat_map(&:lans).compact.uniq - end - end -end diff --git a/app/models/mixins/relationships_aggregation_mixin.rb b/app/models/mixins/relationships_aggregation_mixin.rb index e03dde1215b..601f0bf6d88 100644 --- a/app/models/mixins/relationships_aggregation_mixin.rb +++ b/app/models/mixins/relationships_aggregation_mixin.rb @@ -8,8 +8,6 @@ module RelationshipsAggregationMixin virtual_column :aggregate_vm_cpus, :type => :integer, :uses => :all_relationships virtual_column :aggregate_vm_memory, :type => :integer, :uses => :all_relationships virtual_column :aggregate_disk_capacity, :type => :integer, :uses => :all_relationships - - include AggregationMixin::Methods end # Default implementations which can be overridden with something more optimized diff --git a/app/models/zone.rb b/app/models/zone.rb index bb663b71f7d..fcb735803a8 100644 --- a/app/models/zone.rb +++ b/app/models/zone.rb @@ -27,6 +27,8 @@ class Zone < ApplicationRecord has_many :container_groups, :through => :container_managers has_many :container_replicators, :through => :container_managers has_many :containers, :through => :container_managers + has_many :host_hardwares, :class_name => 'Hardware', :through => :hosts, :source => :hardware + has_many :vm_hardwares, :class_name => 'Hardware', :through => :vms_and_templates, :source => :hardware virtual_has_many :active_miq_servers, :class_name => "MiqServer" before_destroy :remove_servers_if_podified diff --git a/spec/models/ems_cluster_spec.rb b/spec/models/ems_cluster_spec.rb index 22ee9d38d05..e02bbcbf7bd 100644 --- a/spec/models/ems_cluster_spec.rb +++ b/spec/models/ems_cluster_spec.rb @@ -1,4 +1,5 @@ RSpec.describe EmsCluster do + include_examples "AggregationMixin" context("VMware") do before do @cluster = FactoryBot.create(:ems_cluster) @@ -37,16 +38,10 @@ it('#direct_miq_template_ids') { expect(@cluster.direct_miq_template_ids).to match_array [@template1.id, @template2.id] } it('#total_direct_miq_templates') { expect(@cluster.total_direct_miq_templates).to eq(2) } - it('#all_vms_and_templates') { expect(@cluster.all_vms_and_templates).to match_array [@vm1, @vm2, @template1, @template2] } - it('#all_vm_or_template_ids') { expect(@cluster.all_vm_or_template_ids).to match_array [@vm1.id, @vm2.id, @template1.id, @template2.id] } it('#total_vms_and_templates') { expect(@cluster.total_vms_and_templates).to eq(4) } - it('#all_vms') { expect(@cluster.all_vms).to match_array [@vm1, @vm2] } - it('#all_vm_ids') { expect(@cluster.all_vm_ids).to match_array [@vm1.id, @vm2.id] } it('#total_vms') { expect(@cluster.total_vms).to eq(2) } - it('#all_miq_templates') { expect(@cluster.all_miq_templates).to match_array [@template1, @template2] } - it('#all_miq_template_ids') { expect(@cluster.all_miq_template_ids).to match_array [@template1.id, @template2.id] } it('#total_miq_templates') { expect(@cluster.total_miq_templates).to eq(2) } it('ResourcePool#v_direct_vms') { expect(@rp1.v_direct_vms).to eq(1) } @@ -55,7 +50,6 @@ it('ResourcePool#v_direct_miq_templates') { expect(@rp1.v_direct_vms).to eq(1) } it('ResourcePool#v_total_miq_templates') { expect(@rp1.v_total_vms).to eq(2) } it('#hosts') { expect(@cluster.hosts).to match_array [@host1, @host2] } - it('#all_hosts') { expect(@cluster.all_hosts).to match_array [@host1, @host2] } it('#total_hosts') { expect(@cluster.total_hosts).to eq(2) } end @@ -98,44 +92,63 @@ it('#direct_miq_template_ids') { expect(@cluster.direct_miq_template_ids).to match_array [@template1.id, @template2.id] } it('#total_direct_miq_templates') { expect(@cluster.total_direct_miq_templates).to eq(2) } - it('#all_vms_and_templates') { expect(@cluster.all_vms_and_templates).to match_array [@vm1, @vm2, @template1, @template2] } - it('#all_vm_or_template_ids') { expect(@cluster.all_vm_or_template_ids).to match_array [@vm1.id, @vm2.id, @template1.id, @template2.id] } it('#total_vms_and_templates') { expect(@cluster.total_vms_and_templates).to eq(4) } - it('#all_vms') { expect(@cluster.all_vms).to match_array [@vm1, @vm2] } - it('#all_vm_ids') { expect(@cluster.all_vm_ids).to match_array [@vm1.id, @vm2.id] } it('#total_vms') { expect(@cluster.total_vms).to eq(2) } - it('#all_miq_templates') { expect(@cluster.all_miq_templates).to match_array [@template1, @template2] } - it('#all_miq_template_ids') { expect(@cluster.all_miq_template_ids).to match_array [@template1.id, @template2.id] } it('#total_miq_templates') { expect(@cluster.total_miq_templates).to eq(2) } it('#hosts') { expect(@cluster.hosts).to match_array [@host1, @host2] } - it('#all_hosts') { expect(@cluster.all_hosts).to match_array [@host1, @host2] } it('#total_hosts') { expect(@cluster.total_hosts).to eq(2) } end - it "#save_drift_state" do - # TODO: Beef up with more data - cluster = FactoryBot.create(:ems_cluster) - cluster.save_drift_state - - expect(cluster.drift_states.size).to eq(1) - expect(DriftState.count).to eq(1) - - expect(cluster.drift_states.first.data).to eq({ - :aggregate_cpu_speed => 0, - :aggregate_cpu_total_cores => 0, - :aggregate_memory => 0, - :aggregate_physical_cpus => 0, - :aggregate_vm_cpus => 0, - :aggregate_vm_memory => 0, - :class => "EmsCluster", - :id => cluster.id, - :name => cluster.name, - :vms => [], - :miq_templates => [], - :hosts => [], - }) + context("#save_drift_state") do + it "without aggregate data" do + # TODO: Beef up with more data + cluster = FactoryBot.create(:ems_cluster) + cluster.save_drift_state + + expect(cluster.drift_states.size).to eq(1) + expect(DriftState.count).to eq(1) + + expect(cluster.drift_states.first.data).to eq( + :class => "EmsCluster", + :id => cluster.id, + :name => cluster.name, + :vms => [], + :miq_templates => [], + :hosts => [] + ) + end + + it "with aggregate data" do + cluster = FactoryBot.create(:ems_cluster) + host = FactoryBot.create(:host, + :ems_cluster => cluster, + :ext_management_system => FactoryBot.create(:ext_management_system), + :hardware => FactoryBot.build(:hardware, + :cpu_total_cores => 4, + :cpu_speed => 1000, + :memory_mb => 2_048)) + + vm = FactoryBot.create(:vm_redhat, :host => host, :ems_cluster => cluster) + + cluster.save_drift_state + + expect(cluster.drift_states.size).to eq(1) + expect(DriftState.count).to eq(1) + expect(cluster.drift_states.first.data).to eq( + :aggregate_cpu_speed => 4000, + :aggregate_cpu_total_cores => 4, + :aggregate_memory => 2048, + :aggregate_physical_cpus => 1, + :class => "EmsCluster", + :id => cluster.id, + :name => cluster.name, + :vms => [{:class => "ManageIQ::Providers::Redhat::InfraManager::Vm", :id => vm.id}], + :miq_templates => [], + :hosts => [{:class => "Host", :id => host.id}] + ) + end end context("#perf_capture_enabled_host_ids=") do diff --git a/spec/models/ext_management_system_spec.rb b/spec/models/ext_management_system_spec.rb index 94e23f8b342..dd6a6ceadc2 100644 --- a/spec/models/ext_management_system_spec.rb +++ b/spec/models/ext_management_system_spec.rb @@ -1,4 +1,5 @@ RSpec.describe ExtManagementSystem do + include_examples "AggregationMixin" describe ".with_tenant" do # tenant_root # \___ tenant_eye_bee_em (service_template_eye_bee_em) diff --git a/spec/models/host_spec.rb b/spec/models/host_spec.rb index 3218c522dfe..46ed36d90c9 100644 --- a/spec/models/host_spec.rb +++ b/spec/models/host_spec.rb @@ -1,4 +1,5 @@ RSpec.describe Host do + include_examples "AggregationMixin" it "groups and users joins" do user1 = FactoryBot.create(:account_user) user2 = FactoryBot.create(:account_user) diff --git a/spec/models/mixins/aggregation_mixin_spec.rb b/spec/models/mixins/aggregation_mixin_spec.rb deleted file mode 100644 index eed443d7cc8..00000000000 --- a/spec/models/mixins/aggregation_mixin_spec.rb +++ /dev/null @@ -1,138 +0,0 @@ -RSpec.describe AggregationMixin do - let(:cpu_speed) { 2_999 * 8 } - let(:memory) { 2_048 } - let(:hardware_args) do - { - :cpu_sockets => 2, - :cpu_cores_per_socket => 4, - :cpu_total_cores => 8, - :cpu_speed => 2_999, - :disk_capacity => 40, - :memory_mb => memory, - } - end - - # uses parameters - describe "#aggregate_cpu_speed" do - it "calculates a cluster" do - cluster = cluster_2_1_host(hardware_args) - expect(cluster.aggregate_cpu_speed).to eq(cpu_speed * 2) - end - - it "calculates from objects" do - cluster = cluster_2_1_host(hardware_args) - partial_cluster = cluster.hosts[1..1] + cluster.hosts[2..2] - expect(cluster.aggregate_cpu_speed(partial_cluster)).to eq(cpu_speed) - end - - it "calculates from ids" do - cluster = cluster_2_1_host(hardware_args) - partial_cluster = cluster.hosts[1..1] + cluster.hosts[2..2] - expect(cluster.aggregate_cpu_speed(partial_cluster.map(&:id))).to eq(cpu_speed) - end - end - - # uses parameters - describe "#aggregate_cpu_total_cores" do - it "calculates a cluster" do - cluster = cluster_2_1_host(hardware_args) - expect(cluster.aggregate_cpu_total_cores).to eq(8 * 2) - end - - it "calculates from objects" do - cluster = cluster_2_1_host(hardware_args) - partial_cluster = cluster.hosts[1..1] + cluster.hosts[2..2] - expect(cluster.aggregate_cpu_total_cores(partial_cluster)).to eq(8) - end - - it "calculates from ids" do - cluster = cluster_2_1_host(hardware_args) - partial_cluster = cluster.hosts[1..1] + cluster.hosts[2..2] - expect(cluster.aggregate_cpu_total_cores(partial_cluster.map(&:id))).to eq(8) - end - end - - describe "#aggregate_physical_cpus" do - it "calculates a cluster" do - cluster = cluster_2_1_host(hardware_args) - expect(cluster.aggregate_physical_cpus).to eq(2 * 2 + 1) - end - end - - # uses parameters - describe "#aggregate_memory" do - it "calculates a cluster" do - cluster = cluster_2_1_host(hardware_args) - expect(cluster.aggregate_memory).to eq(memory * 2) - end - - it "calculates from objects" do - cluster = cluster_2_1_host(hardware_args) - partial_cluster = cluster.hosts[1..1] + cluster.hosts[2..2] - expect(cluster.aggregate_memory(partial_cluster)).to eq(memory) - end - - it "calculates from ids" do - cluster = cluster_2_1_host(hardware_args) - partial_cluster = cluster.hosts[1..1] + cluster.hosts[2..2] - expect(cluster.aggregate_memory(partial_cluster.map(&:id))).to eq(memory) - end - end - - describe "#aggregate_vm_cpus" do - it "calculates a cluster" do - cluster = cluster_3_1_vm(hardware_args) - expect(cluster.aggregate_vm_cpus).to eq(2 * 3 + 1) - end - end - - describe "#aggregate_vm_memory" do - it "calculates a cluster" do - cluster = cluster_3_1_vm(hardware_args) - expect(cluster.aggregate_vm_memory).to eq(memory * 3) - end - end - - describe "#aggregate_disk_capacity" do - it "calculates a cluster" do - cluster = cluster_2_1_host(hardware_args) - expect(cluster.aggregate_disk_capacity).to eq(40 * 2) - end - - it "calculates from vms" do - cluster = cluster_2_1_host(hardware_args) - partial_cluster = cluster.hosts[1..1] + cluster.hosts[2..2] - expect(cluster.aggregate_disk_capacity(partial_cluster)).to eq(40) - end - - it "calculates from vm ids" do - cluster = cluster_2_1_host(hardware_args) - partial_cluster = cluster.hosts[1..1] + cluster.hosts[2..2] - expect(cluster.aggregate_disk_capacity(partial_cluster.map(&:id))).to eq(40) - end - end - - describe "aggregate_hardware" do - it "calculates from hosts" do - cluster = cluster_2_1_host(hardware_args) - expect(cluster.aggregate_hardware("host", :aggregate_cpu_speed)).to eq(cpu_speed * 2) - end - end - - private - - def cluster_2_1_host(hardware_args) - hosts = Array.new(2) do - FactoryBot.create(:host, :hardware => FactoryBot.create(:hardware, hardware_args)) - end + [FactoryBot.create(:host, :hardware => FactoryBot.create(:hardware))] - FactoryBot.create(:ems_cluster, :hosts => hosts) - end - - def cluster_3_1_vm(hardware_args) - vms = Array.new(3) do - FactoryBot.create(:vm, :hardware => FactoryBot.create(:hardware, hardware_args)) - end + [FactoryBot.create(:vm, :hardware => FactoryBot.create(:hardware))] - - FactoryBot.create(:ems_cluster, :vms => vms) - end -end diff --git a/spec/models/zone_spec.rb b/spec/models/zone_spec.rb index 1f80a983099..e52f547f0b9 100644 --- a/spec/models/zone_spec.rb +++ b/spec/models/zone_spec.rb @@ -1,4 +1,6 @@ RSpec.describe Zone do + include_examples "AggregationMixin", "ext_management_systems" + context ".seed" do before { MiqRegion.seed } include_examples ".seed called multiple times", 2 diff --git a/spec/support/examples_group/shared_examples_for_aggregation_mixin.rb b/spec/support/examples_group/shared_examples_for_aggregation_mixin.rb new file mode 100644 index 00000000000..2f857e4adb7 --- /dev/null +++ b/spec/support/examples_group/shared_examples_for_aggregation_mixin.rb @@ -0,0 +1,69 @@ +shared_examples_for "AggregationMixin" do |through| + context "includes AggregationMixin" do + include Spec::Support::ArelHelper + + let(:hardware) do + FactoryBot.create(:hardware, + :cpu2x2, + :ram1GB, + :cpu_speed => 2_999, + :disk_capacity => 40, + :memory_mb => 2048) + end + + let(:ems) { FactoryBot.create(:ext_management_system) } + let!(:vm) { FactoryBot.create(:vm, :hardware => hardware, :ext_management_system => ems) } + let!(:vm1) { FactoryBot.create(:vm, :hardware => hardware, :ext_management_system => ems) } + let!(:host) { FactoryBot.create(:host, :hardware => hardware, :ext_management_system => ems) } + let!(:host1) { FactoryBot.create(:host, :hardware => hardware, :ext_management_system => ems) } + let(:object) { FactoryBot.create(described_class.to_s.underscore.to_sym) } + + before do + if through == "ext_management_systems" + object.ext_management_systems << ems + elsif through == "computer_systems" + object.computer_system_hardwares << [hardware] + elsif described_class == Host + object.host_hardwares << [hardware] + object.vms_and_templates << [vm, vm1] + else + object.hosts << [host, host1] + object.vms_and_templates << [vm, vm1] + end + end + + describe "calculates single object" do + context "host" do + it "calculates #aggregate_cpu_speed" do + expect { expect(object.aggregate_cpu_speed).to eq(11_996) }.to_not exceed_query_limit(1) + end + + it "calculates #aggregate_cpu_total_cores" do + expect { expect(object.aggregate_cpu_total_cores).to eq(4) }.to_not exceed_query_limit(1) + end + + it "calculates #aggregate_disk_capacity" do + expect { expect(object.aggregate_disk_capacity).to eq(0.4e2) }.to_not exceed_query_limit(1) + end + + it "calculates #aggregate_memory" do + expect { expect(object.aggregate_memory).to eq(2048) }.to_not exceed_query_limit(1) + end + + it "calculates #aggregate_physical_cpus" do + expect { expect(object.aggregate_physical_cpus).to eq(2) }.to_not exceed_query_limit(1) + end + end + + context "vm" do + it "calculates #aggregate_vm_memory" do + expect { expect(object.aggregate_vm_memory).to eq(2048) }.to_not exceed_query_limit(1) + end + + it "calculates #aggregate_vm_cpus" do + expect { expect(object.aggregate_vm_cpus).to eq(2) }.to_not exceed_query_limit(1) + end + end + end + end +end