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 @@ -1,426 +1,528 @@ #!/usr/bin/env ruby require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/parser/collector' describe Puppet::Parser::Collector, "when initializing" do before do @scope = mock 'scope' @resource_type = 'resource_type' @form = :exported @vquery = mock 'vquery' @equery = mock 'equery' @collector = Puppet::Parser::Collector.new(@scope, @resource_type, @equery, @vquery, @form) end it "should require a scope" do @collector.scope.should equal(@scope) end it "should require a resource type" do @collector.type.should == 'Resource_type' end it "should only accept :virtual or :exported as the collector form" do proc { @collector = Puppet::Parser::Collector.new(@scope, @resource_type, @vquery, @equery, :other) }.should raise_error(ArgumentError) end it "should accept an optional virtual query" do @collector.vquery.should equal(@vquery) end it "should accept an optional exported query" do @collector.equery.should equal(@equery) end it "should canonize the type name" 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 before do @scope = mock 'scope' @resource_type = mock 'resource_type' @vquery = mock 'vquery' @equery = mock 'equery' @collector = Puppet::Parser::Collector.new(@scope, @resource_type, @equery, @vquery, :virtual) end it "should not fail when it does not find any resources to collect" do @collector.resources = ["File[virtual1]", "File[virtual2]"] @scope.stubs(:findresource).returns(false) proc { @collector.evaluate }.should_not raise_error end 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 end 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] end 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) @collector.evaluate end 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' @scope.stubs(:compiler).returns(@compiler) @resource_type = "Mytype" @vquery = proc { |res| true } @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]) @collector.evaluate.should == [one, two] 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) @compiler.expects(:resources).returns([one]) @collector.evaluate 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]) @collector.evaluate.should == [one, two] 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) @compiler.expects(:resources).returns([one, two]) @collector = Puppet::Parser::Collector.new(@scope, @resource_type, nil, nil, :virtual) @collector.evaluate.should == [one, two] 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 @compiler.expects(:resources).returns([one, two]) @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 @compiler.expects(:resources).returns([one, two]) @collector.evaluate.should == [one] end end describe Puppet::Parser::Collector, "when collecting exported resources" do confine "Cannot test Rails integration without ActiveRecord" => Puppet.features.rails? before do @scope = stub 'scope', :host => "myhost", :debug => nil @compiler = mock 'compile' @scope.stubs(:compiler).returns(@compiler) @resource_type = "Mytype" @equery = "test = true" @vquery = proc { |r| true } Puppet.settings.stubs(:value).with(:storeconfigs).returns true @collector = Puppet::Parser::Collector.new(@scope, @resource_type, @equery, @vquery, :exported) end # Stub most of our interface to Rails. def stub_rails(everything = false) ActiveRecord::Base.stubs(:connected?).returns(false) Puppet::Rails.stubs(:init) if everything Puppet::Rails::Host.stubs(:find_by_name).returns(nil) Puppet::Rails::Resource.stubs(:find).returns([]) end end it "should just return false if :storeconfigs is not enabled" do Puppet.settings.expects(:value).with(:storeconfigs).returns false @collector.evaluate.should be_false end it "should use initialize the Rails support if ActiveRecord is not connected" do @compiler.stubs(:resources).returns([]) ActiveRecord::Base.expects(:connected?).returns(false) Puppet::Rails.expects(:init) Puppet::Rails::Host.stubs(:find_by_name).returns(nil) Puppet::Rails::Resource.stubs(:find).returns([]) @collector.evaluate end 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=) two.stubs(:exported=) two.stubs(:virtual=) @compiler.expects(:resources).returns([one, two]) @collector.evaluate.should == [one, two] end 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) @compiler.expects(:resources).returns([one]) @collector.evaluate.should == [one] end it "should convert all found resources into parser 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) @compiler.stubs(:add_resource) @collector.evaluate.should == [resource] end + 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, :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 + 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) @compiler.expects(:add_resource).with(@scope, resource) @collector.evaluate.should == [resource] end # This way one host doesn't store another host's resources as exported. it "should mark resources collected from the database as not exported" 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) @compiler.stubs(:add_resource) @collector.evaluate end it "should fail if an equivalent resource already exists in the compile" do stub_rails() Puppet::Rails::Host.stubs(:find_by_name).returns(nil) rails = stub 'one', :restype => "Mytype", :title => "one", :virtual? => true, :exported? => true, :id => 1, :ref => "yay" inmemory = stub 'one', :type => "Mytype", :virtual? => true, :exported? => true, :rails_id => 2 Puppet::Rails::Resource.stubs(:find).returns([rails]) resource = mock 'resource' @compiler.stubs(:resources).returns([]) @scope.stubs(:findresource).returns(inmemory) @compiler.stubs(:add_resource) proc { @collector.evaluate }.should raise_error(Puppet::ParseError) end it "should ignore exported resources that match already-collected resources" do stub_rails() Puppet::Rails::Host.stubs(:find_by_name).returns(nil) rails = stub 'one', :restype => "Mytype", :title => "one", :virtual? => true, :exported? => true, :id => 1, :ref => "yay" inmemory = stub 'one', :type => "Mytype", :virtual? => true, :exported? => true, :rails_id => 1 Puppet::Rails::Resource.stubs(:find).returns([rails]) resource = mock 'resource' @compiler.stubs(:resources).returns([]) @scope.stubs(:findresource).returns(inmemory) @compiler.stubs(:add_resource) proc { @collector.evaluate }.should_not raise_error(Puppet::ParseError) end end describe Puppet::Parser::Collector, "when building its ActiveRecord query for collecting exported resources" do confine "Cannot test Rails integration without ActiveRecord" => Puppet.features.rails? before do @scope = stub 'scope', :host => "myhost", :debug => nil @compiler = mock 'compile' @scope.stubs(:compiler).returns(@compiler) @resource_type = "Mytype" @equery = nil @vquery = proc { |r| true } @collector = Puppet::Parser::Collector.new(@scope, @resource_type, @equery, @vquery, :exported) @compiler.stubs(:resources).returns([]) ActiveRecord::Base.stubs(:connected?).returns(false) Puppet::Rails.stubs(:init) Puppet::Rails::Host.stubs(:find_by_name).returns(nil) Puppet::Rails::Resource.stubs(:find).returns([]) Puppet.settings.stubs(:value).with(:storeconfigs).returns true end it "should exclude all resources from the host if ActiveRecord contains information for this host" do @host = mock 'host' @host.stubs(:id).returns 5 Puppet::Rails::Host.expects(:find_by_name).with(@scope.host).returns(@host) Puppet::Rails::Resource.stubs(:find).with { |*arguments| options = arguments[3] options[:conditions][0] =~ /^host_id != \?/ and options[:conditions][1] == 5 }.returns([]) @collector.evaluate end it "should return parameter names and parameter values when querying ActiveRecord" do Puppet::Rails::Resource.stubs(:find).with { |*arguments| options = arguments[3] options[:include] == {:param_values => :param_name} }.returns([]) @collector.evaluate end it "should only search for exported resources with the matching type" do Puppet::Rails::Resource.stubs(:find).with { |*arguments| options = arguments[3] options[:conditions][0].include?("(exported=? AND restype=?)") and options[:conditions][1] == true and options[:conditions][2] == "Mytype" }.returns([]) end it "should include the export query if one is provided" do @collector = Puppet::Parser::Collector.new(@scope, @resource_type, "test = true", @vquery, :exported) Puppet::Rails::Resource.stubs(:find).with { |*arguments| options = arguments[3] options[:conditions][0].include?("test = true") }.returns([]) end end 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 @@ -1,516 +1,522 @@ #!/usr/bin/env ruby require File.dirname(__FILE__) + '/../lib/puppettest' require 'puppet' require 'puppet/parser/interpreter' require 'puppet/parser/parser' require 'puppet/network/client' require 'puppet/network/handler' require 'puppettest' class TestSnippets < Test::Unit::TestCase include PuppetTest def setup super @file = Puppet::Type.type(:file) Facter.stubs(:to_hash).returns({}) Facter.stubs(:value).returns("whatever") end def self.snippetdir PuppetTest.datadir "snippets" end def assert_file(path, msg = nil) unless file = @catalog.resource(:file, path) msg ||= "Could not find file %s" % path raise msg end end def assert_not_file(path, msg = nil) if file = @catalog.resource(:file, path) msg ||= "File %s exists!" % path raise msg end end def assert_mode_equal(mode, path) unless file = @catalog.resource(:file, path) raise "Could not find file %s" % path end unless mode == file.should(:mode) raise "Mode for %s is incorrect: %o vs %o" % [path, mode, file.should(:mode)] end end def snippet(name) File.join(self.class.snippetdir, name) end def file2ast(file) parser = Puppet::Parser::Parser.new() parser.file = file ast = parser.parse return ast end def snippet2ast(text) parser = Puppet::Parser::Parser.new() parser.string = text ast = parser.parse return ast end def client args = { :Listen => false } Puppet::Network::Client.new(args) end def ast2scope(ast) interp = Puppet::Parser::Interpreter.new( :ast => ast, :client => client() ) scope = Puppet::Parser::Scope.new() ast.evaluate(scope) return scope end def scope2objs(scope) objs = scope.to_trans end def snippet2scope(snippet) ast = snippet2ast(snippet) scope = ast2scope(ast) end def snippet2objs(snippet) ast = snippet2ast(snippet) scope = ast2scope(ast) objs = scope2objs(scope) end def properties(type) properties = type.validproperties end def metaparams(type) mparams = [] Puppet::Type.eachmetaparam { |param| mparams.push param } mparams end def params(type) params = [] type.parameters.each { |name,property| params.push name } params end def randthing(thing,type) list = self.send(thing,type) list[rand(list.length)] end def randeach(type) [:properties, :metaparams, :params].collect { |thing| randthing(thing,type) } end @@snippets = { true => [ %{File { mode => 755 }} ], } def disabled_test_defaults Puppet::Type.eachtype { |type| next if type.name == :puppet or type.name == :component rands = randeach(type) name = type.name.to_s.capitalize [0..1, 0..2].each { |range| params = rands[range] paramstr = params.collect { |param| "%s => fake" % param }.join(", ") str = "%s { %s }" % [name, paramstr] scope = nil assert_nothing_raised { scope = snippet2scope(str) } defaults = nil assert_nothing_raised { defaults = scope.lookupdefaults(name) } p defaults params.each { |param| puts "%s => '%s'" % [name,param] assert(defaults.include?(param)) } } } end # this is here in case no tests get defined; otherwise we get a warning def test_nothing end def snippet_filecreate %w{a b c d}.each { |letter| path = "/tmp/create%stest" % letter assert_file(path) if %w{a b}.include?(letter) assert_mode_equal(0755, path) end } end def snippet_simpledefaults path = "/tmp/defaulttest" assert_file(path) assert_mode_equal(0755, path) end def snippet_simpleselector files = %w{a b c d}.collect { |letter| path = "/tmp/snippetselect%stest" % letter assert_file(path) assert_mode_equal(0755, path) } end def snippet_classpathtest path = "/tmp/classtest" file = @catalog.resource(:file, path) assert(file, "did not create file %s" % path) assert_nothing_raised { assert_equal( "//testing/Mytype[componentname]/File[/tmp/classtest]", file.path) } end def snippet_argumentdefaults path1 = "/tmp/argumenttest1" path2 = "/tmp/argumenttest2" file1 = @catalog.resource(:file, path1) file2 = @catalog.resource(:file, path2) assert_file(path1) assert_mode_equal(0755, path1) assert_file(path2) assert_mode_equal(0644, path2) end def snippet_casestatement paths = %w{ /tmp/existsfile /tmp/existsfile2 /tmp/existsfile3 /tmp/existsfile4 /tmp/existsfile5 } paths.each { |path| file = @catalog.resource(:file, path) assert(file, "File %s is missing" % path) assert_mode_equal(0755, path) } end def snippet_implicititeration paths = %w{a b c d e f g h}.collect { |l| "/tmp/iteration%stest" % l } paths.each { |path| file = @catalog.resource(:file, path) assert_file(path) assert_mode_equal(0755, path) } end def snippet_multipleinstances paths = %w{a b c}.collect { |l| "/tmp/multipleinstances%s" % l } paths.each { |path| assert_file(path) assert_mode_equal(0755, path) } end def snippet_namevartest file = "/tmp/testfiletest" dir = "/tmp/testdirtest" assert_file(file) assert_file(dir) assert_equal(:directory, @catalog.resource(:file, dir).should(:ensure), "Directory is not set to be a directory") end def snippet_scopetest file = "/tmp/scopetest" assert_file(file) assert_mode_equal(0755, file) end def snippet_selectorvalues nums = %w{1 2 3 4 5} files = nums.collect { |n| "/tmp/selectorvalues%s" % n } files.each { |f| assert_file(f) assert_mode_equal(0755, f) } end def snippet_singleselector nums = %w{1 2 3} files = nums.collect { |n| "/tmp/singleselector%s" % n } files.each { |f| assert_file(f) assert_mode_equal(0755, f) } end def snippet_falsevalues file = "/tmp/falsevaluesfalse" assert_file(file) end def disabled_snippet_classargtest [1,2].each { |num| file = "/tmp/classargtest%s" % num assert_file(file) assert_mode_equal(0755, file) } end def snippet_classheirarchy [1,2,3].each { |num| file = "/tmp/classheir%s" % num assert_file(file) assert_mode_equal(0755, file) } end def snippet_singleary [1,2,3,4].each { |num| file = "/tmp/singleary%s" % num assert_file(file) } end def snippet_classincludes [1,2,3].each { |num| file = "/tmp/classincludes%s" % num assert_file(file) assert_mode_equal(0755, file) } end def snippet_componentmetaparams ["/tmp/component1", "/tmp/component2"].each { |file| assert_file(file) } end def snippet_aliastest %w{/tmp/aliastest /tmp/aliastest2 /tmp/aliastest3}.each { |file| assert_file(file) } end def snippet_singlequote { 1 => 'a $quote', 2 => 'some "\yayness\"' }.each { |count, str| path = "/tmp/singlequote%s" % count assert_file(path) assert_equal(str, @catalog.resource(:file, path).parameter(:content).actual_content) } end # There's no way to actually retrieve the list of classes from the # transaction. def snippet_tag end # Make sure that set tags are correctly in place, yo. def snippet_tagged tags = {"testing" => true, "yayness" => false, "both" => false, "bothtrue" => true, "define" => true} tags.each do |tag, retval| assert_file("/tmp/tagged#{tag}#{retval.to_s}") end end def snippet_defineoverrides file = "/tmp/defineoverrides1" assert_file(file) assert_mode_equal(0755, file) end def snippet_deepclassheirarchy 5.times { |i| i += 1 file = "/tmp/deepclassheir%s" % i assert_file(file) } end def snippet_emptyclass # There's nothing to check other than that it works end def snippet_emptyexec assert(@catalog.resource(:exec, "touch /tmp/emptyexectest"), "Did not create exec") end def snippet_multisubs path = "/tmp/multisubtest" assert_file(path) file = @catalog.resource(:file, path) assert_equal("{md5}5fbef65269a99bddc2106251dd89b1dc", file.should(:content), "sub2 did not override content") assert_mode_equal(0755, path) end def snippet_collection assert_file("/tmp/colltest1") assert_nil(@catalog.resource(:file, "/tmp/colltest2"), "Incorrectly collected file") end def snippet_virtualresources %w{1 2 3 4}.each do |num| assert_file("/tmp/virtualtest#{num}") end end def snippet_componentrequire %w{1 2}.each do |num| assert_file("/tmp/testing_component_requires#{num}", "#{num} does not exist") end end def snippet_realize_defined_types assert_file("/tmp/realize_defined_test1") assert_file("/tmp/realize_defined_test2") end def snippet_collection_within_virtual_definitions assert_file("/tmp/collection_within_virtual_definitions1_foo.txt") assert_file("/tmp/collection_within_virtual_definitions2_foo2.txt") end def snippet_fqparents assert_file("/tmp/fqparent1", "Did not make file from parent class") assert_file("/tmp/fqparent2", "Did not make file from subclass") end def snippet_fqdefinition assert_file("/tmp/fqdefinition", "Did not make file from fully-qualified definition") end def snippet_subclass_name_duplication assert_file("/tmp/subclass_name_duplication1", "Did not make first file from duplicate subclass names") assert_file("/tmp/subclass_name_duplication2", "Did not make second file from duplicate subclass names") end def snippet_funccomma assert_file("/tmp/funccomma1", "Did not make first file from trailing function comma") assert_file("/tmp/funccomma2", "Did not make second file from trailing function comma") end def snippet_arraytrailingcomma assert_file("/tmp/arraytrailingcomma1", "Did not make first file from array") assert_file("/tmp/arraytrailingcomma2", "Did not make second file from array") end def snippet_multipleclass assert_file("/tmp/multipleclassone", "one") assert_file("/tmp/multipleclasstwo", "two") end def snippet_multilinecomments 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 =~ /^\./ mname = "snippet_" + file.sub(/\.pp$/, '') if self.method_defined?(mname) #eval("alias %s %s" % [testname, mname]) testname = ("test_" + mname).intern self.send(:define_method, testname) { Puppet[:manifest] = snippet(file) facts = { "hostname" => "testhost", "domain" => "domain.com", "ipaddress" => "127.0.0.1", "fqdn" => "testhost.domain.com" } node = Puppet::Node.new("testhost") node.merge(facts) catalog = nil assert_nothing_raised("Could not compile catalog") { catalog = Puppet::Resource::Catalog.find(node) } assert_nothing_raised("Could not convert catalog") { catalog = catalog.to_ral } @catalog = catalog assert_nothing_raised { self.send(mname) } } mname = mname.intern end } end