foreman/test/unit/lookup_key_test.rb @ 6b4b489f
0265427b | Ohad Levy | require 'test_helper'
|
|
class LookupKeyTest < ActiveSupport::TestCase
|
|||
e14b5758 | Greg Sutcliffe | def setup
|
|
@host1, @host2, @host3 = FactoryGirl.create_list(:host, 3,
|
|||
:location => taxonomies(:location1),
|
|||
:organization => taxonomies(:organization1),
|
|||
:puppetclasses => [puppetclasses(:one)],
|
|||
:environment => environments(:production))
|
|||
end
|
|||
9afa092e | Ohad Levy | def test_element_seperations
|
|
f8d94608 | Amos Benari | key = ""
|
|
as_admin do
|
|||
6b4b489f | Ori Rabin | key = VariableLookupKey.create!(:key => "ntp", :path => "domain,hostgroup\n domain", :puppetclass => Puppetclass.first)
|
|
f8d94608 | Amos Benari | end
|
|
9afa092e | Ohad Levy | elements = key.send(:path_elements) # hack to access private method
|
|
assert_equal "domain", elements[0][0]
|
|||
assert_equal "hostgroup", elements[0][1]
|
|||
assert_equal "domain", elements[1][0]
|
|||
end
|
|||
def test_path2match_single_domain_path
|
|||
f8d94608 | Amos Benari | key = ""
|
|
value = ""
|
|||
as_admin do
|
|||
6b4b489f | Ori Rabin | key = VariableLookupKey.create!(:key => "ntp", :path => "domain", :puppetclass => Puppetclass.first)
|
|
f8d94608 | Amos Benari | value = LookupValue.create!(:value => "ntp.mydomain.net", :match => "domain = mydomain.net", :lookup_key => key)
|
|
end
|
|||
9afa092e | Ohad Levy | ||
e14b5758 | Greg Sutcliffe | @host1.domain = domains(:mydomain)
|
|
assert_equal [value.match], key.send(:path2matches,@host1)
|
|||
9afa092e | Ohad Levy | end
|
|
def test_fetching_the_correct_value_to_a_given_key
|
|||
f8d94608 | Amos Benari | key = ""
|
|
value = ""
|
|||
0f9df583 | Amos Benari | puppetclass = puppetclasses(:one)
|
|
f8d94608 | Amos Benari | as_admin do
|
|
6b4b489f | Ori Rabin | key = PuppetclassLookupKey.create!(:key => "dns", :path => "domain\npuppetversion", :override=>true)
|
|
f8d94608 | Amos Benari | value = LookupValue.create!(:value => "[1.2.3.4,2.3.4.5]", :match => "domain = mydomain.net", :lookup_key => key)
|
|
0f9df583 | Amos Benari | EnvironmentClass.create!(:puppetclass => puppetclass, :environment => environments(:production), :lookup_key => key)
|
|
f8d94608 | Amos Benari | end
|
|
9afa092e | Ohad Levy | ||
f8d94608 | Amos Benari | key.reload
|
|
assert key.lookup_values_count > 0
|
|||
e14b5758 | Greg Sutcliffe | @host1.domain = domains(:mydomain)
|
|
0f9df583 | Amos Benari | ||
e14b5758 | Greg Sutcliffe | assert_equal value.value, Classification::ClassParam.new(:host => @host1).enc['base']['dns']
|
|
9afa092e | Ohad Levy | end
|
|
def test_path2match_single_hostgroup_path
|
|||
f8d94608 | Amos Benari | key = ""
|
|
value = ""
|
|||
as_admin do
|
|||
6b4b489f | Ori Rabin | key = VariableLookupKey.create!(:key => "ntp", :path => "hostgroup", :puppetclass => Puppetclass.first)
|
|
f8d94608 | Amos Benari | value = LookupValue.create!(:value => "ntp.pool.org", :match => "hostgroup = Common", :lookup_key => key)
|
|
end
|
|||
e14b5758 | Greg Sutcliffe | @host1.hostgroup = hostgroups(:common)
|
|
assert_equal [value.match], key.send(:path2matches,@host1)
|
|||
9afa092e | Ohad Levy | end
|
|
def test_multiple_paths
|
|||
e14b5758 | Greg Sutcliffe | @host1.hostgroup = hostgroups(:common)
|
|
@host1.environment = environments(:testing)
|
|||
9afa092e | Ohad Levy | ||
e14b5758 | Greg Sutcliffe | @host2.hostgroup = hostgroups(:unusual)
|
|
@host2.environment = environments(:testing)
|
|||
0f9df583 | Amos Benari | ||
e14b5758 | Greg Sutcliffe | @host3.environment = environments(:testing)
|
|
9afa092e | Ohad Levy | ||
default = "default"
|
|||
f8d94608 | Amos Benari | key = ""
|
|
value1 = ""
|
|||
value2 = ""
|
|||
0f9df583 | Amos Benari | puppetclass = Puppetclass.first
|
|
f8d94608 | Amos Benari | as_admin do
|
|
6b4b489f | Ori Rabin | key = PuppetclassLookupKey.create!(:key => "dns", :path => "environment,hostgroup \n hostgroup", :default_value => default, :override=>true)
|
|
0f9df583 | Amos Benari | value1 = LookupValue.create!(:value => "v1", :match => "environment=testing,hostgroup=Common", :lookup_key => key)
|
|
value2 = LookupValue.create!(:value => "v2", :match => "hostgroup=Unusual", :lookup_key => key)
|
|||
0575b5b0 | Tom Caspy | ||
LookupValue.create!(:value => "v22", :match => "fqdn=#{@host2.fqdn}", :lookup_key => key)
|
|||
0f9df583 | Amos Benari | EnvironmentClass.create!(:puppetclass => puppetclass, :environment => environments(:testing), :lookup_key => key)
|
|
e14b5758 | Greg Sutcliffe | HostClass.create!(:host => @host1,:puppetclass=>puppetclass)
|
|
HostClass.create!(:host => @host2,:puppetclass=>puppetclass)
|
|||
HostClass.create!(:host => @host3,:puppetclass=>puppetclass)
|
|||
f8d94608 | Amos Benari | end
|
|
9afa092e | Ohad Levy | ||
f8d94608 | Amos Benari | key.reload
|
|
e14b5758 | Greg Sutcliffe | assert_equal value1.value, Classification::ClassParam.new(:host=>@host1).enc['apache']['dns']
|
|
assert_equal value2.value, Classification::ClassParam.new(:host=>@host2).enc['apache']['dns']
|
|||
assert_equal default, Classification::ClassParam.new(:host=>@host3).enc['apache']['dns']
|
|||
0575b5b0 | Tom Caspy | assert key.overridden?(@host2)
|
|
refute key.overridden?(@host1)
|
|||
0265427b | Ohad Levy | end
|
|
f8d94608 | Amos Benari | ||
3a96ba00 | Amos Benari | def test_parameters_multiple_paths
|
|
e14b5758 | Greg Sutcliffe | @host1.hostgroup = hostgroups(:common)
|
|
@host2.hostgroup = hostgroups(:unusual)
|
|||
96144a47 | Daniel Lobato | ||
default = "default"
|
|||
key = ""
|
|||
value1 = ""
|
|||
value2 = ""
|
|||
puppetclass = Puppetclass.first
|
|||
e14b5758 | Greg Sutcliffe | ||
96144a47 | Daniel Lobato | as_admin do
|
|
6b4b489f | Ori Rabin | key = VariableLookupKey.create!(:key => "dns", :path => "environment,hostgroup \n hostgroup", :puppetclass => puppetclass,
|
|
96144a47 | Daniel Lobato | :default_value => default, :override=>true)
|
|
e14b5758 | Greg Sutcliffe | value1 = LookupValue.create!(:value => "v1", :match => "hostgroup=Common", :lookup_key => key)
|
|
96144a47 | Daniel Lobato | value2 = LookupValue.create!(:value => "v2", :match => "hostgroup=Unusual", :lookup_key => key)
|
|
e14b5758 | Greg Sutcliffe | ||
@host1.puppetclasses << puppetclass
|
|||
@host2.puppetclasses << puppetclass
|
|||
@host3.puppetclasses << puppetclass
|
|||
96144a47 | Daniel Lobato | end
|
|
key.reload
|
|||
e14b5758 | Greg Sutcliffe | assert_equal value1.value, Classification::GlobalParam.new(:host=>@host1).enc['dns']
|
|
assert_equal value2.value, Classification::GlobalParam.new(:host=>@host2).enc['dns']
|
|||
assert_equal default, Classification::GlobalParam.new(:host=>@host3).enc['dns']
|
|||
96144a47 | Daniel Lobato | end
|
|
3a96ba00 | Amos Benari | ||
f8d94608 | Amos Benari | def test_value_should_not_be_changed
|
|
param = lookup_keys(:three)
|
|||
d076d573 | Joseph Mitchell Magen | default = param.default_value
|
|
f8d94608 | Amos Benari | param.save
|
|
assert_equal default, param.default_value
|
|||
d076d573 | Joseph Mitchell Magen | assert_equal default, param.default_value_before_type_cast
|
|
f8d94608 | Amos Benari | end
|
|
21bf889a | Joseph Mitchell Magen | ||
test "this is a smart variable?" do
|
|||
6b4b489f | Ori Rabin | assert lookup_keys(:two).class == VariableLookupKey
|
|
21bf889a | Joseph Mitchell Magen | end
|
|
test "this is a smart class parameter?" do
|
|||
6b4b489f | Ori Rabin | assert lookup_keys(:complex).puppet?
|
|
21bf889a | Joseph Mitchell Magen | end
|
|
971fee79 | Dominic Cleal | 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
|
|||
assert_difference('Audit.count') do
|
|||
key.override = true
|
|||
key.default_value = "new default value"
|
|||
key.save!
|
|||
end
|
|||
assert_equal pc.name, key.audits.last.associated_name
|
|||
end
|
|||
e6e9f157 | Ori Rabin | ||
test "should create smart variable with the same name as class parameters" do
|
|||
env = FactoryGirl.create(:environment)
|
|||
pc = FactoryGirl.create(:puppetclass, :with_parameters, :environments => [env])
|
|||
key = pc.class_params.first
|
|||
6b4b489f | Ori Rabin | smart_variable = VariableLookupKey.create!(:key => key.key, :path => "hostgroup", :puppetclass => Puppetclass.first)
|
|
e6e9f157 | Ori Rabin | assert_valid smart_variable
|
|
end
|
|||
test "should not create two smart variables with the same name" do
|
|||
6b4b489f | Ori Rabin | 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")
|
|||
e6e9f157 | Ori Rabin | refute_valid smart_variable2
|
|
end
|
|||
ea4eec44 | Ori Rabin | ||
test "should not be able to merge overrides for a string" do
|
|||
6b4b489f | Ori Rabin | key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
|
|
ea4eec44 | Ori Rabin | :override => true, :key_type => 'string', :merge_overrides => true,
|
|
:puppetclass => puppetclasses(:one))
|
|||
refute_valid key
|
|||
assert_equal key.errors[:merge_overrides].first, _("can only be set for array or hash")
|
|||
end
|
|||
test "should be able to merge overrides for a hash" do
|
|||
6b4b489f | Ori Rabin | key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
|
|
ea4eec44 | Ori Rabin | :override => true, :key_type => 'hash', :merge_overrides => true,
|
|
:default_value => {}, :puppetclass => puppetclasses(:one))
|
|||
assert_valid key
|
|||
end
|
|||
8347a5ef | Ori Rabin | test "should be able to merge overrides with default_value for a hash" do
|
|
6b4b489f | Ori Rabin | key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
|
|
8347a5ef | Ori Rabin | :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
|
|||
6b4b489f | Ori Rabin | key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
|
|
8347a5ef | Ori Rabin | :override => true, :key_type => 'hash', :merge_overrides => false, :merge_default => true,
|
|
:default_value => {}, :puppetclass => puppetclasses(:one))
|
|||
refute_valid key
|
|||
assert_equal "can only be set when merge overrides is set", key.errors[:merge_default].first
|
|||
end
|
|||
ea4eec44 | Ori Rabin | test "should not be able to avoid duplicates for a hash" do
|
|
6b4b489f | Ori Rabin | key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
|
|
ea4eec44 | Ori Rabin | :override => true, :key_type => 'hash', :merge_overrides => true, :avoid_duplicates => true,
|
|
:default_value => {}, :puppetclass => puppetclasses(:one))
|
|||
refute_valid key
|
|||
assert_equal key.errors[:avoid_duplicates].first, _("can only be set for arrays that have merge_overrides set to true")
|
|||
end
|
|||
test "should be able to merge overrides for a array" do
|
|||
6b4b489f | Ori Rabin | key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
|
|
ea4eec44 | Ori Rabin | :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
|
|||
6b4b489f | Ori Rabin | key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
|
|
ea4eec44 | Ori Rabin | :override => true, :key_type => 'array', :merge_overrides => false, :avoid_duplicates => true,
|
|
:default_value => [], :puppetclass => puppetclasses(:one))
|
|||
refute_valid key
|
|||
assert_equal key.errors[:avoid_duplicates].first, _("can only be set for arrays that have merge_overrides set to true")
|
|||
end
|
|||
f8a56f5b | Marek Hulan | ||
test "should detect erb" do
|
|||
6b4b489f | Ori Rabin | key = FactoryGirl.build(:puppetclass_lookup_key)
|
|
f8a56f5b | Marek Hulan | assert key.contains_erb?('<% object_id %>')
|
|
assert key.contains_erb?('<%= object_id %>')
|
|||
assert key.contains_erb?('[<% object_id %>, <% self %>]')
|
|||
refute key.contains_erb?('[1,2,3]')
|
|||
refute key.contains_erb?('{a: "b"}')
|
|||
refute key.contains_erb?('plain value')
|
|||
end
|
|||
test "array key is valid even with string value containing erb" do
|
|||
6b4b489f | Ori Rabin | key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
|
|
f8a56f5b | Marek Hulan | :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
|
|||
6b4b489f | Ori Rabin | key = FactoryGirl.build(:puppetclass_lookup_key, :as_smart_class_param,
|
|
f8a56f5b | Marek Hulan | :override => true, :key_type => 'array', :merge_overrides => true, :avoid_duplicates => true,
|
|
:default_value => 'whatever', :puppetclass => puppetclasses(:one))
|
|||
refute key.valid?
|
|||
assert_include key.errors.keys, :default_value
|
|||
end
|
|||
92eb3d7c | Ori Rabin | ||
context "when key is a boolean and default_value is a string" do
|
|||
def setup
|
|||
6b4b489f | Ori Rabin | @key = FactoryGirl.create(:puppetclass_lookup_key, :as_smart_class_param,
|
|
92eb3d7c | Ori Rabin | :override => true, :key_type => 'boolean',
|
|
:default_value => 'whatever', :puppetclass => puppetclasses(:one), :use_puppet_default => true)
|
|||
end
|
|||
test "default_value is not validated if use_puppet_default is true" do
|
|||
assert @key.valid?
|
|||
end
|
|||
test "default_value is validated if use_puppet_default is false" do
|
|||
@key.use_puppet_default = false
|
|||
refute @key.valid?
|
|||
end
|
|||
end
|
|||
0265427b | Ohad Levy | end
|