Project

General

Profile

« Previous | Next » 

Revision 6b4b489f

Added by Ori Rabin over 8 years ago

fixes #10832 - separating lookup keys into puppet and variable

View differences:

test/unit/lookup_key_test.rb
def test_element_seperations
key = ""
as_admin do
key = LookupKey.create!(:key => "ntp", :path => "domain,hostgroup\n domain", :puppetclass => Puppetclass.first)
key = VariableLookupKey.create!(:key => "ntp", :path => "domain,hostgroup\n domain", :puppetclass => Puppetclass.first)
end
elements = key.send(:path_elements) # hack to access private method
assert_equal "domain", elements[0][0]
......
key = ""
value = ""
as_admin do
key = LookupKey.create!(:key => "ntp", :path => "domain", :puppetclass => Puppetclass.first)
key = VariableLookupKey.create!(:key => "ntp", :path => "domain", :puppetclass => Puppetclass.first)
value = LookupValue.create!(:value => "ntp.mydomain.net", :match => "domain = mydomain.net", :lookup_key => key)
end
......
puppetclass = puppetclasses(:one)
as_admin do
key = LookupKey.create!(:key => "dns", :path => "domain\npuppetversion", :puppetclass => puppetclass,:override=>true)
key = PuppetclassLookupKey.create!(:key => "dns", :path => "domain\npuppetversion", :override=>true)
value = LookupValue.create!(:value => "[1.2.3.4,2.3.4.5]", :match => "domain = mydomain.net", :lookup_key => key)
EnvironmentClass.create!(:puppetclass => puppetclass, :environment => environments(:production), :lookup_key => key)
end
......
key = ""
value = ""
as_admin do
key = LookupKey.create!(:key => "ntp", :path => "hostgroup", :puppetclass => Puppetclass.first)
key = VariableLookupKey.create!(:key => "ntp", :path => "hostgroup", :puppetclass => Puppetclass.first)
value = LookupValue.create!(:value => "ntp.pool.org", :match => "hostgroup = Common", :lookup_key => key)
end
@host1.hostgroup = hostgroups(:common)
......
value2 = ""
puppetclass = Puppetclass.first
as_admin do
key = LookupKey.create!(:key => "dns", :path => "environment,hostgroup \n hostgroup", :puppetclass => puppetclass, :default_value => default, :override=>true)
key = PuppetclassLookupKey.create!(:key => "dns", :path => "environment,hostgroup \n hostgroup", :default_value => default, :override=>true)
value1 = LookupValue.create!(:value => "v1", :match => "environment=testing,hostgroup=Common", :lookup_key => key)
value2 = LookupValue.create!(:value => "v2", :match => "hostgroup=Unusual", :lookup_key => key)
......
puppetclass = Puppetclass.first
as_admin do
key = LookupKey.create!(:key => "dns", :path => "environment,hostgroup \n hostgroup", :puppetclass => puppetclass,
key = VariableLookupKey.create!(:key => "dns", :path => "environment,hostgroup \n hostgroup", :puppetclass => puppetclass,
:default_value => default, :override=>true)
value1 = LookupValue.create!(:value => "v1", :match => "hostgroup=Common", :lookup_key => key)
value2 = LookupValue.create!(:value => "v2", :match => "hostgroup=Unusual", :lookup_key => key)
......
end
test "this is a smart variable?" do
assert lookup_keys(:two).is_smart_variable?
assert lookup_keys(:two).class == VariableLookupKey
end
test "this is a smart class parameter?" do
assert lookup_keys(:complex).is_smart_class_parameter?
assert lookup_keys(:complex).puppet?
end
test "when changed, an audit entry should be added" do
......
env = FactoryGirl.create(:environment)
pc = FactoryGirl.create(:puppetclass, :with_parameters, :environments => [env])
key = pc.class_params.first
smart_variable = LookupKey.create!(:key => key.key, :path => "hostgroup", :puppetclass => Puppetclass.first)
smart_variable = VariableLookupKey.create!(:key => key.key, :path => "hostgroup", :puppetclass => Puppetclass.first)
assert_valid smart_variable
end
test "should not create two smart variables with the same name" do
LookupKey.create!(:key => "smart-varialble", :path => "hostgroup", :puppetclass => Puppetclass.first, :default_value => "default")
smart_variable2 = LookupKey.new(:key => "smart-varialble", :path => "hostgroup", :puppetclass => Puppetclass.first, :default_value => "default2")
VariableLookupKey.create!(:key => "smart-varialble", :path => "hostgroup", :puppetclass => Puppetclass.first, :default_value => "default")
smart_variable2 = VariableLookupKey.new(:key => "smart-varialble", :path => "hostgroup", :puppetclass => Puppetclass.first, :default_value => "default2")
refute_valid smart_variable2
end
test "should not be able to merge overrides for a string" do
key = FactoryGirl.build(:lookup_key, :as_smart_class_param,
key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
:override => true, :key_type => 'string', :merge_overrides => true,
:puppetclass => puppetclasses(:one))
refute_valid key
......
end
test "should be able to merge overrides for a hash" do
key = FactoryGirl.build(:lookup_key, :as_smart_class_param,
key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
:override => true, :key_type => 'hash', :merge_overrides => true,
:default_value => {}, :puppetclass => puppetclasses(:one))
assert_valid key
end
test "should be able to merge overrides with default_value for a hash" do
key = FactoryGirl.build(:lookup_key, :as_smart_class_param,
key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
:override => true, :key_type => 'hash', :merge_overrides => true, :merge_default => true,
:default_value => {}, :puppetclass => puppetclasses(:one))
assert_valid key
end
test "should not be able to merge default when merge_override is false" do
key = FactoryGirl.build(:lookup_key, :as_smart_class_param,
key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
:override => true, :key_type => 'hash', :merge_overrides => false, :merge_default => true,
:default_value => {}, :puppetclass => puppetclasses(:one))
refute_valid key
......
end
test "should not be able to avoid duplicates for a hash" do
key = FactoryGirl.build(:lookup_key, :as_smart_class_param,
key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
:override => true, :key_type => 'hash', :merge_overrides => true, :avoid_duplicates => true,
:default_value => {}, :puppetclass => puppetclasses(:one))
refute_valid key
......
end
test "should be able to merge overrides for a array" do
key = FactoryGirl.build(:lookup_key, :as_smart_class_param,
key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
:override => true, :key_type => 'array', :merge_overrides => true, :avoid_duplicates => true,
:default_value => [], :puppetclass => puppetclasses(:one))
assert_valid key
end
test "should not be able to avoid duplicates when merge_override is false" do
key = FactoryGirl.build(:lookup_key, :as_smart_class_param,
key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
:override => true, :key_type => 'array', :merge_overrides => false, :avoid_duplicates => true,
:default_value => [], :puppetclass => puppetclasses(:one))
refute_valid key
......
end
test "should detect erb" do
key = FactoryGirl.build(:lookup_key)
key = FactoryGirl.build(:puppetclass_lookup_key)
assert key.contains_erb?('<% object_id %>')
assert key.contains_erb?('<%= object_id %>')
assert key.contains_erb?('[<% object_id %>, <% self %>]')
......
end
test "array key is valid even with string value containing erb" do
key = FactoryGirl.build(:lookup_key, :as_smart_class_param,
key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
:override => true, :key_type => 'array', :merge_overrides => true, :avoid_duplicates => true,
:default_value => '<%= [1,2,3] %>', :puppetclass => puppetclasses(:one))
assert key.valid?
end
test "array key is invalid with string value without erb" do
key = FactoryGirl.build(:lookup_key, :as_smart_class_param,
key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
:override => true, :key_type => 'array', :merge_overrides => true, :avoid_duplicates => true,
:default_value => 'whatever', :puppetclass => puppetclasses(:one))
refute key.valid?
......
context "when key is a boolean and default_value is a string" do
def setup
@key = FactoryGirl.create(:lookup_key, :as_smart_class_param,
@key = FactoryGirl.create(:puppetclass_lookup_key, :as_smart_class_param,
:override => true, :key_type => 'boolean',
:default_value => 'whatever', :puppetclass => puppetclasses(:one), :use_puppet_default => true)
end

Also available in: Unified diff