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 4c74c0d commit 18e5b44
Show file tree
Hide file tree
Showing 3 changed files with 51 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
45 changes: 45 additions & 0 deletions test/jobs/refresh_resource_quota_utilization_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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_hosts_resources = {
@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,
}
stub_quota_utilization_helper(quota_hosts_resources, {})
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
5 changes: 5 additions & 0 deletions test/test_plugin_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def stub_quota_missing_hosts(return_missing_hosts)
.returns(return_missing_hosts)
end

def stub_quota_utilization_helper(return_hosts_resources, return_missing_hosts)
ForemanResourceQuota::ResourceQuota.any_instance.stubs(:call_utilization_helper)
.returns([return_hosts_resources, return_missing_hosts])
end

def stub_host_utilization(return_utilization, return_missing_hosts)
Host::Managed.any_instance.stubs(:call_utilization_helper).returns([return_utilization, return_missing_hosts])
end

0 comments on commit 18e5b44

Please sign in to comment.