Skip to content

Commit

Permalink
Fixes #2 - Don't blow up with an invalid Hostgroup setting
Browse files Browse the repository at this point in the history
  • Loading branch information
GregSutcliffe committed Oct 8, 2013
1 parent 1c171d2 commit edb0e13
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/default_hostgroup_managed_host_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ module ClassMethods
def importHostAndFacts_with_apply_hostgroup hostname, facts, certname = nil
host, result = importHostAndFacts_without_apply_hostgroup(hostname, facts, certname)
Rails.logger.debug "DefaultHostgroup: performing Hostgroup check"

unless valid_hostgroup?
Rails.logger.debug "DefaultHostgroup: Could not find Hostgroup '#{Setting[:default_hostgroup]}'"
return host, result
end

# host.new_record? will only test for the early return in the core method, a real host
# will have already been saved at least once.
if host.present? && !host.new_record? && host.hostgroup.nil? && host.reports.empty?
Expand All @@ -22,5 +28,9 @@ def importHostAndFacts_with_apply_hostgroup hostname, facts, certname = nil
end
return host, result
end

def valid_hostgroup?
Hostgroup.find_by_label(Setting[:default_hostgroup]) ? true : false
end
end
end
8 changes: 8 additions & 0 deletions test/unit/default_hostgroup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ def setup_hostgroup
assert_equal @hostgroup, Host.find_by_name('sinn1636.lan').hostgroup
end

test "an invalid hostgroup setting does nothing" do
setup_hostgroup
Setting[:default_hostgroup] = "doesnotexist"
raw = parse_json_fixture('/facts.json')
assert Host.importHostAndFacts(raw['name'], raw['facts'])
refute Host.find_by_name('sinn1636.lan').hostgroup
end

end

0 comments on commit edb0e13

Please sign in to comment.