From 7a00ee01b69d984ed23dd2654f16586a8d093c86 Mon Sep 17 00:00:00 2001 From: Bastian Schmidt Date: Tue, 26 Nov 2024 16:13:00 +0100 Subject: [PATCH] Add job test for RefreshResourceQuotaUtilization --- lib/foreman_resource_quota/engine.rb | 2 +- ...refresh_resource_quota_utilization_test.rb | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 test/jobs/refresh_resource_quota_utilization_test.rb diff --git a/lib/foreman_resource_quota/engine.rb b/lib/foreman_resource_quota/engine.rb index f73de8a..5c957fb 100644 --- a/lib/foreman_resource_quota/engine.rb +++ b/lib/foreman_resource_quota/engine.rb @@ -51,7 +51,7 @@ class Engine < ::Rails::Engine # Skip object creation if the admin user is not present # skip database manipulations while tables do not exist, like in migrations - if ActiveRecord::Base.connection.data_source_exists?(ForemanTasks::Task.table_name) && + if ActiveRecord::Base.connection.data_source_exists?(::ForemanTasks::Task.table_name) && User.unscoped.find_by_login(User::ANONYMOUS_ADMIN).present? # Register the scheduled tasks ::ForemanTasks.dynflow.config.on_init(false) do |_world| diff --git a/test/jobs/refresh_resource_quota_utilization_test.rb b/test/jobs/refresh_resource_quota_utilization_test.rb new file mode 100644 index 0000000..99301cc --- /dev/null +++ b/test/jobs/refresh_resource_quota_utilization_test.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require 'test_plugin_helper' +require 'foreman_tasks/test_helpers' + +class RefreshResourceQuotaUtilizationTest < ActiveSupport::TestCase + include ForemanTasks::TestHelpers::WithInThreadExecutor + + setup do + User.current = User.find_by(login: 'secret_admin') + Setting[:resource_quota_global_no_action] = false + Setting[:resource_quota_optional_assignment] = false + User.current.resource_quota_is_optional = false + + stub_host_utilization({ cpu_cores: 2, memory_mb: 1024 * 4, disk_gb: 60 }, {}) + @quota = FactoryBot.create(:resource_quota, cpu_cores: 20, memory_mb: 1024 * 30, disk_gb: 512) + + @host_a = FactoryBot.create(:host, resource_quota: @quota) + @host_b = FactoryBot.create(:host, resource_quota: @quota) + @host_c = FactoryBot.create(:host, resource_quota: @quota) + @host_d = FactoryBot.create(:host, resource_quota: @quota) + @host_e = FactoryBot.create(:host, resource_quota: @quota) + @quota.reload + end + + test 'single resource quota utilization should be updated' do + assert_equal({ cpu_cores: 5 * 2, memory_mb: 5 * 1024 * 4, disk_gb: 5 * 60 }, @quota.utilization) + new_host_utilization = { cpu_cores: 3, memory_mb: 1024 * 5, disk_gb: 61 } + quota_utilization_params = { + @host_a.name => new_host_utilization, + @host_b.name => new_host_utilization, + @host_c.name => new_host_utilization, + @host_d.name => new_host_utilization, + @host_e.name => new_host_utilization, + } + + ForemanResourceQuota::ResourceQuota.any_instance.stubs(:call_utilization_helper) + .returns([quota_utilization_params, {}]) + ForemanTasks.sync_task(ForemanResourceQuota::Async::RefreshResourceQuotaUtilization) + @quota.reload + assert_equal({ + cpu_cores: 5 * new_host_utilization[:cpu_cores], + memory_mb: 5 * new_host_utilization[:memory_mb], + disk_gb: 5 * new_host_utilization[:disk_gb], + }, @quota.utilization) + end +end