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 26, 2024
1 parent 5a336ac commit b9312ea
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions test/jobs/refresh_resource_quota_utilization_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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')
@quota = FactoryBot.create(:resource_quota, :with_existing_host_resources,
host_resources: { cpu_cores: 2 }, cpu_cores: 5)
@host = @quota.hosts[0]

quota = FactoryBot.create(:resource_quota, cpu_cores: 10)
stub_host_utilization({ cpu_cores: 2, memory_mb: 1024 * 4, disk_gb: 60 }, {})

@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)
end

test 'single resource quota utilization should be updated' do
exp_host_b_utilization = {
cpu_cores: 2 + 8,
memory_mb: (1024 * 4) + (1024 * 8),
disk_gb: 60 + 8,
}
assert_equal({ cpu_cores: 5 * 2, memory_mb: 5 * 1024 * 4, disk_gb: 5 * 60 }, @quota.utilization)
@host_b.host_resources.resources = exp_host_b_utilization
@host_b.host_resources.safe!
ForemanTasks.sync_task(ForemanResourceQuota::Async::RefreshResourceQuotaUtilization)
@quota.reload
@host_b.reload
assert_equal({ cpu_cores: (5 * 2) + 8, memory_mb: (5 * 1024 * 4) + (1024 * 8), disk_gb: (5 * 60) + 8 },
@quota.utilization)
assert_equal(exp_host_b_utilization, @host_b.host_resources.resources)
end
end

0 comments on commit b9312ea

Please sign in to comment.