Skip to content

Commit

Permalink
Fixes #37589 - Store the BuildStatus in DB
Browse files Browse the repository at this point in the history
The method to refrsh the build status is called by before_validation and
the BuildStatus object is able to determine the status correctly - but
unfortunately, it is not stored on to the DB.
  • Loading branch information
sbernhard authored and ofedoren committed Jul 8, 2024
1 parent e685987 commit f0e2655
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
14 changes: 8 additions & 6 deletions app/models/host/managed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -779,11 +779,8 @@ def refresh_statuses(which = HostStatus.status_registry)
end
def get_status(type)
status = host_statuses.detect { |s| s.type == type.to_s }
if status.nil?
host_statuses.new(:host => self, :type => type.to_s)
else
status
end
return status unless status.nil?
host_statuses.new(:host => self, :type => type.to_s)
end

def build_global_status(options = {})
Expand Down Expand Up @@ -952,7 +949,12 @@ def remove_reports
end

def refresh_build_status
get_status(HostStatus::BuildStatus).refresh
refresh_method = if new_record?
'refresh'
else
'refresh!'
end
get_status(HostStatus::BuildStatus).send(refresh_method)
end

def extract_params_from_object_ancestors(object)
Expand Down
23 changes: 21 additions & 2 deletions test/models/host_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,25 @@ class HostTest < ActiveSupport::TestCase
assert_equal status, new_status
end

test 'host #get_status(HostStatus::BuildStatus).to_status matches in DB stored "status' do
host = FactoryBot.build(:host)
host.build = false
status = host.get_status(HostStatus::BuildStatus)
original_status = status.to_status
status.save!
host.save!

assert_equal status.status, status.to_status

host.build = true
host.valid?

assert host.build?
status = host.get_status(HostStatus::BuildStatus)
assert_equal status.status, status.to_status
assert_not_equal original_status, status.to_status
end

test 'host #get_status(type) only builds a new status once' do
host = FactoryBot.build_stubbed(:host)
status1 = host.get_status(HostStatus::BuildStatus)
Expand Down Expand Up @@ -518,7 +537,7 @@ class HostTest < ActiveSupport::TestCase
end

test 'build status is updated on host validation' do
host = FactoryBot.build_stubbed(:host)
host = FactoryBot.build(:host)
host.build = false
host.valid?
original_status = host.get_status(HostStatus::BuildStatus).to_status
Expand Down Expand Up @@ -808,7 +827,7 @@ class HostTest < ActiveSupport::TestCase
end

test "should allow build mode for unmanaged hosts" do
h = FactoryBot.build_stubbed(:host)
h = FactoryBot.build(:host)
assert h.valid?
h.build = true
assert h.valid?
Expand Down

0 comments on commit f0e2655

Please sign in to comment.