From af4455e31df403c55a5db06ee8d0d4e0e58c4154 Mon Sep 17 00:00:00 2001 From: Brice Figureau Date: Sat, 29 Nov 2008 17:41:45 +0100 Subject: [PATCH] Fix #1088 - part2 - Add rspec tests Signed-off-by: Brice Figureau --- spec/unit/parser/ast/collection.rb | 63 ++++++++ spec/unit/parser/collector.rb | 178 +++++++++++++++++----- test/data/snippets/collection_override.pp | 8 + test/language/snippets.rb | 6 + 4 files changed, 217 insertions(+), 38 deletions(-) create mode 100755 spec/unit/parser/ast/collection.rb create mode 100644 test/data/snippets/collection_override.pp diff --git a/spec/unit/parser/ast/collection.rb b/spec/unit/parser/ast/collection.rb new file mode 100755 index 000000000..c141bd708 --- /dev/null +++ b/spec/unit/parser/ast/collection.rb @@ -0,0 +1,63 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe Puppet::Parser::AST::Collection do + before :each do + @scope = stub_everything 'scope' + @compiler = stub_everything 'compile' + @scope.stubs(:compiler).returns(@compiler) + + @overrides = stub_everything 'overrides' + @overrides.stubs(:is_a?).with(Puppet::Parser::AST).returns(true) + + end + + it "should evaluate its query" do + query = mock 'query' + collection = Puppet::Parser::AST::Collection.new :query => query, :form => :virtual + + query.expects(:safeevaluate).with(@scope) + + collection.evaluate(@scope) + end + + it "should instantiate a Collector for this type" do + collection = Puppet::Parser::AST::Collection.new :form => :virtual, :type => "test" + + Puppet::Parser::Collector.expects(:new).with(@scope, "test", nil, nil, :virtual) + + collection.evaluate(@scope) + end + + it "should tell the compiler about this collector" do + collection = Puppet::Parser::AST::Collection.new :form => :virtual, :type => "test" + Puppet::Parser::Collector.stubs(:new).returns("whatever") + + @compiler.expects(:add_collection).with("whatever") + + collection.evaluate(@scope) + end + + it "should evaluate overriden paramaters" do + collector = stub_everything 'collector' + collection = Puppet::Parser::AST::Collection.new :form => :virtual, :type => "test", :override => @overrides + Puppet::Parser::Collector.stubs(:new).returns(collector) + + @overrides.expects(:safeevaluate).with(@scope) + + collection.evaluate(@scope) + end + + it "should tell the collector about overrides" do + collector = mock 'collector' + collection = Puppet::Parser::AST::Collection.new :form => :virtual, :type => "test", :override => @overrides + Puppet::Parser::Collector.stubs(:new).returns(collector) + + collector.expects(:add_override) + + collection.evaluate(@scope) + end + + +end diff --git a/spec/unit/parser/collector.rb b/spec/unit/parser/collector.rb index f92b881c8..4756204ce 100755 --- a/spec/unit/parser/collector.rb +++ b/spec/unit/parser/collector.rb @@ -39,6 +39,14 @@ describe Puppet::Parser::Collector, "when initializing" do @collector = Puppet::Parser::Collector.new(@scope, "resource::type", @equery, @vquery, @form) @collector.type.should == "Resource::Type" end + + it "should accept an optional resource override" do + @collector = Puppet::Parser::Collector.new(@scope, "resource::type", @equery, @vquery, @form) + override = { :params => "whatever" } + @collector.add_override(override) + @collector.overrides.should equal(override) + end + end describe Puppet::Parser::Collector, "when collecting specific virtual resources" do @@ -59,8 +67,9 @@ describe Puppet::Parser::Collector, "when collecting specific virtual resources" it "should mark matched resources as non-virtual" do @collector.resources = ["File[virtual1]", "File[virtual2]"] - one = mock 'one' + one = stub_everything 'one' one.expects(:virtual=).with(false) + @scope.stubs(:findresource).with("File[virtual1]").returns(one) @scope.stubs(:findresource).with("File[virtual2]").returns(nil) @collector.evaluate @@ -68,8 +77,7 @@ describe Puppet::Parser::Collector, "when collecting specific virtual resources" it "should return matched resources" do @collector.resources = ["File[virtual1]", "File[virtual2]"] - one = mock 'one' - one.stubs(:virtual=) + one = stub_everything 'one' @scope.stubs(:findresource).with("File[virtual1]").returns(one) @scope.stubs(:findresource).with("File[virtual2]").returns(nil) @collector.evaluate.should == [one] @@ -77,8 +85,7 @@ describe Puppet::Parser::Collector, "when collecting specific virtual resources" it "should delete itself from the compile's collection list if it has found all of its resources" do @collector.resources = ["File[virtual1]"] - one = mock 'one' - one.stubs(:virtual=) + one = stub_everything 'one' @compiler.expects(:delete_collection).with(@collector) @scope.expects(:compiler).returns(@compiler) @scope.stubs(:findresource).with("File[virtual1]").returns(one) @@ -87,15 +94,14 @@ describe Puppet::Parser::Collector, "when collecting specific virtual resources" it "should not delete itself from the compile's collection list if it has unfound resources" do @collector.resources = ["File[virtual1]"] - one = mock 'one' - one.stubs(:virtual=) + one = stub_everything 'one' @compiler.expects(:delete_collection).never @scope.stubs(:findresource).with("File[virtual1]").returns(nil) @collector.evaluate end end -describe Puppet::Parser::Collector, "when collecting virtual resources" do +describe Puppet::Parser::Collector, "when collecting virtual and catalog resources" do before do @scope = mock 'scope' @compiler = mock 'compile' @@ -106,12 +112,18 @@ describe Puppet::Parser::Collector, "when collecting virtual resources" do @collector = Puppet::Parser::Collector.new(@scope, @resource_type, nil, @vquery, :virtual) end - it "should find all resources matching the vquery" do - one = stub 'one', :type => "Mytype", :virtual? => true - two = stub 'two', :type => "Mytype", :virtual? => true + it "should find all virtual resources matching the vquery" do + one = stub_everything 'one', :type => "Mytype", :virtual? => true + two = stub_everything 'two', :type => "Mytype", :virtual? => true - one.stubs(:virtual=) - two.stubs(:virtual=) + @compiler.expects(:resources).returns([one, two]) + + @collector.evaluate.should == [one, two] + end + + it "should find all non-virtual resources matching the vquery" do + one = stub_everything 'one', :type => "Mytype", :virtual? => false + two = stub_everything 'two', :type => "Mytype", :virtual? => false @compiler.expects(:resources).returns([one, two]) @@ -119,7 +131,7 @@ describe Puppet::Parser::Collector, "when collecting virtual resources" do end it "should mark all matched resources as non-virtual" do - one = stub 'one', :type => "Mytype", :virtual? => true + one = stub_everything 'one', :type => "Mytype", :virtual? => true one.expects(:virtual=).with(false) @@ -129,11 +141,8 @@ describe Puppet::Parser::Collector, "when collecting virtual resources" do end it "should return matched resources" do - one = stub 'one', :type => "Mytype", :virtual? => true - two = stub 'two', :type => "Mytype", :virtual? => true - - one.stubs(:virtual=) - two.stubs(:virtual=) + one = stub_everything 'one', :type => "Mytype", :virtual? => true + two = stub_everything 'two', :type => "Mytype", :virtual? => true @compiler.expects(:resources).returns([one, two]) @@ -141,8 +150,8 @@ describe Puppet::Parser::Collector, "when collecting virtual resources" do end it "should return all resources of the correct type if there is no virtual query" do - one = stub 'one', :type => "Mytype", :virtual? => true - two = stub 'two', :type => "Mytype", :virtual? => true + one = stub_everything 'one', :type => "Mytype", :virtual? => true + two = stub_everything 'two', :type => "Mytype", :virtual? => true one.expects(:virtual=).with(false) two.expects(:virtual=).with(false) @@ -155,8 +164,8 @@ describe Puppet::Parser::Collector, "when collecting virtual resources" do end it "should not return or mark resources of a different type" do - one = stub 'one', :type => "Mytype", :virtual? => true - two = stub 'two', :type => :other, :virtual? => true + one = stub_everything 'one', :type => "Mytype", :virtual? => true + two = stub_everything 'two', :type => :other, :virtual? => true one.expects(:virtual=).with(false) two.expects(:virtual=).never @@ -166,23 +175,84 @@ describe Puppet::Parser::Collector, "when collecting virtual resources" do @collector.evaluate.should == [one] end - it "should not return or mark non-virtual resources" do - one = stub 'one', :type => "Mytype", :virtual? => false - two = stub 'two', :type => :other, :virtual? => false + it "should create a resource with overriden parameters" do + one = stub_everything 'one', :type => "Mytype", :virtual? => true, :title => "test" + param = stub 'param' + @compiler.stubs(:add_override) - one.expects(:virtual=).never - two.expects(:virtual=).never + @compiler.expects(:resources).returns([one]) - @compiler.expects(:resources).returns([one, two]) + @collector.add_override(:params => param ) + Puppet::Parser::Resource.expects(:new).with { |h| + h[:params] == param + } + + @collector.evaluate + end + + it "should define a new allow all child_of? on overriden resource" do + one = stub_everything 'one', :type => "Mytype", :virtual? => true, :title => "test" + param = stub 'param' + source = stub 'source' + @compiler.stubs(:add_override) + + @compiler.expects(:resources).returns([one]) + + @collector.add_override(:params => param, :source => source ) + Puppet::Parser::Resource.stubs(:new) + + source.expects(:meta_def).with { |name,block| name == :child_of? } + + @collector.evaluate + end + + + it "should not override already overriden resources for this same collection in a previous run" do + one = stub_everything 'one', :type => "Mytype", :virtual? => true, :title => "test" + param = stub 'param' + @compiler.stubs(:add_override) + + @compiler.expects(:resources).at_least(2).returns([one]) + + @collector.add_override(:params => param ) + Puppet::Parser::Resource.expects(:new).once.with { |h| + h[:params] == param + } + + @collector.evaluate + + @collector.evaluate + end + + it "should not return resources that were collected in a previous run of this collector" do + one = stub_everything 'one', :type => "Mytype", :virtual? => true, :title => "test" + @compiler.stubs(:resources).returns([one]) + + @collector.evaluate @collector.evaluate.should be_false end + + it "should tell the compiler about the overriden resources" do + one = stub_everything 'one', :type => "Mytype", :virtual? => true, :title => "test" + param = stub 'param' + + one.expects(:virtual=).with(false) + @compiler.expects(:resources).returns([one]) + @collector.add_override(:params => param ) + Puppet::Parser::Resource.stubs(:new).returns("whatever") + + @compiler.expects(:add_override).with("whatever") + + @collector.evaluate + end + it "should not return or mark non-matching resources" do @collector.vquery = proc { |res| res.name == :one } - one = stub 'one', :name => :one, :type => "Mytype", :virtual? => true - two = stub 'two', :name => :two, :type => "Mytype", :virtual? => true + one = stub_everything 'one', :name => :one, :type => "Mytype", :virtual? => true + two = stub_everything 'two', :name => :two, :type => "Mytype", :virtual? => true one.expects(:virtual=).with(false) two.expects(:virtual=).never @@ -237,8 +307,8 @@ describe Puppet::Parser::Collector, "when collecting exported resources" do it "should return all matching resources from the current compile and mark them non-virtual and non-exported" do stub_rails(true) - one = stub 'one', :type => "Mytype", :virtual? => true, :exported? => true - two = stub 'two', :type => "Mytype", :virtual? => true, :exported? => true + one = stub 'one', :type => "Mytype", :virtual? => true, :exported? => true, :ref => "one" + two = stub 'two', :type => "Mytype", :virtual? => true, :exported? => true, :ref => "two" one.stubs(:exported=) one.stubs(:virtual=) @@ -253,7 +323,7 @@ describe Puppet::Parser::Collector, "when collecting exported resources" do it "should mark all returned resources as not virtual" do stub_rails(true) - one = stub 'one', :type => "Mytype", :virtual? => true, :exported? => true + one = stub 'one', :type => "Mytype", :virtual? => true, :exported? => true, :ref => "one" one.stubs(:exported=) one.expects(:virtual=).with(false) @@ -267,13 +337,14 @@ describe Puppet::Parser::Collector, "when collecting exported resources" do stub_rails() Puppet::Rails::Host.stubs(:find_by_name).returns(nil) - one = stub 'one', :restype => "Mytype", :title => "one", :virtual? => true, :exported? => true + one = stub 'one', :restype => "Mytype", :title => "one", :virtual? => true, :exported? => true, :ref => "one" Puppet::Rails::Resource.stubs(:find).returns([one]) resource = mock 'resource' one.expects(:to_resource).with(@scope).returns(resource) resource.stubs(:exported=) resource.stubs(:virtual=) + resource.stubs(:ref) @compiler.stubs(:resources).returns([]) @scope.stubs(:findresource).returns(nil) @@ -283,17 +354,47 @@ describe Puppet::Parser::Collector, "when collecting exported resources" do @collector.evaluate.should == [resource] end - it "should store converted resources in the compile's resource list" do + it "should override all exported collected resources if collector has an override" do stub_rails() Puppet::Rails::Host.stubs(:find_by_name).returns(nil) - one = stub 'one', :restype => "Mytype", :title => "one", :virtual? => true, :exported? => true + one = stub 'one', :restype => "Mytype", :title => "one", :virtual? => true, :exported? => true, :ref => "one" Puppet::Rails::Resource.stubs(:find).returns([one]) resource = mock 'resource' one.expects(:to_resource).with(@scope).returns(resource) resource.stubs(:exported=) resource.stubs(:virtual=) + resource.stubs(:ref) + resource.stubs(:title) + + @compiler.stubs(:resources).returns([]) + @scope.stubs(:findresource).returns(nil) + + param = stub 'param' + @compiler.stubs(:add_override) + @compiler.stubs(:add_resource) + + @collector.add_override(:params => param ) + Puppet::Parser::Resource.expects(:new).once.with { |h| + h[:params] == param + } + + @collector.evaluate + end + + it "should store converted resources in the compile's resource list" do + stub_rails() + Puppet::Rails::Host.stubs(:find_by_name).returns(nil) + + one = stub 'one', :restype => "Mytype", :title => "one", :virtual? => true, :exported? => true, :ref => "one" + Puppet::Rails::Resource.stubs(:find).returns([one]) + + resource = mock 'resource' + one.expects(:to_resource).with(@scope).returns(resource) + resource.stubs(:exported=) + resource.stubs(:virtual=) + resource.stubs(:ref) @compiler.stubs(:resources).returns([]) @scope.stubs(:findresource).returns(nil) @@ -308,13 +409,14 @@ describe Puppet::Parser::Collector, "when collecting exported resources" do stub_rails() Puppet::Rails::Host.stubs(:find_by_name).returns(nil) - one = stub 'one', :restype => "Mytype", :title => "one", :virtual? => true, :exported? => true + one = stub 'one', :restype => "Mytype", :title => "one", :virtual? => true, :exported? => true, :ref => "one" Puppet::Rails::Resource.stubs(:find).returns([one]) resource = mock 'resource' one.expects(:to_resource).with(@scope).returns(resource) resource.expects(:exported=).with(false) resource.stubs(:virtual=) + resource.stubs(:ref) @compiler.stubs(:resources).returns([]) @scope.stubs(:findresource).returns(nil) diff --git a/test/data/snippets/collection_override.pp b/test/data/snippets/collection_override.pp new file mode 100644 index 000000000..b1b39ab16 --- /dev/null +++ b/test/data/snippets/collection_override.pp @@ -0,0 +1,8 @@ +@file { + "/tmp/collection": + content => "whatever" +} + +File<| |> { + mode => 0600 +} diff --git a/test/language/snippets.rb b/test/language/snippets.rb index dfd4950e7..eac852ed0 100755 --- a/test/language/snippets.rb +++ b/test/language/snippets.rb @@ -475,6 +475,12 @@ class TestSnippets < Test::Unit::TestCase assert_not_file("/tmp/multilinecomments","Did create a commented resource"); end + def snippet_collection_override + path = "/tmp/collection" + assert_file(path) + assert_mode_equal(0600, path) + end + # Iterate across each of the snippets and create a test. Dir.entries(snippetdir).sort.each { |file| next if file =~ /^\./