Project

General

Profile

« Previous | Next » 

Revision b0eaf7f2

Added by Dominic Cleal over 8 years ago

fixes #11885 - exclude lookup_value_matcher on clone

Keeping lookup_value_matcher populated during a host or
hostgroup#clone caused Rails to believe the cloned object was still
associated to the existing LookupValue associated to the original
object, and it would destroy it during clone.

View differences:

app/models/host/managed.rb
def clone
# do not copy system specific attributes
host = self.deep_clone(:include => [:config_groups, :host_config_groups, :host_classes, :host_parameters, :lookup_values],
:except => [:name, :mac, :ip, :uuid, :certname, :last_report])
:except => [:name, :mac, :ip, :uuid, :certname, :last_report, :lookup_value_matcher])
self.interfaces.each do |nic|
host.interfaces << nic.clone
end
app/models/hostgroup.rb
# Clone the hostgroup
def clone(name = "")
new = self.deep_clone(:include => [:config_groups, :lookup_values, :hostgroup_classes, :locations, :organizations, :group_parameters],
:except => [:name, :title])
:except => [:name, :title, :lookup_value_matcher])
new.name = name
new.title = name
new
test/unit/host_test.rb
host = FactoryGirl.create(:host, :with_puppetclass)
FactoryGirl.create(:puppetclass_lookup_key, :as_smart_class_param, :with_override, :puppetclass => host.puppetclasses.first, :overrides => {host.lookup_value_matcher => 'test'})
copy = host.clone
assert_equal 1, host.lookup_values.reload.size
assert_equal 1, copy.lookup_values.size
assert_equal host.lookup_values.map(&:value), copy.lookup_values.map(&:value)
end
......
assert interface.mac.blank?
assert interface.ip.blank?
end
test 'without save makes no changes' do
host = FactoryGirl.create(:host, :with_config_group, :with_puppetclass, :with_parameter)
FactoryGirl.create(:puppetclass_lookup_key, :as_smart_class_param, :with_override, :puppetclass => host.puppetclasses.first, :overrides => {host.lookup_value_matcher => 'test'})
ActiveRecord::Base.any_instance.expects(:destroy).never
ActiveRecord::Base.any_instance.expects(:save).never
host.clone
end
end
test 'fqdn of host with period in name returns just name with no concatenation of domain' do
test/unit/hostgroup_test.rb
lv.save!
cloned = group.clone("new_name")
cloned.save
assert_equal cloned.lookup_values.map(&:value), group.lookup_values.map(&:value)
assert_equal 1, group.lookup_values.reload.count
assert_equal 1, cloned.lookup_values.count
assert_equal group.lookup_values.map(&:value), cloned.lookup_values.map(&:value)
end
test '#classes etc. on cloned group return the same' do
......
assert_equal group.available_puppetclasses.map(&:id), cloned.available_puppetclasses.map(&:id)
assert_valid cloned
end
test 'without save makes no changes' do
group = FactoryGirl.create(:hostgroup, :with_config_group, :with_puppetclass)
FactoryGirl.create(:puppetclass_lookup_key, :as_smart_class_param, :with_override, :puppetclass => group.puppetclasses.first, :overrides => {group.lookup_value_matcher => 'test'})
ActiveRecord::Base.any_instance.expects(:destroy).never
ActiveRecord::Base.any_instance.expects(:save).never
group.clone
end
end
end

Also available in: Unified diff