From c836f92ab77bea69c09ba00c9f70c8ffed6ec515 Mon Sep 17 00:00:00 2001 From: Bastian Schmidt Date: Tue, 19 Nov 2024 09:43:47 +0100 Subject: [PATCH] Adapt Rails tests to model changes * Split quota stubbing in utilization and missing hosts for better testing * Adapt ResourceQuota Tests and leverage new factory option :with_existing_host_resources * Adapt Host::Managed tests and add host_resources cases --- .../api/v2/resource_quotas_test.rb | 4 +- .../concerns/host_managed_extension_test.rb | 359 +++++++++++++----- test/models/resource_quota_test.rb | 140 ++++--- test/test_plugin_helper.rb | 18 +- 4 files changed, 333 insertions(+), 188 deletions(-) diff --git a/test/controllers/api/v2/resource_quotas_test.rb b/test/controllers/api/v2/resource_quotas_test.rb index a690bdb..03270a8 100644 --- a/test/controllers/api/v2/resource_quotas_test.rb +++ b/test/controllers/api/v2/resource_quotas_test.rb @@ -123,7 +123,7 @@ def setup test 'should show utilization' do exp_utilization = { cpu_cores: 10, memory_mb: 20 } - stub_quota_utilization(exp_utilization, {}) + stub_quota_utilization(exp_utilization) get :utilization, params: { resource_quota_id: @quota.id }, session: set_session_user assert_response :success show_response = ActiveSupport::JSON.decode(@response.body) @@ -134,7 +134,7 @@ def setup test 'should show missing_hosts' do exp_missing_hosts = { 'some_host' => %i[cpu_cores memory_mb] } - stub_quota_utilization({}, exp_missing_hosts) + stub_quota_missing_hosts(exp_missing_hosts) get :missing_hosts, params: { resource_quota_id: @quota.id }, session: set_session_user assert_response :success show_response = ActiveSupport::JSON.decode(@response.body) diff --git a/test/models/concerns/host_managed_extension_test.rb b/test/models/concerns/host_managed_extension_test.rb index 4f42e58..b116237 100644 --- a/test/models/concerns/host_managed_extension_test.rb +++ b/test/models/concerns/host_managed_extension_test.rb @@ -6,16 +6,66 @@ module ForemanResourceQuota class HostManagedExtensionTest < ActiveSupport::TestCase include ForemanResourceQuota::ResourceQuotaHelper - describe 'host create validation' do - def setup - # Set relevant settings explicitly - Setting[:resource_quota_global_no_action] = false - Setting[:resource_quota_optional_assignment] = false - User.current.resource_quota_is_optional = false + def setup + # Set relevant settings explicitly + Setting[:resource_quota_global_no_action] = false + Setting[:resource_quota_optional_assignment] = false + User.current.resource_quota_is_optional = false + end + + describe 'assign resource quota' do + test 'assign a resource quota' do + Setting[:resource_quota_optional_assignment] = true + host = FactoryBot.create(:host) + quota = FactoryBot.create(:resource_quota) + host.resource_quota = quota + assert_equal host.resource_quota_host.resource_quota_id, quota.id + assert_equal host.resource_quota_id, quota.id + end + + test 'disassociate resource quota via .resource_quota' do + Setting[:resource_quota_optional_assignment] = true + host = FactoryBot.create(:host) + quota = FactoryBot.create(:resource_quota) + host.resource_quota = quota + assert_equal host.resource_quota_host.resource_quota_id, quota.id + assert_equal host.resource_quota_id, quota.id + host.resource_quota = nil # destroy the association + host.reload + assert_nil host.resource_quota_host + assert_nil host.resource_quota + end + + test 'disassociate resource quota via .resource_quota_id to nil' do + Setting[:resource_quota_optional_assignment] = true + host = FactoryBot.create(:host) + quota = FactoryBot.create(:resource_quota) + host.resource_quota = quota + assert_equal host.resource_quota_host.resource_quota_id, quota.id + assert_equal host.resource_quota_id, quota.id + host.resource_quota_id = nil # destroy the association + host.reload + assert_nil host.resource_quota_host + assert_nil host.resource_quota + end + + test 'disassociate resource quota via .resource_quota_id to empty string' do + Setting[:resource_quota_optional_assignment] = true + host = FactoryBot.create(:host) + quota = FactoryBot.create(:resource_quota) + host.resource_quota = quota + assert_equal host.resource_quota_host.resource_quota_id, quota.id + assert_equal host.resource_quota_id, quota.id + host.resource_quota_id = '' # destroy the association + host.reload + assert_nil host.resource_quota_host + assert_nil host.resource_quota end + end + describe 'host create validation' do test 'should validate resource quota capacity' do - Host.any_instance.expects(:check_resource_quota_capacity).once + Host.any_instance.expects(:verify_resource_quota).once FactoryBot.create(:host, :with_resource_quota) end @@ -33,172 +83,277 @@ def setup assert FactoryBot.create(:host) end + test 'should have a default host_resources attribute' do + Setting[:resource_quota_optional_assignment] = true + host = FactoryBot.create(:host) + Setting[:resource_quota_optional_assignment] = false + assert_not_nil host.host_resources + end + test 'should fail without resource quota' do assert_raises(ActiveRecord::RecordInvalid) { FactoryBot.create(:host) } end end + describe 'host destroy validation' do + test 'should destroy host' do + quota = FactoryBot.create(:resource_quota, :with_existing_host_resources, + host_resources: { + cpu_cores: 5, + memory_mb: 5 * 1024, + disk_gb: 10, + }, + cpu_cores: 20, + memory_mb: 20 * 1024, + disk_gb: 50) + stub_host_utilization({ cpu_cores: 2, memory_mb: 4 * 1024, disk_gb: 20 }, {}) # pass host utilization + + host = FactoryBot.create(:host, resource_quota: quota) + quota.reload + + assert host.save + assert_equal({ cpu_cores: 7, memory_mb: 9 * 1024, disk_gb: 30 }, quota.utilization) + + assert host.destroy + quota.reload + assert_equal({ cpu_cores: 5, memory_mb: 5 * 1024, disk_gb: 10 }, quota.utilization) + end + + test 'missing_hosts are updated on host destroy' do + Setting[:resource_quota_optional_assignment] = true + host_a = FactoryBot.create :host + host_b = FactoryBot.create :host + host_utilization = { + host_a.name => { cpu_cores: 1 }, + host_b.name => { cpu_cores: 1 }, + } + host_missing_resources = { + host_a.name => [:memory_mb], + host_b.name => [:memory_mb], + } + quota = FactoryBot.create(:resource_quota, cpu_cores: 10, memory_mb: 10) + quota.hosts << [host_a, host_b] + quota.save + + quota.stub(:call_utilization_helper, [host_utilization, host_missing_resources]) do + quota.determine_utilization + end + assert_equal quota.number_of_hosts, quota.number_of_hosts + assert host_a.destroy + assert_equal host_b.name, quota.missing_hosts.keys.first + assert host_b.destroy + assert_equal 0, quota.number_of_hosts + end + end + describe 'resource quota capacity' do - def setup - @host = FactoryBot.create(:host, :with_resource_quota) - @quota = @host.resource_quota - Setting[:resource_quota_global_no_action] = false - Setting[:resource_quota_optional_assignment] = false - User.current.resource_quota_is_optional = false + def validation_error_message_host(resource, exceeding, max) + /Validation failed: Resource quota Host exceeds #{resource} limit of \ +'[\w\s]+'-quota by #{exceeding} \(max\. #{max}\)/ + end + + def validation_error_message_quota(resource, exceeding, max) + /Validation failed: Resource quota #{resource} limit of '[\w\s]+'-quota is \ +already exceeded by #{exceeding} without adding the new host \(max\. #{max}\)/ end test 'should fail at determine utilization' do - stub_quota_utilization({}, { 'my.missing.host': [:cpu_cores] }) # fail on quota utilization + stub_quota_missing_hosts({ 'my.missing.host': [:cpu_cores] }) # fail on quota utilization stub_host_utilization({ cpu_cores: 5 }, {}) # pass host utilization - host = FactoryBot.create(:host, :with_resource_quota) - host.resource_quota.update!(cpu_cores: 10) - assert_not host.save - assert_includes host.errors.full_messages, # TODO: Determine why the error fails - "Resource quota Resource Quota '#{host.resource_quota.name}' cannot determine resources for 1 hosts." + quota = FactoryBot.create(:resource_quota, cpu_cores: 10) + error = assert_raises(ActiveRecord::RecordInvalid) do + FactoryBot.create(:host, resource_quota: quota) + end + + assert_match(/Resource quota Resource Quota '[\w\s]+' cannot determine resources for 1 hosts./, error.message) end test 'should fail at determine host resources' do - stub_quota_utilization({ cpu_cores: 5 }, {}) # pass quota utilization + quota = FactoryBot.create(:resource_quota, :with_existing_host_resources, + host_resources: { cpu_cores: 10 }, cpu_cores: 5) stub_host_utilization({}, { 'my.missing.host': [:cpu_cores] }) # fail on host utilization - host = FactoryBot.create(:host, :with_resource_quota) - host.resource_quota.update!(cpu_cores: 10) + error = assert_raises(ActiveRecord::RecordInvalid) do + FactoryBot.create(:host, hostname: 'my.missing.host', resource_quota: quota) + end - assert_not host.save - assert_includes host.errors.full_messages, - "Resource quota Cannot determine host resources for #{host.name}" + assert_match(/Validation failed: Resource quota Cannot determine host resources for [\w\s]+/, error.message) end test 'should fail due to new host at verify limits (CPU cores)' do - stub_quota_utilization({ cpu_cores: 5 }, {}) # pass quota utilization + quota = FactoryBot.create(:resource_quota, :with_existing_host_resources, + host_resources: { cpu_cores: 5 }, cpu_cores: 10) stub_host_utilization({ cpu_cores: 10 }, {}) # pass host utilization - host = FactoryBot.create(:host, :with_resource_quota) - host.resource_quota.update!(cpu_cores: 10) + error = assert_raises(ActiveRecord::RecordInvalid) { FactoryBot.create(:host, resource_quota: quota) } - assert_not host.save - assert_includes host.errors.full_messages, - "Resource quota Host exceeds CPU cores limit of '#{host.resource_quota.name}'-quota " \ - 'by 5 cores (max. 10 cores)' + assert_match(validation_error_message_host('CPU cores', '5 cores', '10 cores'), error.message) end test 'should fail due to new host at verify limits (disk space)' do - stub_quota_utilization({ disk_gb: 5 }, {}) # pass quota utilization + quota = FactoryBot.create(:resource_quota, :with_existing_host_resources, + host_resources: { disk_gb: 5 }, disk_gb: 10) stub_host_utilization({ disk_gb: 10 }, {}) # pass host utilization - host = FactoryBot.create(:host, :with_resource_quota) - host.resource_quota.update!(disk_gb: 10) + error = assert_raises(ActiveRecord::RecordInvalid) { FactoryBot.create(:host, resource_quota: quota) } - assert_not host.save - assert_includes host.errors.full_messages, - "Resource quota Host exceeds Disk space limit of '#{host.resource_quota.name}'-quota " \ - 'by 5 GB (max. 10 GB)' + assert_match(validation_error_message_host('Disk space', '5 GB', '10 GB'), error.message) end test 'should fail due to new host at verify limits (memory)' do - stub_quota_utilization({ memory_mb: 5 * 1024 }, {}) # pass quota utilization + quota = FactoryBot.create(:resource_quota, :with_existing_host_resources, + host_resources: { memory_mb: 5 * 1024 }, memory_mb: 10 * 1024) stub_host_utilization({ memory_mb: 10 * 1024 }, {}) # pass host utilization - host = FactoryBot.create(:host, :with_resource_quota) - host.resource_quota.update!(memory_mb: 10 * 1024) + error = assert_raises(ActiveRecord::RecordInvalid) { FactoryBot.create(:host, resource_quota: quota) } - assert_not host.save - assert_includes host.errors.full_messages, - "Resource quota Host exceeds Memory limit of '#{host.resource_quota.name}'-quota " \ - 'by 5 GB (max. 10 GB)' + assert_match(validation_error_message_host('Memory', '5 GB', '10 GB'), error.message) end test 'should fail due to quota utilization at verify limits (CPU cores)' do - stub_quota_utilization({ cpu_cores: 15 }, {}) # pass quota utilization - stub_host_utilization({ cpu_cores: 10 }, {}) # pass host utilization + quota = FactoryBot.create(:resource_quota, :with_existing_host_resources, + host_resources: { cpu_cores: 10 }, cpu_cores: 5) + stub_host_utilization({ cpu_cores: 5 }, {}) # pass host utilization - host = FactoryBot.create(:host, :with_resource_quota) - host.resource_quota.update!(cpu_cores: 10) + error = assert_raises(ActiveRecord::RecordInvalid) { FactoryBot.create(:host, resource_quota: quota) } - assert_not host.save - assert_includes host.errors.full_messages, - "Resource quota CPU cores limit of '#{host.resource_quota.name}'-quota " \ - 'is already exceeded by 5 cores without adding the new host (max. 10 cores)' + assert_match(validation_error_message_quota('CPU cores', '5 cores', '5 cores'), error.message) end test 'should fail due to quota utilization at verify limits (disk space)' do - stub_quota_utilization({ disk_gb: 15 }, {}) # pass quota utilization - stub_host_utilization({ disk_gb: 10 }, {}) # pass host utilization + quota = FactoryBot.create(:resource_quota, :with_existing_host_resources, + host_resources: { disk_gb: 10 }, disk_gb: 5) + stub_host_utilization({ disk_gb: 5 }, {}) # pass host utilization - host = FactoryBot.create(:host, :with_resource_quota) - host.resource_quota.update!(disk_gb: 10) + error = assert_raises(ActiveRecord::RecordInvalid) { FactoryBot.create(:host, resource_quota: quota) } - assert_not host.save - assert_includes host.errors.full_messages, - "Resource quota Disk space limit of '#{host.resource_quota.name}'-quota " \ - 'is already exceeded by 5 GB without adding the new host (max. 10 GB)' + assert_match(validation_error_message_quota('Disk space', '5 GB', '5 GB'), error.message) end test 'should fail due to quota utilization at verify limits (memory)' do - stub_quota_utilization({ memory_mb: 15 * 1024 }, {}) # pass quota utilization - stub_host_utilization({ memory_mb: 10 * 1024 }, {}) # pass host utilization + quota = FactoryBot.create(:resource_quota, :with_existing_host_resources, + host_resources: { memory_mb: 10 * 1024 }, memory_mb: 5 * 1024) + stub_host_utilization({ memory_mb: 5 * 1024 }, {}) # pass host utilization - host = FactoryBot.create(:host, :with_resource_quota) - host.resource_quota.update!(memory_mb: 10 * 1024) + error = assert_raises(ActiveRecord::RecordInvalid) { FactoryBot.create(:host, resource_quota: quota) } - assert_not host.save - assert_includes host.errors.full_messages, - "Resource quota Memory limit of '#{host.resource_quota.name}'-quota " \ - 'is already exceeded by 5 GB without adding the new host (max. 10 GB)' + assert_match(validation_error_message_quota('Memory', '5 GB', '5 GB'), error.message) end test 'should validate single host capacity' do - stub_quota_utilization({ memory_mb: 0 }, {}) # pass quota utilization stub_host_utilization({ memory_mb: 10 * 1024 }, {}) # pass host utilization - host = FactoryBot.create(:host, :with_resource_quota) - host.resource_quota.update!(memory_mb: 10 * 1024) + quota = FactoryBot.create(:resource_quota, memory_mb: 20 * 1024) + host = FactoryBot.create(:host, resource_quota: quota) + quota.reload assert host.save - # TODO: Test must be adapted, when host resources are added to resource quota - # assert_equal 10 * 1024, host.resource_quota.utilization[:memory_mb] - assert_nil host.resource_quota.utilization[:cpu_cores] - assert_equal 0, host.resource_quota.utilization[:memory_mb] - assert_nil host.resource_quota.utilization[:disk_gb] + assert_equal(10 * 1024, quota.utilization[:memory_mb]) end test 'should validate multi limit capacity (host only)' do - stub_quota_utilization({ cpu_cores: 0, memory_mb: 0, disk_gb: 0 }, {}) # pass quota utilization stub_host_utilization({ cpu_cores: 5, memory_mb: 10 * 1024, disk_gb: 0 }, {}) # pass host utilization - host = FactoryBot.create(:host, :with_resource_quota) - host.resource_quota.update!(cpu_cores: 20) - host.resource_quota.update!(memory_mb: 20 * 1024) - host.resource_quota.update!(disk_gb: 50) + quota = FactoryBot.create(:resource_quota, cpu_cores: 20, memory_mb: 20 * 1024, disk_gb: 50) + host = FactoryBot.create(:host, resource_quota: quota) + quota.reload assert host.save - # TODO: Test must be adapted, when host resources are added to resource quota - # assert_equal 5, host.resource_quota.utilization[:cpu_cores] - # assert_equal 10 * 1024, host.resource_quota.utilization[:memory_mb] - # assert_equal 0, host.resource_quota.utilization[:disk_gb] - assert_equal 0, host.resource_quota.utilization[:cpu_cores] - assert_equal 0, host.resource_quota.utilization[:memory_mb] - assert_equal 0, host.resource_quota.utilization[:disk_gb] + assert_equal({ cpu_cores: 5, memory_mb: 10 * 1024, disk_gb: 0 }, quota.utilization) end test 'should validate multi limit capacity (with quota utilization)' do - stub_quota_utilization({ cpu_cores: 5, memory_mb: 5 * 1024, disk_gb: 10 }, {}) # pass quota utilization + quota = FactoryBot.create(:resource_quota, :with_existing_host_resources, + host_resources: { + cpu_cores: 5, + memory_mb: 5 * 1024, + disk_gb: 10, + }, + cpu_cores: 20, + memory_mb: 20 * 1024, + disk_gb: 50) + stub_host_utilization({ cpu_cores: 2, memory_mb: 4 * 1024, disk_gb: 20 }, {}) # pass host utilization + + host = FactoryBot.create(:host, resource_quota: quota) + quota.reload + + assert host.save + assert_equal({ cpu_cores: 7, memory_mb: 9 * 1024, disk_gb: 30 }, quota.utilization) + end + + test 'should remove host capacity from quota utilization' do + quota = FactoryBot.create(:resource_quota, :with_existing_host_resources, + host_resources: { + cpu_cores: 5, + memory_mb: 5 * 1024, + disk_gb: 10, + }, + cpu_cores: 20, + memory_mb: 20 * 1024, + disk_gb: 50) stub_host_utilization({ cpu_cores: 2, memory_mb: 4 * 1024, disk_gb: 20 }, {}) # pass host utilization - host = FactoryBot.create(:host, :with_resource_quota) - host.resource_quota.update!(cpu_cores: 20) - host.resource_quota.update!(memory_mb: 20 * 1024) - host.resource_quota.update!(disk_gb: 50) + host = FactoryBot.create(:host, resource_quota: quota) + quota.reload assert host.save - # TODO: Test must be adapted, when host resources are added to resource quota - # assert_equal 7, host.resource_quota.utilization[:cpu_cores] - # assert_equal 9 * 1024, host.resource_quota.utilization[:memory_mb] - # assert_equal 30, host.resource_quota.utilization[:disk_gb] - assert_equal 5, host.resource_quota.utilization[:cpu_cores] - assert_equal 5 * 1024, host.resource_quota.utilization[:memory_mb] - assert_equal 10, host.resource_quota.utilization[:disk_gb] + assert_equal({ cpu_cores: 7, memory_mb: 9 * 1024, disk_gb: 30 }, quota.utilization) + + host.destroy! + quota.reload + assert_equal({ cpu_cores: 5, memory_mb: 5 * 1024, disk_gb: 10 }, quota.utilization) + end + + test 'should add host capacity of two hosts to quota utilization' do + quota = FactoryBot.create(:resource_quota, :with_existing_host_resources, + host_resources: { + cpu_cores: 5, + memory_mb: 5 * 1024, + disk_gb: 10, + }, + cpu_cores: 20, + memory_mb: 20 * 1024, + disk_gb: 50) + stub_host_utilization({ cpu_cores: 2, memory_mb: 4 * 1024, disk_gb: 20 }, {}) # pass host utilization + + host_a = FactoryBot.create(:host, resource_quota: quota) + host_b = FactoryBot.create(:host, resource_quota: quota) + quota.reload + + assert host_a.save + assert host_b.save + assert_equal({ cpu_cores: 9, memory_mb: 13 * 1024, disk_gb: 50 }, quota.utilization) + + host_a.destroy! + quota.reload + assert_equal({ cpu_cores: 7, memory_mb: 9 * 1024, disk_gb: 30 }, quota.utilization) + end + + test 'should re-associate host capacity when changing resource quota' do + stub_host_utilization({ cpu_cores: 2, memory_mb: 4 * 1024, disk_gb: 20 }, {}) # pass host utilization + + quota_a = FactoryBot.create(:resource_quota, + cpu_cores: 20, + memory_mb: 20 * 1024, + disk_gb: 50) + quota_b = FactoryBot.create(:resource_quota, + cpu_cores: 20, + memory_mb: 20 * 1024, + disk_gb: 50) + host = FactoryBot.create(:host, resource_quota: quota_a) + quota_a.reload + + assert_equal({ cpu_cores: 2, memory_mb: 4 * 1024, disk_gb: 20 }, quota_a.utilization) + assert_equal({ cpu_cores: 0, memory_mb: 0, disk_gb: 0 }, quota_b.utilization) + + host.resource_quota = quota_b + host.save + quota_a.reload + quota_b.reload + + assert_equal({ cpu_cores: 0, memory_mb: 0, disk_gb: 0 }, quota_a.utilization) + assert_equal({ cpu_cores: 2, memory_mb: 4 * 1024, disk_gb: 20 }, quota_b.utilization) end end end diff --git a/test/models/resource_quota_test.rb b/test/models/resource_quota_test.rb index 64d8c4c..36e3191 100644 --- a/test/models/resource_quota_test.rb +++ b/test/models/resource_quota_test.rb @@ -77,50 +77,62 @@ def setup assert_equal 3, @quota.number_of_usergroups end + test 'utilization is empty' do + @quota.cpu_cores = 50 + exp_utilization = { cpu_cores: 0, memory_mb: nil, disk_gb: nil } + + assert_equal exp_utilization, @quota.utilization + end + + test 'utilization is nil' do + exp_utilization = { cpu_cores: nil, memory_mb: nil, disk_gb: nil } + + assert_equal exp_utilization, @quota.utilization + end + test 'utilization is set (cpu_cores)' do - @quota.utilization_cpu_cores = 13 + @quota.cpu_cores = 50 + @quota.hosts << @host + @quota.update_hosts_resources({ @host.name => { cpu_cores: 13 } }) + assert_equal 13, @quota.utilization[:cpu_cores] end test 'utilization is set (memory_mb)' do - @quota.utilization_memory_mb = 14 + @quota.memory_mb = 50 + @quota.hosts << @host + @quota.update_hosts_resources({ @host.name => { memory_mb: 14 } }) + assert_equal 14, @quota.utilization[:memory_mb] end test 'utilization is set (disk_gb)' do - @quota.utilization_disk_gb = 15 + @quota.disk_gb = 50 + @quota.hosts << @host + @quota.update_hosts_resources({ @host.name => { disk_gb: 15 } }) + assert_equal 15, @quota.utilization[:disk_gb] end test 'utilization is set (all parameters)' do exp_utilization = { cpu_cores: 3, memory_mb: 4, disk_gb: 5 } - @quota.utilization_cpu_cores = exp_utilization[:cpu_cores] - @quota.utilization_memory_mb = exp_utilization[:memory_mb] - @quota.utilization_disk_gb = exp_utilization[:disk_gb] - assert_equal exp_utilization, @quota.utilization - end + @quota.update(cpu_cores: 50, memory_mb: 50, disk_gb: 50) + @quota.hosts << @host + @quota.update_hosts_resources({ @host.name => exp_utilization }) - test 'utilization_ is set by utilization' do - exp_utilization = { cpu_cores: 6, memory_mb: 7, disk_gb: 8 } - @quota.utilization = exp_utilization assert_equal exp_utilization, @quota.utilization end - test 'utilization sets attributes' do - second_usergroup = FactoryBot.create :usergroup - third_usergroup = FactoryBot.create :usergroup - @quota.usergroups << [@usergroup, second_usergroup, third_usergroup] - assert_equal 3, @quota.number_of_usergroups - end - test 'determine utilization' do exp_utilization = { cpu_cores: 1, memory_mb: 1, disk_gb: 2 } exp_missing_hosts = {} + host_utilization = { + @host.name => exp_utilization, + } @quota.hosts << @host @quota.update(cpu_cores: 10, memory_mb: 10, disk_gb: 10) - as_admin { @quota.save! } - @quota.stub(:call_utilization_helper, [exp_utilization, exp_missing_hosts]) do + @quota.stub(:call_utilization_helper, [host_utilization, exp_missing_hosts]) do @quota.determine_utilization end assert_equal exp_utilization, @quota.utilization @@ -131,90 +143,72 @@ def setup host_a = FactoryBot.create :host host_b = FactoryBot.create :host exp_utilization = { cpu_cores: 1, memory_mb: 1, disk_gb: 2 } + host_utilization = { + host_a.name => { memory_mb: 1, disk_gb: 1 }, + host_b.name => { cpu_cores: 1, disk_gb: 1 }, + } exp_missing_hosts = { host_a.name => [:cpu_cores], host_b.name => [:memory_mb] } - @quota.hosts << [host_a, host_b] - @quota.update(cpu_cores: 10, memory_mb: 10, disk_gb: 10) - as_admin { @quota.save! } - - @quota.stub(:call_utilization_helper, [exp_utilization, exp_missing_hosts]) do - @quota.determine_utilization - end - assert_equal exp_utilization, @quota.utilization - assert_equal exp_missing_hosts, @quota.missing_hosts - end - - test 'utilization uses quota utilization_ fields' do - exp_utilization = { cpu_cores: 1, memory_mb: 1, disk_gb: 2 } - @quota.utilization_cpu_cores = exp_utilization[:cpu_cores] - @quota.utilization_memory_mb = exp_utilization[:memory_mb] - @quota.utilization_disk_gb = exp_utilization[:disk_gb] - - assert_equal exp_utilization, @quota.utilization - end - test 'missing_hosts are constructed' do - host_a = FactoryBot.create :host - host_b = FactoryBot.create :host - exp_utilization = { cpu_cores: 1, memory_mb: 1, disk_gb: 2 } - exp_missing_hosts = { host_a.name => [:cpu_cores], host_b.name => [:memory_mb] } @quota.hosts << [host_a, host_b] @quota.update(cpu_cores: 10, memory_mb: 10, disk_gb: 10) - as_admin { @quota.save! } - @quota.stub(:call_utilization_helper, [exp_utilization, exp_missing_hosts]) do + @quota.stub(:call_utilization_helper, [host_utilization, exp_missing_hosts]) do @quota.determine_utilization end - @quota.reload + assert_equal exp_utilization, @quota.utilization assert_equal exp_missing_hosts, @quota.missing_hosts - assert_equal 2, @quota.resource_quotas_missing_hosts.size - assert_equal host_a.id, @quota.resource_quotas_missing_hosts.find_by(missing_host_id: host_a.id).missing_host_id - assert_equal host_b.id, @quota.resource_quotas_missing_hosts.find_by(missing_host_id: host_b.id).missing_host_id - assert_equal host_a.resource_quota_missing_resources.resource_quota.id, @quota.id end test 'missing_hosts are destroyed on host destroy' do host_a = FactoryBot.create :host host_b = FactoryBot.create :host - exp_utilization = { cpu_cores: 1, memory_mb: 1, disk_gb: 2 } + host_utilization = { + host_a.name => { memory_mb: 1, disk_gb: 1 }, + host_b.name => { cpu_cores: 1, disk_gb: 1 }, + } exp_missing_hosts = { host_a.name => [:cpu_cores], host_b.name => [:memory_mb] } @quota.hosts << [host_a, host_b] @quota.update(cpu_cores: 10, memory_mb: 10, disk_gb: 10) - as_admin { @quota.save! } + @quota.save! - @quota.stub(:call_utilization_helper, [exp_utilization, exp_missing_hosts]) do + @quota.stub(:call_utilization_helper, [host_utilization, exp_missing_hosts]) do @quota.determine_utilization end - assert_equal 2, @quota.resource_quotas_missing_hosts.size - host_a.destroy! - @quota.reload - assert_equal 1, @quota.resource_quotas_missing_hosts.size - assert_equal host_b.id, @quota.resource_quotas_missing_hosts[0].missing_host.id - host_b.destroy! - @quota.reload - assert_equal 0, @quota.resource_quotas_missing_hosts.size + assert_equal @quota.number_of_missing_hosts, @quota.number_of_hosts + host_a.destroy + assert_equal 1, @quota.number_of_missing_hosts + assert_equal host_b.name, @quota.missing_hosts.keys.first + host_b.destroy + assert_equal 0, @quota.number_of_missing_hosts end test 'missing_hosts are destroyed on re-computing utilization' do + @quota.update(cpu_cores: 10, memory_mb: 10) host_a = FactoryBot.create :host host_b = FactoryBot.create :host - exp_utilization = { cpu_cores: 1, memory_mb: 1, disk_gb: 2 } - exp_missing_hosts_two = { host_a.name => [:cpu_cores], host_b.name => [:memory_mb] } - exp_missing_hosts_one = { host_b.name => [:memory_mb] } + host_utilization_two = { + host_a.name => { cpu_cores: 1, memory_mb: nil }, + host_b.name => { cpu_cores: nil, memory_mb: 1 }, + } + host_utilization_one = { + host_a.name => { cpu_cores: 1, memory_mb: 1 }, + host_b.name => { cpu_cores: nil, memory_mb: 1 }, + } + exp_missing_hosts_two = { host_a.name => [:memory_mb], host_b.name => [:cpu_cores] } + exp_missing_hosts_one = { host_b.name => [:cpu_cores] } + @quota.hosts << [host_a, host_b] - @quota.update(cpu_cores: 10, memory_mb: 10, disk_gb: 10) - as_admin { @quota.save! } - @quota.stub(:call_utilization_helper, [exp_utilization, exp_missing_hosts_two]) do + @quota.stub(:call_utilization_helper, [host_utilization_two, exp_missing_hosts_two]) do @quota.determine_utilization end - assert_equal 2, @quota.resource_quotas_missing_hosts.size - @quota.stub(:call_utilization_helper, [exp_utilization, exp_missing_hosts_one]) do + assert_equal 2, @quota.number_of_missing_hosts + @quota.stub(:call_utilization_helper, [host_utilization_one, exp_missing_hosts_one]) do @quota.determine_utilization end @quota.reload - assert_equal 1, @quota.resource_quotas_missing_hosts.size - assert_equal host_b.id, @quota.resource_quotas_missing_hosts - .find_by(missing_host_id: host_b.id).missing_host_id + assert_equal 1, @quota.number_of_missing_hosts + assert_equal [host_b.name], @quota.missing_hosts.keys end end end diff --git a/test/test_plugin_helper.rb b/test/test_plugin_helper.rb index 83a97a6..7b692ee 100644 --- a/test/test_plugin_helper.rb +++ b/test/test_plugin_helper.rb @@ -30,20 +30,16 @@ def assert_quota_equal(expexted_list, quota) end end -def stub_quota_utilization(return_utilization, return_missing_hosts) - ForemanResourceQuota::ResourceQuota.any_instance.stubs(:call_utilization_helper) - .returns([return_utilization, return_missing_hosts]) +def stub_quota_utilization(return_hosts_utilization) + ForemanResourceQuota::ResourceQuota.any_instance.stubs(:utilization) + .returns(return_hosts_utilization) +end + +def stub_quota_missing_hosts(return_missing_hosts) ForemanResourceQuota::ResourceQuota.any_instance.stubs(:missing_hosts) .returns(return_missing_hosts) - ForemanResourceQuota::ResourceQuota.any_instance.stubs(:missing_hosts=) - .returns - ForemanResourceQuota::ResourceQuota.any_instance.stubs(:utilization) - .returns(return_utilization) - ForemanResourceQuota::ResourceQuota.any_instance.stubs(:utilization=) - .returns end def stub_host_utilization(return_utilization, return_missing_hosts) - Host::Managed.any_instance.stubs(:call_utilization_helper) - .returns([return_utilization, return_missing_hosts]) + Host::Managed.any_instance.stubs(:call_utilization_helper).returns([return_utilization, return_missing_hosts]) end