Skip to content

Commit

Permalink
Add job test for RefreshResourceQuotaUtilization
Browse files Browse the repository at this point in the history
  • Loading branch information
bastian-src committed Nov 27, 2024
1 parent 4885f93 commit 7a00ee0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/foreman_resource_quota/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
47 changes: 47 additions & 0 deletions test/jobs/refresh_resource_quota_utilization_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7a00ee0

Please sign in to comment.