diff options
Diffstat (limited to 'spec/unit/parser/functions')
-rw-r--r-- | spec/unit/parser/functions/contain_spec.rb | 51 | ||||
-rwxr-xr-x | spec/unit/parser/functions/create_resources_spec.rb | 9 | ||||
-rwxr-xr-x | spec/unit/parser/functions/digest_spec.rb | 31 | ||||
-rw-r--r-- | spec/unit/parser/functions/epp_spec.rb | 103 | ||||
-rwxr-xr-x | spec/unit/parser/functions/file_spec.rb | 53 | ||||
-rwxr-xr-x | spec/unit/parser/functions/include_spec.rb | 16 | ||||
-rw-r--r-- | spec/unit/parser/functions/inline_epp_spec.rb | 82 | ||||
-rwxr-xr-x | spec/unit/parser/functions/realize_spec.rb | 78 | ||||
-rwxr-xr-x | spec/unit/parser/functions/require_spec.rb | 24 | ||||
-rwxr-xr-x | spec/unit/parser/functions/search_spec.rb | 5 | ||||
-rw-r--r-- | spec/unit/parser/functions/shared.rb | 82 |
11 files changed, 302 insertions, 232 deletions
diff --git a/spec/unit/parser/functions/contain_spec.rb b/spec/unit/parser/functions/contain_spec.rb index 3150e0c8e..2a5aa57c7 100644 --- a/spec/unit/parser/functions/contain_spec.rb +++ b/spec/unit/parser/functions/contain_spec.rb @@ -3,11 +3,15 @@ require 'spec_helper' require 'puppet_spec/compiler' require 'puppet/parser/functions' require 'matchers/containment_matchers' +require 'matchers/resource' require 'matchers/include_in_order' +require 'unit/parser/functions/shared' + describe 'The "contain" function' do include PuppetSpec::Compiler include ContainmentMatchers + include Matchers::Resource it "includes the class" do catalog = compile_to_catalog(<<-MANIFEST) @@ -25,6 +29,41 @@ describe 'The "contain" function' do expect(catalog.classes).to include("contained") end + it "includes the class when using a fully qualified anchored name" do + catalog = compile_to_catalog(<<-MANIFEST) + class contained { + notify { "contained": } + } + + class container { + contain ::contained + } + + include container + MANIFEST + + expect(catalog.classes).to include("contained") + end + + it "ensures that the edge is with the correct class" do + catalog = compile_to_catalog(<<-MANIFEST) + class outer { + class named { } + contain named + } + + class named { } + + include named + include outer + MANIFEST + + expect(catalog).to have_resource("Class[Named]") + expect(catalog).to have_resource("Class[Outer]") + expect(catalog).to have_resource("Class[Outer::Named]") + expect(catalog).to contain_class("outer::named").in("outer") + end + it "makes the class contained in the current class" do catalog = compile_to_catalog(<<-MANIFEST) class contained { @@ -182,4 +221,16 @@ describe 'The "contain" function' do ) end end + + describe "When the future parser is in use" do + require 'puppet/pops' + before(:each) do + Puppet[:parser] = 'future' + compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("foo")) + @scope = Puppet::Parser::Scope.new(compiler) + end + + it_should_behave_like 'all functions transforming relative to absolute names', :function_contain + it_should_behave_like 'an inclusion function, regardless of the type of class reference,', :contain + end end diff --git a/spec/unit/parser/functions/create_resources_spec.rb b/spec/unit/parser/functions/create_resources_spec.rb index 79ed02f22..3e7bd8015 100755 --- a/spec/unit/parser/functions/create_resources_spec.rb +++ b/spec/unit/parser/functions/create_resources_spec.rb @@ -49,7 +49,7 @@ describe 'function for dynamically creating resources' do end it 'should be able to add exported resources' do - catalog = compile_to_catalog("create_resources('@@file', {'/etc/foo'=>{'ensure'=>'present'}})") + catalog = compile_to_catalog("create_resources('@@file', {'/etc/foo'=>{'ensure'=>'present'}}) realize(File['/etc/foo'])") catalog.resource(:file, "/etc/foo")['ensure'].should == 'present' catalog.resource(:file, "/etc/foo").exported.should == true end @@ -202,5 +202,12 @@ describe 'function for dynamically creating resources' do catalog.resource(:notify, "test")['message'].should == 'two' catalog.resource(:class, "bar").should_not be_nil end + + it 'should fail with a correct error message if the syntax of an imported file is incorrect' do + expect{ + Puppet[:modulepath] = my_fixture_dir + compile_to_catalog('include foo') + }.to raise_error(Puppet::Error, /Syntax error at.*/) + end end end diff --git a/spec/unit/parser/functions/digest_spec.rb b/spec/unit/parser/functions/digest_spec.rb new file mode 100755 index 000000000..e3c0762d4 --- /dev/null +++ b/spec/unit/parser/functions/digest_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the digest function", :uses_checksums => true do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + n = Puppet::Node.new('unnamed') + c = Puppet::Parser::Compiler.new(n) + @scope = Puppet::Parser::Scope.new(c) + end + + it "should exist" do + Puppet::Parser::Functions.function("digest").should == "function_digest" + end + + with_digest_algorithms do + it "should use the proper digest function" do + result = @scope.function_digest([plaintext]) + result.should(eql( checksum )) + end + + it "should only accept one parameter" do + expect do + @scope.function_digest(['foo', 'bar']) + end.to raise_error(ArgumentError) + end + end +end diff --git a/spec/unit/parser/functions/epp_spec.rb b/spec/unit/parser/functions/epp_spec.rb deleted file mode 100644 index b88d3da8f..000000000 --- a/spec/unit/parser/functions/epp_spec.rb +++ /dev/null @@ -1,103 +0,0 @@ - -require 'spec_helper' - -describe "the epp function" do - include PuppetSpec::Files - - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - Puppet[:parser] = 'future' - end - - let :node do Puppet::Node.new('localhost') end - let :compiler do Puppet::Parser::Compiler.new(node) end - let :scope do Puppet::Parser::Scope.new(compiler) end - - context "when accessing scope variables as $ variables" do - it "looks up the value from the scope" do - scope["what"] = "are belong" - eval_template("all your base <%= $what %> to us").should == "all your base are belong to us" - end - - it "get nil accessing a variable that does not exist" do - eval_template("<%= $kryptonite == undef %>").should == "true" - end - - it "get nil accessing a variable that is undef" do - scope['undef_var'] = :undef - eval_template("<%= $undef_var == undef %>").should == "true" - end - - it "gets shadowed variable if args are given" do - scope['phantom'] = 'of the opera' - eval_template_with_args("<%= $phantom == dragos %>", 'phantom' => 'dragos').should == "true" - end - - it "gets shadowed variable if args are given and parameters are specified" do - scope['x'] = 'wrong one' - eval_template_with_args("<%-( $x )-%><%= $x == correct %>", 'x' => 'correct').should == "true" - end - - it "raises an error if required variable is not given" do - scope['x'] = 'wrong one' - expect { - eval_template_with_args("<%-| $x |-%><%= $x == correct %>", 'y' => 'correct') - }.to raise_error(/no value given for required parameters x/) - end - - it "raises an error if too many arguments are given" do - scope['x'] = 'wrong one' - expect { - eval_template_with_args("<%-| $x |-%><%= $x == correct %>", 'x' => 'correct', 'y' => 'surplus') - }.to raise_error(/Too many arguments: 2 for 1/) - end - end - - # although never a problem with epp - it "is not interfered with by having a variable named 'string' (#14093)" do - scope['string'] = "this output should not be seen" - eval_template("some text that is static").should == "some text that is static" - end - - it "has access to a variable named 'string' (#14093)" do - scope['string'] = "the string value" - eval_template("string was: <%= $string %>").should == "string was: the string value" - end - - describe 'when loading from modules' do - include PuppetSpec::Files - it 'an epp template is found' do - modules_dir = dir_containing('modules', { - 'testmodule' => { - 'templates' => { - 'the_x.epp' => 'The x is <%= $x %>' - } - }}) - Puppet.override({:current_environment => (env = Puppet::Node::Environment.create(:testload, [ modules_dir ]))}, "test") do - node.environment = env - expect(scope.function_epp([ 'testmodule/the_x.epp', { 'x' => '3'} ])).to eql("The x is 3") - end - end - end - - def eval_template_with_args(content, args_hash) - file_path = tmpdir('epp_spec_content') - filename = File.join(file_path, "template.epp") - File.open(filename, "w+") { |f| f.write(content) } - - Puppet::Parser::Files.stubs(:find_template).returns(filename) - scope.function_epp(['template', args_hash]) - end - - def eval_template(content) - file_path = tmpdir('epp_spec_content') - filename = File.join(file_path, "template.epp") - File.open(filename, "w+") { |f| f.write(content) } - - Puppet::Parser::Files.stubs(:find_template).returns(filename) - scope.function_epp(['template']) - end -end diff --git a/spec/unit/parser/functions/file_spec.rb b/spec/unit/parser/functions/file_spec.rb index 34d12e2f2..c5f157300 100755 --- a/spec/unit/parser/functions/file_spec.rb +++ b/spec/unit/parser/functions/file_spec.rb @@ -13,10 +13,6 @@ describe "the 'file' function" do let :compiler do Puppet::Parser::Compiler.new(node) end let :scope do Puppet::Parser::Scope.new(compiler) end - it "should exist" do - Puppet::Parser::Functions.function("file").should == "function_file" - end - def with_file_content(content) path = tmpfile('file-function') file = File.new(path, 'w') @@ -31,7 +27,17 @@ describe "the 'file' function" do end end - it "should return the first file if given two files" do + it "should read a file from a module path" do + with_file_content('file content') do |name| + mod = mock 'module' + mod.stubs(:file).with('myfile').returns(name) + compiler.environment.stubs(:module).with('mymod').returns(mod) + + scope.function_file(['mymod/myfile']).should == 'file content' + end + end + + it "should return the first file if given two files with absolute paths" do with_file_content('one') do |one| with_file_content('two') do |two| scope.function_file([one, two]).should == "one" @@ -39,6 +45,43 @@ describe "the 'file' function" do end end + it "should return the first file if given two files with module paths" do + with_file_content('one') do |one| + with_file_content('two') do |two| + mod = mock 'module' + compiler.environment.expects(:module).with('mymod').returns(mod) + mod.expects(:file).with('one').returns(one) + mod.stubs(:file).with('two').returns(two) + + scope.function_file(['mymod/one','mymod/two']).should == 'one' + end + end + end + + it "should return the first file if given two files with mixed paths, absolute first" do + with_file_content('one') do |one| + with_file_content('two') do |two| + mod = mock 'module' + compiler.environment.stubs(:module).with('mymod').returns(mod) + mod.stubs(:file).with('two').returns(two) + + scope.function_file([one,'mymod/two']).should == 'one' + end + end + end + + it "should return the first file if given two files with mixed paths, module first" do + with_file_content('one') do |one| + with_file_content('two') do |two| + mod = mock 'module' + compiler.environment.expects(:module).with('mymod').returns(mod) + mod.stubs(:file).with('two').returns(two) + + scope.function_file(['mymod/two',one]).should == 'two' + end + end + end + it "should not fail when some files are absent" do expect { with_file_content('one') do |one| diff --git a/spec/unit/parser/functions/include_spec.rb b/spec/unit/parser/functions/include_spec.rb index c1a5cbd5c..3fa0da35d 100755 --- a/spec/unit/parser/functions/include_spec.rb +++ b/spec/unit/parser/functions/include_spec.rb @@ -1,5 +1,6 @@ #! /usr/bin/env ruby require 'spec_helper' +require 'unit/parser/functions/shared' describe "the 'include' function" do before :all do @@ -46,6 +47,19 @@ describe "the 'include' function" do it "should raise if the class is not found" do @scope.stubs(:source).returns(true) - expect { @scope.function_include(["nosuchclass"]) }.to raise_error Puppet::Error + expect { @scope.function_include(["nosuchclass"]) }.to raise_error(Puppet::Error) + end + + describe "When the future parser is in use" do + require 'puppet/pops' + require 'puppet_spec/compiler' + include PuppetSpec::Compiler + + before(:each) do + Puppet[:parser] = 'future' + end + + it_should_behave_like 'all functions transforming relative to absolute names', :function_include + it_should_behave_like 'an inclusion function, regardless of the type of class reference,', :include end end diff --git a/spec/unit/parser/functions/inline_epp_spec.rb b/spec/unit/parser/functions/inline_epp_spec.rb deleted file mode 100644 index 44b24528b..000000000 --- a/spec/unit/parser/functions/inline_epp_spec.rb +++ /dev/null @@ -1,82 +0,0 @@ - -require 'spec_helper' - -describe "the inline_epp function" do - include PuppetSpec::Files - - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - Puppet[:parser] = 'future' - end - - let :node do Puppet::Node.new('localhost') end - let :compiler do Puppet::Parser::Compiler.new(node) end - let :scope do Puppet::Parser::Scope.new(compiler) end - - context "when accessing scope variables as $ variables" do - it "looks up the value from the scope" do - scope["what"] = "are belong" - eval_template("all your base <%= $what %> to us").should == "all your base are belong to us" - end - - it "get nil accessing a variable that does not exist" do - eval_template("<%= $kryptonite == undef %>").should == "true" - end - - it "get nil accessing a variable that is undef" do - scope['undef_var'] = :undef - eval_template("<%= $undef_var == undef %>").should == "true" - end - - it "gets shadowed variable if args are given" do - scope['phantom'] = 'of the opera' - eval_template_with_args("<%= $phantom == dragos %>", 'phantom' => 'dragos').should == "true" - end - - it "gets shadowed variable if args are given and parameters are specified" do - scope['x'] = 'wrong one' - eval_template_with_args("<%-| $x |-%><%= $x == correct %>", 'x' => 'correct').should == "true" - end - - it "raises an error if required variable is not given" do - scope['x'] = 'wrong one' - expect { - eval_template_with_args("<%-| $x |-%><%= $x == correct %>", 'y' => 'correct') - }.to raise_error(/no value given for required parameters x/) - end - - it "raises an error if too many arguments are given" do - scope['x'] = 'wrong one' - expect { - eval_template_with_args("<%-| $x |-%><%= $x == correct %>", 'x' => 'correct', 'y' => 'surplus') - }.to raise_error(/Too many arguments: 2 for 1/) - end - end - - it "renders a block expression" do - eval_template_with_args("<%= {($x) $x + 1} %>", 'x' => 2).should == "3" - end - - # although never a problem with epp - it "is not interfered with by having a variable named 'string' (#14093)" do - scope['string'] = "this output should not be seen" - eval_template("some text that is static").should == "some text that is static" - end - - it "has access to a variable named 'string' (#14093)" do - scope['string'] = "the string value" - eval_template("string was: <%= $string %>").should == "string was: the string value" - end - - - def eval_template_with_args(content, args_hash) - scope.function_inline_epp([content, args_hash]) - end - - def eval_template(content) - scope.function_inline_epp([content]) - end -end diff --git a/spec/unit/parser/functions/realize_spec.rb b/spec/unit/parser/functions/realize_spec.rb index 9f53f5a76..79e5eb155 100755 --- a/spec/unit/parser/functions/realize_spec.rb +++ b/spec/unit/parser/functions/realize_spec.rb @@ -1,53 +1,61 @@ -#! /usr/bin/env ruby require 'spec_helper' +require 'matchers/resource' +require 'puppet_spec/compiler' describe "the realize function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end + include Matchers::Resource + include PuppetSpec::Compiler - before :each do - @collector = stub_everything 'collector' - node = Puppet::Node.new('localhost') - @compiler = Puppet::Parser::Compiler.new(node) - @scope = Puppet::Parser::Scope.new(@compiler) - @compiler.stubs(:add_collection).with(@collector) - end + it "realizes a single, referenced resource" do + catalog = compile_to_catalog(<<-EOM) + @notify { testing: } + realize(Notify[testing]) + EOM - it "should exist" do - Puppet::Parser::Functions.function("realize").should == "function_realize" + expect(catalog).to have_resource("Notify[testing]") end - it "should create a Collector when called" do - - Puppet::Parser::Collector.expects(:new).returns(@collector) + it "realizes multiple resources" do + catalog = compile_to_catalog(<<-EOM) + @notify { testing: } + @notify { other: } + realize(Notify[testing], Notify[other]) + EOM - @scope.function_realize(["test"]) + expect(catalog).to have_resource("Notify[testing]") + expect(catalog).to have_resource("Notify[other]") end - it "should assign the passed-in resources to the collector" do - Puppet::Parser::Collector.stubs(:new).returns(@collector) + it "realizes resources provided in arrays" do + catalog = compile_to_catalog(<<-EOM) + @notify { testing: } + @notify { other: } + realize([Notify[testing], [Notify[other]]]) + EOM - @collector.expects(:resources=).with(["test"]) - - @scope.function_realize(["test"]) + expect(catalog).to have_resource("Notify[testing]") + expect(catalog).to have_resource("Notify[other]") end - it "should flatten the resources assigned to the collector" do - Puppet::Parser::Collector.stubs(:new).returns(@collector) - - @collector.expects(:resources=).with(["test"]) - - @scope.function_realize([["test"]]) + it "fails when the resource does not exist" do + expect do + compile_to_catalog(<<-EOM) + realize(Notify[missing]) + EOM + end.to raise_error(Puppet::Error, /Failed to realize/) end - it "should let the compiler know this collector" do - Puppet::Parser::Collector.stubs(:new).returns(@collector) - @collector.stubs(:resources=).with(["test"]) - - @compiler.expects(:add_collection).with(@collector) - - @scope.function_realize(["test"]) + it "fails when no parameters given" do + expect do + compile_to_catalog(<<-EOM) + realize() + EOM + end.to raise_error(Puppet::Error, /Wrong number of arguments/) end + it "silently does nothing when an empty array of resources is given" do + compile_to_catalog(<<-EOM) + realize([]) + EOM + end end diff --git a/spec/unit/parser/functions/require_spec.rb b/spec/unit/parser/functions/require_spec.rb index 72c3f9f5f..f0b4fcc28 100755 --- a/spec/unit/parser/functions/require_spec.rb +++ b/spec/unit/parser/functions/require_spec.rb @@ -1,5 +1,6 @@ #! /usr/bin/env ruby require 'spec_helper' +require 'unit/parser/functions/shared' describe "the require function" do before :all do @@ -26,13 +27,13 @@ describe "the require function" do end it "should delegate to the 'include' puppet function" do - @scope.expects(:function_include).with(["myclass"]) + @scope.compiler.expects(:evaluate_classes).with(["myclass"], @scope, false) @scope.function_require(["myclass"]) end - it "should set the 'require' prarameter on the resource to a resource reference" do - @scope.stubs(:function_include) + it "should set the 'require' parameter on the resource to a resource reference" do + @scope.compiler.stubs(:evaluate_classes) @scope.function_require(["myclass"]) @resource["require"].should be_instance_of(Array) @@ -40,7 +41,7 @@ describe "the require function" do end it "should lookup the absolute class path" do - @scope.stubs(:function_include) + @scope.compiler.stubs(:evaluate_classes) @scope.expects(:find_hostclass).with("myclass").returns(@klass) @klass.expects(:name).returns("myclass") @@ -49,7 +50,7 @@ describe "the require function" do end it "should append the required class to the require parameter" do - @scope.stubs(:function_include) + @scope.compiler.stubs(:evaluate_classes) one = Puppet::Resource.new(:file, "/one") @resource[:require] = one @@ -58,4 +59,17 @@ describe "the require function" do @resource[:require].should be_include(one) @resource[:require].detect { |r| r.to_s == "Class[Myclass]" }.should be_instance_of(Puppet::Resource) end + + describe "When the future parser is in use" do + require 'puppet/pops' + require 'puppet_spec/compiler' + include PuppetSpec::Compiler + + before(:each) do + Puppet[:parser] = 'future' + end + + it_should_behave_like 'all functions transforming relative to absolute names', :function_require + it_should_behave_like 'an inclusion function, regardless of the type of class reference,', :require + end end diff --git a/spec/unit/parser/functions/search_spec.rb b/spec/unit/parser/functions/search_spec.rb index b2c042b04..54054bd6a 100755 --- a/spec/unit/parser/functions/search_spec.rb +++ b/spec/unit/parser/functions/search_spec.rb @@ -20,4 +20,9 @@ describe "the 'search' function" do scope.expects(:add_namespace).with("who") scope.function_search(["where", "what", "who"]) end + + it "is deprecated" do + Puppet.expects(:deprecation_warning).with("The 'search' function is deprecated. See http://links.puppetlabs.com/search-function-deprecation") + scope.function_search(['wat']) + end end diff --git a/spec/unit/parser/functions/shared.rb b/spec/unit/parser/functions/shared.rb new file mode 100644 index 000000000..f5adcd811 --- /dev/null +++ b/spec/unit/parser/functions/shared.rb @@ -0,0 +1,82 @@ +shared_examples_for 'all functions transforming relative to absolute names' do |func_method| + + it 'transforms relative names to absolute' do + @scope.compiler.expects(:evaluate_classes).with(["::myclass"], @scope, false) + @scope.send(func_method, ["myclass"]) + end + + it 'accepts a Class[name] type' do + @scope.compiler.expects(:evaluate_classes).with(["::myclass"], @scope, false) + @scope.send(func_method, [Puppet::Pops::Types::TypeFactory.host_class('myclass')]) + end + + it 'accepts a Resource[class, name] type' do + @scope.compiler.expects(:evaluate_classes).with(["::myclass"], @scope, false) + @scope.send(func_method, [Puppet::Pops::Types::TypeFactory.resource('class', 'myclass')]) + end + + it 'raises and error for unspecific Class' do + expect { + @scope.send(func_method, [Puppet::Pops::Types::TypeFactory.host_class()]) + }.to raise_error(ArgumentError, /Cannot use an unspecific Class\[\] Type/) + end + + it 'raises and error for Resource that is not of class type' do + expect { + @scope.send(func_method, [Puppet::Pops::Types::TypeFactory.resource('file')]) + }.to raise_error(ArgumentError, /Cannot use a Resource\[file\] where a Resource\['class', name\] is expected/) + end + + it 'raises and error for Resource that is unspecific' do + expect { + @scope.send(func_method, [Puppet::Pops::Types::TypeFactory.resource()]) + }.to raise_error(ArgumentError, /Cannot use an unspecific Resource\[\] where a Resource\['class', name\] is expected/) + end + + it 'raises and error for Resource[class] that is unspecific' do + expect { + @scope.send(func_method, [Puppet::Pops::Types::TypeFactory.resource('class')]) + }.to raise_error(ArgumentError, /Cannot use an unspecific Resource\['class'\] where a Resource\['class', name\] is expected/) + end + +end + +shared_examples_for 'an inclusion function, regardless of the type of class reference,' do |function| + + it "and #{function} a class absolutely, even when a relative namespaced class of the same name is present" do + catalog = compile_to_catalog(<<-MANIFEST) + class foo { + class bar { } + #{function} bar + } + class bar { } + include foo + MANIFEST + expect(catalog.classes).to include('foo','bar') + end + + it "and #{function} a class absolutely by Class['type'] reference" do + catalog = compile_to_catalog(<<-MANIFEST) + class foo { + class bar { } + #{function} Class['bar'] + } + class bar { } + include foo + MANIFEST + expect(catalog.classes).to include('foo','bar') + end + + it "and #{function} a class absolutely by Resource['type','title'] reference" do + catalog = compile_to_catalog(<<-MANIFEST) + class foo { + class bar { } + #{function} Resource['class','bar'] + } + class bar { } + include foo + MANIFEST + expect(catalog.classes).to include('foo','bar') + end + +end |