Project

General

Profile

« Previous | Next » 

Revision 19d9e229

Added by Shimon Shtein over 8 years ago

Fixes #12618 - Now calls for compute attributes are explicit.

Prevents compute attributes from being overwritten unconditionally from
the hostgroup's compute profile.

View differences:

app/controllers/hosts_controller.rb
Host.new(params[:host])
end
@host.set_hostgroup_defaults
@host.set_compute_attributes unless params[:host][:compute_profile_id]
render :partial => "form"
end
app/models/host/managed.rb
def set_hostgroup_defaults
return unless hostgroup
assign_hostgroup_attributes(%w{domain_id compute_profile_id})
set_compute_attributes if compute_profile_id_changed?
assign_hostgroup_attributes(%w{domain_id})
if SETTINGS[:unattended] and (new_record? or managed?)
assign_hostgroup_attributes(%w{operatingsystem_id architecture_id})
test/functional/hosts_controller_test.rb
assert_not_nil flash[:error]
end
test '#process_hostgroup changes compute attributes' do
Fog.mock!
group1 = FactoryGirl.create(:hostgroup, :compute_profile => compute_profiles(:one))
host = FactoryGirl.build(:host, :managed, :on_compute_resource)
#remove unneeded expectation to :queue_compute
host.unstub(:queue_compute)
host.hostgroup = group1
host.compute_resource = compute_resources(:one)
host.compute_profile = compute_profiles(:one)
host.set_compute_attributes
group2 = FactoryGirl.create(:hostgroup, :compute_profile => compute_profiles(:two))
attrs = host.attributes.except('id', 'created_at', 'updated_at')
attrs['hostgroup_id'] = group2.id
attrs.delete 'compute_profile_id'
host.compute_resource.class.any_instance.expects(:max_cpu_count).returns(10)
host.compute_resource.class.any_instance.expects(:max_memory).returns(10000000000)
xhr :put, :process_hostgroup, { :host => attrs }, set_session_user
assert_response :success
assert_template :partial => '_form'
assert_select '#host_compute_attributes_cpus option[selected]', "5"
Fog.unmock!
end
test '#process_hostgroup does not change compute attributes if compute profile selected manually' do
Fog.mock!
group1 = FactoryGirl.create(:hostgroup, :compute_profile => compute_profiles(:one))
host = FactoryGirl.build(:host, :managed, :on_compute_resource)
#remove unneeded expectation to :queue_compute
host.unstub(:queue_compute)
host.hostgroup = group1
host.compute_resource = compute_resources(:one)
host.compute_profile = compute_profiles(:one)
host.set_compute_attributes
Foreman::Model::Libvirt.any_instance.stubs(:hypervisor).returns(stub(:hypervisor))
group2 = FactoryGirl.create(:hostgroup, :compute_profile => compute_profiles(:two))
attrs = host.attributes.except('id', 'created_at', 'updated_at')
attrs['hostgroup_id'] = group2.id
attrs['compute_attributes'] = { 'cpus' => 3 }
attrs.delete 'uuid'
host.compute_resource.class.any_instance.expects(:max_cpu_count).returns(10)
host.compute_resource.class.any_instance.expects(:max_memory).returns(10000000000)
xhr :put, :process_hostgroup, { :host => attrs }, set_session_user
assert_response :success
assert_template :partial => '_form'
assert_select '#host_compute_attributes_cpus option[selected]', "3"
Fog.unmock!
end
private
def initialize_host
test/unit/host_test.rb
end
end
context 'hostgroup defaults' do
context 'compute resources' do
setup do
@group1 = FactoryGirl.create(:hostgroup, :compute_profile => compute_profiles(:one))
@group2 = FactoryGirl.create(:hostgroup, :compute_profile => compute_profiles(:two))
end
test 'sets proper compute attributes on hostgroup change' do
test 'set_hostgroup_defaults doesnt touch compute attributes' do
host = FactoryGirl.create(:host, :managed, :compute_resource => compute_resources(:one), :hostgroup => @group1)
assert_not_equal 4, host.compute_attributes['cpus']
host.attributes = host.apply_inherited_attributes('hostgroup_id' => @group2.id)
host.set_hostgroup_defaults
assert_not_equal 4, host.compute_attributes['cpus']
end
test 'set_compute_attributes changes the compute attributes' do
host = FactoryGirl.create(:host, :managed, :compute_resource => compute_resources(:one), :hostgroup => @group1)
assert_not_equal 4, host.compute_attributes['cpus']
host.attributes = host.apply_inherited_attributes('hostgroup_id' => @group2.id)
host.set_compute_attributes
assert_equal 4, host.compute_attributes['cpus']
end
end

Also available in: Unified diff