diff options
Diffstat (limited to 'spec/shared_behaviours')
-rw-r--r-- | spec/shared_behaviours/hiera_indirections.rb | 99 | ||||
-rw-r--r-- | spec/shared_behaviours/iterative_functions.rb | 69 |
2 files changed, 168 insertions, 0 deletions
diff --git a/spec/shared_behaviours/hiera_indirections.rb b/spec/shared_behaviours/hiera_indirections.rb new file mode 100644 index 000000000..fe6dc35ad --- /dev/null +++ b/spec/shared_behaviours/hiera_indirections.rb @@ -0,0 +1,99 @@ +shared_examples_for "Hiera indirection" do |test_klass, fixture_dir| + include PuppetSpec::Files + + def write_hiera_config(config_file, datadir) + File.open(config_file, 'w') do |f| + f.write("--- + :yaml: + :datadir: #{datadir} + :hierarchy: ['global', 'invalid'] + :logger: 'noop' + :backends: ['yaml'] + ") + end + end + + def request(key) + Puppet::Indirector::Request.new(:hiera, :find, key, nil) + end + + before do + hiera_config_file = tmpfile("hiera.yaml") + Puppet.settings[:hiera_config] = hiera_config_file + write_hiera_config(hiera_config_file, fixture_dir) + end + + after do + test_klass.instance_variable_set(:@hiera, nil) + end + + it "should be the default data_binding terminus" do + Puppet.settings[:data_binding_terminus].should == :hiera + end + + it "should raise an error if we don't have the hiera feature" do + Puppet.features.expects(:hiera?).returns(false) + lambda { test_klass.new }.should raise_error RuntimeError, + "Hiera terminus not supported without hiera library" + end + + describe "the behavior of the hiera_config method", :if => Puppet.features.hiera? do + it "should override the logger and set it to puppet" do + test_klass.hiera_config[:logger].should == "puppet" + end + + context "when the Hiera configuration file does not exist" do + let(:path) { File.expand_path('/doesnotexist') } + + before do + Puppet.settings[:hiera_config] = path + end + + it "should log a warning" do + Puppet.expects(:warning).with( + "Config file #{path} not found, using Hiera defaults") + test_klass.hiera_config + end + + it "should only configure the logger and set it to puppet" do + Puppet.expects(:warning).with( + "Config file #{path} not found, using Hiera defaults") + test_klass.hiera_config.should == { :logger => 'puppet' } + end + end + end + + describe "the behavior of the find method", :if => Puppet.features.hiera? do + + let(:data_binder) { test_klass.new } + + it "should support looking up an integer" do + data_binder.find(request("integer")).should == 3000 + end + + it "should support looking up a string" do + data_binder.find(request("string")).should == 'apache' + end + + it "should support looking up an array" do + data_binder.find(request("array")).should == [ + '0.ntp.puppetlabs.com', + '1.ntp.puppetlabs.com', + ] + end + + it "should support looking up a hash" do + data_binder.find(request("hash")).should == { + 'user' => 'Hightower', + 'group' => 'admin', + 'mode' => '0644' + } + end + + it "raises a data binding error if hiera cannot parse the yaml data" do + expect do + data_binder.find(request('invalid')) + end.to raise_error(Puppet::DataBinding::LookupError) + end + end +end diff --git a/spec/shared_behaviours/iterative_functions.rb b/spec/shared_behaviours/iterative_functions.rb new file mode 100644 index 000000000..1910a3c80 --- /dev/null +++ b/spec/shared_behaviours/iterative_functions.rb @@ -0,0 +1,69 @@ + +shared_examples_for 'all iterative functions hash handling' do |func| + it 'passes a hash entry as an array of the key and value' do + catalog = compile_to_catalog(<<-MANIFEST) + {a=>1}.#{func} |$v| { notify { "${v[0]} ${v[1]}": } } + MANIFEST + + catalog.resource(:notify, "a 1").should_not be_nil + end +end + +shared_examples_for 'all iterative functions argument checks' do |func| + + it 'raises an error when used against an unsupported type' do + expect do + compile_to_catalog(<<-MANIFEST) + 3.14.#{func} |$k, $v| { } + MANIFEST + end.to raise_error(Puppet::Error, /must be something enumerable/) + end + + it 'raises an error when called with any parameters besides a block' do + expect do + compile_to_catalog(<<-MANIFEST) + [1].#{func}(1) |$v| { } + MANIFEST + end.to raise_error(Puppet::Error, /mis-matched arguments.*expected.*arg count \{2\}.*actual.*arg count \{3\}/m) + end + + it 'raises an error when called without a block' do + expect do + compile_to_catalog(<<-MANIFEST) + [1].#{func}() + MANIFEST + end.to raise_error(Puppet::Error, /mis-matched arguments.*expected.*arg count \{2\}.*actual.*arg count \{1\}/m) + end + + it 'raises an error when called with something that is not a block' do + expect do + compile_to_catalog(<<-MANIFEST) + [1].#{func}(1) + MANIFEST + end.to raise_error(Puppet::Error, /mis-matched arguments.*expected.*Callable.*actual(?!Callable\)).*/m) + end + + it 'raises an error when called with a block with too many required parameters' do + expect do + compile_to_catalog(<<-MANIFEST) + [1].#{func}() |$v1, $v2, $v3| { } + MANIFEST + end.to raise_error(Puppet::Error, /mis-matched arguments.*expected.*arg count \{2\}.*actual.*Callable\[Any, Any, Any\]/m) + end + + it 'raises an error when called with a block with too few parameters' do + expect do + compile_to_catalog(<<-MANIFEST) + [1].#{func}() | | { } + MANIFEST + end.to raise_error(Puppet::Error, /mis-matched arguments.*expected.*arg count \{2\}.*actual.*Callable\[0, 0\]/m) + end + + it 'does not raise an error when called with a block with too many but optional arguments' do + expect do + compile_to_catalog(<<-MANIFEST) + [1].#{func}() |$v1, $v2, $v3=extra| { } + MANIFEST + end.to_not raise_error + end +end |