Skip to content

Commit

Permalink
Fixes #11708 - removed auto_provisioning RABL, improved unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lzap committed Sep 11, 2015
1 parent e31a50d commit 5b111bd
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
15 changes: 12 additions & 3 deletions app/controllers/api/v2/discovered_hosts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,13 @@ def facts
def auto_provision
Host.transaction do
if rule = find_discovery_rule(@discovered_host)
msg = _("Host %{host} was provisioned with rule %{rule}") % {:host => @discovered_host.name, :rule => rule.name}
process_response perform_auto_provision(@discovered_host, rule), msg
if perform_auto_provision(@discovered_host, rule)
msg = _("Host %{host} was provisioned with rule %{rule}") % {:host => @discovered_host.name, :rule => rule.name}
render :json => {:message => msg}
else
msg = _("Unable to provision %{host}: %{errors}") % {:host => @discovered_host.name, :rule => @discovered_host.errors.join(' ')}
render :json => {:message => msg}, :status => :unprocessable_entity
end
else
render_error :custom_error, :status => :not_found,
:locals => {
Expand All @@ -145,6 +150,7 @@ def auto_provision_all
result = false
end

total_count = 0
Host.transaction do
overall_errors = ""
Host::Discovered.all.each do |discovered_host|
Expand All @@ -154,14 +160,17 @@ def auto_provision_all
errors = discovered_host.errors.full_messages.join(' ')
logger.warn "Failed to auto provision host %s: %s" % [discovered_host.name, errors]
overall_errors << "#{discovered_host.name}: #{errors} "
else
total_count += 1
end
else
logger.warn "No rule found for host %s" % discovered_host.name
end
end

if result
process_success _("Discovered hosts are provisioning now")
msg = _("%s discovered hosts were provisioned") % total_count
render :json => {:message => msg}
else
render_error :custom_error,
:status => :unprocessable_entity,
Expand Down
3 changes: 0 additions & 3 deletions app/views/api/v2/discovered_hosts/auto_provision.json.rabl

This file was deleted.

This file was deleted.

8 changes: 4 additions & 4 deletions test/functional/api/v2/discovered_hosts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ def test_auto_provision_success
disable_orchestration
facts = @facts.merge({"somefact" => "abc"})
host = Host::Discovered.import_host_and_facts(facts).first
FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc",
rule = FactoryGirl.create(:discovery_rule, :priority => 1, :search => "facts.somefact = abc",
:hostgroup => FactoryGirl.create(:hostgroup, :with_os, :with_rootpass),
:organizations => [host.organization], :locations => [host.location])
post :auto_provision, { :id => host.id }
assert_match host.name, @response.body
assert_match /Host #{host.name} was provisioned with rule #{rule.name}/, @response.body
assert_response :success
end

Expand Down Expand Up @@ -144,7 +144,7 @@ def test_auto_provision_all_success
:hostgroup => FactoryGirl.create(:hostgroup, :with_os, :with_rootpass), :organizations => [host.organization],
:locations => [host.location])
post :auto_provision_all, {}
assert_equal '{}', @response.body
assert_match /1 discovered hosts were provisioned/, @response.body
assert_response :success
end

Expand All @@ -153,7 +153,7 @@ def test_auto_provision_all_no_rule_success
facts = @facts.merge({"somefact" => "abc"})
Host::Discovered.import_host_and_facts(facts).first
post :auto_provision_all, {}
assert_equal '{}', @response.body
assert_match /0 discovered hosts were provisioned/, @response.body
assert_response :success
end

Expand Down
16 changes: 8 additions & 8 deletions test/unit/discovered_extensions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def setup_safemode_renderer
:hostname => '<%= "" %>',
:organizations => [host.organization],
:locations => [host.location])
perform_auto_provision host, r1
refute perform_auto_provision host, r1
assert_equal "macaabbccddeeff", host.name
end

Expand All @@ -146,7 +146,7 @@ def setup_safemode_renderer
:hostname => 'x<%= 1+1 %>',
:organizations => [host.organization],
:locations => [host.location])
perform_auto_provision host, r1
refute perform_auto_provision host, r1
assert_equal "x2", host.name
end

Expand All @@ -157,7 +157,7 @@ def setup_safemode_renderer
:hostname => 'x<%= rand(4) %>',
:organizations => [host.organization],
:locations => [host.location])
perform_auto_provision host, r1
refute perform_auto_provision host, r1
assert_match(/x[0123]/, host.name)
end

Expand All @@ -168,7 +168,7 @@ def setup_safemode_renderer
:hostname => 'x<%= @host.name %>',
:organizations => [host.organization],
:locations => [host.location])
perform_auto_provision host, r1
refute perform_auto_provision host, r1
assert_equal "xmacaabbccddeeff", host.name
end

Expand All @@ -179,19 +179,19 @@ def setup_safemode_renderer
:hostname => 'x<%= @host.ip.gsub(".","-") %>',
:organizations => [host.organization],
:locations => [host.location])
perform_auto_provision host, r1
refute perform_auto_provision host, r1
assert_equal "x192-168-100-42", host.name
end

test "hostname attribute facts_hash is renderer properly using #{renderer_name}" do
skip "until http://projects.theforeman.org/issues/2948 is fixed"
host = Host::Discovered.import_host_and_facts(@facts).first
facts = @facts.merge({"somefact" => "abc"})
host = Host::Discovered.import_host_and_facts(facts).first
r1 = FactoryGirl.create(:discovery_rule,
:search => "facts.somefact = abc",
:hostname => 'x<%= @host.facts["somefact"] %>',
:organizations => [host.organization],
:locations => [host.location])
perform_auto_provision host, r1
refute perform_auto_provision host, r1
assert_equal "xabc", host.name
end

Expand Down

0 comments on commit 5b111bd

Please sign in to comment.