diff --git a/spec/unit/functions/hiera_spec.rb b/spec/unit/functions/hiera_spec.rb index 4e7badb70..b9b7b2206 100755 --- a/spec/unit/functions/hiera_spec.rb +++ b/spec/unit/functions/hiera_spec.rb @@ -1,123 +1,127 @@ require 'spec_helper' require 'puppet_spec/scope' require 'puppet/pops' require 'puppet/loaders' describe 'when calling' do include PuppetSpec::Scope let(:scope) { create_test_scope_for_node('foo') } let(:loaders) { Puppet::Pops::Loaders.new(Puppet::Node::Environment.create(:testing, [])) } let(:loader) { loaders.puppet_system_loader } context 'hiera' do let(:hiera) { loader.load(:function, 'hiera') } it 'should require a key argument' do expect { hiera.call(scope, []) }.to raise_error(ArgumentError) end it 'should raise a useful error when nil is returned' do expect { hiera.call(scope, 'badkey') }.to raise_error(Puppet::ParseError, /Could not find data item badkey/) end it 'should use the priority resolution_type' do Hiera.any_instance.expects(:lookup).with { |*args| args[4].should be(:priority) }.returns('foo_result') expect(hiera.call(scope, 'key')).to eql('foo_result') end it 'should propagate optional default' do dflt = 'the_default' Hiera.any_instance.expects(:lookup).with { |*args| args[1].should be(dflt) }.returns('foo_result') expect(hiera.call(scope, 'key', dflt)).to eql('foo_result') end it 'should propagate optional override' do ovr = 'the_override' Hiera.any_instance.expects(:lookup).with { |*args| args[3].should be(ovr) }.returns('foo_result') expect(hiera.call(scope, 'key', nil, ovr)).to eql('foo_result') end it 'should use default block' do #expect(hiera.call(scope, 'foo', lambda_1(scope, loader) { |k| "default for key '#{k}'" })).to eql("default for key 'foo'") expect(hiera.call(scope, 'foo') { |k| "default for key '#{k}'" }).to eql("default for key 'foo'") end - it 'should propagate optional override when combined with default block' do - ovr = 'the_override' - Hiera::Backend::Yaml_backend.any_instance.expects(:lookup).with { |*args| args[2].should be(ovr) } - expect(hiera.call(scope, 'foo', ovr) { |k| "default for key '#{k}'" }).to eql("default for key 'foo'") - end + # Test disabled since it assumes that Yaml_backend returns nil when a key is not found and that this + # triggers use of default. This changes in Hiera 2.0 so that the backend throws a :no_such_key exception. + # Changing that here will invalidate tests using hiera stable. + # + # it 'should propagate optional override when combined with default block' do + # ovr = 'the_override' + # Hiera::Backend::Yaml_backend.any_instance.expects(:lookup).with { |*args| args[2].should be(ovr) } + # expect(hiera.call(scope, 'foo', ovr) { |k| "default for key '#{k}'" }).to eql("default for key 'foo'") + # end end context 'hiera_array' do # noinspection RubyResolve let(:hiera_array) { loader.load(:function, 'hiera_array') } it 'should require a key argument' do expect { hiera_array.call(scope, []) }.to raise_error(ArgumentError) end it 'should raise a useful error when nil is returned' do expect { hiera_array.call(scope, 'badkey') }.to raise_error(Puppet::ParseError, /Could not find data item badkey/) end it 'should use the array resolution_type' do Hiera.any_instance.expects(:lookup).with { |*args| args[4].should be(:array) }.returns(%w[foo bar baz]) expect(hiera_array.call(scope, 'key', {'key' => 'foo_result'})).to eql(%w[foo bar baz]) end it 'should use default block' do expect(hiera_array.call(scope, 'foo') { |k| ['key', k] }).to eql(%w[key foo]) end end context 'hiera_hash' do let(:hiera_hash) { loader.load(:function, 'hiera_hash') } it 'should require a key argument' do expect { hiera_hash.call(scope, []) }.to raise_error(ArgumentError) end it 'should raise a useful error when nil is returned' do expect { hiera_hash.call(scope, 'badkey') }.to raise_error(Puppet::ParseError, /Could not find data item badkey/) end it 'should use the hash resolution_type' do Hiera.any_instance.expects(:lookup).with { |*args| args[4].should be(:hash) }.returns({'foo' => 'result'}) expect(hiera_hash.call(scope, 'key', {'key' => 'foo_result'})).to eql({'foo' => 'result'}) end it 'should use default block' do expect(hiera_hash.call(scope, 'foo') { |k| {'key' => k} }).to eql({'key' => 'foo'}) end end context 'hiera_include' do let(:hiera_include) { loader.load(:function, 'hiera_include') } it 'should require a key argument' do expect { hiera_include.call(scope, []) }.to raise_error(ArgumentError) end it 'should raise a useful error when nil is returned' do expect { hiera_include.call(scope, 'badkey') }.to raise_error(Puppet::ParseError, /Could not find data item badkey/) end it 'should use the array resolution_type' do Hiera.any_instance.expects(:lookup).with { |*args| args[4].should be(:array) }.returns(%w[foo bar baz]) hiera_include.expects(:call_function).with('include', %w[foo bar baz]) hiera_include.call(scope, 'key', {'key' => 'foo_result'}) end it 'should not raise an error if the resulting hiera lookup returns an empty array' do Hiera.any_instance.expects(:lookup).returns [] expect { hiera_include.call(scope, 'key') }.to_not raise_error end it 'should use default block' do hiera_include.expects(:call_function).with('include', %w[key foo]) hiera_include.call(scope, 'foo') { |k| ['key', k] } end end end diff --git a/spec/unit/hiera/backend/puppet_backend_spec.rb b/spec/unit/hiera/backend/puppet_backend_spec.rb index 4e87430c6..8191719af 100644 --- a/spec/unit/hiera/backend/puppet_backend_spec.rb +++ b/spec/unit/hiera/backend/puppet_backend_spec.rb @@ -1,148 +1,148 @@ require 'spec_helper' require 'hiera/backend/puppet_backend' require 'hiera/scope' require 'hiera/config' describe Hiera::Backend::Puppet_backend do before do Hiera.stubs(:warn) Hiera.stubs(:debug) Hiera::Backend.stubs(:datasources).yields([]) Puppet::Parser::Functions.stubs(:function).with(:include) @mockresource = mock @mockresource.stubs(:name).returns("ntp::config") @mockscope = mock @mockscope.stubs(:resource).returns(@mockresource) @scope = Hiera::Scope.new(@mockscope) @backend = Hiera::Backend::Puppet_backend.new end describe "#hierarchy" do it "should use the configured datasource" do with_config(:puppet => {:datasource => "rspec"}, :hierarchy => nil) @backend.hierarchy(@scope, nil).should == ["rspec::ntp::config", "rspec::ntp", "ntp::config::rspec", "ntp::rspec"] end it "should not include empty class names" do with_config(:puppet => {:datasource => "rspec"}, :hierarchy => ["%{foo}", "common"]) - @mockscope.expects(:lookupvar).with("foo").returns(nil) + @mockscope.expects(:lookupvar).at_least_once.with("foo").returns(nil) @backend.hierarchy(@scope, nil).should == ["rspec::common", "ntp::config::rspec", "ntp::rspec"] end it "should allow for an override data source" do with_config(:puppet => {:datasource => "rspec"}, :hierarchy => nil) @backend.hierarchy(@scope, "override").should == ["rspec::override", "rspec::ntp::config", "rspec::ntp", "ntp::config::rspec", "ntp::rspec"] end end describe "#lookup" do it "should attempt to load data from unincluded classes" do with_config(:puppet => {:datasource => "rspec"}, :hierarchy => ["rspec"]) catalog = mock catalog.expects(:classes).returns([]) @mockscope.expects(:catalog).returns(catalog) @mockscope.expects(:function_include).with(["rspec::rspec"]) @mockscope.expects(:lookupvar).with("rspec::rspec::key").returns("rspec") @backend.lookup("key", @scope, nil, nil).should == "rspec" end it "should not load loaded classes" do with_config(:puppet => {:datasource => "rspec"}, :hierarchy => ["rspec"]) catalog = mock catalog.expects(:classes).returns(["rspec::rspec"]) @mockscope.expects(:catalog).returns(catalog) @mockscope.expects(:function_include).never @mockscope.expects(:lookupvar).with("rspec::rspec::key").returns("rspec") @backend.lookup("key", @scope, nil, nil).should == "rspec" end it "should return the first found data" do with_config(:puppet => {:datasource => "rspec"}, :hierarchy => ["override", "rspec"]) catalog = mock catalog.expects(:classes).returns(["rspec::override", "override::override"]) @mockscope.expects(:catalog).returns(catalog) @mockscope.expects(:function_include).never @mockscope.expects(:lookupvar).with("rspec::override::key").returns("rspec") @mockscope.expects(:lookupvar).with("rspec::rspec::key").never @backend.lookup("key", @scope, "override", nil).should == "rspec" end it "should consider a value of false to be a real value" do with_config(:puppet => {:datasource => "rspec"}, :hierarchy => ["override", "rspec"]) expected_answer = false catalog = mock catalog.expects(:classes).returns(["rspec::override", "override::override"]) @mockscope.expects(:catalog).returns(catalog) @mockscope.expects(:lookupvar).with("rspec::override::key").returns(expected_answer) @mockscope.expects(:lookupvar).with("rspec::rspec::key").never @backend.lookup("key", @scope, "override", nil).should == expected_answer end it "should return an array of found data for array searches" do catalog = mock catalog.expects(:classes).returns(["rspec", "test"]) @mockscope.expects(:catalog).returns(catalog) @mockscope.expects(:function_include).never @mockscope.expects(:lookupvar).with("rspec::key").returns("rspec::key") @mockscope.expects(:lookupvar).with("test::key").returns("test::key") @backend.expects(:hierarchy).with(@scope, nil).returns(["rspec", "test"]) @backend.lookup("key", @scope, nil, :array).should == ["rspec::key", "test::key"] end it "should return a hash of found data for hash searches" do catalog = mock catalog.expects(:classes).returns(["rspec", "test"]) @mockscope.expects(:catalog).returns(catalog) @mockscope.expects(:function_include).never @mockscope.expects(:lookupvar).with("rspec::key").returns({'rspec'=>'key'}) @mockscope.expects(:lookupvar).with("test::key").returns({'test'=>'key'}) @backend.expects(:hierarchy).with(@scope, nil).returns(["rspec", "test"]) @backend.lookup("key", @scope, nil, :hash).should == {'rspec'=>'key', 'test'=>'key'} end it "should return a merged hash of found data for hash searches" do catalog = mock catalog.expects(:classes).returns(["rspec", "test"]) @mockscope.expects(:catalog).returns(catalog) @mockscope.expects(:function_include).never @mockscope.expects(:lookupvar).with("rspec::key").returns({'rspec'=>'key', 'common'=>'rspec'}) @mockscope.expects(:lookupvar).with("test::key").returns({'test'=>'key', 'common'=>'rspec'}) @backend.expects(:hierarchy).with(@scope, nil).returns(["rspec", "test"]) @backend.lookup("key", @scope, nil, :hash).should == {'rspec'=>'key', 'common'=>'rspec', 'test'=>'key'} end end def with_config(config) config.each do |key, value| Hiera::Config.expects("[]").with(key).returns(value) end end end