Project

General

Profile

Download (35.4 KB) Statistics
| Branch: | Tag: | Revision:
2f077f63 Ohad Levy
require 'test_helper'

class HostsControllerTest < ActionController::TestCase
d91160ea Lucas Tolchinsky
setup :initialize_host

510d53cd Marek Hulan
test 'show' do
86744a6b Ohad Levy
get :show, {:id => Host.first.name}, set_session_user
b09b4515 Ohad Levy
assert_template 'show'
2f077f63 Ohad Levy
end
8f10d0f8 Juan Manuel
510d53cd Marek Hulan
test 'create_invalid' do
b09b4515 Ohad Levy
Host.any_instance.stubs(:valid?).returns(false)
445dd7ab Daniel Lobato
post :create, {:host => {:name => nil}}, set_session_user
b09b4515 Ohad Levy
assert_template 'new'
8f10d0f8 Juan Manuel
end

510d53cd Marek Hulan
test 'create_valid' do
b09b4515 Ohad Levy
Host.any_instance.stubs(:valid?).returns(true)
9c0e127b Paul Kelly
post :create, {:host => {:name => "test"}}, set_session_user
assert_redirected_to host_url(assigns('host'))
8f10d0f8 Juan Manuel
end

9c0e127b Paul Kelly
test "should get index" do
get :index, {}, set_session_user
assert_response :success
assert_template 'index'
end

b6e0cb16 Ohad Levy
test "should render 404 when host is not found" do
4e7b2b98 Paul Kelly
get :show, {:id => "no.such.host"}, set_session_user
b6e0cb16 Ohad Levy
assert_response :missing
assert_template 'common/404'
end

9c0e127b Paul Kelly
test "should get new" do
get :new, {}, set_session_user
assert_response :success
assert_template 'new'
end

test "should create new host" do
assert_difference 'Host.count' do
post :create, { :commit => "Create",
:host => {:name => "myotherfullhost",
6874bbd9 Paul Kelly
:mac => "aabbecddee06",
90b83222 Ohad Levy
:ip => "2.3.4.125",
feacea35 Amos Benari
:domain_id => domains(:mydomain).id,
:operatingsystem_id => operatingsystems(:redhat).id,
:architecture_id => architectures(:x86_64).id,
:environment_id => environments(:production).id,
:subnet_id => subnets(:one).id,
6568415c Joseph Magen
:medium_id => media(:one).id,
77f70152 Stephen Benjamin
:realm_id => realms(:myrealm).id,
316a4ccd Ohad Levy
:disk => "empty partition",
c4bfd47f Stephen Benjamin
:puppet_proxy_id => smart_proxies(:puppetmaster).id,
160e24ea Joseph Magen
:root_pass => "xybxa6JUkz63w",
:location_id => taxonomies(:location1).id,
:organization_id => taxonomies(:organization1).id
6874bbd9 Paul Kelly
}
9c0e127b Paul Kelly
}, set_session_user
end
6874bbd9 Paul Kelly
assert_redirected_to host_url(assigns['host'])
04cb74c0 Shimon Shtein
end

test "should create new host with hostgroup inherited fields" do
leftovers = Host.search_for('myotherfullhost').first
refute leftovers
hostgroup = hostgroups(:common)
assert_difference 'Host.count' do
post :create, { :commit => "Create",
:host => {:name => "myotherfullhost",
:mac => "aabbecddee06",
:ip => "2.3.4.125",
:domain_id => domains(:mydomain).id,
:hostgroup_id => hostgroup.id,
:operatingsystem_id => operatingsystems(:redhat).id,
:architecture_id => architectures(:x86_64).id,
:subnet_id => subnets(:one).id,
:medium_id => media(:one).id,
:realm_id => realms(:myrealm).id,
:disk => "empty partition",
:root_pass => "xybxa6JUkz63w",
:location_id => taxonomies(:location1).id,
:organization_id => taxonomies(:organization1).id
}
}, set_session_user
end
new_host = Host.search_for('myotherfullhost').first
assert_equal new_host.environment, hostgroup.environment
assert_equal new_host.puppet_proxy, hostgroup.puppet_proxy
assert_redirected_to host_url(assigns['host'])
9c0e127b Paul Kelly
end

test "should get edit" do
86744a6b Ohad Levy
get :edit, {:id => @host.name}, set_session_user
9c0e127b Paul Kelly
assert_response :success
b09b4515 Ohad Levy
assert_template 'edit'
end
fa9836c1 Lucas Tolchinsky
9c0e127b Paul Kelly
test "should update host" do
86744a6b Ohad Levy
put :update, { :commit => "Update", :id => @host.name, :host => {:disk => "ntfs"} }, set_session_user
@host = Host.find(@host)
9c0e127b Paul Kelly
assert_equal @host.disk, "ntfs"
end

b09b4515 Ohad Levy
def test_update_invalid
Host.any_instance.stubs(:valid?).returns(false)
445dd7ab Daniel Lobato
put :update, {:id => Host.first.name, :host => {:disk => 'ntfs'}}, set_session_user
b09b4515 Ohad Levy
assert_template 'edit'
8f10d0f8 Juan Manuel
end

b09b4515 Ohad Levy
def test_update_valid
Host.any_instance.stubs(:valid?).returns(true)
445dd7ab Daniel Lobato
put :update, {:id => Host.first.name, :host => {:name => "Updated_#{Host.first.name}"}}, set_session_user
b09b4515 Ohad Levy
assert_redirected_to host_url(assigns(:host))
8f10d0f8 Juan Manuel
end
cd060345 Lucas Tolchinsky
6874bbd9 Paul Kelly
test "should destroy host" do
assert_difference('Host.count', -1) do
86744a6b Ohad Levy
delete :destroy, {:id => @host.name}, set_session_user
6874bbd9 Paul Kelly
end
b09b4515 Ohad Levy
assert_redirected_to hosts_url
end
9c0e127b Paul Kelly
86744a6b Ohad Levy
test "externalNodes should render correctly when format text/html is given" do
0323590f Dominic Cleal
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
358ec5a3 Dominic Cleal
get :externalNodes, {:name => @host.name}, set_session_user
cd060345 Lucas Tolchinsky
assert_response :success
445dd7ab Daniel Lobato
assert_equal "<pre>#{ERB::Util.html_escape(@host.info.to_yaml)}</pre>", response.body
cd060345 Lucas Tolchinsky
end

test "externalNodes should render yml request correctly" do
0323590f Dominic Cleal
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
137f08b9 Ohad Levy
get :externalNodes, {:name => @host.name, :format => "yml"}, set_session_user
cd060345 Lucas Tolchinsky
assert_response :success
445dd7ab Daniel Lobato
assert_equal @host.info.to_yaml, response.body
cd060345 Lucas Tolchinsky
end

6874bbd9 Paul Kelly
test "when host is not saved after setBuild, the flash should inform it" do
d7611b24 Greg Sutcliffe
Host.any_instance.stubs(:setBuild).returns(false)
cd060345 Lucas Tolchinsky
@request.env['HTTP_REFERER'] = hosts_path

6e916e52 Shlomi Zadok
put :setBuild, {:id => @host.name}, set_session_user
cd060345 Lucas Tolchinsky
assert_response :found
assert_redirected_to hosts_path
754b1a01 Ohad Levy
assert_not_nil flash[:error]
90b83222 Ohad Levy
assert flash[:error] =~ /Failed to enable #{@host} for installation/
cd060345 Lucas Tolchinsky
end

6e916e52 Shlomi Zadok
context "when host is saved after setBuild" do
setup do
@request.env['HTTP_REFERER'] = hosts_path
end

teardown do
afe02d30 Daniel Lobato
Host::Managed.any_instance.unstub(:setBuild)
6e916e52 Shlomi Zadok
@request.env['HTTP_REFERER'] = ''
end

test "the flash should inform it" do
Host::Managed.any_instance.stubs(:setBuild).returns(true)
put :setBuild, {:id => @host.name}, set_session_user
assert_response :found
assert_redirected_to hosts_path
assert_not_nil flash[:notice]
assert flash[:notice] == "Enabled #{@host} for rebuild on next boot"
end

test 'and reboot was requested, the flash should inform it' do
Host::Managed.any_instance.stubs(:setBuild).returns(true)
# Setup a power mockup
class PowerShmocker
def reset
true
end
end
Host::Managed.any_instance.stubs(:power).returns(PowerShmocker.new())

put :setBuild, {:id => @host.name, :host => {:build => '1'}}, set_session_user
assert_response :found
assert_redirected_to hosts_path
assert_not_nil flash[:notice]
assert_equal(flash[:notice], "Enabled #{@host} for reboot and rebuild")
end

test 'and reboot requested and reboot failed, the flash should inform it' do
Host::Managed.any_instance.stubs(:setBuild).returns(true)
put :setBuild, {:id => @host.name, :host => {:build => '1'}}, set_session_user
assert_raise Foreman::Exception do
@host.power.reset
end
assert_response :found
assert_redirected_to hosts_path
assert_not_nil flash[:notice]
assert_equal(flash[:notice], "Enabled #{@host} for rebuild on next boot")
end
end

0203bf27 Ohad Levy
def test_clone
f5ab56ae Dmitri Dolguikh
ComputeResource.any_instance.stubs(:vm_compute_attributes_for).returns({})
86744a6b Ohad Levy
get :clone, {:id => Host.first.name}, set_session_user
4638a878 Martin Matuska
assert assigns(:clone_host)
43c4bd72 Marek Hulan
assert_template 'clone'
0203bf27 Ohad Levy
end

4638a878 Martin Matuska
def test_clone_empties_fields
f5ab56ae Dmitri Dolguikh
ComputeResource.any_instance.stubs(:vm_compute_attributes_for).returns({})
4638a878 Martin Matuska
get :clone, {:id => Host.first.name}, set_session_user
refute assigns(:host).name
refute assigns(:host).ip
refute assigns(:host).mac
end

5081efd9 Shimon Shtein
def test_clone_with_hostgroup
ComputeResource.any_instance.stubs(:vm_compute_attributes_for).returns({})
host = FactoryGirl.create(:host, :with_hostgroup)
get :clone, {:id => host.id}, set_session_user
assert assigns(:clone_host)
assert_template 'clone'
assert_response :success
end

5f029ed6 Daniel Lobato
def setup_user(operation, type = 'hosts', filter = nil)
acfbc458 Marek Hulan
super
end

def setup_user_and_host(operation, filter = nil, &block)
setup_user operation, 'hosts', filter, &block

9fd7478e Paul Kelly
as_admin do
e14b5758 Greg Sutcliffe
@host1 = FactoryGirl.create(:host)
9fd7478e Paul Kelly
@host1.owner = users(:admin)
@host1.save!
e14b5758 Greg Sutcliffe
@host2 = FactoryGirl.create(:host)
9fd7478e Paul Kelly
@host2.owner = users(:admin)
@host2.save!
end
96144a47 Daniel Lobato
Host.per_page = 1000
9fd7478e Paul Kelly
@request.session[:user] = @one.id
end

acfbc458 Marek Hulan
test 'user with view host rights and domain is set should succeed in viewing host1 but fail for host2' do
setup_user_and_host "view", "domain_id = #{domains(:mydomain).id}"

9fd7478e Paul Kelly
as_admin do
6f0fc8f1 Ohad Levy
@host1.update_attribute(:domain, domains(:mydomain))
@host2.update_attribute(:domain, domains(:yourdomain))
dde34153 Paul Kelly
end
d7bd2f22 Ohad Levy
get :index, {}, set_session_user.merge(:user => @one.id)
43c4bd72 Marek Hulan
9fd7478e Paul Kelly
assert_response :success
0fe147c7 Joseph Mitchell Magen
assert_match /#{@host1.shortname}/, @response.body
43c4bd72 Marek Hulan
refute_match /#{@host2.shortname}/, @response.body
9fd7478e Paul Kelly
end
0203bf27 Ohad Levy
acfbc458 Marek Hulan
test 'user with view host rights and ownership is set should succeed in viewing host1 but fail for host2' do
setup_user_and_host "view", "owner_id = #{users(:one).id} and owner_type = User"
9fd7478e Paul Kelly
as_admin do
@host1.owner = @one
@host2.owner = users(:two)
@host1.save!
@host2.save!
end
d7bd2f22 Ohad Levy
get :index, {}, set_session_user.merge(:user => @one.id)
9fd7478e Paul Kelly
assert_response :success
acfbc458 Marek Hulan
assert_match /#{@host1.name}/, @response.body
refute_match /#{@host2.name}/, @response.body
9fd7478e Paul Kelly
end

acfbc458 Marek Hulan
test 'user with view host rights and hostgroup is set should succeed in viewing host1 but fail for host2' do
setup_user_and_host "view", "hostgroup_id = #{hostgroups(:common).id}"
9fd7478e Paul Kelly
as_admin do
@host1.hostgroup = hostgroups(:common)
@host2.hostgroup = hostgroups(:unusual)
@host1.save!
@host2.save!
end
d7bd2f22 Ohad Levy
get :index, {}, set_session_user.merge(:user => @one.id)
9fd7478e Paul Kelly
assert_response :success
acfbc458 Marek Hulan
assert_match /#{@host1.name}/, @response.body
refute_match /#{@host2.name}/, @response.body
9fd7478e Paul Kelly
end

acfbc458 Marek Hulan
test 'user with edit host rights and facts are set should succeed in viewing host1 but fail for host2' do
setup_user_and_host "view", "facts.architecture = \"x86_64\""
9fd7478e Paul Kelly
as_admin do
5d264a2d Ohad Levy
fn_id = FactName.find_or_create_by_name("architecture").id
9fd7478e Paul Kelly
FactValue.create! :host => @host1, :fact_name_id => fn_id, :value => "x86_64"
FactValue.create! :host => @host2, :fact_name_id => fn_id, :value => "i386"
end
d7bd2f22 Ohad Levy
get :index, {}, set_session_user.merge(:user => @one.id)
9fd7478e Paul Kelly
assert_response :success
acfbc458 Marek Hulan
assert_match /#{@host1.name}/, @response.body
refute_match /#{@host2.name}/, @response.body
9fd7478e Paul Kelly
end

test 'user with view host rights should fail to edit host' do
acfbc458 Marek Hulan
setup_user_and_host "view"
d7bd2f22 Ohad Levy
get :edit, {:id => @host1.id}, set_session_user.merge(:user => @one.id)
assert_equal @response.status, 403
9fd7478e Paul Kelly
end

2b0af5e7 Ohad Levy
test 'multiple without hosts' do
2d9308eb Paul Kelly
post :update_multiple_hostgroup, {}, set_session_user
2b0af5e7 Ohad Levy
assert_redirected_to hosts_url
9a280163 Dominic Cleal
assert_equal "No hosts selected", flash[:error]
2b0af5e7 Ohad Levy
# now try to pass an invalid id
2d9308eb Paul Kelly
post :update_multiple_hostgroup, {:host_ids => [-1], :host_names => ["no.such.host"]}, set_session_user
2b0af5e7 Ohad Levy
assert_redirected_to hosts_url
assert_equal "No hosts were found with that id or name", flash[:error]
end

test 'multiple hostgroup change by host ids' do
bf228dd6 Paul Kelly
@request.env['HTTP_REFERER'] = hosts_path
2b0af5e7 Ohad Levy
# check that we have hosts and their hostgroup is empty
e14b5758 Greg Sutcliffe
hosts = FactoryGirl.create_list(:host, 2)
2b0af5e7 Ohad Levy
hosts.each { |host| assert_nil host.hostgroup }

hostgroup = hostgroups(:unusual)
2d9308eb Paul Kelly
post :update_multiple_hostgroup, { :host_ids => hosts.map(&:id), :hostgroup => { :id => hostgroup.id } }, set_session_user
acfbc458 Marek Hulan
assert_response :redirect
2b0af5e7 Ohad Levy
# reloads hosts
hosts.map! {|h| Host.find(h.id)}
hosts.each { |host| assert_equal hostgroup, host.hostgroup }
end

test 'multiple hostgroup change by host names' do
bf228dd6 Paul Kelly
@request.env['HTTP_REFERER'] = hosts_path
e14b5758 Greg Sutcliffe
hosts = FactoryGirl.create_list(:host, 2)
host_names = hosts.map(&:name)
2b0af5e7 Ohad Levy
# check that we have hosts and their hostgroup is empty
host_names.each do |name|
host = Host.find_by_name name
assert_not_nil host
assert_nil host.hostgroup
end

hostgroup = hostgroups(:common)
2d9308eb Paul Kelly
post :update_multiple_hostgroup, { :host_names => host_names, :hostgroup => { :id => hostgroup.id} }, set_session_user
acfbc458 Marek Hulan
assert_response :redirect
2b0af5e7 Ohad Levy
host_names.each do |name|
host = Host.find_by_name name
assert_not_nil host
assert_equal host.hostgroup, hostgroup
end
end

9fd7478e Paul Kelly
def setup_multiple_environments
setup_user_and_host "edit"
as_admin do
e14b5758 Greg Sutcliffe
@host1, @host2 = FactoryGirl.create_list(:host, 2, :environment => environments(:production))
9fd7478e Paul Kelly
end
end

test "user with edit host rights with update environments should change environments" do
017e1049 Ohad Levy
@request.env['HTTP_REFERER'] = hosts_path
9fd7478e Paul Kelly
setup_multiple_environments
assert @host1.environment == environments(:production)
assert @host2.environment == environments(:production)
1a51088d Ohad Levy
post :update_multiple_environment, { :host_ids => [@host1.id, @host2.id],
:environment => { :id => environments(:global_puppetmaster).id}},
e07f9a12 Dominic Cleal
set_session_user.merge(:user => users(:admin).id)
9fd7478e Paul Kelly
assert Host.find(@host1.id).environment == environments(:global_puppetmaster)
assert Host.find(@host2.id).environment == environments(:global_puppetmaster)
end
dde34153 Paul Kelly
dd9afdbf Daniel Lobato
test "should inherit the hostgroup environment if *inherit from hostgroup* selected" do
@request.env['HTTP_REFERER'] = hosts_path
setup_multiple_environments
assert @host1.environment == environments(:production)
assert @host2.environment == environments(:production)

hostgroup = hostgroups(:common)
hostgroup.environment = environments(:global_puppetmaster)
hostgroup.save(:validate => false)

@host1.hostgroup = hostgroup
@host1.save(:validate => false)
@host2.hostgroup = hostgroup
@host2.save(:validate => false)

params = { :host_ids => [@host1.id, @host2.id],
:environment => { :id => 'inherit' } }

post :update_multiple_environment, params,
e07f9a12 Dominic Cleal
set_session_user.merge(:user => users(:admin).id)
dd9afdbf Daniel Lobato
assert Host.find(@host1.id).environment == hostgroup.environment
assert Host.find(@host2.id).environment == hostgroup.environment
end

9fd7478e Paul Kelly
test "user with edit host rights with update parameters should change parameters" do
setup_multiple_environments
@host1.host_parameters = [HostParameter.create(:name => "p1", :value => "yo")]
@host2.host_parameters = [HostParameter.create(:name => "p1", :value => "hi")]
post :update_multiple_parameters,
1a51088d Ohad Levy
{:name => { "p1" => "hello"},:host_ids => [@host1.id, @host2.id]},
e07f9a12 Dominic Cleal
set_session_user.merge(:user => users(:admin).id)
9fd7478e Paul Kelly
assert Host.find(@host1.id).host_parameters[0][:value] == "hello"
assert Host.find(@host2.id).host_parameters[0][:value] == "hello"
dde34153 Paul Kelly
end
6e8defed Ohad Levy
test "should get errors" do
get :errors, {}, set_session_user
assert_response :success
assert_template 'index'
end

test "should get active" do
get :active, {}, set_session_user
assert_response :success
d7bd2f22 Ohad Levy
assert_template :partial => "_list"
6e8defed Ohad Levy
assert_template 'index'
end

test "should get out of sync" do
get :out_of_sync, {}, set_session_user
assert_response :success
assert_template 'index'
end

9b41cf08 Ohad Levy
test "should get pending" do
get :pending, {}, set_session_user
assert_response :success
assert_template 'index'
end

6e8defed Ohad Levy
test "should get disabled hosts" do
get :disabled, {}, set_session_user
assert_response :success
assert_template 'index'
end

76e5dd41 Joseph Mitchell Magen
test "should get disabled hosts for a user with a fact_filter" do
02acc2ec Ohad Levy
one = users(:one)
one.roles << [roles(:manager)]
2312cccf Daniel Lobato
FactName.create :name =>"architecture"
76e5dd41 Joseph Mitchell Magen
get :disabled, {:user => one.id}, set_session_user
7adf0ee3 Nacho Barrientos
assert_response :success
end

test "if only authorize_login_delegation is set, REMOTE_USER should be
ignored for API requests" do
Setting[:authorize_login_delegation] = true
Setting[:authorize_login_delegation_api] = false
set_remote_user_to users(:admin)
852ff714 Daniel Lobato
User.current = nil # User.current is admin at this point (from initialize_host)
7adf0ee3 Nacho Barrientos
host = Host.first
get :show, {:id => host.to_param, :format => 'json'}
assert_response 401
get :show, {:id => host.to_param}
assert_response :success
end

test "if both authorize_login_delegation{,_api} are unset,
REMOTE_USER should ignored in all cases" do
Setting[:authorize_login_delegation] = false
Setting[:authorize_login_delegation_api] = false
set_remote_user_to users(:admin)
852ff714 Daniel Lobato
User.current = nil # User.current is admin at this point (from initialize_host)
7adf0ee3 Nacho Barrientos
host = Host.first
get :show, {:id => host.to_param, :format => 'json'}
assert_response 401
get :show, {:id => host.to_param}
assert_redirected_to "/users/login"
end

5f029ed6 Daniel Lobato
def set_remote_user_to(user)
7adf0ee3 Nacho Barrientos
@request.env['REMOTE_USER'] = user.login
end

80e0157c Paul Kelly
def test_submit_multiple_build
e14b5758 Greg Sutcliffe
host1, host2 = FactoryGirl.create_list(:host, 2, :managed)
assert !host1.build
assert !host2.build
post :submit_multiple_build, {:host_ids => [host1.id, host2.id]}, set_session_user
6285a614 Ohad Levy
assert_response :found
80e0157c Paul Kelly
assert_redirected_to hosts_path
assert flash[:notice] == "The selected hosts will execute a build operation on next reboot"
e14b5758 Greg Sutcliffe
assert Host.find(host1).build
assert Host.find(host2).build
80e0157c Paul Kelly
end

a67e7179 Paul Kelly
def test_set_manage
@request.env['HTTP_REFERER'] = edit_host_path @host
assert @host.update_attribute :managed, false
acfbc458 Marek Hulan
assert_empty @host.errors
a67e7179 Paul Kelly
put :toggle_manage, {:id => @host.name}, set_session_user
assert_redirected_to :controller => :hosts, :action => :edit
assert flash[:notice] == "Foreman now manages the build cycle for #{@host.name}"
end

def test_unset_manage
@request.env['HTTP_REFERER'] = edit_host_path @host
assert @host.update_attribute :managed, true
acfbc458 Marek Hulan
assert_empty @host.errors
a67e7179 Paul Kelly
put :toggle_manage, {:id => @host.name}, set_session_user
assert_redirected_to :controller => :hosts, :action => :edit
assert flash[:notice] == "Foreman now no longer manages the build cycle for #{@host.name}"
end

c3b33536 Stephen Benjamin
test 'when ":restrict_registered_smart_proxies" is false, HTTP requests should be able to get externalNodes' do
358ec5a3 Dominic Cleal
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = false
358ec5a3 Dominic Cleal
SETTINGS[:require_ssl] = false

Resolv.any_instance.stubs(:getnames).returns(['else.where'])
get :externalNodes, {:name => @host.name, :format => "yml"}
assert_response :success
end

test 'hosts with a registered smart proxy on should get externalNodes successfully' do
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = false
358ec5a3 Dominic Cleal
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
get :externalNodes, {:name => @host.name, :format => "yml"}
assert_response :success
end

test 'hosts without a registered smart proxy on should not be able to get externalNodes' do
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = false
358ec5a3 Dominic Cleal
Resolv.any_instance.stubs(:getnames).returns(['another.host'])
get :externalNodes, {:name => @host.name, :format => "yml"}
assert_equal 403, @response.status
end

test 'hosts with a registered smart proxy and SSL cert should get externalNodes successfully' do
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
358ec5a3 Dominic Cleal
@request.env['HTTPS'] = 'on'
a79b633b Dominic Cleal
@request.env['SSL_CLIENT_S_DN'] = 'CN=else.where'
358ec5a3 Dominic Cleal
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
get :externalNodes, {:name => @host.name, :format => "yml"}
0323590f Dominic Cleal
assert_response :success
end

test 'hosts in trusted hosts list and SSL cert should get externalNodes successfully' do
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
0323590f Dominic Cleal
Setting[:trusted_puppetmaster_hosts] = ['else.where']

@request.env['HTTPS'] = 'on'
@request.env['SSL_CLIENT_S_DN'] = 'CN=else.where'
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
get :externalNodes, {:name => @host.name, :format => "yml"}
358ec5a3 Dominic Cleal
assert_response :success
end

2821b5e2 red-tux
test 'hosts with comma-separated SSL DN should get externalNodes successfully' do
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
2821b5e2 red-tux
Setting[:trusted_puppetmaster_hosts] = ['foreman.example']

@request.env['HTTPS'] = 'on'
@request.env['SSL_CLIENT_S_DN'] = 'CN=foreman.example,OU=PUPPET,O=FOREMAN,ST=North Carolina,C=US'
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
get :externalNodes, {:name => @host.name, :format => "yml"}
assert_response :success
end

test 'hosts with slash-separated SSL DN should get externalNodes successfully' do
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
2821b5e2 red-tux
Setting[:trusted_puppetmaster_hosts] = ['foreman.linux.lab.local']

@request.env['HTTPS'] = 'on'
@request.env['SSL_CLIENT_S_DN'] = '/C=US/ST=NC/L=City/O=Example/OU=IT/CN=foreman.linux.lab.local/emailAddress=user@example.com'
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
Resolv.any_instance.stubs(:getnames).returns(['else.where'])
get :externalNodes, {:name => @host.name, :format => "yml"}
assert_response :success
end

358ec5a3 Dominic Cleal
test 'hosts without a registered smart proxy but with an SSL cert should not be able to get externalNodes' do
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
358ec5a3 Dominic Cleal
@request.env['HTTPS'] = 'on'
a79b633b Dominic Cleal
@request.env['SSL_CLIENT_S_DN'] = 'CN=another.host'
358ec5a3 Dominic Cleal
@request.env['SSL_CLIENT_VERIFY'] = 'SUCCESS'
get :externalNodes, {:name => @host.name, :format => "yml"}
assert_equal 403, @response.status
end

test 'hosts with an unverified SSL cert should not be able to get externalNodes' do
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
358ec5a3 Dominic Cleal
@request.env['HTTPS'] = 'on'
a79b633b Dominic Cleal
@request.env['SSL_CLIENT_S_DN'] = 'CN=else.where'
358ec5a3 Dominic Cleal
@request.env['SSL_CLIENT_VERIFY'] = 'FAILURE'
get :externalNodes, {:name => @host.name, :format => "yml"}
assert_equal 403, @response.status
end

c3b33536 Stephen Benjamin
test 'when "require_ssl_smart_proxies" and "require_ssl" are true, HTTP requests should not be able to get externalNodes' do
358ec5a3 Dominic Cleal
User.current = nil
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
358ec5a3 Dominic Cleal
SETTINGS[:require_ssl] = true

Resolv.any_instance.stubs(:getnames).returns(['else.where'])
get :externalNodes, {:name => @host.name, :format => "yml"}
assert_equal 403, @response.status
end

c3b33536 Stephen Benjamin
test 'when "require_ssl_smart_proxies" is true and "require_ssl" is false, HTTP requests should be able to get externalNodes' do
358ec5a3 Dominic Cleal
User.current = nil
c3b33536 Stephen Benjamin
# since require_ssl_smart_proxies is only applicable to HTTPS connections, both should be set
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
358ec5a3 Dominic Cleal
SETTINGS[:require_ssl] = false

Resolv.any_instance.stubs(:getnames).returns(['else.where'])
get :externalNodes, {:name => @host.name, :format => "yml"}
assert_response :success
end

test 'authenticated users over HTTP should be able to get externalNodes' do
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
358ec5a3 Dominic Cleal
SETTINGS[:require_ssl] = false

Resolv.any_instance.stubs(:getnames).returns(['users.host'])
get :externalNodes, {:name => @host.name, :format => "yml"}, set_session_user
assert_response :success
end

test 'authenticated users over HTTPS should be able to get externalNodes' do
c3b33536 Stephen Benjamin
Setting[:restrict_registered_smart_proxies] = true
Setting[:require_ssl_smart_proxies] = true
358ec5a3 Dominic Cleal
SETTINGS[:require_ssl] = false

Resolv.any_instance.stubs(:getnames).returns(['users.host'])
@request.env['HTTPS'] = 'on'
get :externalNodes, {:name => @host.name, :format => "yml"}, set_session_user
assert_response :success
end

671b45e9 Joseph Mitchell Magen
#Pessimistic - Location
test "update multiple location fails on pessimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
location = taxonomies(:location1)
post :update_multiple_location, {
96144a47 Daniel Lobato
:location => {:id => location.id, :optimistic_import => "no"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
96144a47 Daniel Lobato
}, set_session_user
671b45e9 Joseph Mitchell Magen
assert_redirected_to :controller => :hosts, :action => :index
assert flash[:error] == "Cannot update Location to Location 1 because of mismatch in settings"
end
test "update multiple location does not update location of hosts if fails on pessimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
location = taxonomies(:location1)
assert_difference "location.hosts.count", 0 do
post :update_multiple_location, {
96144a47 Daniel Lobato
:location => {:id => location.id, :optimistic_import => "no"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
96144a47 Daniel Lobato
}, set_session_user
671b45e9 Joseph Mitchell Magen
end
end
test "update multiple location does not import taxable_taxonomies rows if fails on pessimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
location = taxonomies(:location1)
assert_difference "location.taxable_taxonomies.count", 0 do
post :update_multiple_location, {
96144a47 Daniel Lobato
:location => {:id => location.id, :optimistic_import => "no"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
96144a47 Daniel Lobato
}, set_session_user
671b45e9 Joseph Mitchell Magen
end
end

#Optimistic - Location
test "update multiple location updates location of hosts if succeeds on optimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
location = taxonomies(:location1)
cnt_hosts_location = location.hosts.count
assert_difference "location.hosts.count", (Host.count - cnt_hosts_location) do
post :update_multiple_location, {
96144a47 Daniel Lobato
:location => {:id => location.id, :optimistic_import => "yes"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
96144a47 Daniel Lobato
}, set_session_user
671b45e9 Joseph Mitchell Magen
end
510d53cd Marek Hulan
assert_redirected_to :controller => :hosts, :action => :index
assert_equal "Updated hosts: Changed Location", flash[:notice]
671b45e9 Joseph Mitchell Magen
end
test "update multiple location imports taxable_taxonomies rows if succeeds on optimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
location = taxonomies(:location1)
e14b5758 Greg Sutcliffe
domain = FactoryGirl.create(:domain, :locations => [taxonomies(:location2)])
hosts = FactoryGirl.create_list(:host, 2, :domain => domain,
:environment => environments(:production),
:location => taxonomies(:location2))
assert_difference "location.taxable_taxonomies.count", 1 do
671b45e9 Joseph Mitchell Magen
post :update_multiple_location, {
96144a47 Daniel Lobato
:location => {:id => location.id, :optimistic_import => "yes"},
e14b5758 Greg Sutcliffe
:host_ids => hosts.map(&:id)
96144a47 Daniel Lobato
}, set_session_user
671b45e9 Joseph Mitchell Magen
end
end

#Pessimistic - organization
test "update multiple organization fails on pessimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
organization = taxonomies(:organization1)
post :update_multiple_organization, {
96144a47 Daniel Lobato
:organization => {:id => organization.id, :optimistic_import => "no"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
96144a47 Daniel Lobato
}, set_session_user
671b45e9 Joseph Mitchell Magen
assert_redirected_to :controller => :hosts, :action => :index
fc94df42 Dominic Cleal
assert_equal "Cannot update Organization to Organization 1 because of mismatch in settings", flash[:error]
671b45e9 Joseph Mitchell Magen
end
test "update multiple organization does not update organization of hosts if fails on pessimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
organization = taxonomies(:organization1)
assert_difference "organization.hosts.count", 0 do
post :update_multiple_organization, {
96144a47 Daniel Lobato
:organization => {:id => organization.id, :optimistic_import => "no"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
96144a47 Daniel Lobato
}, set_session_user
671b45e9 Joseph Mitchell Magen
end
end
test "update multiple organization does not import taxable_taxonomies rows if fails on pessimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
organization = taxonomies(:organization1)
assert_difference "organization.taxable_taxonomies.count", 0 do
post :update_multiple_organization, {
96144a47 Daniel Lobato
:organization => {:id => organization.id, :optimistic_import => "no"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
96144a47 Daniel Lobato
}, set_session_user
671b45e9 Joseph Mitchell Magen
end
end

#Optimistic - Organization
test "update multiple organization succeeds on optimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
organization = taxonomies(:organization1)
post :update_multiple_organization, {
96144a47 Daniel Lobato
:organization => {:id => organization.id, :optimistic_import => "yes"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
96144a47 Daniel Lobato
}, set_session_user
671b45e9 Joseph Mitchell Magen
assert_redirected_to :controller => :hosts, :action => :index
fc94df42 Dominic Cleal
assert_equal "Updated hosts: Changed Organization", flash[:notice]
671b45e9 Joseph Mitchell Magen
end
test "update multiple organization updates organization of hosts if succeeds on optimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
organization = taxonomies(:organization1)
cnt_hosts_organization = organization.hosts.count
assert_difference "organization.hosts.count", (Host.count - cnt_hosts_organization) do
post :update_multiple_organization, {
96144a47 Daniel Lobato
:organization => {:id => organization.id, :optimistic_import => "yes"},
9e312588 Tomer Brisker
:host_ids => Host.pluck('hosts.id')
96144a47 Daniel Lobato
}, set_session_user
671b45e9 Joseph Mitchell Magen
end
end
test "update multiple organization imports taxable_taxonomies rows if succeeds on optimistic import" do
@request.env['HTTP_REFERER'] = hosts_path
organization = taxonomies(:organization1)
e14b5758 Greg Sutcliffe
domain = FactoryGirl.create(:domain, :organizations => [taxonomies(:organization2)])
hosts = FactoryGirl.create_list(:host, 2, :domain => domain,
:environment => environments(:production),
:organization => taxonomies(:organization2))
assert_difference "organization.taxable_taxonomies.count", 1 do
671b45e9 Joseph Mitchell Magen
post :update_multiple_organization, {
96144a47 Daniel Lobato
:organization => { :id => organization.id, :optimistic_import => "yes"},
e14b5758 Greg Sutcliffe
:host_ids => hosts.map(&:id)
96144a47 Daniel Lobato
}, set_session_user
671b45e9 Joseph Mitchell Magen
end
end

796352ed Greg Sutcliffe
test "can change sti type to valid subtype" do
43c4bd72 Marek Hulan
class Host::Valid < Host::Managed; end
445dd7ab Daniel Lobato
put :update, { :commit => "Update", :id => @host.name, :host => {:type => "Host::Valid"} }, set_session_user
@host = Host::Base.find(@host.id)
assert_equal "Host::Valid", @host.type
796352ed Greg Sutcliffe
end

test "cannot change sti type to invalid subtype" do
old_type = @host.type
put :update, { :commit => "Update", :id => @host.name, :host => {:type => "Host::Notvalid"} }, set_session_user
feacea35 Amos Benari
@host = Host.find(@host.id)
796352ed Greg Sutcliffe
assert_equal old_type, @host.type
end

3595a70c Joseph Mitchell Magen
test "blank root password submitted does not erase existing password" do
old_root_pass = @host.root_pass
445dd7ab Daniel Lobato
put :update, { :commit => "Update", :id => @host.name, :host => {:root_pass => '' } }, set_session_user
3595a70c Joseph Mitchell Magen
@host = Host.find(@host.id)
assert_equal old_root_pass, @host.root_pass
end

test "blank BMC password submitted does not erase existing password" do
bmc1 = @host.interfaces.build(:name => "bmc1", :mac => '52:54:00:b0:0c:fc', :type => 'Nic::BMC',
:ip => '10.0.1.101', :username => 'user1111', :password => 'abc123456', :provider => 'IPMI')
assert bmc1.save
old_password = bmc1.password
put :update, { :commit => "Update", :id => @host.name, :host => {:interfaces_attributes => {"0" => {:id => bmc1.id, :password => ''} } } }, set_session_user
@host = Host.find(@host.id)
assert_equal old_password, @host.interfaces.bmc.first.password
end

ecd9c9c6 Joseph Mitchell Magen
# To test that work-around for Rails bug - https://github.com/rails/rails/issues/11031
test "BMC password updates successful even if attrs serialized field is the only dirty field" do
eaa6aec0 Joseph Mitchell Magen
bmc1 = @host.interfaces.build(:name => "bmc1", :mac => '52:54:00:b0:0c:fc', :type => 'Nic::BMC',
:ip => '10.0.1.101', :username => 'user1111', :password => 'abc123456', :provider => 'IPMI')
assert bmc1.save
new_password = "topsecret"
ecd9c9c6 Joseph Mitchell Magen
put :update, { :commit => "Update", :id => @host.name, :host => {:interfaces_attributes => {"0" => {:id => bmc1.id, :password => new_password, :mac => bmc1.mac} } } }, set_session_user
eaa6aec0 Joseph Mitchell Magen
@host = Host.find(@host.id)
assert_equal new_password, @host.interfaces.bmc.first.password
end

084f7554 Dominic Cleal
test "#disassociate shows error when used on non-CR host" do
host = FactoryGirl.create(:host)
@request.env["HTTP_REFERER"] = hosts_path
put :disassociate, {:id => host.to_param}, set_session_user
assert_response :redirect, hosts_path
assert_not_nil flash[:error]
end

test "#disassociate removes UUID and CR association from host" do
host = FactoryGirl.create(:host, :on_compute_resource)
@request.env["HTTP_REFERER"] = hosts_path
put :disassociate, {:id => host.to_param}, set_session_user
assert_response :redirect, hosts_path
host.reload
refute host.uuid
refute host.compute_resource_id
end

test '#update_multiple_disassociate' do
host = FactoryGirl.create(:host, :on_compute_resource)
post :update_multiple_disassociate, {:host_ids => [host.id], :host_names => [host.name]}, set_session_user
assert_response :redirect, hosts_path
assert_not_nil flash[:notice]
host.reload
refute host.uuid
refute host.compute_resource_id
end

6e916e52 Shlomi Zadok
test '#review_before_build' do
HostBuildStatus.any_instance.stubs(:host_status).returns(true)
xhr :get, :review_before_build, {:id => @host.name}, set_session_user
assert_response :success
assert_template 'review_before_build'
end

d4e53f27 Avi Tal
test 'template_used returns templates with interfaces' do
be4602e8 Greg Sutcliffe
@host.setBuild
nic=FactoryGirl.create(:nic_managed, :host => @host)
attrs = @host.attributes
attrs[:interfaces_attributes] = nic.attributes
xhr :put, :template_used, {:provisioning => 'build', :host => attrs }, set_session_user
assert_response :success
assert_template :partial => '_provisioning'
end

d4e53f27 Avi Tal
test 'template_used returns templates with host parameters' do
@host.setBuild
attrs = @host.attributes
attrs[:host_parameters_attributes] = {'0' => {:name => 'foo', :value => 'bar', :id => '34'}}
xhr :put, :template_used, {:provisioning => 'build', :host => attrs }, set_session_user
assert_response :success
assert_template :partial => '_provisioning'
end

be4602e8 Greg Sutcliffe
test 'process_taxonomy renders a host from the params correctly' do
nic=FactoryGirl.create(:nic_managed, :host => @host)
attrs = @host.attributes
attrs[:interfaces_attributes] = nic.attributes
xhr :put, :process_taxonomy, { :host => attrs }, set_session_user
assert_response :success
assert_template :partial => '_form'
end

d91160ea Lucas Tolchinsky
private
abd8f1d1 Daniel Lobato
d91160ea Lucas Tolchinsky
def initialize_host
9fd7478e Paul Kelly
User.current = users(:admin)
a6f4f5f7 Ohad Levy
disable_orchestration
96144a47 Daniel Lobato
@host = Host.create(:name => "myfullhost",
160e24ea Joseph Magen
:mac => "aabbecddeeff",
:ip => "2.3.4.99",
feacea35 Amos Benari
:domain_id => domains(:mydomain).id,
:operatingsystem_id => operatingsystems(:redhat).id,
:architecture_id => architectures(:x86_64).id,
:environment_id => environments(:production).id,
:subnet_id => subnets(:one).id,
160e24ea Joseph Magen
:disk => "empty partition",
3595a70c Joseph Mitchell Magen
:puppet_proxy_id => smart_proxies(:puppetmaster).id,
160e24ea Joseph Magen
:root_pass => "123456789",
:location_id => taxonomies(:location1).id,
:organization_id => taxonomies(:organization1).id
017e1049 Ohad Levy
)
cd060345 Lucas Tolchinsky
end
2f077f63 Ohad Levy
end