diff --git a/acceptance/tests/apply/virtual/should_realize.rb b/acceptance/tests/apply/virtual/should_realize.rb deleted file mode 100755 index 7dc2406cd..000000000 --- a/acceptance/tests/apply/virtual/should_realize.rb +++ /dev/null @@ -1,25 +0,0 @@ -test_name "should realize" - -agents.each do |agent| - out = agent.tmpfile('should_realize') - name = "test-#{Time.new.to_i}-host" - -manifest = %Q{ - @host{'#{name}': ip=>'127.0.0.2', target=>'#{out}', ensure=>present} - realize(Host['#{name}']) -} - - step "clean the system ready for testing" - on agent, "rm -f #{out}" - - step "realize the resource on the host" - apply_manifest_on agent, manifest - - step "verify the content of the file" - on(agent, "cat #{out}") do - fail_test "missing host definition" unless stdout.include? name - end - - step "final cleanup of the system" - on agent, "rm -f #{out}" -end diff --git a/acceptance/tests/apply/virtual/should_realize_many.rb b/acceptance/tests/apply/virtual/should_realize_many.rb deleted file mode 100755 index 80cf3f580..000000000 --- a/acceptance/tests/apply/virtual/should_realize_many.rb +++ /dev/null @@ -1,24 +0,0 @@ -test_name "test that realize function takes a list" - -agents.each do |agent| - out = agent.tmpfile('should_realize_many') - name = "test-#{Time.new.to_i}-host" - -manifest = %Q{ - @host{'#{name}1': ip=>'127.0.0.2', target=>'#{out}', ensure=>present} - @host{'#{name}2': ip=>'127.0.0.2', target=>'#{out}', ensure=>present} - realize(Host['#{name}1'], Host['#{name}2']) -} - - step "clean up target system for test" - on agent, "rm -f #{out}" - - step "run the manifest" - apply_manifest_on agent, manifest - - step "verify the file output" - on(agent, "cat #{out}") do - fail_test "first host not found in output" unless stdout.include? "#{name}1" - fail_test "second host not found in output" unless stdout.include? "#{name}2" - end -end diff --git a/acceptance/tests/apply/virtual/should_realize_query.rb b/acceptance/tests/apply/virtual/should_realize_query.rb deleted file mode 100755 index ae1ff3eb3..000000000 --- a/acceptance/tests/apply/virtual/should_realize_query.rb +++ /dev/null @@ -1,27 +0,0 @@ -test_name "should realize query" - -agents.each do |agent| - out = agent.tmpfile('should_realize_query') - name = "test-#{Time.new.to_i}-host" - -manifest = %Q{ - @host { '#{name}': - ip => '127.0.0.2', - target => '#{out}', - host_aliases => 'alias', - ensure => present, - } - Host<| ip == '127.0.0.2' |> -} - - step "clean up target system for test" - on agent, "rm -f #{out}" - - step "run the manifest" - apply_manifest_on agent, manifest - - step "verify the file output" - on(agent, "cat #{out}") do - fail_test "host not found in output" unless stdout.include? name - end -end diff --git a/spec/integration/parser/collector_spec.rb b/spec/integration/parser/collector_spec.rb index 2e698da1b..f4fa4f0ee 100755 --- a/spec/integration/parser/collector_spec.rb +++ b/spec/integration/parser/collector_spec.rb @@ -1,144 +1,200 @@ #! /usr/bin/env ruby require 'spec_helper' require 'puppet_spec/compiler' require 'puppet/parser/collector' describe Puppet::Parser::Collector do include PuppetSpec::Compiler def expect_the_message_to_be(expected_messages, code, node = Puppet::Node.new('the node')) catalog = compile_to_catalog(code, node) messages = catalog.resources.find_all { |resource| resource.type == 'Notify' }. collect { |notify| notify[:message] } messages.should include(*expected_messages) end shared_examples_for "virtual resource collection" do + it "matches everything when no query given" do + expect_the_message_to_be(["the other message", "the message"], <<-MANIFEST) + @notify { "testing": message => "the message" } + @notify { "other": message => "the other message" } + + Notify <| |> + MANIFEST + end + + it "matches on tags" do + expect_the_message_to_be(["wanted"], <<-MANIFEST) + @notify { "testing": tag => ["one"], message => "wanted" } + @notify { "other": tag => ["two"], message => "unwanted" } + + Notify <| tag == one |> + MANIFEST + end + it "matches on title" do expect_the_message_to_be(["the message"], <<-MANIFEST) @notify { "testing": message => "the message" } Notify <| title == "testing" |> MANIFEST end it "matches on other parameters" do expect_the_message_to_be(["the message"], <<-MANIFEST) @notify { "testing": message => "the message" } @notify { "other testing": message => "the wrong message" } Notify <| message == "the message" |> MANIFEST end it "matches against elements of an array valued parameter" do expect_the_message_to_be([["the", "message"]], <<-MANIFEST) @notify { "testing": message => ["the", "message"] } @notify { "other testing": message => ["not", "here"] } Notify <| message == "message" |> MANIFEST end it "allows criteria to be combined with 'and'" do expect_the_message_to_be(["the message"], <<-MANIFEST) @notify { "testing": message => "the message" } @notify { "other": message => "the message" } Notify <| title == "testing" and message == "the message" |> MANIFEST end it "allows criteria to be combined with 'or'" do expect_the_message_to_be(["the message", "other message"], <<-MANIFEST) @notify { "testing": message => "the message" } @notify { "other": message => "other message" } @notify { "yet another": message => "different message" } Notify <| title == "testing" or message == "other message" |> MANIFEST end it "allows criteria to be combined with 'or'" do expect_the_message_to_be(["the message", "other message"], <<-MANIFEST) @notify { "testing": message => "the message" } @notify { "other": message => "other message" } @notify { "yet another": message => "different message" } Notify <| title == "testing" or message == "other message" |> MANIFEST end it "allows criteria to be grouped with parens" do expect_the_message_to_be(["the message", "different message"], <<-MANIFEST) @notify { "testing": message => "different message", withpath => true } @notify { "other": message => "the message" } @notify { "yet another": message => "the message", withpath => true } Notify <| (title == "testing" or message == "the message") and withpath == true |> MANIFEST end it "does not do anything if nothing matches" do expect_the_message_to_be([], <<-MANIFEST) @notify { "testing": message => "different message" } Notify <| title == "does not exist" |> MANIFEST end it "excludes items with inequalities" do expect_the_message_to_be(["good message"], <<-MANIFEST) @notify { "testing": message => "good message" } @notify { "the wrong one": message => "bad message" } Notify <| title != "the wrong one" |> MANIFEST end - context "issue #10963" do - it "collects with override when inside a class" do + it "does not exclude resources with unequal arrays" do + expect_the_message_to_be(["message", ["not this message", "or this one"]], <<-MANIFEST) + @notify { "testing": message => "message" } + @notify { "the wrong one": message => ["not this message", "or this one"] } + + Notify <| message != "not this message" |> + MANIFEST + end + + it "does not exclude tags with inequalities" do + expect_the_message_to_be(["wanted message", "the way it works"], <<-MANIFEST) + @notify { "testing": tag => ["wanted"], message => "wanted message" } + @notify { "other": tag => ["why"], message => "the way it works" } + + Notify <| tag != "why" |> + MANIFEST + end + + context "overrides" do + it "modifies an existing array" do + expect_the_message_to_be([["original message", "extra message"]], <<-MANIFEST) + @notify { "testing": message => ["original message"] } + + Notify <| |> { + message +> "extra message" + } + MANIFEST + end + + it "converts a scalar to an array" do + expect_the_message_to_be([["original message", "extra message"]], <<-MANIFEST) + @notify { "testing": message => "original message" } + + Notify <| |> { + message +> "extra message" + } + MANIFEST + end + + it "collects with override when inside a class (#10963)" do expect_the_message_to_be(["overridden message"], <<-MANIFEST) @notify { "testing": message => "original message" } include collector_test class collector_test { Notify <| |> { message => "overridden message" } } MANIFEST end - it "collects with override when inside a define" do + it "collects with override when inside a define (#10963)" do expect_the_message_to_be(["overridden message"], <<-MANIFEST) @notify { "testing": message => "original message" } collector_test { testing: } define collector_test() { Notify <| |> { message => "overridden message" } } MANIFEST end end end describe "in the current parser" do before :each do Puppet[:parser] = 'current' end it_behaves_like "virtual resource collection" end describe "in the future parser" do before :each do Puppet[:parser] = 'future' end it_behaves_like "virtual resource collection" 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