diff options
27 files changed, 2636 insertions, 2683 deletions
@@ -1,3 +1,11 @@ + Fixed #1018 -- resources now have their namevars added as + aliases in the resource catalog, just like they were added + in the resource classes. + + Fixed #1037 -- remote unreadable files no longer have the + permission denied exceptions caught, thus forbidding them + from being replaced with 'nil'. + Fixed #1043 -- autoloading now searches the plugins directory in each module, in addition to the lib directory. The 'lib' directory is also deprecated, but supported for now to give diff --git a/autotest/puppet_rspec.rb b/autotest/puppet_rspec.rb index bfc1a6a43..5ff794e9d 100644 --- a/autotest/puppet_rspec.rb +++ b/autotest/puppet_rspec.rb @@ -51,9 +51,9 @@ class Autotest::PuppetRspec < Autotest::Rspec # * our local vendor/gems/rspec/bin/spec def spec_commands [ + File.join('vendor', 'gems', 'rspec', 'bin', 'spec') , File.join('bin', 'spec'), - File.join(Config::CONFIG['bindir'], 'spec'), - File.join('vendor', 'gems', 'rspec', 'bin', 'spec') + File.join(Config::CONFIG['bindir'], 'spec') ] end diff --git a/lib/puppet/node/catalog.rb b/lib/puppet/node/catalog.rb index b74947107..ee4cedd4b 100644 --- a/lib/puppet/node/catalog.rb +++ b/lib/puppet/node/catalog.rb @@ -68,7 +68,11 @@ class Puppet::Node::Catalog < Puppet::PGraph @resource_table[ref] = resource + # If the name and title differ, set up an alias + self.alias(resource, resource.name) if resource.respond_to?(:name) and resource.respond_to?(:title) and resource.name != resource.title + resource.catalog = self if resource.respond_to?(:catalog=) and ! is_relationship_graph + add_vertex(resource) end end @@ -78,7 +82,10 @@ class Puppet::Node::Catalog < Puppet::PGraph resource.ref =~ /^(.+)\[/ newref = "%s[%s]" % [$1 || resource.class.name, name] - raise(ArgumentError, "Cannot alias %s to %s; resource %s already exists" % [resource.ref, name, newref]) if @resource_table[newref] + if existing = @resource_table[newref] + return if existing == resource + raise(ArgumentError, "Cannot alias %s to %s; resource %s already exists" % [resource.ref, name, newref]) + end @resource_table[newref] = resource @aliases[resource.ref] << newref end diff --git a/lib/puppet/type/file.rb b/lib/puppet/type/file.rb index 65ee7e72e..0ec54d907 100644 --- a/lib/puppet/type/file.rb +++ b/lib/puppet/type/file.rb @@ -642,7 +642,6 @@ module Puppet # :file. return nil unless child = catalog.create_implicit_resource(self.class.name, args) rescue => detail - puts detail.backtrace self.notice "Cannot manage: %s" % [detail] return nil end @@ -769,11 +768,8 @@ module Puppet begin File.unlink(newfile) rescue => detail - if Puppet[:trace] - puts detail.backtrace - end - self.err "Could not remove old backup: %s" % - detail + puts detail.backtrace if Puppet[:trace] + self.err "Could not remove old backup: %s" % detail return false end end @@ -979,7 +975,7 @@ module Puppet end def uri2obj(source) - sourceobj = FileSource.new + sourceobj = Puppet::Type::File::FileSource.new path = nil unless source devfail "Got a nil source" @@ -1152,7 +1148,7 @@ module Puppet # the filesource class can't include the path, because the path # changes for every file instance - class FileSource + class ::Puppet::Type::File::FileSource attr_accessor :mount, :root, :server, :local end diff --git a/lib/puppet/type/file/source.rb b/lib/puppet/type/file/source.rb index 3dfb5cccd..a3e533c31 100755 --- a/lib/puppet/type/file/source.rb +++ b/lib/puppet/type/file/source.rb @@ -101,8 +101,7 @@ module Puppet begin desc = server.describe(path, @resource[:links]) rescue Puppet::Network::XMLRPCClientError => detail - self.err "Could not describe %s: %s" % - [path, detail] + self.err "Could not describe %s: %s" % [path, detail] return nil end @@ -163,7 +162,7 @@ module Puppet # Diff the contents if they ask it. This is quite annoying -- we need to do this in # 'insync?' because they might be in noop mode, but we don't want to do the file - # retrieval twice, so we cache the value annoyingly. + # retrieval twice, so we cache the value. if ! result and Puppet[:show_diff] and File.exists?(@resource[:path]) and ! @stats[:_diffed] @stats[:_remote_content] = get_remote_content string_file_diff(@resource[:path], @stats[:_remote_content]) @@ -203,13 +202,10 @@ module Puppet case @stats[:type] when "directory", "file": - unless @resource.deleting? - @resource[:ensure] = @stats[:type] - end + @resource[:ensure] = @stats[:type] unless @resource.deleting? else self.info @stats.inspect - self.err "Cannot use files of type %s as sources" % - @stats[:type] + self.err "Cannot use files of type %s as sources" % @stats[:type] return :nocopy end @@ -221,11 +217,9 @@ module Puppet # was the stat already specified, or should the value # be inherited from the source? - unless @resource.argument?(stat) - @resource[stat] = value - end + @resource[stat] = value unless @resource.argument?(stat) } - + return @stats[:checksum] end @@ -261,34 +255,22 @@ module Puppet end private + def get_remote_content - unless @stats[:type] == "file" - #if @stats[:type] == "directory" - #[@resource.name, @should.inspect] - #end - raise Puppet::DevError, "Got told to copy non-file %s" % - @resource[:path] - end + raise Puppet::DevError, "Got told to copy non-file %s" % @resource[:path] unless @stats[:type] == "file" sourceobj, path = @resource.uri2obj(@source) begin contents = sourceobj.server.retrieve(path, @resource[:links]) - rescue Puppet::Network::XMLRPCClientError => detail - self.err "Could not retrieve %s: %s" % - [path, detail] - return nil + rescue => detail + self.fail "Could not retrieve %s: %s" % [path, detail] end - # FIXME It's stupid that this isn't taken care of in the - # protocol. - unless sourceobj.server.local - contents = CGI.unescape(contents) - end + contents = CGI.unescape(contents) unless sourceobj.server.local if contents == "" - self.notice "Could not retrieve contents for %s" % - @source + self.notice "Could not retrieve contents for %s" % @source end return contents diff --git a/spec/unit/file_serving/configuration.rb b/spec/unit/file_serving/configuration.rb index df46b9b6a..eecaefe5f 100755 --- a/spec/unit/file_serving/configuration.rb +++ b/spec/unit/file_serving/configuration.rb @@ -4,17 +4,6 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/file_serving/configuration' -module FSConfigurationTesting - def setup - @path = "/path/to/configuration/file.conf" - Puppet.settings.stubs(:value).with(:fileserverconfig).returns(@path) - end - - def teardown - Puppet::FileServing::Configuration.clear_cache - end -end - describe Puppet::FileServing::Configuration do it "should make :new a private method" do proc { Puppet::FileServing::Configuration.new }.should raise_error @@ -35,193 +24,201 @@ describe Puppet::FileServing::Configuration do end end -describe Puppet::FileServing::Configuration, " when initializing" do - include FSConfigurationTesting - - it "should work without a configuration file" do - FileTest.stubs(:exists?).with(@path).returns(false) - proc { Puppet::FileServing::Configuration.create }.should_not raise_error - end - - it "should parse the configuration file if present" do - FileTest.stubs(:exists?).with(@path).returns(true) - @parser = mock 'parser' - @parser.expects(:parse).returns({}) - Puppet::FileServing::Configuration::Parser.stubs(:new).returns(@parser) - Puppet::FileServing::Configuration.create - end - - it "should determine the path to the configuration file from the Puppet settings" do - Puppet::FileServing::Configuration.create - end -end - -describe Puppet::FileServing::Configuration, " when parsing the configuration file" do - include FSConfigurationTesting - - before do - FileTest.stubs(:exists?).with(@path).returns(true) - @parser = mock 'parser' - Puppet::FileServing::Configuration::Parser.stubs(:new).returns(@parser) - end - - it "should set the mount list to the results of parsing" do - @parser.expects(:parse).returns("one" => mock("mount")) - config = Puppet::FileServing::Configuration.create - config.mounted?("one").should be_true - end - - it "should not raise exceptions" do - @parser.expects(:parse).raises(ArgumentError) - proc { Puppet::FileServing::Configuration.create }.should_not raise_error - end - - it "should replace the existing mount list with the results of reparsing" do - @parser.expects(:parse).returns("one" => mock("mount")) - config = Puppet::FileServing::Configuration.create - config.mounted?("one").should be_true - # Now parse again - @parser.expects(:parse).returns("two" => mock('other')) - config.send(:readconfig, false) - config.mounted?("one").should be_false - config.mounted?("two").should be_true - end - - it "should not replace the mount list until the file is entirely parsed successfully" do - @parser.expects(:parse).returns("one" => mock("mount")) - @parser.expects(:parse).raises(ArgumentError) - config = Puppet::FileServing::Configuration.create - # Now parse again, so the exception gets thrown - config.send(:readconfig, false) - config.mounted?("one").should be_true - end -end - -describe Puppet::FileServing::Configuration, " when finding files" do - include FSConfigurationTesting - - before do - @parser = mock 'parser' - @parser.stubs(:changed?).returns true - FileTest.stubs(:exists?).with(@path).returns(true) - Puppet::FileServing::Configuration::Parser.stubs(:new).returns(@parser) - - @mount1 = stub 'mount', :name => "one" - @mounts = {"one" => @mount1} - - Facter.stubs(:value).with("hostname").returns("whatever") - - @config = Puppet::FileServing::Configuration.create - end - - it "should fail if the uri does not match a leading slash followed by a valid mount name" do - @parser.expects(:parse).returns(@mounts) - proc { @config.file_path("something") }.should raise_error(ArgumentError) - end - - it "should use the first term after the first slash for the mount name" do - @parser.expects(:parse).returns(@mounts) - FileTest.stubs(:exists?).returns(true) - @mount1.expects(:file) - @config.file_path("/one") - end - - it "should use the remainder of the URI after the mount name as the file name" do - @parser.expects(:parse).returns(@mounts) - @mount1.expects(:file).with("something/else", {}) - FileTest.stubs(:exists?).returns(true) - @config.file_path("/one/something/else") - end - - it "should treat a bare name as a mount and no relative file" do - @parser.expects(:parse).returns(@mounts) - @mount1.expects(:file).with(nil, {}) - FileTest.stubs(:exists?).returns(true) - @config.file_path("/one") - end - - it "should treat a name with a trailing slash equivalently to a name with no trailing slash" do - @parser.expects(:parse).returns(@mounts) - @mount1.expects(:file).with(nil, {}) - FileTest.stubs(:exists?).returns(true) - @config.file_path("/one/") - end - - it "should return nil if the mount cannot be found" do - @parser.expects(:changed?).returns(true) - @parser.expects(:parse).returns({}) - @config.file_path("/one/something").should be_nil - end - - it "should return nil if the mount does not contain the file" do - @parser.expects(:parse).returns(@mounts) - @mount1.expects(:file).with("something/else", {}).returns(nil) - @config.file_path("/one/something/else").should be_nil - end - - it "should return the fully qualified path if the mount exists" do - @parser.expects(:parse).returns(@mounts) - @mount1.expects(:file).with("something/else", {}).returns("/full/path") - @config.file_path("/one/something/else").should == "/full/path" - end - - it "should reparse the configuration file when it has changed" do - @mount1.stubs(:file).returns("whatever") - @parser.expects(:changed?).returns(true) - @parser.expects(:parse).returns(@mounts) - FileTest.stubs(:exists?).returns(true) - @config.file_path("/one/something") - - @parser.expects(:changed?).returns(true) - @parser.expects(:parse).returns({}) - @config.file_path("/one/something").should be_nil - end -end - -describe Puppet::FileServing::Configuration, " when checking authorization" do - include FSConfigurationTesting - - before do - @parser = mock 'parser' - @parser.stubs(:changed?).returns true - FileTest.stubs(:exists?).with(@path).returns(true) - Puppet::FileServing::Configuration::Parser.stubs(:new).returns(@parser) - - @mount1 = stub 'mount', :name => "one" - @mounts = {"one" => @mount1} - @parser.stubs(:parse).returns(@mounts) - - Facter.stubs(:value).with("hostname").returns("whatever") - - @config = Puppet::FileServing::Configuration.create - end - - it "should return false if the mount cannot be found" do - @config.authorized?("/nope/my/file").should be_false - end - - it "should use the mount to determine authorization" do - @mount1.expects(:allowed?) - @config.authorized?("/one/my/file") - end - - it "should pass the client's name to the mount if provided" do - @mount1.expects(:allowed?).with("myhost", nil) - @config.authorized?("/one/my/file", :node => "myhost") - end +describe Puppet::FileServing::Configuration do - it "should pass the client's IP to the mount if provided" do - @mount1.expects(:allowed?).with("myhost", "myip") - @config.authorized?("/one/my/file", :node => "myhost", :ipaddress => "myip") + before :each do + @path = "/path/to/configuration/file.conf" + Puppet.settings.stubs(:value).with(:fileserverconfig).returns(@path) end - it "should return true if the mount allows the client" do - @mount1.expects(:allowed?).returns(true) - @config.authorized?("/one/my/file").should be_true + after :each do + Puppet::FileServing::Configuration.clear_cache end - it "should return false if the mount denies the client" do - @mount1.expects(:allowed?).returns(false) - @config.authorized?("/one/my/file").should be_false - end -end + describe Puppet::FileServing::Configuration, " when initializing" do + + it "should work without a configuration file" do + FileTest.stubs(:exists?).with(@path).returns(false) + proc { Puppet::FileServing::Configuration.create }.should_not raise_error + end + + it "should parse the configuration file if present" do + FileTest.stubs(:exists?).with(@path).returns(true) + @parser = mock 'parser' + @parser.expects(:parse).returns({}) + Puppet::FileServing::Configuration::Parser.stubs(:new).returns(@parser) + Puppet::FileServing::Configuration.create + end + + it "should determine the path to the configuration file from the Puppet settings" do + Puppet::FileServing::Configuration.create + end + end + + describe Puppet::FileServing::Configuration, " when parsing the configuration file" do + + before do + FileTest.stubs(:exists?).with(@path).returns(true) + @parser = mock 'parser' + Puppet::FileServing::Configuration::Parser.stubs(:new).returns(@parser) + end + + it "should set the mount list to the results of parsing" do + @parser.expects(:parse).returns("one" => mock("mount")) + config = Puppet::FileServing::Configuration.create + config.mounted?("one").should be_true + end + + it "should not raise exceptions" do + @parser.expects(:parse).raises(ArgumentError) + proc { Puppet::FileServing::Configuration.create }.should_not raise_error + end + + it "should replace the existing mount list with the results of reparsing" do + @parser.expects(:parse).returns("one" => mock("mount")) + config = Puppet::FileServing::Configuration.create + config.mounted?("one").should be_true + # Now parse again + @parser.expects(:parse).returns("two" => mock('other')) + config.send(:readconfig, false) + config.mounted?("one").should be_false + config.mounted?("two").should be_true + end + + it "should not replace the mount list until the file is entirely parsed successfully" do + @parser.expects(:parse).returns("one" => mock("mount")) + @parser.expects(:parse).raises(ArgumentError) + config = Puppet::FileServing::Configuration.create + # Now parse again, so the exception gets thrown + config.send(:readconfig, false) + config.mounted?("one").should be_true + end + end + + describe Puppet::FileServing::Configuration, " when finding files" do + + before do + @parser = mock 'parser' + @parser.stubs(:changed?).returns true + FileTest.stubs(:exists?).with(@path).returns(true) + Puppet::FileServing::Configuration::Parser.stubs(:new).returns(@parser) + + @mount1 = stub 'mount', :name => "one" + @mounts = {"one" => @mount1} + + Facter.stubs(:value).with("hostname").returns("whatever") + + @config = Puppet::FileServing::Configuration.create + end + + it "should fail if the uri does not match a leading slash followed by a valid mount name" do + @parser.expects(:parse).returns(@mounts) + proc { @config.file_path("something") }.should raise_error(ArgumentError) + end + + it "should use the first term after the first slash for the mount name" do + @parser.expects(:parse).returns(@mounts) + FileTest.stubs(:exists?).returns(true) + @mount1.expects(:file) + @config.file_path("/one") + end + + it "should use the remainder of the URI after the mount name as the file name" do + @parser.expects(:parse).returns(@mounts) + @mount1.expects(:file).with("something/else", {}) + FileTest.stubs(:exists?).returns(true) + @config.file_path("/one/something/else") + end + + it "should treat a bare name as a mount and no relative file" do + @parser.expects(:parse).returns(@mounts) + @mount1.expects(:file).with(nil, {}) + FileTest.stubs(:exists?).returns(true) + @config.file_path("/one") + end + + it "should treat a name with a trailing slash equivalently to a name with no trailing slash" do + @parser.expects(:parse).returns(@mounts) + @mount1.expects(:file).with(nil, {}) + FileTest.stubs(:exists?).returns(true) + @config.file_path("/one/") + end + + it "should return nil if the mount cannot be found" do + @parser.expects(:changed?).returns(true) + @parser.expects(:parse).returns({}) + @config.file_path("/one/something").should be_nil + end + + it "should return nil if the mount does not contain the file" do + @parser.expects(:parse).returns(@mounts) + @mount1.expects(:file).with("something/else", {}).returns(nil) + @config.file_path("/one/something/else").should be_nil + end + + it "should return the fully qualified path if the mount exists" do + @parser.expects(:parse).returns(@mounts) + @mount1.expects(:file).with("something/else", {}).returns("/full/path") + @config.file_path("/one/something/else").should == "/full/path" + end + + it "should reparse the configuration file when it has changed" do + @mount1.stubs(:file).returns("whatever") + @parser.expects(:changed?).returns(true) + @parser.expects(:parse).returns(@mounts) + FileTest.stubs(:exists?).returns(true) + @config.file_path("/one/something") + + @parser.expects(:changed?).returns(true) + @parser.expects(:parse).returns({}) + @config.file_path("/one/something").should be_nil + end + end + + describe Puppet::FileServing::Configuration, " when checking authorization" do + + before do + @parser = mock 'parser' + @parser.stubs(:changed?).returns true + FileTest.stubs(:exists?).with(@path).returns(true) + Puppet::FileServing::Configuration::Parser.stubs(:new).returns(@parser) + + @mount1 = stub 'mount', :name => "one" + @mounts = {"one" => @mount1} + @parser.stubs(:parse).returns(@mounts) + + Facter.stubs(:value).with("hostname").returns("whatever") + + @config = Puppet::FileServing::Configuration.create + end + + it "should return false if the mount cannot be found" do + @config.authorized?("/nope/my/file").should be_false + end + + it "should use the mount to determine authorization" do + @mount1.expects(:allowed?) + @config.authorized?("/one/my/file") + end + + it "should pass the client's name to the mount if provided" do + @mount1.expects(:allowed?).with("myhost", nil) + @config.authorized?("/one/my/file", :node => "myhost") + end + + it "should pass the client's IP to the mount if provided" do + @mount1.expects(:allowed?).with("myhost", "myip") + @config.authorized?("/one/my/file", :node => "myhost", :ipaddress => "myip") + end + + it "should return true if the mount allows the client" do + @mount1.expects(:allowed?).returns(true) + @config.authorized?("/one/my/file").should be_true + end + + it "should return false if the mount denies the client" do + @mount1.expects(:allowed?).returns(false) + @config.authorized?("/one/my/file").should be_false + end + end +end
\ No newline at end of file diff --git a/spec/unit/file_serving/configuration/parser.rb b/spec/unit/file_serving/configuration/parser.rb index aa296cf43..df2f629d5 100755 --- a/spec/unit/file_serving/configuration/parser.rb +++ b/spec/unit/file_serving/configuration/parser.rb @@ -10,8 +10,17 @@ describe Puppet::FileServing::Configuration::Parser do end end + module FSConfigurationParserTesting - def setup + def mock_file_content(content) + # We want an array, but we actually want our carriage returns on all of it. + lines = content.split("\n").collect { |l| l + "\n" } + @filehandle.stubs(:each).multiple_yields(*lines) + end +end + +describe Puppet::FileServing::Configuration::Parser do + before :each do @path = "/my/config.conf" FileTest.stubs(:exists?).with(@path).returns(true) FileTest.stubs(:readable?).with(@path).returns(true) @@ -20,113 +29,107 @@ module FSConfigurationParserTesting @parser = Puppet::FileServing::Configuration::Parser.new(@path) end - def mock_file_content(content) - # We want an array, but we actually want our carriage returns on all of it. - lines = content.split("\n").collect { |l| l + "\n" } - @filehandle.stubs(:each).multiple_yields(*lines) - end -end - -describe Puppet::FileServing::Configuration::Parser, " when parsing" do - include FSConfigurationParserTesting + describe Puppet::FileServing::Configuration::Parser, " when parsing" do + include FSConfigurationParserTesting - before do - @parser.stubs(:add_modules_mount) - end + before do + @parser.stubs(:add_modules_mount) + end - it "should allow comments" do - @filehandle.expects(:each).yields("# this is a comment\n") - proc { @parser.parse }.should_not raise_error - end + it "should allow comments" do + @filehandle.expects(:each).yields("# this is a comment\n") + proc { @parser.parse }.should_not raise_error + end - it "should allow blank lines" do - @filehandle.expects(:each).yields("\n") - proc { @parser.parse }.should_not raise_error - end + it "should allow blank lines" do + @filehandle.expects(:each).yields("\n") + proc { @parser.parse }.should_not raise_error + end - it "should create a new mount for each section in the configuration" do - mount1 = mock 'one' - mount2 = mock 'two' - Puppet::FileServing::Mount.expects(:new).with("one").returns(mount1) - Puppet::FileServing::Mount.expects(:new).with("two").returns(mount2) - mock_file_content "[one]\n[two]\n" - @parser.parse - end + it "should create a new mount for each section in the configuration" do + mount1 = mock 'one' + mount2 = mock 'two' + Puppet::FileServing::Mount.expects(:new).with("one").returns(mount1) + Puppet::FileServing::Mount.expects(:new).with("two").returns(mount2) + mock_file_content "[one]\n[two]\n" + @parser.parse + end - # This test is almost the exact same as the previous one. - it "should return a hash of the created mounts" do - mount1 = mock 'one' - mount2 = mock 'two' - Puppet::FileServing::Mount.expects(:new).with("one").returns(mount1) - Puppet::FileServing::Mount.expects(:new).with("two").returns(mount2) - mock_file_content "[one]\n[two]\n" + # This test is almost the exact same as the previous one. + it "should return a hash of the created mounts" do + mount1 = mock 'one' + mount2 = mock 'two' + Puppet::FileServing::Mount.expects(:new).with("one").returns(mount1) + Puppet::FileServing::Mount.expects(:new).with("two").returns(mount2) + mock_file_content "[one]\n[two]\n" - @parser.parse.should == {"one" => mount1, "two" => mount2} - end + @parser.parse.should == {"one" => mount1, "two" => mount2} + end - it "should only allow mount names that are alphanumeric plus dashes" do - mock_file_content "[a*b]\n" - proc { @parser.parse }.should raise_error(ArgumentError) - end + it "should only allow mount names that are alphanumeric plus dashes" do + mock_file_content "[a*b]\n" + proc { @parser.parse }.should raise_error(ArgumentError) + end - it "should fail if the value for path/allow/deny starts with an equals sign" do - mock_file_content "[one]\npath = /testing" - proc { @parser.parse }.should raise_error(ArgumentError) + it "should fail if the value for path/allow/deny starts with an equals sign" do + mock_file_content "[one]\npath = /testing" + proc { @parser.parse }.should raise_error(ArgumentError) + end end -end -describe Puppet::FileServing::Configuration::Parser, " when parsing mount attributes" do - include FSConfigurationParserTesting + describe Puppet::FileServing::Configuration::Parser, " when parsing mount attributes" do + include FSConfigurationParserTesting - before do - @mount = stub 'mount', :name => "one" - Puppet::FileServing::Mount.expects(:new).with("one").returns(@mount) - @parser.stubs(:add_modules_mount) - end + before do + @mount = stub 'mount', :name => "one" + Puppet::FileServing::Mount.expects(:new).with("one").returns(@mount) + @parser.stubs(:add_modules_mount) + end - it "should set the mount path to the path attribute from that section" do - mock_file_content "[one]\npath /some/path\n" + it "should set the mount path to the path attribute from that section" do + mock_file_content "[one]\npath /some/path\n" - @mount.expects(:path=).with("/some/path") - @parser.parse - end + @mount.expects(:path=).with("/some/path") + @parser.parse + end - it "should tell the mount to allow any allow values from the section" do - mock_file_content "[one]\nallow something\n" + it "should tell the mount to allow any allow values from the section" do + mock_file_content "[one]\nallow something\n" - @mount.expects(:info) - @mount.expects(:allow).with("something") - @parser.parse - end + @mount.expects(:info) + @mount.expects(:allow).with("something") + @parser.parse + end - it "should tell the mount to deny any deny values from the section" do - mock_file_content "[one]\ndeny something\n" + it "should tell the mount to deny any deny values from the section" do + mock_file_content "[one]\ndeny something\n" - @mount.expects(:info) - @mount.expects(:deny).with("something") - @parser.parse - end + @mount.expects(:info) + @mount.expects(:deny).with("something") + @parser.parse + end - it "should fail on any attributes other than path, allow, and deny" do - mock_file_content "[one]\ndo something\n" + it "should fail on any attributes other than path, allow, and deny" do + mock_file_content "[one]\ndo something\n" - proc { @parser.parse }.should raise_error(ArgumentError) + proc { @parser.parse }.should raise_error(ArgumentError) + end end -end -describe Puppet::FileServing::Configuration::Parser, " when parsing the modules mount" do - include FSConfigurationParserTesting + describe Puppet::FileServing::Configuration::Parser, " when parsing the modules mount" do + include FSConfigurationParserTesting - before do - @mount = stub 'mount', :name => "modules" - Puppet::FileServing::Mount.expects(:new).with("modules").returns(@mount) - end + before do + @mount = stub 'mount', :name => "modules" + Puppet::FileServing::Mount.expects(:new).with("modules").returns(@mount) + end - it "should warn if a path is set" do - mock_file_content "[modules]\npath /some/path\n" + it "should warn if a path is set" do + mock_file_content "[modules]\npath /some/path\n" - @modules.expects(:path=).never - Puppet.expects(:warning) - @parser.parse + @modules.expects(:path=).never + Puppet.expects(:warning) + @parser.parse + end end -end +end
\ No newline at end of file diff --git a/spec/unit/indirector/checksum/file.rb b/spec/unit/indirector/checksum/file.rb index 82319fa40..4f8ee98b2 100755 --- a/spec/unit/indirector/checksum/file.rb +++ b/spec/unit/indirector/checksum/file.rb @@ -7,22 +7,6 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/checksum/file' -module FileChecksumTesting - def setup - Puppet.settings.stubs(:use) - @store = Puppet::Checksum::File.new - - @value = "70924d6fa4b2d745185fa4660703a5c0" - @sum = stub 'sum', :name => @value - - @dir = "/what/ever" - - Puppet.stubs(:[]).with(:bucketdir).returns(@dir) - - @path = @store.path(@value) - end -end - describe Puppet::Checksum::File do it "should be a subclass of the File terminus class" do Puppet::Checksum::File.superclass.should equal(Puppet::Indirector::File) @@ -40,103 +24,117 @@ describe Puppet::Checksum::File, " when initializing" do end end -describe Puppet::Checksum::File, " when determining file paths" do - include FileChecksumTesting - # I was previously passing the object in. - it "should use the value passed in to path() as the checksum" do - @value.expects(:name).never - @store.path(@value) - end +describe Puppet::Checksum::File do + before :each do + Puppet.settings.stubs(:use) + @store = Puppet::Checksum::File.new - it "should use the value of the :bucketdir setting as the root directory" do - @path.should =~ %r{^#{@dir}} - end + @value = "70924d6fa4b2d745185fa4660703a5c0" + @sum = stub 'sum', :name => @value - it "should choose a path 8 directories deep with each directory name being the respective character in the checksum" do - dirs = @value[0..7].split("").join(File::SEPARATOR) - @path.should be_include(dirs) - end + @dir = "/what/ever" - it "should use the full checksum as the final directory name" do - File.basename(File.dirname(@path)).should == @value - end + Puppet.stubs(:[]).with(:bucketdir).returns(@dir) - it "should use 'contents' as the actual file name" do - File.basename(@path).should == "contents" + @path = @store.path(@value) end - it "should use the bucketdir, the 8 sum character directories, the full checksum, and 'contents' as the full file name" do - @path.should == [@dir, @value[0..7].split(""), @value, "contents"].flatten.join(File::SEPARATOR) - end -end -describe Puppet::Checksum::File, " when retrieving files" do - include FileChecksumTesting + describe Puppet::Checksum::File, " when determining file paths" do - # The smallest test that will use the calculated path - it "should look for the calculated path" do - File.expects(:exist?).with(@path).returns(false) - @store.find(@value) - end + # I was previously passing the object in. + it "should use the value passed in to path() as the checksum" do + @value.expects(:name).never + @store.path(@value) + end - it "should return an instance of Puppet::Checksum created with the content if the file exists" do - content = "my content" - sum = stub 'file' - Puppet::Checksum.expects(:new).with(content).returns(sum) + it "should use the value of the :bucketdir setting as the root directory" do + @path.should =~ %r{^#{@dir}} + end - File.expects(:exist?).with(@path).returns(true) - File.expects(:read).with(@path).returns(content) + it "should choose a path 8 directories deep with each directory name being the respective character in the checksum" do + dirs = @value[0..7].split("").join(File::SEPARATOR) + @path.should be_include(dirs) + end - @store.find(@value).should equal(sum) - end + it "should use the full checksum as the final directory name" do + File.basename(File.dirname(@path)).should == @value + end - it "should return nil if no file is found" do - File.expects(:exist?).with(@path).returns(false) - @store.find(@value).should be_nil - end + it "should use 'contents' as the actual file name" do + File.basename(@path).should == "contents" + end - it "should fail intelligently if a found file cannot be read" do - File.expects(:exist?).with(@path).returns(true) - File.expects(:read).with(@path).raises(RuntimeError) - proc { @store.find(@value) }.should raise_error(Puppet::Error) + it "should use the bucketdir, the 8 sum character directories, the full checksum, and 'contents' as the full file name" do + @path.should == [@dir, @value[0..7].split(""), @value, "contents"].flatten.join(File::SEPARATOR) + end end -end -describe Puppet::Checksum::File, " when saving files" do - include FileChecksumTesting + describe Puppet::Checksum::File, " when retrieving files" do + + # The smallest test that will use the calculated path + it "should look for the calculated path" do + File.expects(:exist?).with(@path).returns(false) + @store.find(@value) + end + + it "should return an instance of Puppet::Checksum created with the content if the file exists" do + content = "my content" + sum = stub 'file' + Puppet::Checksum.expects(:new).with(content).returns(sum) + + File.expects(:exist?).with(@path).returns(true) + File.expects(:read).with(@path).returns(content) - # LAK:FIXME I don't know how to include in the spec the fact that we're - # using the superclass's save() method and thus are acquiring all of - # it's behaviours. - it "should save the content to the calculated path" do - File.stubs(:directory?).with(File.dirname(@path)).returns(true) - File.expects(:open).with(@path, "w") + @store.find(@value).should equal(sum) + end - file = stub 'file', :name => @value - @store.save(file) + it "should return nil if no file is found" do + File.expects(:exist?).with(@path).returns(false) + @store.find(@value).should be_nil + end + + it "should fail intelligently if a found file cannot be read" do + File.expects(:exist?).with(@path).returns(true) + File.expects(:read).with(@path).raises(RuntimeError) + proc { @store.find(@value) }.should raise_error(Puppet::Error) + end end - it "should make any directories necessary for storage" do - FileUtils.expects(:mkdir_p).with do |arg| - File.umask == 0007 and arg == File.dirname(@path) + describe Puppet::Checksum::File, " when saving files" do + + # LAK:FIXME I don't know how to include in the spec the fact that we're + # using the superclass's save() method and thus are acquiring all of + # it's behaviours. + it "should save the content to the calculated path" do + File.stubs(:directory?).with(File.dirname(@path)).returns(true) + File.expects(:open).with(@path, "w") + + file = stub 'file', :name => @value + @store.save(file) end - File.expects(:directory?).with(File.dirname(@path)).returns(true) - File.expects(:open).with(@path, "w") - file = stub 'file', :name => @value - @store.save(file) + it "should make any directories necessary for storage" do + FileUtils.expects(:mkdir_p).with do |arg| + File.umask == 0007 and arg == File.dirname(@path) + end + File.expects(:directory?).with(File.dirname(@path)).returns(true) + File.expects(:open).with(@path, "w") + + file = stub 'file', :name => @value + @store.save(file) + end end -end -describe Puppet::Checksum::File, " when deleting files" do - include FileChecksumTesting + describe Puppet::Checksum::File, " when deleting files" do - it "should remove the file at the calculated path" do - File.expects(:exist?).with(@path).returns(true) - File.expects(:unlink).with(@path) + it "should remove the file at the calculated path" do + File.expects(:exist?).with(@path).returns(true) + File.expects(:unlink).with(@path) - file = stub 'file', :name => @value - @store.destroy(file) + file = stub 'file', :name => @value + @store.destroy(file) + end end -end +end
\ No newline at end of file diff --git a/spec/unit/indirector/direct_file_server.rb b/spec/unit/indirector/direct_file_server.rb index 2a8ec1a49..9f3652536 100755 --- a/spec/unit/indirector/direct_file_server.rb +++ b/spec/unit/indirector/direct_file_server.rb @@ -7,8 +7,8 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector/direct_file_server' -module DirectFileServerTerminusTesting - def setup +describe Puppet::Indirector::DirectFileServer do + before :each do Puppet::Indirector::Terminus.stubs(:register_terminus_class) @model = mock 'model' @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model @@ -24,72 +24,69 @@ module DirectFileServerTerminusTesting @uri = "file:///my/local" end -end -describe Puppet::Indirector::DirectFileServer, "when finding a single file" do - include DirectFileServerTerminusTesting + describe Puppet::Indirector::DirectFileServer, "when finding a single file" do - it "should return nil if the file does not exist" do - FileTest.expects(:exists?).with("/my/local").returns false - @server.find(@uri).should be_nil - end + it "should return nil if the file does not exist" do + FileTest.expects(:exists?).with("/my/local").returns false + @server.find(@uri).should be_nil + end - it "should return a Content instance created with the full path to the file if the file exists" do - FileTest.expects(:exists?).with("/my/local").returns true - @model.expects(:new).returns(:mycontent) - @server.find(@uri).should == :mycontent + it "should return a Content instance created with the full path to the file if the file exists" do + FileTest.expects(:exists?).with("/my/local").returns true + @model.expects(:new).returns(:mycontent) + @server.find(@uri).should == :mycontent + end end -end -describe Puppet::Indirector::DirectFileServer, "when creating the instance for a single found file" do - include DirectFileServerTerminusTesting + describe Puppet::Indirector::DirectFileServer, "when creating the instance for a single found file" do - before do - @data = mock 'content' - @data.stubs(:collect_attributes) - FileTest.expects(:exists?).with("/my/local").returns true - end + before do + @data = mock 'content' + @data.stubs(:collect_attributes) + FileTest.expects(:exists?).with("/my/local").returns true + end - it "should create the Content instance with the original key as the key" do - @model.expects(:new).with { |key, options| key == @uri }.returns(@data) - @server.find(@uri) - end + it "should create the Content instance with the original key as the key" do + @model.expects(:new).with { |key, options| key == @uri }.returns(@data) + @server.find(@uri) + end - it "should pass the full path to the instance" do - @model.expects(:new).with { |key, options| options[:path] == "/my/local" }.returns(@data) - @server.find(@uri) - end + it "should pass the full path to the instance" do + @model.expects(:new).with { |key, options| options[:path] == "/my/local" }.returns(@data) + @server.find(@uri) + end - it "should pass the :links setting on to the created Content instance if the file exists and there is a value for :links" do - @model.expects(:new).returns(@data) - @data.expects(:links=).with(:manage) - @server.find(@uri, :links => :manage) + it "should pass the :links setting on to the created Content instance if the file exists and there is a value for :links" do + @model.expects(:new).returns(@data) + @data.expects(:links=).with(:manage) + @server.find(@uri, :links => :manage) + end end -end -describe Puppet::Indirector::DirectFileServer, "when searching for multiple files" do - include DirectFileServerTerminusTesting + describe Puppet::Indirector::DirectFileServer, "when searching for multiple files" do - it "should return nil if the file does not exist" do - FileTest.expects(:exists?).with("/my/local").returns false - @server.find(@uri).should be_nil - end + it "should return nil if the file does not exist" do + FileTest.expects(:exists?).with("/my/local").returns false + @server.find(@uri).should be_nil + end - it "should pass the original key to :path2instances" do - FileTest.expects(:exists?).with("/my/local").returns true - @server.expects(:path2instances).with { |uri, path, options| uri == @uri } - @server.search(@uri) - end + it "should pass the original key to :path2instances" do + FileTest.expects(:exists?).with("/my/local").returns true + @server.expects(:path2instances).with { |uri, path, options| uri == @uri } + @server.search(@uri) + end - it "should use :path2instances from the terminus_helper to return instances if the file exists" do - FileTest.expects(:exists?).with("/my/local").returns true - @server.expects(:path2instances) - @server.search(@uri) - end + it "should use :path2instances from the terminus_helper to return instances if the file exists" do + FileTest.expects(:exists?).with("/my/local").returns true + @server.expects(:path2instances) + @server.search(@uri) + end - it "should pass any options on to :path2instances" do - FileTest.expects(:exists?).with("/my/local").returns true - @server.expects(:path2instances).with { |uri, path, options| options == {:testing => :one, :other => :two}} - @server.search(@uri, :testing => :one, :other => :two) + it "should pass any options on to :path2instances" do + FileTest.expects(:exists?).with("/my/local").returns true + @server.expects(:path2instances).with { |uri, path, options| options == {:testing => :one, :other => :two}} + @server.search(@uri, :testing => :one, :other => :two) + end end -end +end
\ No newline at end of file diff --git a/spec/unit/indirector/facts/facter.rb b/spec/unit/indirector/facts/facter.rb index 1fad4c859..0974a60ec 100755 --- a/spec/unit/indirector/facts/facter.rb +++ b/spec/unit/indirector/facts/facter.rb @@ -31,49 +31,46 @@ describe Puppet::Node::Facts::Facter do end end -module TestingCodeFacts - def setup +describe Puppet::Node::Facts::Facter do + before :each do @facter = Puppet::Node::Facts::Facter.new Facter.stubs(:to_hash).returns({}) @name = "me" @facts = @facter.find(@name) end -end -describe Puppet::Node::Facts::Facter, " when finding facts" do - include TestingCodeFacts + describe Puppet::Node::Facts::Facter, " when finding facts" do - it "should return a Facts instance" do - @facts.should be_instance_of(Puppet::Node::Facts) - end + it "should return a Facts instance" do + @facts.should be_instance_of(Puppet::Node::Facts) + end - it "should return a Facts instance with the provided key as the name" do - @facts.name.should == @name - end + it "should return a Facts instance with the provided key as the name" do + @facts.name.should == @name + end - it "should return the Facter facts as the values in the Facts instance" do - Facter.expects(:to_hash).returns("one" => "two") - facts = @facter.find(@name) - facts.values["one"].should == "two" + it "should return the Facter facts as the values in the Facts instance" do + Facter.expects(:to_hash).returns("one" => "two") + facts = @facter.find(@name) + facts.values["one"].should == "two" + end end -end -describe Puppet::Node::Facts::Facter, " when saving facts" do - include TestingCodeFacts + describe Puppet::Node::Facts::Facter, " when saving facts" do - it "should fail" do - proc { @facter.save(@facts) }.should raise_error(Puppet::DevError) + it "should fail" do + proc { @facter.save(@facts) }.should raise_error(Puppet::DevError) + end end -end -describe Puppet::Node::Facts::Facter, " when destroying facts" do - include TestingCodeFacts + describe Puppet::Node::Facts::Facter, " when destroying facts" do - it "should fail" do - proc { @facter.destroy(@facts) }.should raise_error(Puppet::DevError) + it "should fail" do + proc { @facter.destroy(@facts) }.should raise_error(Puppet::DevError) + end end -end -describe Puppet::Node::Facts::Facter, " when loading facts from the factpath" do - it "should load every fact in each factpath directory" -end + describe Puppet::Node::Facts::Facter, " when loading facts from the factpath" do + it "should load every fact in each factpath directory" + end +end
\ No newline at end of file diff --git a/spec/unit/indirector/file.rb b/spec/unit/indirector/file.rb index 216c9bfe1..37740f0d0 100755 --- a/spec/unit/indirector/file.rb +++ b/spec/unit/indirector/file.rb @@ -3,8 +3,9 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector/file' -module FileTerminusTesting - def setup + +describe Puppet::Indirector::File do + before :each do Puppet::Indirector::Terminus.stubs(:register_terminus_class) @model = mock 'model' @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model @@ -21,140 +22,137 @@ module FileTerminusTesting @path = "/my/file" @dir = "/my" end -end + + describe Puppet::Indirector::File, " when finding files" do -describe Puppet::Indirector::File, " when finding files" do - include FileTerminusTesting + it "should provide a method to return file contents at a specified path" do + @searcher.should respond_to(:find) + end - it "should provide a method to return file contents at a specified path" do - @searcher.should respond_to(:find) - end + it "should return file contents as an instance of the model" do + content = "my content" - it "should return file contents as an instance of the model" do - content = "my content" + file = mock 'file' + @model.expects(:new).with(content).returns(file) - file = mock 'file' - @model.expects(:new).with(content).returns(file) + File.expects(:exist?).with(@path).returns(true) + File.expects(:read).with(@path).returns(content) + @searcher.find(@path) + end - File.expects(:exist?).with(@path).returns(true) - File.expects(:read).with(@path).returns(content) - @searcher.find(@path) - end + it "should create the model instance with the content as the only argument to initialization" do + content = "my content" - it "should create the model instance with the content as the only argument to initialization" do - content = "my content" + file = mock 'file' + @model.expects(:new).with(content).returns(file) - file = mock 'file' - @model.expects(:new).with(content).returns(file) + File.expects(:exist?).with(@path).returns(true) + File.expects(:read).with(@path).returns(content) + @searcher.find(@path).should equal(file) + end - File.expects(:exist?).with(@path).returns(true) - File.expects(:read).with(@path).returns(content) - @searcher.find(@path).should equal(file) - end + it "should return nil if no file is found" do + File.expects(:exist?).with(@path).returns(false) + @searcher.find(@path).should be_nil + end - it "should return nil if no file is found" do - File.expects(:exist?).with(@path).returns(false) - @searcher.find(@path).should be_nil - end + it "should fail intelligently if a found file cannot be read" do + File.expects(:exist?).with(@path).returns(true) + File.expects(:read).with(@path).raises(RuntimeError) + proc { @searcher.find(@path) }.should raise_error(Puppet::Error) + end - it "should fail intelligently if a found file cannot be read" do - File.expects(:exist?).with(@path).returns(true) - File.expects(:read).with(@path).raises(RuntimeError) - proc { @searcher.find(@path) }.should raise_error(Puppet::Error) - end + it "should use the path() method to calculate the path if it exists" do + @searcher.meta_def(:path) do |name| + name.upcase + end - it "should use the path() method to calculate the path if it exists" do - @searcher.meta_def(:path) do |name| - name.upcase + File.expects(:exist?).with(@path.upcase).returns(false) + @searcher.find(@path) end - - File.expects(:exist?).with(@path.upcase).returns(false) - @searcher.find(@path) end -end -describe Puppet::Indirector::File, " when saving files" do - include FileTerminusTesting + describe Puppet::Indirector::File, " when saving files" do - it "should provide a method to save file contents at a specified path" do - filehandle = mock 'file' - content = "my content" - File.expects(:directory?).with(@dir).returns(true) - File.expects(:open).with(@path, "w").yields(filehandle) - filehandle.expects(:print).with(content) + it "should provide a method to save file contents at a specified path" do + filehandle = mock 'file' + content = "my content" + File.expects(:directory?).with(@dir).returns(true) + File.expects(:open).with(@path, "w").yields(filehandle) + filehandle.expects(:print).with(content) - file = stub 'file', :content => content, :path => @path, :name => @path + file = stub 'file', :content => content, :path => @path, :name => @path - @searcher.save(file) - end + @searcher.save(file) + end - it "should fail intelligently if the file's parent directory does not exist" do - File.expects(:directory?).with(@dir).returns(false) + it "should fail intelligently if the file's parent directory does not exist" do + File.expects(:directory?).with(@dir).returns(false) - file = stub 'file', :path => @path, :name => @path + file = stub 'file', :path => @path, :name => @path - proc { @searcher.save(file) }.should raise_error(Puppet::Error) - end - - it "should fail intelligently if a file cannot be written" do - filehandle = mock 'file' - content = "my content" - File.expects(:directory?).with(@dir).returns(true) - File.expects(:open).with(@path, "w").yields(filehandle) - filehandle.expects(:print).with(content).raises(ArgumentError) + proc { @searcher.save(file) }.should raise_error(Puppet::Error) + end - file = stub 'file', :content => content, :path => @path, :name => @path + it "should fail intelligently if a file cannot be written" do + filehandle = mock 'file' + content = "my content" + File.expects(:directory?).with(@dir).returns(true) + File.expects(:open).with(@path, "w").yields(filehandle) + filehandle.expects(:print).with(content).raises(ArgumentError) - proc { @searcher.save(file) }.should raise_error(Puppet::Error) - end + file = stub 'file', :content => content, :path => @path, :name => @path - it "should use the path() method to calculate the path if it exists" do - @searcher.meta_def(:path) do |name| - name.upcase + proc { @searcher.save(file) }.should raise_error(Puppet::Error) end - file = stub 'file', :name => "/yay" + it "should use the path() method to calculate the path if it exists" do + @searcher.meta_def(:path) do |name| + name.upcase + end + + file = stub 'file', :name => "/yay" - File.expects(:open).with("/YAY", "w") - @searcher.save(file) + File.expects(:open).with("/YAY", "w") + @searcher.save(file) + end end -end -describe Puppet::Indirector::File, " when removing files" do - include FileTerminusTesting + describe Puppet::Indirector::File, " when removing files" do - it "should provide a method to remove files at a specified path" do - file = stub 'file', :path => @path, :name => @path - File.expects(:exist?).with(@path).returns(true) - File.expects(:unlink).with(@path) - - @searcher.destroy(file) - end + it "should provide a method to remove files at a specified path" do + file = stub 'file', :path => @path, :name => @path + File.expects(:exist?).with(@path).returns(true) + File.expects(:unlink).with(@path) - it "should throw an exception if the file is not found" do - file = stub 'file', :path => @path, :name => @path - File.expects(:exist?).with(@path).returns(false) + @searcher.destroy(file) + end - proc { @searcher.destroy(file) }.should raise_error(Puppet::Error) - end + it "should throw an exception if the file is not found" do + file = stub 'file', :path => @path, :name => @path + File.expects(:exist?).with(@path).returns(false) - it "should fail intelligently if the file cannot be removed" do - file = stub 'file', :path => @path, :name => @path - File.expects(:exist?).with(@path).returns(true) - File.expects(:unlink).with(@path).raises(ArgumentError) + proc { @searcher.destroy(file) }.should raise_error(Puppet::Error) + end - proc { @searcher.destroy(file) }.should raise_error(Puppet::Error) - end + it "should fail intelligently if the file cannot be removed" do + file = stub 'file', :path => @path, :name => @path + File.expects(:exist?).with(@path).returns(true) + File.expects(:unlink).with(@path).raises(ArgumentError) - it "should use the path() method to calculate the path if it exists" do - @searcher.meta_def(:path) do |name| - name.upcase + proc { @searcher.destroy(file) }.should raise_error(Puppet::Error) end - file = stub 'file', :name => "/yay" - File.expects(:exist?).with("/YAY").returns(true) - File.expects(:unlink).with("/YAY") + it "should use the path() method to calculate the path if it exists" do + @searcher.meta_def(:path) do |name| + name.upcase + end + + file = stub 'file', :name => "/yay" + File.expects(:exist?).with("/YAY").returns(true) + File.expects(:unlink).with("/YAY") - @searcher.destroy(file) + @searcher.destroy(file) + end end -end +end
\ No newline at end of file diff --git a/spec/unit/indirector/file_server.rb b/spec/unit/indirector/file_server.rb index fda60f1ec..974b95e0e 100755 --- a/spec/unit/indirector/file_server.rb +++ b/spec/unit/indirector/file_server.rb @@ -8,8 +8,9 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector/file_server' require 'puppet/file_serving/configuration' -module FileServerTerminusTesting - def setup +describe Puppet::Indirector::FileServer do + + before :each do Puppet::Indirector::Terminus.stubs(:register_terminus_class) @model = mock 'model' @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model @@ -27,147 +28,141 @@ module FileServerTerminusTesting @configuration = mock 'configuration' Puppet::FileServing::Configuration.stubs(:create).returns(@configuration) end -end -describe Puppet::Indirector::FileServer, " when finding files" do - include FileServerTerminusTesting + describe Puppet::Indirector::FileServer, " when finding files" do - it "should use the path portion of the URI as the file name" do - @configuration.expects(:file_path).with("/my/local/file", :node => nil) - @file_server.find(@uri) - end + it "should use the path portion of the URI as the file name" do + @configuration.expects(:file_path).with("/my/local/file", :node => nil) + @file_server.find(@uri) + end - it "should use the FileServing configuration to convert the file name to a fully qualified path" do - @configuration.expects(:file_path).with("/my/local/file", :node => nil) - @file_server.find(@uri) - end + it "should use the FileServing configuration to convert the file name to a fully qualified path" do + @configuration.expects(:file_path).with("/my/local/file", :node => nil) + @file_server.find(@uri) + end - it "should pass the node name to the FileServing configuration if one is provided" do - @configuration.expects(:file_path).with("/my/local/file", :node => "testing") - @file_server.find(@uri, :node => "testing") - end + it "should pass the node name to the FileServing configuration if one is provided" do + @configuration.expects(:file_path).with("/my/local/file", :node => "testing") + @file_server.find(@uri, :node => "testing") + end - it "should return nil if no fully qualified path is found" do - @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns(nil) - @file_server.find(@uri).should be_nil - end + it "should return nil if no fully qualified path is found" do + @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns(nil) + @file_server.find(@uri).should be_nil + end - it "should return an instance of the model created with the full path if a file is found" do - @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/some/file") - @model.expects(:new).returns(:myinstance) - @file_server.find(@uri).should == :myinstance + it "should return an instance of the model created with the full path if a file is found" do + @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/some/file") + @model.expects(:new).returns(:myinstance) + @file_server.find(@uri).should == :myinstance + end end -end + describe Puppet::Indirector::FileServer, " when returning instances" do + before :each do + @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/some/file") + @instance = mock 'instance' + end -describe Puppet::Indirector::FileServer, " when returning instances" do - include FileServerTerminusTesting - - before do - @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/some/file") - @instance = mock 'instance' - end - - it "should create the instance with the key used to find the instance" do - @model.expects(:new).with { |key, *options| key == @uri } - @file_server.find(@uri) - end + it "should create the instance with the key used to find the instance" do + @model.expects(:new).with { |key, *options| key == @uri } + @file_server.find(@uri) + end - it "should create the instance with the path at which the instance was found" do - @model.expects(:new).with { |key, options| options[:path] == "/some/file" } - @file_server.find(@uri) - end + it "should create the instance with the path at which the instance was found" do + @model.expects(:new).with { |key, options| options[:path] == "/some/file" } + @file_server.find(@uri) + end - it "should set the provided :links setting on to the instance if one is provided" do - @model.expects(:new).returns(@instance) - @instance.expects(:links=).with(:mytest) - @file_server.find(@uri, :links => :mytest) - end + it "should set the provided :links setting on to the instance if one is provided" do + @model.expects(:new).returns(@instance) + @instance.expects(:links=).with(:mytest) + @file_server.find(@uri, :links => :mytest) + end - it "should not set a :links value if no :links parameter is provided" do - @model.expects(:new).returns(@instance) - @file_server.find(@uri) + it "should not set a :links value if no :links parameter is provided" do + @model.expects(:new).returns(@instance) + @file_server.find(@uri) + end end -end -describe Puppet::Indirector::FileServer, " when checking authorization" do - include FileServerTerminusTesting + describe Puppet::Indirector::FileServer, " when checking authorization" do - it "should have an authorization hook" do - @file_server.should respond_to(:authorized?) - end + it "should have an authorization hook" do + @file_server.should respond_to(:authorized?) + end - it "should deny the :destroy method" do - @file_server.authorized?(:destroy, "whatever").should be_false - end + it "should deny the :destroy method" do + @file_server.authorized?(:destroy, "whatever").should be_false + end - it "should deny the :save method" do - @file_server.authorized?(:save, "whatever").should be_false - end + it "should deny the :save method" do + @file_server.authorized?(:save, "whatever").should be_false + end - it "should use the file server configuration to determine authorization" do - @configuration.expects(:authorized?) - @file_server.authorized?(:find, "puppetmounts://host/my/file") - end + it "should use the file server configuration to determine authorization" do + @configuration.expects(:authorized?) + @file_server.authorized?(:find, "puppetmounts://host/my/file") + end - it "should pass the file path from the URI to the file server configuration" do - @configuration.expects(:authorized?).with { |uri, *args| uri == "/my/file" } - @file_server.authorized?(:find, "puppetmounts://host/my/file") - end + it "should pass the file path from the URI to the file server configuration" do + @configuration.expects(:authorized?).with { |uri, *args| uri == "/my/file" } + @file_server.authorized?(:find, "puppetmounts://host/my/file") + end - it "should pass the node name to the file server configuration" do - @configuration.expects(:authorized?).with { |key, options| options[:node] == "mynode" } - @file_server.authorized?(:find, "puppetmounts://host/my/file", :node => "mynode") - end + it "should pass the node name to the file server configuration" do + @configuration.expects(:authorized?).with { |key, options| options[:node] == "mynode" } + @file_server.authorized?(:find, "puppetmounts://host/my/file", :node => "mynode") + end - it "should pass the IP address to the file server configuration" do - @configuration.expects(:authorized?).with { |key, options| options[:ipaddress] == "myip" } - @file_server.authorized?(:find, "puppetmounts://host/my/file", :ipaddress => "myip") - end + it "should pass the IP address to the file server configuration" do + @configuration.expects(:authorized?).with { |key, options| options[:ipaddress] == "myip" } + @file_server.authorized?(:find, "puppetmounts://host/my/file", :ipaddress => "myip") + end - it "should return false if the file server configuration denies authorization" do - @configuration.expects(:authorized?).returns(false) - @file_server.authorized?(:find, "puppetmounts://host/my/file").should be_false - end + it "should return false if the file server configuration denies authorization" do + @configuration.expects(:authorized?).returns(false) + @file_server.authorized?(:find, "puppetmounts://host/my/file").should be_false + end - it "should return true if the file server configuration approves authorization" do - @configuration.expects(:authorized?).returns(true) - @file_server.authorized?(:find, "puppetmounts://host/my/file").should be_true + it "should return true if the file server configuration approves authorization" do + @configuration.expects(:authorized?).returns(true) + @file_server.authorized?(:find, "puppetmounts://host/my/file").should be_true + end end -end -describe Puppet::Indirector::FileServer, " when searching for files" do - include FileServerTerminusTesting + describe Puppet::Indirector::FileServer, " when searching for files" do - it "should use the path portion of the URI as the file name" do - @configuration.expects(:file_path).with("/my/local/file", :node => nil) - @file_server.search(@uri) - end + it "should use the path portion of the URI as the file name" do + @configuration.expects(:file_path).with("/my/local/file", :node => nil) + @file_server.search(@uri) + end - it "should use the FileServing configuration to convert the file name to a fully qualified path" do - @configuration.expects(:file_path).with("/my/local/file", :node => nil) - @file_server.search(@uri) - end + it "should use the FileServing configuration to convert the file name to a fully qualified path" do + @configuration.expects(:file_path).with("/my/local/file", :node => nil) + @file_server.search(@uri) + end - it "should pass the node name to the FileServing configuration if one is provided" do - @configuration.expects(:file_path).with("/my/local/file", :node => "testing") - @file_server.search(@uri, :node => "testing") - end + it "should pass the node name to the FileServing configuration if one is provided" do + @configuration.expects(:file_path).with("/my/local/file", :node => "testing") + @file_server.search(@uri, :node => "testing") + end - it "should return nil if no fully qualified path is found" do - @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns(nil) - @file_server.search(@uri).should be_nil - end + it "should return nil if no fully qualified path is found" do + @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns(nil) + @file_server.search(@uri).should be_nil + end - it "should use :path2instances from the terminus_helper to return instances if a module is found and the file exists" do - @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/my/file") - @file_server.expects(:path2instances).with(@uri, "/my/file", {}) - @file_server.search(@uri) - end + it "should use :path2instances from the terminus_helper to return instances if a module is found and the file exists" do + @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/my/file") + @file_server.expects(:path2instances).with(@uri, "/my/file", {}) + @file_server.search(@uri) + end - it "should pass any options on to :path2instances" do - @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/my/file") - @file_server.expects(:path2instances).with(@uri, "/my/file", :testing => :one, :other => :two) - @file_server.search(@uri, :testing => :one, :other => :two) + it "should pass any options on to :path2instances" do + @configuration.expects(:file_path).with("/my/local/file", :node => nil).returns("/my/file") + @file_server.expects(:path2instances).with(@uri, "/my/file", :testing => :one, :other => :two) + @file_server.search(@uri, :testing => :one, :other => :two) + end end -end +end
\ No newline at end of file diff --git a/spec/unit/indirector/indirection.rb b/spec/unit/indirector/indirection.rb index 36192b8e5..fe456b61e 100755 --- a/spec/unit/indirector/indirection.rb +++ b/spec/unit/indirector/indirection.rb @@ -4,21 +4,7 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector' -module IndirectionTesting - def setup - @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) - @terminus = stub 'terminus', :has_most_recent? => false - @indirection.stubs(:terminus).returns(@terminus) - @indirection.stubs(:terminus_class).returns(:whatever) - @instance = stub 'instance', :version => nil, :version= => nil, :name => "whatever" - @name = :mything - end - - def teardown - @indirection.delete - Puppet::Indirector::Indirection.clear_cache - end -end + describe Puppet::Indirector::Indirection, " when initializing" do # (LAK) I've no idea how to test this, really. @@ -55,103 +41,183 @@ describe Puppet::Indirector::Indirection, " when initializing" do end end -describe Puppet::Indirector::Indirection, " when looking for a model instance" do - include IndirectionTesting +describe Puppet::Indirector::Indirection do + before :each do + @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) + @terminus = stub 'terminus', :has_most_recent? => false + @indirection.stubs(:terminus).returns(@terminus) + @indirection.stubs(:terminus_class).returns(:whatever) + @instance = stub 'instance', :version => nil, :version= => nil, :name => "whatever" + @name = :mything + end + + describe Puppet::Indirector::Indirection, " when looking for a model instance" do + + it "should let the appropriate terminus perform the lookup" do + @terminus.expects(:find).with(@name).returns(@instance) + @indirection.find(@name).should == @instance + end + + it "should not attempt to set a timestamp if the terminus cannot find the instance" do + @terminus.expects(:find).with(@name).returns(nil) + proc { @indirection.find(@name) }.should_not raise_error + end - it "should let the appropriate terminus perform the lookup" do - @terminus.expects(:find).with(@name).returns(@instance) - @indirection.find(@name).should == @instance + it "should pass the instance to the :post_find hook if there is one" do + class << @terminus + def post_find + end + end + @terminus.expects(:post_find).with(@instance) + @terminus.expects(:find).with(@name).returns(@instance) + @indirection.find(@name) + end end + + describe Puppet::Indirector::Indirection, " when removing a model instance" do - it "should not attempt to set a timestamp if the terminus cannot find the instance" do - @terminus.expects(:find).with(@name).returns(nil) - proc { @indirection.find(@name) }.should_not raise_error + it "should let the appropriate terminus remove the instance" do + @terminus.expects(:destroy).with(@name).returns(@instance) + @indirection.destroy(@name).should == @instance + end end - it "should pass the instance to the :post_find hook if there is one" do - class << @terminus - def post_find + describe Puppet::Indirector::Indirection, " when searching for multiple model instances" do + + it "should let the appropriate terminus find the matching instances" do + @terminus.expects(:search).with(@name).returns(@instance) + @indirection.search(@name).should == @instance + end + + it "should pass the instances to the :post_search hook if there is one" do + class << @terminus + def post_search + end end + @terminus.expects(:post_search).with(@instance) + @terminus.expects(:search).with(@name).returns(@instance) + @indirection.search(@name) end - @terminus.expects(:post_find).with(@instance) - @terminus.expects(:find).with(@name).returns(@instance) - @indirection.find(@name) end -end -describe Puppet::Indirector::Indirection, " when removing a model instance" do - include IndirectionTesting + describe Puppet::Indirector::Indirection, " when storing a model instance" do - it "should let the appropriate terminus remove the instance" do - @terminus.expects(:destroy).with(@name).returns(@instance) - @indirection.destroy(@name).should == @instance + it "should let the appropriate terminus store the instance" do + @terminus.expects(:save).with(@instance).returns(@instance) + @indirection.save(@instance).should == @instance + end end -end + + describe Puppet::Indirector::Indirection, " when handling instance versions" do -describe Puppet::Indirector::Indirection, " when searching for multiple model instances" do - include IndirectionTesting - - it "should let the appropriate terminus find the matching instances" do - @terminus.expects(:search).with(@name).returns(@instance) - @indirection.search(@name).should == @instance - end + it "should let the appropriate terminus perform the lookup" do + @terminus.expects(:version).with(@name).returns(5) + @indirection.version(@name).should == 5 + end + + it "should add versions to found instances that do not already have them" do + @terminus.expects(:find).with(@name).returns(@instance) + time = mock 'time' + time.expects(:utc).returns(:mystamp) + Time.expects(:now).returns(time) + @instance.expects(:version=).with(:mystamp) + @indirection.find(@name) + end + + it "should add versions to saved instances that do not already have them" do + time = mock 'time' + time.expects(:utc).returns(:mystamp) + Time.expects(:now).returns(time) + @instance.expects(:version=).with(:mystamp) + @terminus.stubs(:save) + @indirection.save(@instance) + end - it "should pass the instances to the :post_search hook if there is one" do - class << @terminus - def post_search + # We've already tested this, basically, but... + it "should use the current time in UTC for versions" do + @instance.expects(:version=).with do |time| + time.utc? end + @terminus.stubs(:save) + @indirection.save(@instance) end - @terminus.expects(:post_search).with(@instance) - @terminus.expects(:search).with(@name).returns(@instance) - @indirection.search(@name) end -end -describe Puppet::Indirector::Indirection, " when storing a model instance" do - include IndirectionTesting - - it "should let the appropriate terminus store the instance" do - @terminus.expects(:save).with(@instance).returns(@instance) - @indirection.save(@instance).should == @instance - end -end -describe Puppet::Indirector::Indirection, " when handling instance versions" do - include IndirectionTesting - - it "should let the appropriate terminus perform the lookup" do - @terminus.expects(:version).with(@name).returns(5) - @indirection.version(@name).should == 5 - end + describe Puppet::Indirector::Indirection, " when an authorization hook is present" do - it "should add versions to found instances that do not already have them" do - @terminus.expects(:find).with(@name).returns(@instance) - time = mock 'time' - time.expects(:utc).returns(:mystamp) - Time.expects(:now).returns(time) - @instance.expects(:version=).with(:mystamp) - @indirection.find(@name) - end + before do + # So the :respond_to? turns out right. + class << @terminus + def authorized? + end + end + end - it "should add versions to saved instances that do not already have them" do - time = mock 'time' - time.expects(:utc).returns(:mystamp) - Time.expects(:now).returns(time) - @instance.expects(:version=).with(:mystamp) - @terminus.stubs(:save) - @indirection.save(@instance) - end + it "should not check authorization if a node name is not provided" do + @terminus.expects(:authorized?).never + @terminus.stubs(:find) + @indirection.find("/my/key") + end + + it "should fail while finding instances if authorization returns false" do + @terminus.expects(:authorized?).with(:find, "/my/key", :node => "mynode").returns(false) + @terminus.stubs(:find) + proc { @indirection.find("/my/key", :node => "mynode") }.should raise_error(ArgumentError) + end + + it "should continue finding instances if authorization returns true" do + @terminus.expects(:authorized?).with(:find, "/my/key", :node => "mynode").returns(true) + @terminus.stubs(:find) + @indirection.find("/my/key", :node => "mynode") + end + + it "should fail while saving instances if authorization returns false" do + @terminus.expects(:authorized?).with(:save, :myinstance, :node => "mynode").returns(false) + @terminus.stubs(:save) + proc { @indirection.save(:myinstance, :node => "mynode") }.should raise_error(ArgumentError) + end + + it "should continue saving instances if authorization returns true" do + instance = stub 'instance', :version => 1.0, :name => "eh" + @terminus.expects(:authorized?).with(:save, instance, :node => "mynode").returns(true) + @terminus.stubs(:save) + @indirection.save(instance, :node => "mynode") + end + + it "should fail while destroying instances if authorization returns false" do + @terminus.expects(:authorized?).with(:destroy, "/my/key", :node => "mynode").returns(false) + @terminus.stubs(:destroy) + proc { @indirection.destroy("/my/key", :node => "mynode") }.should raise_error(ArgumentError) + end + + it "should continue destroying instances if authorization returns true" do + instance = stub 'instance', :version => 1.0, :name => "eh" + @terminus.expects(:authorized?).with(:destroy, instance, :node => "mynode").returns(true) + @terminus.stubs(:destroy) + @indirection.destroy(instance, :node => "mynode") + end + + it "should fail while searching for instances if authorization returns false" do + @terminus.expects(:authorized?).with(:search, "/my/key", :node => "mynode").returns(false) + @terminus.stubs(:search) + proc { @indirection.search("/my/key", :node => "mynode") }.should raise_error(ArgumentError) + end - # We've already tested this, basically, but... - it "should use the current time in UTC for versions" do - @instance.expects(:version=).with do |time| - time.utc? + it "should continue searching for instances if authorization returns true" do + @terminus.expects(:authorized?).with(:search, "/my/key", :node => "mynode").returns(true) + @terminus.stubs(:search) + @indirection.search("/my/key", :node => "mynode") end - @terminus.stubs(:save) - @indirection.save(@instance) + end + + after :each do + @indirection.delete + Puppet::Indirector::Indirection.clear_cache end end + describe Puppet::Indirector::Indirection, " when managing indirection instances" do it "should allow an indirection to be retrieved by name" do @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) @@ -397,8 +463,8 @@ describe Puppet::Indirector::Indirection, " when deciding whether to cache" do end end -module IndirectionCacheTesting - def setup +describe Puppet::Indirector::Indirection do + before :each do Puppet.settings.stubs(:value).with("test_terminus").returns("test_terminus") @terminus_class = mock 'terminus_class' @terminus = mock 'terminus' @@ -411,175 +477,104 @@ module IndirectionCacheTesting @indirection.terminus_class = :test_terminus end - def teardown - @indirection.delete - Puppet::Indirector::Indirection.clear_cache - end -end - -describe Puppet::Indirector::Indirection, " when managing the cache terminus" do - include IndirectionCacheTesting - - it "should not create a cache terminus at initialization" do - # This is weird, because all of the code is in the setup. If we got - # new() called on the cache class, we'd get an exception here. - end - - it "should reuse the cache terminus" do - @cache_class.expects(:new).returns(@cache) - Puppet.settings.stubs(:value).with("test_cache").returns("cache_terminus") - @indirection.cache_class = :cache_terminus - @indirection.cache.should equal(@cache) - @indirection.cache.should equal(@cache) - end - - it "should remove the cache terminus when all other terminus instances are cleared" do - cache2 = mock 'cache2' - @cache_class.stubs(:new).returns(@cache, cache2) - @indirection.cache_class = :cache_terminus - @indirection.cache.should equal(@cache) - @indirection.clear_cache - @indirection.cache.should equal(cache2) - end -end - -describe Puppet::Indirector::Indirection, " when saving and using a cache" do - include IndirectionCacheTesting - - before do - @indirection.cache_class = :cache_terminus - @cache_class.expects(:new).returns(@cache) - @name = "testing" - @instance = stub 'instance', :version => 5, :name => @name - end - - it "should not update the cache or terminus if the new object is not different" do - @cache.expects(:has_most_recent?).with(@name, 5).returns(true) - @indirection.save(@instance) - end + describe Puppet::Indirector::Indirection, " when managing the cache terminus" do - it "should update the original and the cache if the cached object is different" do - @cache.expects(:has_most_recent?).with(@name, 5).returns(false) - @terminus.expects(:save).with(@instance) - @cache.expects(:save).with(@instance) - @indirection.save(@instance) - end -end + it "should not create a cache terminus at initialization" do + # This is weird, because all of the code is in the setup. If we got + # new() called on the cache class, we'd get an exception here. + end -describe Puppet::Indirector::Indirection, " when finding and using a cache" do - include IndirectionCacheTesting + it "should reuse the cache terminus" do + @cache_class.expects(:new).returns(@cache) + Puppet.settings.stubs(:value).with("test_cache").returns("cache_terminus") + @indirection.cache_class = :cache_terminus + @indirection.cache.should equal(@cache) + @indirection.cache.should equal(@cache) + end - before do - @indirection.cache_class = :cache_terminus - @cache_class.expects(:new).returns(@cache) + it "should remove the cache terminus when all other terminus instances are cleared" do + cache2 = mock 'cache2' + @cache_class.stubs(:new).returns(@cache, cache2) + @indirection.cache_class = :cache_terminus + @indirection.cache.should equal(@cache) + @indirection.clear_cache + @indirection.cache.should equal(cache2) + end end - it "should return the cached object if the cache is up to date" do - cached = mock 'cached object' + describe Puppet::Indirector::Indirection, " when saving and using a cache" do - name = "myobject" - - @terminus.expects(:version).with(name).returns(1) - @cache.expects(:has_most_recent?).with(name, 1).returns(true) + before do + @indirection.cache_class = :cache_terminus + @cache_class.expects(:new).returns(@cache) + @name = "testing" + @instance = stub 'instance', :version => 5, :name => @name + end - @cache.expects(:find).with(name).returns(cached) + it "should not update the cache or terminus if the new object is not different" do + @cache.expects(:has_most_recent?).with(@name, 5).returns(true) + @indirection.save(@instance) + end - @indirection.find(name).should equal(cached) + it "should update the original and the cache if the cached object is different" do + @cache.expects(:has_most_recent?).with(@name, 5).returns(false) + @terminus.expects(:save).with(@instance) + @cache.expects(:save).with(@instance) + @indirection.save(@instance) + end end + + describe Puppet::Indirector::Indirection, " when finding and using a cache" do - it "should return the original object if the cache is not up to date" do - real = stub 'real object', :version => 1 - - name = "myobject" + before do + @indirection.cache_class = :cache_terminus + @cache_class.expects(:new).returns(@cache) + end - @cache.stubs(:save) - @cache.expects(:has_most_recent?).with(name, 1).returns(false) - @terminus.expects(:version).with(name).returns(1) + it "should return the cached object if the cache is up to date" do + cached = mock 'cached object' - @terminus.expects(:find).with(name).returns(real) + name = "myobject" - @indirection.find(name).should equal(real) - end + @terminus.expects(:version).with(name).returns(1) + @cache.expects(:has_most_recent?).with(name, 1).returns(true) - it "should cache any newly returned objects" do - real = stub 'real object', :version => 1 + @cache.expects(:find).with(name).returns(cached) - name = "myobject" + @indirection.find(name).should equal(cached) + end - @terminus.expects(:version).with(name).returns(1) - @cache.expects(:has_most_recent?).with(name, 1).returns(false) + it "should return the original object if the cache is not up to date" do + real = stub 'real object', :version => 1 - @terminus.expects(:find).with(name).returns(real) - @cache.expects(:save).with(real) + name = "myobject" - @indirection.find(name).should equal(real) - end -end + @cache.stubs(:save) + @cache.expects(:has_most_recent?).with(name, 1).returns(false) + @terminus.expects(:version).with(name).returns(1) -describe Puppet::Indirector::Indirection, " when an authorization hook is present" do - include IndirectionTesting + @terminus.expects(:find).with(name).returns(real) - before do - # So the :respond_to? turns out right. - class << @terminus - def authorized? - end + @indirection.find(name).should equal(real) end - end - it "should not check authorization if a node name is not provided" do - @terminus.expects(:authorized?).never - @terminus.stubs(:find) - @indirection.find("/my/key") - end - - it "should fail while finding instances if authorization returns false" do - @terminus.expects(:authorized?).with(:find, "/my/key", :node => "mynode").returns(false) - @terminus.stubs(:find) - proc { @indirection.find("/my/key", :node => "mynode") }.should raise_error(ArgumentError) - end - - it "should continue finding instances if authorization returns true" do - @terminus.expects(:authorized?).with(:find, "/my/key", :node => "mynode").returns(true) - @terminus.stubs(:find) - @indirection.find("/my/key", :node => "mynode") - end + it "should cache any newly returned objects" do + real = stub 'real object', :version => 1 - it "should fail while saving instances if authorization returns false" do - @terminus.expects(:authorized?).with(:save, :myinstance, :node => "mynode").returns(false) - @terminus.stubs(:save) - proc { @indirection.save(:myinstance, :node => "mynode") }.should raise_error(ArgumentError) - end + name = "myobject" - it "should continue saving instances if authorization returns true" do - instance = stub 'instance', :version => 1.0, :name => "eh" - @terminus.expects(:authorized?).with(:save, instance, :node => "mynode").returns(true) - @terminus.stubs(:save) - @indirection.save(instance, :node => "mynode") - end + @terminus.expects(:version).with(name).returns(1) + @cache.expects(:has_most_recent?).with(name, 1).returns(false) - it "should fail while destroying instances if authorization returns false" do - @terminus.expects(:authorized?).with(:destroy, "/my/key", :node => "mynode").returns(false) - @terminus.stubs(:destroy) - proc { @indirection.destroy("/my/key", :node => "mynode") }.should raise_error(ArgumentError) - end + @terminus.expects(:find).with(name).returns(real) + @cache.expects(:save).with(real) - it "should continue destroying instances if authorization returns true" do - instance = stub 'instance', :version => 1.0, :name => "eh" - @terminus.expects(:authorized?).with(:destroy, instance, :node => "mynode").returns(true) - @terminus.stubs(:destroy) - @indirection.destroy(instance, :node => "mynode") - end - - it "should fail while searching for instances if authorization returns false" do - @terminus.expects(:authorized?).with(:search, "/my/key", :node => "mynode").returns(false) - @terminus.stubs(:search) - proc { @indirection.search("/my/key", :node => "mynode") }.should raise_error(ArgumentError) + @indirection.find(name).should equal(real) + end end - - it "should continue searching for instances if authorization returns true" do - @terminus.expects(:authorized?).with(:search, "/my/key", :node => "mynode").returns(true) - @terminus.stubs(:search) - @indirection.search("/my/key", :node => "mynode") + + after :each do + @indirection.delete + Puppet::Indirector::Indirection.clear_cache end end diff --git a/spec/unit/indirector/module_files.rb b/spec/unit/indirector/module_files.rb index 9011530dd..0f6dbb6df 100755 --- a/spec/unit/indirector/module_files.rb +++ b/spec/unit/indirector/module_files.rb @@ -7,239 +7,237 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector/module_files' -module ModuleFilesTerminusTesting - def setup - Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) - Puppet::Indirector::Terminus.stubs(:register_terminus_class) - @model = mock 'model' - @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model - Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) - @module_files_class = Class.new(Puppet::Indirector::ModuleFiles) do - def self.to_s - "Testing::Mytype" - end - end +describe Puppet::Indirector::ModuleFiles do - @module_files = @module_files_class.new + before :each do + Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) + Puppet::Indirector::Terminus.stubs(:register_terminus_class) + @model = mock 'model' + @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model + Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection) - @uri = "puppetmounts://host/modules/my/local/file" - @module = Puppet::Module.new("mymod", "/module/path") - end -end + @module_files_class = Class.new(Puppet::Indirector::ModuleFiles) do + def self.to_s + "Testing::Mytype" + end + end -describe Puppet::Indirector::ModuleFiles, " when finding files" do - include ModuleFilesTerminusTesting + @module_files = @module_files_class.new - it "should strip off the leading '/modules' mount name" do - Puppet::Module.expects(:find).with('my', "myenv").returns @module - @module_files.find(@uri) - end + @uri = "puppetmounts://host/modules/my/local/file" + @module = Puppet::Module.new("mymod", "/module/path") + end - it "should not strip off leading terms that start with '/modules' but are longer words" do - Puppet::Module.expects(:find).with('modulestart', "myenv").returns nil - @module_files.find("puppetmounts://host/modulestart/my/local/file") - end + describe Puppet::Indirector::ModuleFiles, " when finding files" do - it "should search for a module whose name is the first term in the remaining file path" do - Puppet::Module.expects(:find).with('my', "myenv").returns @module - @module_files.find(@uri) - end + it "should strip off the leading '/modules' mount name" do + Puppet::Module.expects(:find).with('my', "myenv").returns @module + @module_files.find(@uri) + end - it "should search for a file relative to the module's files directory" do - Puppet::Module.expects(:find).with('my', "myenv").returns @module - FileTest.expects(:exists?).with("/module/path/files/local/file") - @module_files.find(@uri) - end + it "should not strip off leading terms that start with '/modules' but are longer words" do + Puppet::Module.expects(:find).with('modulestart', "myenv").returns nil + @module_files.find("puppetmounts://host/modulestart/my/local/file") + end - it "should return nil if the module does not exist" do - Puppet::Module.expects(:find).with('my', "myenv").returns nil - @module_files.find(@uri).should be_nil - end + it "should search for a module whose name is the first term in the remaining file path" do + Puppet::Module.expects(:find).with('my', "myenv").returns @module + @module_files.find(@uri) + end - it "should return nil if the module exists but the file does not" do - Puppet::Module.expects(:find).with('my', "myenv").returns @module - FileTest.expects(:exists?).with("/module/path/files/local/file").returns(false) - @module_files.find(@uri).should be_nil - end + it "should search for a file relative to the module's files directory" do + Puppet::Module.expects(:find).with('my', "myenv").returns @module + FileTest.expects(:exists?).with("/module/path/files/local/file") + @module_files.find(@uri) + end - it "should return an instance of the model if a module is found and the file exists" do - Puppet::Module.expects(:find).with('my', "myenv").returns @module - FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) - @model.expects(:new).returns(:myinstance) - @module_files.find(@uri).should == :myinstance - end + it "should return nil if the module does not exist" do + Puppet::Module.expects(:find).with('my', "myenv").returns nil + @module_files.find(@uri).should be_nil + end - it "should use the node's environment to look up the module if the node name is provided" do - node = stub "node", :environment => "testing" - Puppet::Node.expects(:find).with("mynode").returns(node) - Puppet::Module.expects(:find).with('my', "testing") - @module_files.find(@uri, :node => "mynode") - end + it "should return nil if the module exists but the file does not" do + Puppet::Module.expects(:find).with('my', "myenv").returns @module + FileTest.expects(:exists?).with("/module/path/files/local/file").returns(false) + @module_files.find(@uri).should be_nil + end - it "should use the default environment setting to look up the module if the node name is not provided" do - env = stub "environment", :name => "testing" - Puppet::Node::Environment.stubs(:new).returns(env) - Puppet::Module.expects(:find).with('my', "testing") - @module_files.find(@uri) - end -end + it "should return an instance of the model if a module is found and the file exists" do + Puppet::Module.expects(:find).with('my', "myenv").returns @module + FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) + @model.expects(:new).returns(:myinstance) + @module_files.find(@uri).should == :myinstance + end -describe Puppet::Indirector::ModuleFiles, " when returning instances" do - include ModuleFilesTerminusTesting + it "should use the node's environment to look up the module if the node name is provided" do + node = stub "node", :environment => "testing" + Puppet::Node.expects(:find).with("mynode").returns(node) + Puppet::Module.expects(:find).with('my', "testing") + @module_files.find(@uri, :node => "mynode") + end - before do - Puppet::Module.expects(:find).with('my', "myenv").returns @module - FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) - @instance = mock 'instance' + it "should use the default environment setting to look up the module if the node name is not provided" do + env = stub "environment", :name => "testing" + Puppet::Node::Environment.stubs(:new).returns(env) + Puppet::Module.expects(:find).with('my', "testing") + @module_files.find(@uri) + end end - it "should create the instance with the key used to find the instance" do - @model.expects(:new).with { |key, *options| key == @uri } - @module_files.find(@uri) - end + describe Puppet::Indirector::ModuleFiles, " when returning instances" do - it "should create the instance with the path at which the instance was found" do - @model.expects(:new).with { |key, options| options[:path] == "/module/path/files/local/file" } - @module_files.find(@uri) - end + before do + Puppet::Module.expects(:find).with('my', "myenv").returns @module + FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) + @instance = mock 'instance' + end - it "should set the provided :links setting on to the instance if one is provided" do - @model.expects(:new).returns(@instance) - @instance.expects(:links=).with(:mytest) - @module_files.find(@uri, :links => :mytest) - end + it "should create the instance with the key used to find the instance" do + @model.expects(:new).with { |key, *options| key == @uri } + @module_files.find(@uri) + end - it "should not set a :links value if no :links parameter is provided" do - @model.expects(:new).returns(@instance) - @module_files.find(@uri) - end -end + it "should create the instance with the path at which the instance was found" do + @model.expects(:new).with { |key, options| options[:path] == "/module/path/files/local/file" } + @module_files.find(@uri) + end -describe Puppet::Indirector::ModuleFiles, " when authorizing" do - include ModuleFilesTerminusTesting + it "should set the provided :links setting on to the instance if one is provided" do + @model.expects(:new).returns(@instance) + @instance.expects(:links=).with(:mytest) + @module_files.find(@uri, :links => :mytest) + end - before do - @configuration = mock 'configuration' - Puppet::FileServing::Configuration.stubs(:create).returns(@configuration) + it "should not set a :links value if no :links parameter is provided" do + @model.expects(:new).returns(@instance) + @module_files.find(@uri) + end end - it "should have an authorization hook" do - @module_files.should respond_to(:authorized?) - end + describe Puppet::Indirector::ModuleFiles, " when authorizing" do - it "should deny the :destroy method" do - @module_files.authorized?(:destroy, "whatever").should be_false - end + before do + @configuration = mock 'configuration' + Puppet::FileServing::Configuration.stubs(:create).returns(@configuration) + end - it "should deny the :save method" do - @module_files.authorized?(:save, "whatever").should be_false - end + it "should have an authorization hook" do + @module_files.should respond_to(:authorized?) + end - it "should use the file server configuration to determine authorization" do - @configuration.expects(:authorized?) - @module_files.authorized?(:find, "puppetmounts://host/my/file") - end + it "should deny the :destroy method" do + @module_files.authorized?(:destroy, "whatever").should be_false + end - it "should use the path directly from the URI if it already includes /modules" do - @configuration.expects(:authorized?).with { |uri, *args| uri == "/modules/my/file" } - @module_files.authorized?(:find, "puppetmounts://host/modules/my/file") - end + it "should deny the :save method" do + @module_files.authorized?(:save, "whatever").should be_false + end - it "should add /modules to the file path if it's not included in the URI" do - @configuration.expects(:authorized?).with { |uri, *args| uri == "/modules/my/file" } - @module_files.authorized?(:find, "puppetmounts://host/my/file") - end + it "should use the file server configuration to determine authorization" do + @configuration.expects(:authorized?) + @module_files.authorized?(:find, "puppetmounts://host/my/file") + end - it "should pass the node name to the file server configuration" do - @configuration.expects(:authorized?).with { |key, options| options[:node] == "mynode" } - @module_files.authorized?(:find, "puppetmounts://host/my/file", :node => "mynode") - end + it "should use the path directly from the URI if it already includes /modules" do + @configuration.expects(:authorized?).with { |uri, *args| uri == "/modules/my/file" } + @module_files.authorized?(:find, "puppetmounts://host/modules/my/file") + end - it "should pass the IP address to the file server configuration" do - @configuration.expects(:authorized?).with { |key, options| options[:ipaddress] == "myip" } - @module_files.authorized?(:find, "puppetmounts://host/my/file", :ipaddress => "myip") - end + it "should add /modules to the file path if it's not included in the URI" do + @configuration.expects(:authorized?).with { |uri, *args| uri == "/modules/my/file" } + @module_files.authorized?(:find, "puppetmounts://host/my/file") + end - it "should return false if the file server configuration denies authorization" do - @configuration.expects(:authorized?).returns(false) - @module_files.authorized?(:find, "puppetmounts://host/my/file").should be_false - end + it "should pass the node name to the file server configuration" do + @configuration.expects(:authorized?).with { |key, options| options[:node] == "mynode" } + @module_files.authorized?(:find, "puppetmounts://host/my/file", :node => "mynode") + end - it "should return true if the file server configuration approves authorization" do - @configuration.expects(:authorized?).returns(true) - @module_files.authorized?(:find, "puppetmounts://host/my/file").should be_true - end -end + it "should pass the IP address to the file server configuration" do + @configuration.expects(:authorized?).with { |key, options| options[:ipaddress] == "myip" } + @module_files.authorized?(:find, "puppetmounts://host/my/file", :ipaddress => "myip") + end -describe Puppet::Indirector::ModuleFiles, " when searching for files" do - include ModuleFilesTerminusTesting + it "should return false if the file server configuration denies authorization" do + @configuration.expects(:authorized?).returns(false) + @module_files.authorized?(:find, "puppetmounts://host/my/file").should be_false + end - it "should strip off the leading '/modules' mount name" do - Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) - Puppet::Module.expects(:find).with('my', "myenv").returns @module - @module_files.search(@uri) + it "should return true if the file server configuration approves authorization" do + @configuration.expects(:authorized?).returns(true) + @module_files.authorized?(:find, "puppetmounts://host/my/file").should be_true + end end - it "should not strip off leading terms that start with '/modules' but are longer words" do - Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) - Puppet::Module.expects(:find).with('modulestart', "myenv").returns nil - @module_files.search("puppetmounts://host/modulestart/my/local/file") - end + describe Puppet::Indirector::ModuleFiles, " when searching for files" do - it "should search for a module whose name is the first term in the remaining file path" do - Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) - Puppet::Module.expects(:find).with('my', "myenv").returns @module - @module_files.search(@uri) - end + it "should strip off the leading '/modules' mount name" do + Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) + Puppet::Module.expects(:find).with('my', "myenv").returns @module + @module_files.search(@uri) + end - it "should search for a file relative to the module's files directory" do - Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) - Puppet::Module.expects(:find).with('my', "myenv").returns @module - FileTest.expects(:exists?).with("/module/path/files/local/file") - @module_files.search(@uri) - end + it "should not strip off leading terms that start with '/modules' but are longer words" do + Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) + Puppet::Module.expects(:find).with('modulestart', "myenv").returns nil + @module_files.search("puppetmounts://host/modulestart/my/local/file") + end - it "should return nil if the module does not exist" do - Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) - Puppet::Module.expects(:find).with('my', "myenv").returns nil - @module_files.search(@uri).should be_nil - end + it "should search for a module whose name is the first term in the remaining file path" do + Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) + Puppet::Module.expects(:find).with('my', "myenv").returns @module + @module_files.search(@uri) + end - it "should return nil if the module exists but the file does not" do - Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) - Puppet::Module.expects(:find).with('my', "myenv").returns @module - FileTest.expects(:exists?).with("/module/path/files/local/file").returns(false) - @module_files.search(@uri).should be_nil - end + it "should search for a file relative to the module's files directory" do + Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) + Puppet::Module.expects(:find).with('my', "myenv").returns @module + FileTest.expects(:exists?).with("/module/path/files/local/file") + @module_files.search(@uri) + end - it "should use the node's environment to look up the module if the node name is provided" do - node = stub "node", :environment => "testing" - Puppet::Node.expects(:find).with("mynode").returns(node) - Puppet::Module.expects(:find).with('my', "testing") - @module_files.search(@uri, :node => "mynode") - end + it "should return nil if the module does not exist" do + Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) + Puppet::Module.expects(:find).with('my', "myenv").returns nil + @module_files.search(@uri).should be_nil + end - it "should use the default environment setting to look up the module if the node name is not provided and the environment is not set to ''" do - env = stub 'env', :name => "testing" - Puppet::Node::Environment.stubs(:new).returns(env) - Puppet::Module.expects(:find).with('my', "testing") - @module_files.search(@uri) - end + it "should return nil if the module exists but the file does not" do + Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) + Puppet::Module.expects(:find).with('my', "myenv").returns @module + FileTest.expects(:exists?).with("/module/path/files/local/file").returns(false) + @module_files.search(@uri).should be_nil + end - it "should use :path2instances from the terminus_helper to return instances if a module is found and the file exists" do - Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) - Puppet::Module.expects(:find).with('my', "myenv").returns @module - FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) - @module_files.expects(:path2instances).with(@uri, "/module/path/files/local/file", {}) - @module_files.search(@uri) - end + it "should use the node's environment to look up the module if the node name is provided" do + node = stub "node", :environment => "testing" + Puppet::Node.expects(:find).with("mynode").returns(node) + Puppet::Module.expects(:find).with('my', "testing") + @module_files.search(@uri, :node => "mynode") + end - it "should pass any options on to :path2instances" do - Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) - Puppet::Module.expects(:find).with('my', "myenv").returns @module - FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) - @module_files.expects(:path2instances).with(@uri, "/module/path/files/local/file", :testing => :one, :other => :two) - @module_files.search(@uri, :testing => :one, :other => :two) + it "should use the default environment setting to look up the module if the node name is not provided and the environment is not set to ''" do + env = stub 'env', :name => "testing" + Puppet::Node::Environment.stubs(:new).returns(env) + Puppet::Module.expects(:find).with('my', "testing") + @module_files.search(@uri) + end + + it "should use :path2instances from the terminus_helper to return instances if a module is found and the file exists" do + Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) + Puppet::Module.expects(:find).with('my', "myenv").returns @module + FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) + @module_files.expects(:path2instances).with(@uri, "/module/path/files/local/file", {}) + @module_files.search(@uri) + end + + it "should pass any options on to :path2instances" do + Puppet::Node::Environment.stubs(:new).returns(stub('env', :name => "myenv")) + Puppet::Module.expects(:find).with('my', "myenv").returns @module + FileTest.expects(:exists?).with("/module/path/files/local/file").returns(true) + @module_files.expects(:path2instances).with(@uri, "/module/path/files/local/file", :testing => :one, :other => :two) + @module_files.search(@uri, :testing => :one, :other => :two) + end end end diff --git a/spec/unit/indirector/node/ldap.rb b/spec/unit/indirector/node/ldap.rb index 8c6888357..95b73de60 100755 --- a/spec/unit/indirector/node/ldap.rb +++ b/spec/unit/indirector/node/ldap.rb @@ -4,8 +4,8 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/indirector/node/ldap' -module LdapNodeSearching - def setup +describe Puppet::Node::Ldap do + before :each do @searcher = Puppet::Node::Ldap.new @entries = {} entries = @entries @@ -24,148 +24,146 @@ module LdapNodeSearching @name = "mynode" Puppet::Node.stubs(:new).with(@name).returns(@node) end -end -describe Puppet::Node::Ldap, " when searching for nodes" do - include LdapNodeSearching + describe Puppet::Node::Ldap, " when searching for nodes" do - it "should return nil if no results are found in ldap" do - @connection.stubs(:search) - @searcher.find("mynode").should be_nil - end + it "should return nil if no results are found in ldap" do + @connection.stubs(:search) + @searcher.find("mynode").should be_nil + end - it "should return a node object if results are found in ldap" do - @entry.stubs(:to_hash).returns({}) - @searcher.find("mynode").should equal(@node) - end + it "should return a node object if results are found in ldap" do + @entry.stubs(:to_hash).returns({}) + @searcher.find("mynode").should equal(@node) + end - it "should deduplicate class values" do - @entry.stubs(:to_hash).returns({}) - @searcher.stubs(:class_attributes).returns(%w{one two}) - @entry.stubs(:vals).with("one").returns(%w{a b}) - @entry.stubs(:vals).with("two").returns(%w{b c}) - @node.expects(:classes=).with(%w{a b c}) - @searcher.find("mynode") - end + it "should deduplicate class values" do + @entry.stubs(:to_hash).returns({}) + @searcher.stubs(:class_attributes).returns(%w{one two}) + @entry.stubs(:vals).with("one").returns(%w{a b}) + @entry.stubs(:vals).with("two").returns(%w{b c}) + @node.expects(:classes=).with(%w{a b c}) + @searcher.find("mynode") + end - it "should add any values stored in the class_attributes attributes to the node classes" do - @entry.stubs(:to_hash).returns({}) - @searcher.stubs(:class_attributes).returns(%w{one two}) - @entry.stubs(:vals).with("one").returns(%w{a b}) - @entry.stubs(:vals).with("two").returns(%w{c d}) - @node.expects(:classes=).with(%w{a b c d}) - @searcher.find("mynode") - end + it "should add any values stored in the class_attributes attributes to the node classes" do + @entry.stubs(:to_hash).returns({}) + @searcher.stubs(:class_attributes).returns(%w{one two}) + @entry.stubs(:vals).with("one").returns(%w{a b}) + @entry.stubs(:vals).with("two").returns(%w{c d}) + @node.expects(:classes=).with(%w{a b c d}) + @searcher.find("mynode") + end - it "should add all entry attributes as node parameters" do - @entry.stubs(:to_hash).returns("one" => ["two"], "three" => ["four"]) - @node.expects(:parameters=).with("one" => "two", "three" => "four") - @searcher.find("mynode") - end + it "should add all entry attributes as node parameters" do + @entry.stubs(:to_hash).returns("one" => ["two"], "three" => ["four"]) + @node.expects(:parameters=).with("one" => "two", "three" => "four") + @searcher.find("mynode") + end - it "should retain false parameter values" do - @entry.stubs(:to_hash).returns("one" => [false]) - @node.expects(:parameters=).with("one" => false) - @searcher.find("mynode") - end + it "should retain false parameter values" do + @entry.stubs(:to_hash).returns("one" => [false]) + @node.expects(:parameters=).with("one" => false) + @searcher.find("mynode") + end - it "should turn single-value parameter value arrays into single non-arrays" do - @entry.stubs(:to_hash).returns("one" => ["a"]) - @node.expects(:parameters=).with("one" => "a") - @searcher.find("mynode") - end + it "should turn single-value parameter value arrays into single non-arrays" do + @entry.stubs(:to_hash).returns("one" => ["a"]) + @node.expects(:parameters=).with("one" => "a") + @searcher.find("mynode") + end - it "should keep multi-valued parametes as arrays" do - @entry.stubs(:to_hash).returns("one" => ["a", "b"]) - @node.expects(:parameters=).with("one" => ["a", "b"]) - @searcher.find("mynode") + it "should keep multi-valued parametes as arrays" do + @entry.stubs(:to_hash).returns("one" => ["a", "b"]) + @node.expects(:parameters=).with("one" => ["a", "b"]) + @searcher.find("mynode") + end end -end -describe Puppet::Node::Ldap, " when a parent node is specified" do - include LdapNodeSearching + describe Puppet::Node::Ldap, " when a parent node is specified" do - before do - @parent = mock 'parent' - @parent_parent = mock 'parent_parent' + before do + @parent = mock 'parent' + @parent_parent = mock 'parent_parent' - @searcher.meta_def(:search_filter) do |name| - return name - end - @connection.stubs(:search).with { |*args| args[2] == @name }.yields(@entry) - @connection.stubs(:search).with { |*args| args[2] == 'parent' }.yields(@parent) - @connection.stubs(:search).with { |*args| args[2] == 'parent_parent' }.yields(@parent_parent) + @searcher.meta_def(:search_filter) do |name| + return name + end + @connection.stubs(:search).with { |*args| args[2] == @name }.yields(@entry) + @connection.stubs(:search).with { |*args| args[2] == 'parent' }.yields(@parent) + @connection.stubs(:search).with { |*args| args[2] == 'parent_parent' }.yields(@parent_parent) - @searcher.stubs(:parent_attribute).returns(:parent) - end + @searcher.stubs(:parent_attribute).returns(:parent) + end - it "should fail if the parent cannot be found" do - @connection.stubs(:search).with { |*args| args[2] == 'parent' }.returns("whatever") + it "should fail if the parent cannot be found" do + @connection.stubs(:search).with { |*args| args[2] == 'parent' }.returns("whatever") - @entry.stubs(:to_hash).returns({}) - @entry.stubs(:vals).with(:parent).returns(%w{parent}) + @entry.stubs(:to_hash).returns({}) + @entry.stubs(:vals).with(:parent).returns(%w{parent}) - proc { @searcher.find("mynode") }.should raise_error(Puppet::Error) - end + proc { @searcher.find("mynode") }.should raise_error(Puppet::Error) + end - it "should add any parent classes to the node's classes" do - @entry.stubs(:to_hash).returns({}) - @entry.stubs(:vals).with(:parent).returns(%w{parent}) - @entry.stubs(:vals).with("classes").returns(%w{a b}) + it "should add any parent classes to the node's classes" do + @entry.stubs(:to_hash).returns({}) + @entry.stubs(:vals).with(:parent).returns(%w{parent}) + @entry.stubs(:vals).with("classes").returns(%w{a b}) - @parent.stubs(:to_hash).returns({}) - @parent.stubs(:vals).with("classes").returns(%w{c d}) - @parent.stubs(:vals).with(:parent).returns(nil) + @parent.stubs(:to_hash).returns({}) + @parent.stubs(:vals).with("classes").returns(%w{c d}) + @parent.stubs(:vals).with(:parent).returns(nil) - @searcher.stubs(:class_attributes).returns(%w{classes}) - @node.expects(:classes=).with(%w{a b c d}) - @searcher.find("mynode") - end + @searcher.stubs(:class_attributes).returns(%w{classes}) + @node.expects(:classes=).with(%w{a b c d}) + @searcher.find("mynode") + end - it "should add any parent parameters to the node's parameters" do - @entry.stubs(:to_hash).returns("one" => "two") - @entry.stubs(:vals).with(:parent).returns(%w{parent}) + it "should add any parent parameters to the node's parameters" do + @entry.stubs(:to_hash).returns("one" => "two") + @entry.stubs(:vals).with(:parent).returns(%w{parent}) - @parent.stubs(:to_hash).returns("three" => "four") - @parent.stubs(:vals).with(:parent).returns(nil) + @parent.stubs(:to_hash).returns("three" => "four") + @parent.stubs(:vals).with(:parent).returns(nil) - @node.expects(:parameters=).with("one" => "two", "three" => "four") - @searcher.find("mynode") - end + @node.expects(:parameters=).with("one" => "two", "three" => "four") + @searcher.find("mynode") + end - it "should prefer node parameters over parent parameters" do - @entry.stubs(:to_hash).returns("one" => "two") - @entry.stubs(:vals).with(:parent).returns(%w{parent}) + it "should prefer node parameters over parent parameters" do + @entry.stubs(:to_hash).returns("one" => "two") + @entry.stubs(:vals).with(:parent).returns(%w{parent}) - @parent.stubs(:to_hash).returns("one" => "three") - @parent.stubs(:vals).with(:parent).returns(nil) + @parent.stubs(:to_hash).returns("one" => "three") + @parent.stubs(:vals).with(:parent).returns(nil) - @node.expects(:parameters=).with("one" => "two") - @searcher.find("mynode") - end + @node.expects(:parameters=).with("one" => "two") + @searcher.find("mynode") + end - it "should recursively look up parent information" do - @entry.stubs(:to_hash).returns("one" => "two") - @entry.stubs(:vals).with(:parent).returns(%w{parent}) + it "should recursively look up parent information" do + @entry.stubs(:to_hash).returns("one" => "two") + @entry.stubs(:vals).with(:parent).returns(%w{parent}) - @parent.stubs(:to_hash).returns("three" => "four") - @parent.stubs(:vals).with(:parent).returns(['parent_parent']) + @parent.stubs(:to_hash).returns("three" => "four") + @parent.stubs(:vals).with(:parent).returns(['parent_parent']) - @parent_parent.stubs(:to_hash).returns("five" => "six") - @parent_parent.stubs(:vals).with(:parent).returns(nil) - @parent_parent.stubs(:vals).with(:parent).returns(nil) + @parent_parent.stubs(:to_hash).returns("five" => "six") + @parent_parent.stubs(:vals).with(:parent).returns(nil) + @parent_parent.stubs(:vals).with(:parent).returns(nil) - @node.expects(:parameters=).with("one" => "two", "three" => "four", "five" => "six") - @searcher.find("mynode") - end + @node.expects(:parameters=).with("one" => "two", "three" => "four", "five" => "six") + @searcher.find("mynode") + end - it "should not allow loops in parent declarations" do - @entry.stubs(:to_hash).returns("one" => "two") - @entry.stubs(:vals).with(:parent).returns(%w{parent}) + it "should not allow loops in parent declarations" do + @entry.stubs(:to_hash).returns("one" => "two") + @entry.stubs(:vals).with(:parent).returns(%w{parent}) - @parent.stubs(:to_hash).returns("three" => "four") - @parent.stubs(:vals).with(:parent).returns([@name]) - proc { @searcher.find("mynode") }.should raise_error(ArgumentError) + @parent.stubs(:to_hash).returns("three" => "four") + @parent.stubs(:vals).with(:parent).returns([@name]) + proc { @searcher.find("mynode") }.should raise_error(ArgumentError) + end end end diff --git a/spec/unit/indirector/terminus.rb b/spec/unit/indirector/terminus.rb index 99193dbc3..86813e4e5 100755 --- a/spec/unit/indirector/terminus.rb +++ b/spec/unit/indirector/terminus.rb @@ -5,8 +5,8 @@ require 'puppet/defaults' require 'puppet/indirector' require 'puppet/indirector/file' -module TerminusInstanceTesting - def setup +describe Puppet::Indirector::Terminus do + before :each do Puppet::Indirector::Terminus.stubs(:register_terminus_class) @indirection = stub 'indirection', :name => :my_stuff, :register_terminus_type => nil Puppet::Indirector::Indirection.stubs(:instance).with(:my_stuff).returns(@indirection) @@ -22,51 +22,143 @@ module TerminusInstanceTesting end @terminus = @terminus_class.new end -end -describe Puppet::Indirector::Terminus do - include TerminusInstanceTesting + describe Puppet::Indirector::Terminus do - it "should provide a method for setting terminus class documentation" do - @terminus_class.should respond_to(:desc) - end + it "should provide a method for setting terminus class documentation" do + @terminus_class.should respond_to(:desc) + end - it "should support a class-level name attribute" do - @terminus_class.should respond_to(:name) - end + it "should support a class-level name attribute" do + @terminus_class.should respond_to(:name) + end - it "should support a class-level indirection attribute" do - @terminus_class.should respond_to(:indirection) - end + it "should support a class-level indirection attribute" do + @terminus_class.should respond_to(:indirection) + end - it "should support a class-level terminus-type attribute" do - @terminus_class.should respond_to(:terminus_type) - end + it "should support a class-level terminus-type attribute" do + @terminus_class.should respond_to(:terminus_type) + end - it "should support a class-level model attribute" do - @terminus_class.should respond_to(:model) + it "should support a class-level model attribute" do + @terminus_class.should respond_to(:model) + end + + it "should accept indirection instances as its indirection" do + indirection = stub 'indirection', :is_a? => true, :register_terminus_type => nil + proc { @terminus_class.indirection = indirection }.should_not raise_error + @terminus_class.indirection.should equal(indirection) + end + + it "should look up indirection instances when only a name has been provided" do + indirection = mock 'indirection' + Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns(indirection) + @terminus_class.indirection = :myind + @terminus_class.indirection.should equal(indirection) + end + + it "should fail when provided a name that does not resolve to an indirection" do + Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns(nil) + proc { @terminus_class.indirection = :myind }.should raise_error(ArgumentError) + + # It shouldn't overwrite our existing one (or, more normally, it shouldn't set + # anything). + @terminus_class.indirection.should equal(@indirection) + end end - it "should accept indirection instances as its indirection" do - indirection = stub 'indirection', :is_a? => true, :register_terminus_type => nil - proc { @terminus_class.indirection = indirection }.should_not raise_error - @terminus_class.indirection.should equal(indirection) + describe Puppet::Indirector::Terminus, " when creating terminus classes" do + it "should associate the subclass with an indirection based on the subclass constant" do + @terminus.indirection.should equal(@indirection) + end + + it "should set the subclass's type to the abstract terminus name" do + @terminus.terminus_type.should == :abstract + end + + it "should set the subclass's name to the indirection name" do + @terminus.name.should == :term_type + end + + it "should set the subclass's model to the indirection model" do + @indirection.expects(:model).returns :yay + @terminus.model.should == :yay + end end - it "should look up indirection instances when only a name has been provided" do - indirection = mock 'indirection' - Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns(indirection) - @terminus_class.indirection = :myind - @terminus_class.indirection.should equal(indirection) + describe Puppet::Indirector::Terminus, " when a terminus instance" do + + it "should return the class's name as its name" do + @terminus.name.should == :term_type + end + + it "should return the class's indirection as its indirection" do + @terminus.indirection.should equal(@indirection) + end + + it "should set the instances's type to the abstract terminus type's name" do + @terminus.terminus_type.should == :abstract + end + + it "should set the instances's model to the indirection's model" do + @indirection.expects(:model).returns :yay + @terminus.model.should == :yay + end end - it "should fail when provided a name that does not resolve to an indirection" do - Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns(nil) - proc { @terminus_class.indirection = :myind }.should raise_error(ArgumentError) + describe Puppet::Indirector::Terminus, " when managing indirected instances" do + + it "should support comparing an instance's version with the terminus's version using just the instance's key" do + @terminus.should respond_to(:has_most_recent?) + end + + it "should fail if the :version method has not been overridden and no :find method is available" do + proc { @terminus.version('yay') }.should raise_error(Puppet::DevError) + end + + it "should use a found instance's version by default" do + name = 'instance' + instance = stub name, :version => 2 + @terminus.expects(:find).with(name).returns(instance) + @terminus.version(name).should == 2 + end - # It shouldn't overwrite our existing one (or, more normally, it shouldn't set - # anything). - @terminus_class.indirection.should equal(@indirection) + it "should return nil as the version if no instance can be found" do + name = 'instance' + @terminus.expects(:find).with(name).returns(nil) + @terminus.version(name).should be_nil + end + + it "should consider an instance fresh if its version is more recent than the version provided" do + name = "yay" + @terminus.expects(:version).with(name).returns(5) + @terminus.has_most_recent?(name, 4).should be_true + end + + it "should consider an instance fresh if its version is equal to the version provided" do + name = "yay" + @terminus.expects(:version).with(name).returns(5) + @terminus.has_most_recent?(name, 5).should be_true + end + + it "should consider an instance not fresh if the provided version is more recent than its version" do + name = "yay" + @terminus.expects(:version).with(name).returns(4) + @terminus.has_most_recent?(name, 5).should be_false + end + + # Times annoyingly can't be compared directly to numbers, and our + # default version is 0. + it "should convert versions to floats when checking for freshness" do + existing = mock 'existing version' + new = mock 'new version' + existing.expects(:to_f).returns(1.0) + new.expects(:to_f).returns(1.0) + name = "yay" + @terminus.expects(:version).with(name).returns(existing) + @terminus.has_most_recent?(name, new) + end end end @@ -205,98 +297,3 @@ describe Puppet::Indirector::Terminus, " when creating terminus class types" do end end -describe Puppet::Indirector::Terminus, " when creating terminus classes" do - include TerminusInstanceTesting - it "should associate the subclass with an indirection based on the subclass constant" do - @terminus.indirection.should equal(@indirection) - end - - it "should set the subclass's type to the abstract terminus name" do - @terminus.terminus_type.should == :abstract - end - - it "should set the subclass's name to the indirection name" do - @terminus.name.should == :term_type - end - - it "should set the subclass's model to the indirection model" do - @indirection.expects(:model).returns :yay - @terminus.model.should == :yay - end -end - -describe Puppet::Indirector::Terminus, " when a terminus instance" do - include TerminusInstanceTesting - - it "should return the class's name as its name" do - @terminus.name.should == :term_type - end - - it "should return the class's indirection as its indirection" do - @terminus.indirection.should equal(@indirection) - end - - it "should set the instances's type to the abstract terminus type's name" do - @terminus.terminus_type.should == :abstract - end - - it "should set the instances's model to the indirection's model" do - @indirection.expects(:model).returns :yay - @terminus.model.should == :yay - end -end - -describe Puppet::Indirector::Terminus, " when managing indirected instances" do - include TerminusInstanceTesting - - it "should support comparing an instance's version with the terminus's version using just the instance's key" do - @terminus.should respond_to(:has_most_recent?) - end - - it "should fail if the :version method has not been overridden and no :find method is available" do - proc { @terminus.version('yay') }.should raise_error(Puppet::DevError) - end - - it "should use a found instance's version by default" do - name = 'instance' - instance = stub name, :version => 2 - @terminus.expects(:find).with(name).returns(instance) - @terminus.version(name).should == 2 - end - - it "should return nil as the version if no instance can be found" do - name = 'instance' - @terminus.expects(:find).with(name).returns(nil) - @terminus.version(name).should be_nil - end - - it "should consider an instance fresh if its version is more recent than the version provided" do - name = "yay" - @terminus.expects(:version).with(name).returns(5) - @terminus.has_most_recent?(name, 4).should be_true - end - - it "should consider an instance fresh if its version is equal to the version provided" do - name = "yay" - @terminus.expects(:version).with(name).returns(5) - @terminus.has_most_recent?(name, 5).should be_true - end - - it "should consider an instance not fresh if the provided version is more recent than its version" do - name = "yay" - @terminus.expects(:version).with(name).returns(4) - @terminus.has_most_recent?(name, 5).should be_false - end - - # Times annoyingly can't be compared directly to numbers, and our - # default version is 0. - it "should convert versions to floats when checking for freshness" do - existing = mock 'existing version' - new = mock 'new version' - existing.expects(:to_f).returns(1.0) - new.expects(:to_f).returns(1.0) - name = "yay" - @terminus.expects(:version).with(name).returns(existing) - @terminus.has_most_recent?(name, new) - end -end diff --git a/spec/unit/indirector/yaml.rb b/spec/unit/indirector/yaml.rb index f217a31b5..2e0b453d7 100755 --- a/spec/unit/indirector/yaml.rb +++ b/spec/unit/indirector/yaml.rb @@ -4,8 +4,8 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/indirector/yaml' -module YamlTesting - def setup +describe Puppet::Indirector::Yaml, " when choosing file location" do + before :each do @indirection = stub 'indirection', :name => :my_yaml, :register_terminus_type => nil Puppet::Indirector::Indirection.stubs(:instance).with(:my_yaml).returns(@indirection) @store_class = Class.new(Puppet::Indirector::Yaml) do @@ -23,82 +23,79 @@ module YamlTesting Puppet.settings.stubs(:use) Puppet.settings.stubs(:value).with(:yamldir).returns(@dir) end -end -describe Puppet::Indirector::Yaml, " when choosing file location" do - include YamlTesting + describe Puppet::Indirector::Yaml, " when choosing file location" do - it "should store all files in a single file root set in the Puppet defaults" do - @store.send(:path, :me).should =~ %r{^#{@dir}} - end + it "should store all files in a single file root set in the Puppet defaults" do + @store.send(:path, :me).should =~ %r{^#{@dir}} + end - it "should use the terminus name for choosing the subdirectory" do - @store.send(:path, :me).should =~ %r{^#{@dir}/my_yaml} - end + it "should use the terminus name for choosing the subdirectory" do + @store.send(:path, :me).should =~ %r{^#{@dir}/my_yaml} + end - it "should use the object's name to determine the file name" do - @store.send(:path, :me).should =~ %r{me.yaml$} + it "should use the object's name to determine the file name" do + @store.send(:path, :me).should =~ %r{me.yaml$} + end end -end -describe Puppet::Indirector::Yaml, " when storing objects as YAML" do - include YamlTesting + describe Puppet::Indirector::Yaml, " when storing objects as YAML" do - it "should only store objects that respond to :name" do - proc { @store.save(Object.new) }.should raise_error(ArgumentError) - end + it "should only store objects that respond to :name" do + proc { @store.save(Object.new) }.should raise_error(ArgumentError) + end - it "should convert Ruby objects to YAML and write them to disk" do - yaml = @subject.to_yaml - file = mock 'file' - path = @store.send(:path, @subject.name) - FileTest.expects(:exist?).with(File.dirname(path)).returns(true) - File.expects(:open).with(path, "w", 0660).yields(file) - file.expects(:print).with(yaml) + it "should convert Ruby objects to YAML and write them to disk" do + yaml = @subject.to_yaml + file = mock 'file' + path = @store.send(:path, @subject.name) + FileTest.expects(:exist?).with(File.dirname(path)).returns(true) + File.expects(:open).with(path, "w", 0660).yields(file) + file.expects(:print).with(yaml) - @store.save(@subject) - end + @store.save(@subject) + end - it "should create the indirection subdirectory if it does not exist" do - yaml = @subject.to_yaml - file = mock 'file' - path = @store.send(:path, @subject.name) - dir = File.dirname(path) - FileTest.expects(:exist?).with(dir).returns(false) - Dir.expects(:mkdir).with(dir) - File.expects(:open).with(path, "w", 0660).yields(file) - file.expects(:print).with(yaml) - - @store.save(@subject) + it "should create the indirection subdirectory if it does not exist" do + yaml = @subject.to_yaml + file = mock 'file' + path = @store.send(:path, @subject.name) + dir = File.dirname(path) + FileTest.expects(:exist?).with(dir).returns(false) + Dir.expects(:mkdir).with(dir) + File.expects(:open).with(path, "w", 0660).yields(file) + file.expects(:print).with(yaml) + + @store.save(@subject) + end end -end -describe Puppet::Indirector::Yaml, " when retrieving YAML" do - include YamlTesting + describe Puppet::Indirector::Yaml, " when retrieving YAML" do - it "should require the name of the object to retrieve" do - proc { @store.find(nil) }.should raise_error(ArgumentError) - end + it "should require the name of the object to retrieve" do + proc { @store.find(nil) }.should raise_error(ArgumentError) + end - it "should read YAML in from disk and convert it to Ruby objects" do - path = @store.send(:path, @subject.name) + it "should read YAML in from disk and convert it to Ruby objects" do + path = @store.send(:path, @subject.name) - yaml = @subject.to_yaml - FileTest.expects(:exist?).with(path).returns(true) - File.expects(:read).with(path).returns(yaml) + yaml = @subject.to_yaml + FileTest.expects(:exist?).with(path).returns(true) + File.expects(:read).with(path).returns(yaml) - @store.find(@subject.name).instance_variable_get("@name").should == :me - end + @store.find(@subject.name).instance_variable_get("@name").should == :me + end - it "should fail coherently when the stored YAML is invalid" do - path = @store.send(:path, @subject.name) + it "should fail coherently when the stored YAML is invalid" do + path = @store.send(:path, @subject.name) - # Something that will fail in yaml - yaml = "--- !ruby/object:Hash" + # Something that will fail in yaml + yaml = "--- !ruby/object:Hash" - FileTest.expects(:exist?).with(path).returns(true) - File.expects(:read).with(path).returns(yaml) + FileTest.expects(:exist?).with(path).returns(true) + File.expects(:read).with(path).returns(yaml) - proc { @store.find(@subject.name) }.should raise_error(Puppet::Error) + proc { @store.find(@subject.name) }.should raise_error(Puppet::Error) + end end -end +end
\ No newline at end of file diff --git a/spec/unit/node/catalog.rb b/spec/unit/node/catalog.rb index aa49909e2..ecbd20487 100755 --- a/spec/unit/node/catalog.rb +++ b/spec/unit/node/catalog.rb @@ -455,6 +455,11 @@ describe Puppet::Node::Catalog, " when functioning as a resource container" do proc { @catalog.alias @two, "one" }.should raise_error(ArgumentError) end + it "should not fail when a resource has duplicate aliases created" do + @catalog.add_resource @one + proc { @catalog.alias @one, "one" }.should_not raise_error + end + it "should remove resource aliases when the target resource is removed" do @catalog.add_resource @one @catalog.alias(@one, "other") @@ -463,13 +468,21 @@ describe Puppet::Node::Catalog, " when functioning as a resource container" do @catalog.resource("me", "other").should be_nil end + it "should add an alias for the namevar when the title and name differ" do + @one.stubs(:name).returns "other" + resource = Puppet::Type.type(:file).create :path => "/something", :title => "other", :content => "blah" + @catalog.add_resource(resource) + @catalog.resource(:file, "other").should equal(resource) + @catalog.resource(:file, "/something").should equal(resource) + end + after do Puppet::Type.allclear end end -module ApplyingCatalogs - def setup +describe Puppet::Node::Catalog do + before :each do @catalog = Puppet::Node::Catalog.new("host") @catalog.retrieval_duration = Time.now @@ -479,114 +492,111 @@ module ApplyingCatalogs @transaction.stubs(:cleanup) @transaction.stubs(:addtimes) end -end -describe Puppet::Node::Catalog, " when applying" do - include ApplyingCatalogs + describe Puppet::Node::Catalog, " when applying" do - it "should create and evaluate a transaction" do - @transaction.expects(:evaluate) - @catalog.apply - end + it "should create and evaluate a transaction" do + @transaction.expects(:evaluate) + @catalog.apply + end - it "should provide the catalog time to the transaction" do - @transaction.expects(:addtimes).with do |arg| - arg[:config_retrieval].should be_instance_of(Time) - true + it "should provide the catalog time to the transaction" do + @transaction.expects(:addtimes).with do |arg| + arg[:config_retrieval].should be_instance_of(Time) + true + end + @catalog.apply end - @catalog.apply - end - it "should clean up the transaction" do - @transaction.expects :cleanup - @catalog.apply - end + it "should clean up the transaction" do + @transaction.expects :cleanup + @catalog.apply + end - it "should return the transaction" do - @catalog.apply.should equal(@transaction) - end + it "should return the transaction" do + @catalog.apply.should equal(@transaction) + end - it "should yield the transaction if a block is provided" do - @catalog.apply do |trans| - trans.should equal(@transaction) + it "should yield the transaction if a block is provided" do + @catalog.apply do |trans| + trans.should equal(@transaction) + end end - end - it "should default to not being a host catalog" do - @catalog.host_config.should be_nil - end + it "should default to not being a host catalog" do + @catalog.host_config.should be_nil + end - it "should pass supplied tags on to the transaction" do - @transaction.expects(:tags=).with(%w{one two}) - @catalog.apply(:tags => %w{one two}) - end + it "should pass supplied tags on to the transaction" do + @transaction.expects(:tags=).with(%w{one two}) + @catalog.apply(:tags => %w{one two}) + end - it "should set ignoreschedules on the transaction if specified in apply()" do - @transaction.expects(:ignoreschedules=).with(true) - @catalog.apply(:ignoreschedules => true) + it "should set ignoreschedules on the transaction if specified in apply()" do + @transaction.expects(:ignoreschedules=).with(true) + @catalog.apply(:ignoreschedules => true) + end end -end -describe Puppet::Node::Catalog, " when applying host catalogs" do - include ApplyingCatalogs + describe Puppet::Node::Catalog, " when applying host catalogs" do - # super() doesn't work in the setup method for some reason - before do - @catalog.host_config = true - end + # super() doesn't work in the setup method for some reason + before do + @catalog.host_config = true + end - it "should send a report if reporting is enabled" do - Puppet[:report] = true - @transaction.expects :send_report - @transaction.stubs :any_failed? => false - @catalog.apply - end + it "should send a report if reporting is enabled" do + Puppet[:report] = true + @transaction.expects :send_report + @transaction.stubs :any_failed? => false + @catalog.apply + end - it "should send a report if report summaries are enabled" do - Puppet[:summarize] = true - @transaction.expects :send_report - @transaction.stubs :any_failed? => false - @catalog.apply - end + it "should send a report if report summaries are enabled" do + Puppet[:summarize] = true + @transaction.expects :send_report + @transaction.stubs :any_failed? => false + @catalog.apply + end - it "should initialize the state database before applying a catalog" do - Puppet::Util::Storage.expects(:load) + it "should initialize the state database before applying a catalog" do + Puppet::Util::Storage.expects(:load) - # Short-circuit the apply, so we know we're loading before the transaction - Puppet::Transaction.expects(:new).raises ArgumentError - proc { @catalog.apply }.should raise_error(ArgumentError) - end + # Short-circuit the apply, so we know we're loading before the transaction + Puppet::Transaction.expects(:new).raises ArgumentError + proc { @catalog.apply }.should raise_error(ArgumentError) + end - it "should sync the state database after applying" do - Puppet::Util::Storage.expects(:store) - @transaction.stubs :any_failed? => false - @catalog.apply - end + it "should sync the state database after applying" do + Puppet::Util::Storage.expects(:store) + @transaction.stubs :any_failed? => false + @catalog.apply + end - after { Puppet.settings.clear } -end + after { Puppet.settings.clear } + end -describe Puppet::Node::Catalog, " when applying non-host catalogs" do - include ApplyingCatalogs + describe Puppet::Node::Catalog, " when applying non-host catalogs" do - before do - @catalog.host_config = false - end + before do + @catalog.host_config = false + end - it "should never send reports" do - Puppet[:report] = true - Puppet[:summarize] = true - @transaction.expects(:send_report).never - @catalog.apply - end + it "should never send reports" do + Puppet[:report] = true + Puppet[:summarize] = true + @transaction.expects(:send_report).never + @catalog.apply + end - it "should never modify the state database" do - Puppet::Util::Storage.expects(:load).never - Puppet::Util::Storage.expects(:store).never - @catalog.apply - end + it "should never modify the state database" do + Puppet::Util::Storage.expects(:load).never + Puppet::Util::Storage.expects(:store).never + @catalog.apply + end - after { Puppet.settings.clear } + after { Puppet.settings.clear } + end end describe Puppet::Node::Catalog, " when creating a relationship graph" do diff --git a/spec/unit/parser/ast/hostclass.rb b/spec/unit/parser/ast/hostclass.rb index 422861857..a53c3b092 100755 --- a/spec/unit/parser/ast/hostclass.rb +++ b/spec/unit/parser/ast/hostclass.rb @@ -2,8 +2,8 @@ require File.dirname(__FILE__) + '/../../../spec_helper' -module HostClassTesting - def setup +describe Puppet::Parser::AST::HostClass do + before :each do @node = Puppet::Node.new "testnode" @parser = Puppet::Parser::Parser.new :environment => "development" @scope_resource = stub 'scope_resource', :builtin? => true @@ -11,121 +11,119 @@ module HostClassTesting @scope = @compiler.topscope end -end -describe Puppet::Parser::AST::HostClass, "when evaluating" do - include HostClassTesting + describe Puppet::Parser::AST::HostClass, "when evaluating" do - before do - @top = @parser.newclass "top" - @middle = @parser.newclass "middle", :parent => "top" - end + before do + @top = @parser.newclass "top" + @middle = @parser.newclass "middle", :parent => "top" + end - it "should create a resource that references itself" do - @top.evaluate(@scope) + it "should create a resource that references itself" do + @top.evaluate(@scope) - @compiler.catalog.resource(:class, "top").should be_instance_of(Puppet::Parser::Resource) - end + @compiler.catalog.resource(:class, "top").should be_instance_of(Puppet::Parser::Resource) + end - it "should evaluate the parent class if one exists" do - @middle.evaluate(@scope) + it "should evaluate the parent class if one exists" do + @middle.evaluate(@scope) - @compiler.catalog.resource(:class, "top").should be_instance_of(Puppet::Parser::Resource) - end + @compiler.catalog.resource(:class, "top").should be_instance_of(Puppet::Parser::Resource) + end - it "should fail to evaluate if a parent class is defined but cannot be found" do - othertop = @parser.newclass "something", :parent => "yay" - lambda { othertop.evaluate(@scope) }.should raise_error(Puppet::ParseError) - end + it "should fail to evaluate if a parent class is defined but cannot be found" do + othertop = @parser.newclass "something", :parent => "yay" + lambda { othertop.evaluate(@scope) }.should raise_error(Puppet::ParseError) + end - it "should not create a new resource if one already exists" do - @compiler.catalog.expects(:resource).with(:class, "top").returns("something") - @compiler.catalog.expects(:add_resource).never - @top.evaluate(@scope) - end + it "should not create a new resource if one already exists" do + @compiler.catalog.expects(:resource).with(:class, "top").returns("something") + @compiler.catalog.expects(:add_resource).never + @top.evaluate(@scope) + end - it "should not create a new parent resource if one already exists and it has a parent class" do - @top.evaluate(@scope) + it "should not create a new parent resource if one already exists and it has a parent class" do + @top.evaluate(@scope) - top_resource = @compiler.catalog.resource(:class, "top") + top_resource = @compiler.catalog.resource(:class, "top") - @middle.evaluate(@scope) + @middle.evaluate(@scope) - @compiler.catalog.resource(:class, "top").should equal(top_resource) - end + @compiler.catalog.resource(:class, "top").should equal(top_resource) + end - # #795 - tag before evaluation. - it "should tag the catalog with the resource tags when it is evaluated" do - @middle.evaluate(@scope) + # #795 - tag before evaluation. + it "should tag the catalog with the resource tags when it is evaluated" do + @middle.evaluate(@scope) - @compiler.catalog.should be_tagged("middle") - end + @compiler.catalog.should be_tagged("middle") + end - it "should tag the catalog with the parent class tags when it is evaluated" do - @middle.evaluate(@scope) + it "should tag the catalog with the parent class tags when it is evaluated" do + @middle.evaluate(@scope) - @compiler.catalog.should be_tagged("top") + @compiler.catalog.should be_tagged("top") + end end -end -describe Puppet::Parser::AST::HostClass, "when evaluating code" do - include HostClassTesting + describe Puppet::Parser::AST::HostClass, "when evaluating code" do - before do - @top_resource = stub "top_resource" - @top = @parser.newclass "top", :code => @top_resource + before do + @top_resource = stub "top_resource" + @top = @parser.newclass "top", :code => @top_resource - @middle_resource = stub "middle_resource" - @middle = @parser.newclass "top::middle", :parent => "top", :code => @middle_resource - end + @middle_resource = stub "middle_resource" + @middle = @parser.newclass "top::middle", :parent => "top", :code => @middle_resource + end - it "should set its namespace to its fully qualified name" do - @middle.namespace.should == "top::middle" - end + it "should set its namespace to its fully qualified name" do + @middle.namespace.should == "top::middle" + end - it "should evaluate the code referred to by the class" do - @top_resource.expects(:safeevaluate) + it "should evaluate the code referred to by the class" do + @top_resource.expects(:safeevaluate) - resource = @top.evaluate(@scope) + resource = @top.evaluate(@scope) - @top.evaluate_code(resource) - end + @top.evaluate_code(resource) + end - it "should evaluate the parent class's code if it has a parent" do - @top_resource.expects(:safeevaluate) - @middle_resource.expects(:safeevaluate) + it "should evaluate the parent class's code if it has a parent" do + @top_resource.expects(:safeevaluate) + @middle_resource.expects(:safeevaluate) - resource = @middle.evaluate(@scope) + resource = @middle.evaluate(@scope) - @middle.evaluate_code(resource) - end + @middle.evaluate_code(resource) + end - it "should not evaluate the parent class's code if the parent has already been evaluated" do - @top_resource.stubs(:safeevaluate) - resource = @top.evaluate(@scope) - @top.evaluate_code(resource) + it "should not evaluate the parent class's code if the parent has already been evaluated" do + @top_resource.stubs(:safeevaluate) + resource = @top.evaluate(@scope) + @top.evaluate_code(resource) - @top_resource.expects(:safeevaluate).never - @middle_resource.stubs(:safeevaluate) - resource = @middle.evaluate(@scope) - @middle.evaluate_code(resource) - end + @top_resource.expects(:safeevaluate).never + @middle_resource.stubs(:safeevaluate) + resource = @middle.evaluate(@scope) + @middle.evaluate_code(resource) + end - it "should use the parent class's scope as its parent scope" do - @top_resource.stubs(:safeevaluate) - @middle_resource.stubs(:safeevaluate) - resource = @middle.evaluate(@scope) - @middle.evaluate_code(resource) + it "should use the parent class's scope as its parent scope" do + @top_resource.stubs(:safeevaluate) + @middle_resource.stubs(:safeevaluate) + resource = @middle.evaluate(@scope) + @middle.evaluate_code(resource) - @compiler.class_scope(@middle).parent.should equal(@compiler.class_scope(@top)) - end + @compiler.class_scope(@middle).parent.should equal(@compiler.class_scope(@top)) + end - it "should add the parent class's namespace to its namespace search path" do - @top_resource.stubs(:safeevaluate) - @middle_resource.stubs(:safeevaluate) - resource = @middle.evaluate(@scope) - @middle.evaluate_code(resource) + it "should add the parent class's namespace to its namespace search path" do + @top_resource.stubs(:safeevaluate) + @middle_resource.stubs(:safeevaluate) + resource = @middle.evaluate(@scope) + @middle.evaluate_code(resource) - @compiler.class_scope(@middle).namespaces.should be_include(@top.namespace) + @compiler.class_scope(@middle).namespaces.should be_include(@top.namespace) + end end -end +end
\ No newline at end of file diff --git a/spec/unit/parser/ast/node.rb b/spec/unit/parser/ast/node.rb index 340630194..757934415 100755 --- a/spec/unit/parser/ast/node.rb +++ b/spec/unit/parser/ast/node.rb @@ -2,8 +2,8 @@ require File.dirname(__FILE__) + '/../../../spec_helper' -module ASTNodeTesting - def setup +describe Puppet::Parser::AST::Node do + before :each do @node = Puppet::Node.new "testnode" @parser = Puppet::Parser::Parser.new :environment => "development" @scope_resource = stub 'scope_resource', :builtin? => true @@ -11,117 +11,115 @@ module ASTNodeTesting @scope = @compiler.topscope end -end -describe Puppet::Parser::AST::Node, "when evaluating" do - include ASTNodeTesting + describe Puppet::Parser::AST::Node, "when evaluating" do - before do - @top = @parser.newnode("top").shift - @middle = @parser.newnode("middle", :parent => "top").shift - end + before do + @top = @parser.newnode("top").shift + @middle = @parser.newnode("middle", :parent => "top").shift + end - it "should create a resource that references itself" do - @top.evaluate(@scope) + it "should create a resource that references itself" do + @top.evaluate(@scope) - @compiler.catalog.resource(:node, "top").should be_an_instance_of(Puppet::Parser::Resource) - end + @compiler.catalog.resource(:node, "top").should be_an_instance_of(Puppet::Parser::Resource) + end - it "should evaluate the parent class if one exists" do - @middle.evaluate(@scope) + it "should evaluate the parent class if one exists" do + @middle.evaluate(@scope) - @compiler.catalog.resource(:node, "top").should be_an_instance_of(Puppet::Parser::Resource) - end + @compiler.catalog.resource(:node, "top").should be_an_instance_of(Puppet::Parser::Resource) + end - it "should fail to evaluate if a parent class is defined but cannot be found" do - othertop = @parser.newnode("something", :parent => "yay").shift - lambda { othertop.evaluate(@scope) }.should raise_error(Puppet::ParseError) - end + it "should fail to evaluate if a parent class is defined but cannot be found" do + othertop = @parser.newnode("something", :parent => "yay").shift + lambda { othertop.evaluate(@scope) }.should raise_error(Puppet::ParseError) + end - it "should not create a new resource if one already exists" do - @compiler.catalog.expects(:resource).with(:node, "top").returns("something") - @compiler.catalog.expects(:add_resource).never - @top.evaluate(@scope) - end + it "should not create a new resource if one already exists" do + @compiler.catalog.expects(:resource).with(:node, "top").returns("something") + @compiler.catalog.expects(:add_resource).never + @top.evaluate(@scope) + end - it "should not create a new parent resource if one already exists and it has a parent class" do - @top.evaluate(@scope) + it "should not create a new parent resource if one already exists and it has a parent class" do + @top.evaluate(@scope) - top_resource = @compiler.catalog.resource(:node, "top") + top_resource = @compiler.catalog.resource(:node, "top") - @middle.evaluate(@scope) + @middle.evaluate(@scope) - @compiler.catalog.resource(:node, "top").should equal(top_resource) - end + @compiler.catalog.resource(:node, "top").should equal(top_resource) + end - # #795 - tag before evaluation. - it "should tag the catalog with the resource tags when it is evaluated" do - @middle.evaluate(@scope) + # #795 - tag before evaluation. + it "should tag the catalog with the resource tags when it is evaluated" do + @middle.evaluate(@scope) - @compiler.catalog.should be_tagged("middle") - end + @compiler.catalog.should be_tagged("middle") + end - it "should tag the catalog with the parent class tags when it is evaluated" do - @middle.evaluate(@scope) + it "should tag the catalog with the parent class tags when it is evaluated" do + @middle.evaluate(@scope) - @compiler.catalog.should be_tagged("top") + @compiler.catalog.should be_tagged("top") + end end -end -describe Puppet::Parser::AST::Node, "when evaluating code" do - include ASTNodeTesting + describe Puppet::Parser::AST::Node, "when evaluating code" do - before do - @top_resource = stub "top_resource" - @top = @parser.newnode("top", :code => @top_resource).shift + before do + @top_resource = stub "top_resource" + @top = @parser.newnode("top", :code => @top_resource).shift - @middle_resource = stub "middle_resource" - @middle = @parser.newnode("middle", :parent => "top", :code => @middle_resource).shift - end + @middle_resource = stub "middle_resource" + @middle = @parser.newnode("middle", :parent => "top", :code => @middle_resource).shift + end - it "should evaluate the code referred to by the class" do - @top_resource.expects(:safeevaluate) + it "should evaluate the code referred to by the class" do + @top_resource.expects(:safeevaluate) - resource = @top.evaluate(@scope) + resource = @top.evaluate(@scope) - @top.evaluate_code(resource) - end + @top.evaluate_code(resource) + end - it "should evaluate the parent class's code if it has a parent" do - @top_resource.expects(:safeevaluate) - @middle_resource.expects(:safeevaluate) + it "should evaluate the parent class's code if it has a parent" do + @top_resource.expects(:safeevaluate) + @middle_resource.expects(:safeevaluate) - resource = @middle.evaluate(@scope) + resource = @middle.evaluate(@scope) - @middle.evaluate_code(resource) - end + @middle.evaluate_code(resource) + end - it "should not evaluate the parent class's code if the parent has already been evaluated" do - @top_resource.stubs(:safeevaluate) - resource = @top.evaluate(@scope) - @top.evaluate_code(resource) + it "should not evaluate the parent class's code if the parent has already been evaluated" do + @top_resource.stubs(:safeevaluate) + resource = @top.evaluate(@scope) + @top.evaluate_code(resource) - @top_resource.expects(:safeevaluate).never - @middle_resource.stubs(:safeevaluate) - resource = @middle.evaluate(@scope) - @middle.evaluate_code(resource) - end + @top_resource.expects(:safeevaluate).never + @middle_resource.stubs(:safeevaluate) + resource = @middle.evaluate(@scope) + @middle.evaluate_code(resource) + end - it "should use the parent class's scope as its parent scope" do - @top_resource.stubs(:safeevaluate) - @middle_resource.stubs(:safeevaluate) - resource = @middle.evaluate(@scope) - @middle.evaluate_code(resource) + it "should use the parent class's scope as its parent scope" do + @top_resource.stubs(:safeevaluate) + @middle_resource.stubs(:safeevaluate) + resource = @middle.evaluate(@scope) + @middle.evaluate_code(resource) - @compiler.class_scope(@middle).parent.should equal(@compiler.class_scope(@top)) - end + @compiler.class_scope(@middle).parent.should equal(@compiler.class_scope(@top)) + end - it "should add the parent class's namespace to its namespace search path" do - @top_resource.stubs(:safeevaluate) - @middle_resource.stubs(:safeevaluate) - resource = @middle.evaluate(@scope) - @middle.evaluate_code(resource) + it "should add the parent class's namespace to its namespace search path" do + @top_resource.stubs(:safeevaluate) + @middle_resource.stubs(:safeevaluate) + resource = @middle.evaluate(@scope) + @middle.evaluate_code(resource) - @compiler.class_scope(@middle).namespaces.should be_include(@top.namespace) + @compiler.class_scope(@middle).namespaces.should be_include(@top.namespace) + end end -end +end
\ No newline at end of file diff --git a/spec/unit/parser/compiler.rb b/spec/unit/parser/compiler.rb index 6b821977d..9980f2c6a 100755 --- a/spec/unit/parser/compiler.rb +++ b/spec/unit/parser/compiler.rb @@ -2,8 +2,8 @@ require File.dirname(__FILE__) + '/../../spec_helper' -module CompilerTesting - def setup +describe Puppet::Parser::Compiler do + before :each do @node = Puppet::Node.new "testnode" @parser = Puppet::Parser::Parser.new :environment => "development" @@ -11,534 +11,522 @@ module CompilerTesting @scope = stub 'scope', :resource => @scope_resource, :source => mock("source") @compiler = Puppet::Parser::Compiler.new(@node, @parser) end -end - -describe Puppet::Parser::Compiler do - include CompilerTesting - - it "should be able to store references to class scopes" do - lambda { @compiler.class_set "myname", "myscope" }.should_not raise_error - end - - it "should be able to retrieve class scopes by name" do - @compiler.class_set "myname", "myscope" - @compiler.class_scope("myname").should == "myscope" - end - - it "should be able to retrieve class scopes by object" do - klass = mock 'ast_class' - klass.expects(:classname).returns("myname") - @compiler.class_set "myname", "myscope" - @compiler.class_scope(klass).should == "myscope" - end - - it "should be able to return a class list containing all set classes" do - @compiler.class_set "", "empty" - @compiler.class_set "one", "yep" - @compiler.class_set "two", "nope" - - @compiler.classlist.sort.should == %w{one two}.sort - end -end -describe Puppet::Parser::Compiler, " when initializing" do - include CompilerTesting - - it "should set its node attribute" do - @compiler.node.should equal(@node) - end - - it "should set its parser attribute" do - @compiler.parser.should equal(@parser) - end + describe Puppet::Parser::Compiler do - it "should detect when ast nodes are absent" do - @compiler.ast_nodes?.should be_false - end + it "should be able to store references to class scopes" do + lambda { @compiler.class_set "myname", "myscope" }.should_not raise_error + end - it "should detect when ast nodes are present" do - @parser.nodes["testing"] = "yay" - @compiler.ast_nodes?.should be_true - end -end + it "should be able to retrieve class scopes by name" do + @compiler.class_set "myname", "myscope" + @compiler.class_scope("myname").should == "myscope" + end -describe Puppet::Parser::Compiler, "when managing scopes" do - include CompilerTesting + it "should be able to retrieve class scopes by object" do + klass = mock 'ast_class' + klass.expects(:classname).returns("myname") + @compiler.class_set "myname", "myscope" + @compiler.class_scope(klass).should == "myscope" + end - it "should create a top scope" do - @compiler.topscope.should be_instance_of(Puppet::Parser::Scope) - end + it "should be able to return a class list containing all set classes" do + @compiler.class_set "", "empty" + @compiler.class_set "one", "yep" + @compiler.class_set "two", "nope" - it "should be able to create new scopes" do - @compiler.newscope(@compiler.topscope).should be_instance_of(Puppet::Parser::Scope) + @compiler.classlist.sort.should == %w{one two}.sort + end end - it "should correctly set the level of newly created scopes" do - @compiler.newscope(@compiler.topscope, :level => 5).level.should == 5 - end + describe Puppet::Parser::Compiler, " when initializing" do - it "should set the parent scope of the new scope to be the passed-in parent" do - scope = mock 'scope' - newscope = @compiler.newscope(scope) + it "should set its node attribute" do + @compiler.node.should equal(@node) + end - @compiler.parent(newscope).should equal(scope) - end -end + it "should set its parser attribute" do + @compiler.parser.should equal(@parser) + end -describe Puppet::Parser::Compiler, " when compiling" do - include CompilerTesting + it "should detect when ast nodes are absent" do + @compiler.ast_nodes?.should be_false + end - def compile_methods - [:set_node_parameters, :evaluate_main, :evaluate_ast_node, :evaluate_node_classes, :evaluate_generators, :fail_on_unevaluated, - :finish, :store, :extract] + it "should detect when ast nodes are present" do + @parser.nodes["testing"] = "yay" + @compiler.ast_nodes?.should be_true + end end - # Stub all of the main compile methods except the ones we're specifically interested in. - def compile_stub(*except) - (compile_methods - except).each { |m| @compiler.stubs(m) } - end + describe Puppet::Parser::Compiler, "when managing scopes" do - it "should set node parameters as variables in the top scope" do - params = {"a" => "b", "c" => "d"} - @node.stubs(:parameters).returns(params) - compile_stub(:set_node_parameters) - @compiler.compile - @compiler.topscope.lookupvar("a").should == "b" - @compiler.topscope.lookupvar("c").should == "d" - end + it "should create a top scope" do + @compiler.topscope.should be_instance_of(Puppet::Parser::Scope) + end - it "should evaluate any existing classes named in the node" do - classes = %w{one two three four} - main = stub 'main' - one = stub 'one', :classname => "one" - three = stub 'three', :classname => "three" - @node.stubs(:name).returns("whatever") - @node.stubs(:classes).returns(classes) + it "should be able to create new scopes" do + @compiler.newscope(@compiler.topscope).should be_instance_of(Puppet::Parser::Scope) + end - @compiler.expects(:evaluate_classes).with(classes, @compiler.topscope) - @compiler.class.publicize_methods(:evaluate_node_classes) { @compiler.evaluate_node_classes } - end + it "should correctly set the level of newly created scopes" do + @compiler.newscope(@compiler.topscope, :level => 5).level.should == 5 + end - it "should enable ast_nodes if the parser has any nodes" do - @parser.expects(:nodes).returns(:one => :yay) - @compiler.ast_nodes?.should be_true - end + it "should set the parent scope of the new scope to be the passed-in parent" do + scope = mock 'scope' + newscope = @compiler.newscope(scope) - it "should disable ast_nodes if the parser has no nodes" do - @parser.expects(:nodes).returns({}) - @compiler.ast_nodes?.should be_false + @compiler.parent(newscope).should equal(scope) + end end - it "should evaluate the main class if it exists" do - compile_stub(:evaluate_main) - main_class = mock 'main_class' - main_class.expects(:evaluate_code).with { |r| r.is_a?(Puppet::Parser::Resource) } - @compiler.topscope.expects(:source=).with(main_class) - @parser.stubs(:findclass).with("", "").returns(main_class) - - @compiler.compile - end + describe Puppet::Parser::Compiler, " when compiling" do - it "should evaluate any node classes" do - @node.stubs(:classes).returns(%w{one two three four}) - @compiler.expects(:evaluate_classes).with(%w{one two three four}, @compiler.topscope) - @compiler.send(:evaluate_node_classes) - end + def compile_methods + [:set_node_parameters, :evaluate_main, :evaluate_ast_node, :evaluate_node_classes, :evaluate_generators, :fail_on_unevaluated, + :finish, :store, :extract] + end - it "should evaluate all added collections" do - colls = [] - # And when the collections fail to evaluate. - colls << mock("coll1-false") - colls << mock("coll2-false") - colls.each { |c| c.expects(:evaluate).returns(false) } + # Stub all of the main compile methods except the ones we're specifically interested in. + def compile_stub(*except) + (compile_methods - except).each { |m| @compiler.stubs(m) } + end - @compiler.add_collection(colls[0]) - @compiler.add_collection(colls[1]) + it "should set node parameters as variables in the top scope" do + params = {"a" => "b", "c" => "d"} + @node.stubs(:parameters).returns(params) + compile_stub(:set_node_parameters) + @compiler.compile + @compiler.topscope.lookupvar("a").should == "b" + @compiler.topscope.lookupvar("c").should == "d" + end - compile_stub(:evaluate_generators) - @compiler.compile - end + it "should evaluate any existing classes named in the node" do + classes = %w{one two three four} + main = stub 'main' + one = stub 'one', :classname => "one" + three = stub 'three', :classname => "three" + @node.stubs(:name).returns("whatever") + @node.stubs(:classes).returns(classes) - it "should ignore builtin resources" do - resource = stub 'builtin', :ref => "File[testing]", :builtin? => true + @compiler.expects(:evaluate_classes).with(classes, @compiler.topscope) + @compiler.class.publicize_methods(:evaluate_node_classes) { @compiler.evaluate_node_classes } + end + + it "should enable ast_nodes if the parser has any nodes" do + @parser.expects(:nodes).returns(:one => :yay) + @compiler.ast_nodes?.should be_true + end + + it "should disable ast_nodes if the parser has no nodes" do + @parser.expects(:nodes).returns({}) + @compiler.ast_nodes?.should be_false + end + + it "should evaluate the main class if it exists" do + compile_stub(:evaluate_main) + main_class = mock 'main_class' + main_class.expects(:evaluate_code).with { |r| r.is_a?(Puppet::Parser::Resource) } + @compiler.topscope.expects(:source=).with(main_class) + @parser.stubs(:findclass).with("", "").returns(main_class) + + @compiler.compile + end - @compiler.add_resource(@scope, resource) - resource.expects(:evaluate).never + it "should evaluate any node classes" do + @node.stubs(:classes).returns(%w{one two three four}) + @compiler.expects(:evaluate_classes).with(%w{one two three four}, @compiler.topscope) + @compiler.send(:evaluate_node_classes) + end + + it "should evaluate all added collections" do + colls = [] + # And when the collections fail to evaluate. + colls << mock("coll1-false") + colls << mock("coll2-false") + colls.each { |c| c.expects(:evaluate).returns(false) } + + @compiler.add_collection(colls[0]) + @compiler.add_collection(colls[1]) + + compile_stub(:evaluate_generators) + @compiler.compile + end + + it "should ignore builtin resources" do + resource = stub 'builtin', :ref => "File[testing]", :builtin? => true + + @compiler.add_resource(@scope, resource) + resource.expects(:evaluate).never - @compiler.compile - end + @compiler.compile + end - it "should evaluate unevaluated resources" do - resource = stub 'notevaluated', :ref => "File[testing]", :builtin? => false, :evaluated? => false, :virtual? => false - @compiler.add_resource(@scope, resource) + it "should evaluate unevaluated resources" do + resource = stub 'notevaluated', :ref => "File[testing]", :builtin? => false, :evaluated? => false, :virtual? => false + @compiler.add_resource(@scope, resource) - # We have to now mark the resource as evaluated - resource.expects(:evaluate).with { |*whatever| resource.stubs(:evaluated?).returns true } + # We have to now mark the resource as evaluated + resource.expects(:evaluate).with { |*whatever| resource.stubs(:evaluated?).returns true } - @compiler.compile - end + @compiler.compile + end - it "should not evaluate already-evaluated resources" do - resource = stub 'already_evaluated', :ref => "File[testing]", :builtin? => false, :evaluated? => true, :virtual? => false - @compiler.add_resource(@scope, resource) - resource.expects(:evaluate).never + it "should not evaluate already-evaluated resources" do + resource = stub 'already_evaluated', :ref => "File[testing]", :builtin? => false, :evaluated? => true, :virtual? => false + @compiler.add_resource(@scope, resource) + resource.expects(:evaluate).never - @compiler.compile - end + @compiler.compile + end - it "should evaluate unevaluated resources created by evaluating other resources" do - resource = stub 'notevaluated', :ref => "File[testing]", :builtin? => false, :evaluated? => false, :virtual? => false - @compiler.add_resource(@scope, resource) + it "should evaluate unevaluated resources created by evaluating other resources" do + resource = stub 'notevaluated', :ref => "File[testing]", :builtin? => false, :evaluated? => false, :virtual? => false + @compiler.add_resource(@scope, resource) - resource2 = stub 'created', :ref => "File[other]", :builtin? => false, :evaluated? => false, :virtual? => false + resource2 = stub 'created', :ref => "File[other]", :builtin? => false, :evaluated? => false, :virtual? => false - # We have to now mark the resource as evaluated - resource.expects(:evaluate).with { |*whatever| resource.stubs(:evaluated?).returns(true); @compiler.add_resource(@scope, resource2) } - resource2.expects(:evaluate).with { |*whatever| resource2.stubs(:evaluated?).returns(true) } + # We have to now mark the resource as evaluated + resource.expects(:evaluate).with { |*whatever| resource.stubs(:evaluated?).returns(true); @compiler.add_resource(@scope, resource2) } + resource2.expects(:evaluate).with { |*whatever| resource2.stubs(:evaluated?).returns(true) } - @compiler.compile - end + @compiler.compile + end - it "should call finish() on all resources" do - # Add a resource that does respond to :finish - resource = Puppet::Parser::Resource.new :scope => @scope, :type => "file", :title => "finish" - resource.expects(:finish) + it "should call finish() on all resources" do + # Add a resource that does respond to :finish + resource = Puppet::Parser::Resource.new :scope => @scope, :type => "file", :title => "finish" + resource.expects(:finish) - @compiler.add_resource(@scope, resource) + @compiler.add_resource(@scope, resource) - # And one that does not - dnf = stub "dnf", :ref => "File[dnf]" + # And one that does not + dnf = stub "dnf", :ref => "File[dnf]" - @compiler.add_resource(@scope, dnf) + @compiler.add_resource(@scope, dnf) - @compiler.send(:finish) - end + @compiler.send(:finish) + end - it "should add resources that do not conflict with existing resources" do - resource = stub "noconflict", :ref => "File[yay]" - @compiler.add_resource(@scope, resource) + it "should add resources that do not conflict with existing resources" do + resource = stub "noconflict", :ref => "File[yay]" + @compiler.add_resource(@scope, resource) - @compiler.catalog.should be_vertex(resource) - end + @compiler.catalog.should be_vertex(resource) + end - it "should fail to add resources that conflict with existing resources" do - type = stub 'faketype', :isomorphic? => true, :name => "mytype" - Puppet::Type.stubs(:type).with("mytype").returns(type) + it "should fail to add resources that conflict with existing resources" do + type = stub 'faketype', :isomorphic? => true, :name => "mytype" + Puppet::Type.stubs(:type).with("mytype").returns(type) - resource1 = stub "iso1conflict", :ref => "Mytype[yay]", :type => "mytype", :file => "eh", :line => 0 - resource2 = stub "iso2conflict", :ref => "Mytype[yay]", :type => "mytype", :file => "eh", :line => 0 + resource1 = stub "iso1conflict", :ref => "Mytype[yay]", :type => "mytype", :file => "eh", :line => 0 + resource2 = stub "iso2conflict", :ref => "Mytype[yay]", :type => "mytype", :file => "eh", :line => 0 - @compiler.add_resource(@scope, resource1) - lambda { @compiler.add_resource(@scope, resource2) }.should raise_error(ArgumentError) - end + @compiler.add_resource(@scope, resource1) + lambda { @compiler.add_resource(@scope, resource2) }.should raise_error(ArgumentError) + end - it "should have a method for looking up resources" do - resource = stub 'resource', :ref => "Yay[foo]" - @compiler.add_resource(@scope, resource) - @compiler.findresource("Yay[foo]").should equal(resource) - end + it "should have a method for looking up resources" do + resource = stub 'resource', :ref => "Yay[foo]" + @compiler.add_resource(@scope, resource) + @compiler.findresource("Yay[foo]").should equal(resource) + end - it "should be able to look resources up by type and title" do - resource = stub 'resource', :ref => "Yay[foo]" - @compiler.add_resource(@scope, resource) - @compiler.findresource("Yay", "foo").should equal(resource) - end + it "should be able to look resources up by type and title" do + resource = stub 'resource', :ref => "Yay[foo]" + @compiler.add_resource(@scope, resource) + @compiler.findresource("Yay", "foo").should equal(resource) + end - it "should not evaluate virtual defined resources" do - resource = stub 'notevaluated', :ref => "File[testing]", :builtin? => false, :evaluated? => false, :virtual? => true - @compiler.add_resource(@scope, resource) + it "should not evaluate virtual defined resources" do + resource = stub 'notevaluated', :ref => "File[testing]", :builtin? => false, :evaluated? => false, :virtual? => true + @compiler.add_resource(@scope, resource) - resource.expects(:evaluate).never + resource.expects(:evaluate).never - @compiler.compile + @compiler.compile + end end -end -describe Puppet::Parser::Compiler, " when evaluating collections" do - include CompilerTesting + describe Puppet::Parser::Compiler, " when evaluating collections" do - it "should evaluate each collection" do - 2.times { |i| - coll = mock 'coll%s' % i - @compiler.add_collection(coll) + it "should evaluate each collection" do + 2.times { |i| + coll = mock 'coll%s' % i + @compiler.add_collection(coll) - # This is the hard part -- we have to emulate the fact that - # collections delete themselves if they are done evaluating. - coll.expects(:evaluate).with do - @compiler.delete_collection(coll) - end - } - - @compiler.class.publicize_methods(:evaluate_collections) { @compiler.evaluate_collections } - end + # This is the hard part -- we have to emulate the fact that + # collections delete themselves if they are done evaluating. + coll.expects(:evaluate).with do + @compiler.delete_collection(coll) + end + } - it "should not fail when there are unevaluated resource collections that do not refer to specific resources" do - coll = stub 'coll', :evaluate => false - coll.expects(:resources).returns(nil) + @compiler.class.publicize_methods(:evaluate_collections) { @compiler.evaluate_collections } + end - @compiler.add_collection(coll) + it "should not fail when there are unevaluated resource collections that do not refer to specific resources" do + coll = stub 'coll', :evaluate => false + coll.expects(:resources).returns(nil) - lambda { @compiler.compile }.should_not raise_error - end + @compiler.add_collection(coll) - it "should fail when there are unevaluated resource collections that refer to a specific resource" do - coll = stub 'coll', :evaluate => false - coll.expects(:resources).returns(:something) + lambda { @compiler.compile }.should_not raise_error + end - @compiler.add_collection(coll) + it "should fail when there are unevaluated resource collections that refer to a specific resource" do + coll = stub 'coll', :evaluate => false + coll.expects(:resources).returns(:something) - lambda { @compiler.compile }.should raise_error(Puppet::ParseError) - end + @compiler.add_collection(coll) - it "should fail when there are unevaluated resource collections that refer to multiple specific resources" do - coll = stub 'coll', :evaluate => false - coll.expects(:resources).returns([:one, :two]) + lambda { @compiler.compile }.should raise_error(Puppet::ParseError) + end - @compiler.add_collection(coll) + it "should fail when there are unevaluated resource collections that refer to multiple specific resources" do + coll = stub 'coll', :evaluate => false + coll.expects(:resources).returns([:one, :two]) - lambda { @compiler.compile }.should raise_error(Puppet::ParseError) + @compiler.add_collection(coll) + + lambda { @compiler.compile }.should raise_error(Puppet::ParseError) + end end -end -describe Puppet::Parser::Compiler, "when told to evaluate missing classes" do - include CompilerTesting + describe Puppet::Parser::Compiler, "when told to evaluate missing classes" do - it "should fail if there's no source listed for the scope" do - scope = stub 'scope', :source => nil - proc { @compiler.evaluate_classes(%w{one two}, scope) }.should raise_error(Puppet::DevError) - end + it "should fail if there's no source listed for the scope" do + scope = stub 'scope', :source => nil + proc { @compiler.evaluate_classes(%w{one two}, scope) }.should raise_error(Puppet::DevError) + end - it "should tag the catalog with the name of each not-found class" do - @compiler.catalog.expects(:tag).with("notfound") - @scope.expects(:findclass).with("notfound").returns(nil) - @compiler.evaluate_classes(%w{notfound}, @scope) + it "should tag the catalog with the name of each not-found class" do + @compiler.catalog.expects(:tag).with("notfound") + @scope.expects(:findclass).with("notfound").returns(nil) + @compiler.evaluate_classes(%w{notfound}, @scope) + end end -end -describe Puppet::Parser::Compiler, " when evaluating found classes" do - include CompilerTesting + describe Puppet::Parser::Compiler, " when evaluating found classes" do - before do - @class = stub 'class', :classname => "my::class" - @scope.stubs(:findclass).with("myclass").returns(@class) + before do + @class = stub 'class', :classname => "my::class" + @scope.stubs(:findclass).with("myclass").returns(@class) - @resource = stub 'resource', :ref => "Class[myclass]" - end + @resource = stub 'resource', :ref => "Class[myclass]" + end - it "should evaluate each class" do - @compiler.catalog.stubs(:tag) + it "should evaluate each class" do + @compiler.catalog.stubs(:tag) - @class.expects(:evaluate).with(@scope) + @class.expects(:evaluate).with(@scope) - @compiler.evaluate_classes(%w{myclass}, @scope) - end + @compiler.evaluate_classes(%w{myclass}, @scope) + end - it "should not evaluate the resources created for found classes unless asked" do - @compiler.catalog.stubs(:tag) + it "should not evaluate the resources created for found classes unless asked" do + @compiler.catalog.stubs(:tag) - @resource.expects(:evaluate).never + @resource.expects(:evaluate).never - @class.expects(:evaluate).returns(@resource) + @class.expects(:evaluate).returns(@resource) - @compiler.evaluate_classes(%w{myclass}, @scope) - end + @compiler.evaluate_classes(%w{myclass}, @scope) + end - it "should immediately evaluate the resources created for found classes when asked" do - @compiler.catalog.stubs(:tag) + it "should immediately evaluate the resources created for found classes when asked" do + @compiler.catalog.stubs(:tag) - @resource.expects(:evaluate) - @class.expects(:evaluate).returns(@resource) + @resource.expects(:evaluate) + @class.expects(:evaluate).returns(@resource) - @compiler.evaluate_classes(%w{myclass}, @scope, false) - end + @compiler.evaluate_classes(%w{myclass}, @scope, false) + end - it "should skip classes that have already been evaluated" do - @compiler.catalog.stubs(:tag) + it "should skip classes that have already been evaluated" do + @compiler.catalog.stubs(:tag) - @compiler.expects(:class_scope).with(@class).returns("something") + @compiler.expects(:class_scope).with(@class).returns("something") - @compiler.expects(:add_resource).never + @compiler.expects(:add_resource).never - @resource.expects(:evaluate).never + @resource.expects(:evaluate).never - Puppet::Parser::Resource.expects(:new).never - @compiler.evaluate_classes(%w{myclass}, @scope, false) - end + Puppet::Parser::Resource.expects(:new).never + @compiler.evaluate_classes(%w{myclass}, @scope, false) + end - it "should return the list of found classes" do - @compiler.catalog.stubs(:tag) + it "should return the list of found classes" do + @compiler.catalog.stubs(:tag) - @compiler.stubs(:add_resource) - @scope.stubs(:findclass).with("notfound").returns(nil) + @compiler.stubs(:add_resource) + @scope.stubs(:findclass).with("notfound").returns(nil) - Puppet::Parser::Resource.stubs(:new).returns(@resource) - @class.stubs :evaluate - @compiler.evaluate_classes(%w{myclass notfound}, @scope).should == %w{myclass} + Puppet::Parser::Resource.stubs(:new).returns(@resource) + @class.stubs :evaluate + @compiler.evaluate_classes(%w{myclass notfound}, @scope).should == %w{myclass} + end end -end -describe Puppet::Parser::Compiler, " when evaluating AST nodes with no AST nodes present" do - include CompilerTesting + describe Puppet::Parser::Compiler, " when evaluating AST nodes with no AST nodes present" do - it "should do nothing" do - @compiler.expects(:ast_nodes?).returns(false) - @compiler.parser.expects(:nodes).never - Puppet::Parser::Resource.expects(:new).never + it "should do nothing" do + @compiler.expects(:ast_nodes?).returns(false) + @compiler.parser.expects(:nodes).never + Puppet::Parser::Resource.expects(:new).never - @compiler.send(:evaluate_ast_node) + @compiler.send(:evaluate_ast_node) + end end -end -describe Puppet::Parser::Compiler, " when evaluating AST nodes with AST nodes present" do - include CompilerTesting + describe Puppet::Parser::Compiler, " when evaluating AST nodes with AST nodes present" do - before do - @nodes = mock 'node_hash' - @compiler.stubs(:ast_nodes?).returns(true) - @compiler.parser.stubs(:nodes).returns(@nodes) + before do + @nodes = mock 'node_hash' + @compiler.stubs(:ast_nodes?).returns(true) + @compiler.parser.stubs(:nodes).returns(@nodes) - # Set some names for our test - @node.stubs(:names).returns(%w{a b c}) - @nodes.stubs(:[]).with("a").returns(nil) - @nodes.stubs(:[]).with("b").returns(nil) - @nodes.stubs(:[]).with("c").returns(nil) + # Set some names for our test + @node.stubs(:names).returns(%w{a b c}) + @nodes.stubs(:[]).with("a").returns(nil) + @nodes.stubs(:[]).with("b").returns(nil) + @nodes.stubs(:[]).with("c").returns(nil) - # It should check this last, of course. - @nodes.stubs(:[]).with("default").returns(nil) - end + # It should check this last, of course. + @nodes.stubs(:[]).with("default").returns(nil) + end - it "should fail if the named node cannot be found" do - proc { @compiler.send(:evaluate_ast_node) }.should raise_error(Puppet::ParseError) - end + it "should fail if the named node cannot be found" do + proc { @compiler.send(:evaluate_ast_node) }.should raise_error(Puppet::ParseError) + end - it "should evaluate the first node class matching the node name" do - node_class = stub 'node', :classname => "c", :evaluate_code => nil - @nodes.stubs(:[]).with("c").returns(node_class) + it "should evaluate the first node class matching the node name" do + node_class = stub 'node', :classname => "c", :evaluate_code => nil + @nodes.stubs(:[]).with("c").returns(node_class) - node_resource = stub 'node resource', :ref => "Node[c]", :evaluate => nil - node_class.expects(:evaluate).returns(node_resource) + node_resource = stub 'node resource', :ref => "Node[c]", :evaluate => nil + node_class.expects(:evaluate).returns(node_resource) - @compiler.compile - end + @compiler.compile + end - it "should match the default node if no matching node can be found" do - node_class = stub 'node', :classname => "default", :evaluate_code => nil - @nodes.stubs(:[]).with("default").returns(node_class) + it "should match the default node if no matching node can be found" do + node_class = stub 'node', :classname => "default", :evaluate_code => nil + @nodes.stubs(:[]).with("default").returns(node_class) - node_resource = stub 'node resource', :ref => "Node[default]", :evaluate => nil - node_class.expects(:evaluate).returns(node_resource) + node_resource = stub 'node resource', :ref => "Node[default]", :evaluate => nil + node_class.expects(:evaluate).returns(node_resource) - @compiler.compile - end + @compiler.compile + end - it "should evaluate the node resource immediately rather than using lazy evaluation" do - node_class = stub 'node', :classname => "c" - @nodes.stubs(:[]).with("c").returns(node_class) + it "should evaluate the node resource immediately rather than using lazy evaluation" do + node_class = stub 'node', :classname => "c" + @nodes.stubs(:[]).with("c").returns(node_class) - node_resource = stub 'node resource', :ref => "Node[c]" - node_class.expects(:evaluate).returns(node_resource) + node_resource = stub 'node resource', :ref => "Node[c]" + node_class.expects(:evaluate).returns(node_resource) - node_resource.expects(:evaluate) + node_resource.expects(:evaluate) - @compiler.send(:evaluate_ast_node) - end + @compiler.send(:evaluate_ast_node) + end - it "should set the node's scope as the top scope" do - node_resource = stub 'node resource', :ref => "Node[c]", :evaluate => nil - node_class = stub 'node', :classname => "c", :evaluate => node_resource + it "should set the node's scope as the top scope" do + node_resource = stub 'node resource', :ref => "Node[c]", :evaluate => nil + node_class = stub 'node', :classname => "c", :evaluate => node_resource - @nodes.stubs(:[]).with("c").returns(node_class) + @nodes.stubs(:[]).with("c").returns(node_class) - # The #evaluate method normally does this. - scope = stub 'scope', :source => "mysource" - @compiler.class_set(node_class.classname, scope) - node_resource.stubs(:evaluate) + # The #evaluate method normally does this. + scope = stub 'scope', :source => "mysource" + @compiler.class_set(node_class.classname, scope) + node_resource.stubs(:evaluate) - @compiler.compile + @compiler.compile - @compiler.topscope.should equal(scope) + @compiler.topscope.should equal(scope) + end end -end -describe Puppet::Parser::Compiler, "when storing compiled resources" do - include CompilerTesting + describe Puppet::Parser::Compiler, "when storing compiled resources" do - it "should store the resources" do - Puppet.features.expects(:rails?).returns(true) - Puppet::Rails.expects(:connect) + it "should store the resources" do + Puppet.features.expects(:rails?).returns(true) + Puppet::Rails.expects(:connect) - @compiler.catalog.expects(:vertices).returns(:resources) + @compiler.catalog.expects(:vertices).returns(:resources) - @compiler.expects(:store_to_active_record).with(@node, :resources) - @compiler.send(:store) - end + @compiler.expects(:store_to_active_record).with(@node, :resources) + @compiler.send(:store) + end - it "should store to active_record" do - @node.expects(:name).returns("myname") - Puppet::Rails::Host.stubs(:transaction).yields - Puppet::Rails::Host.expects(:store).with(@node, :resources) - @compiler.send(:store_to_active_record, @node, :resources) + it "should store to active_record" do + @node.expects(:name).returns("myname") + Puppet::Rails::Host.stubs(:transaction).yields + Puppet::Rails::Host.expects(:store).with(@node, :resources) + @compiler.send(:store_to_active_record, @node, :resources) + end end -end -describe Puppet::Parser::Compiler, "when managing resource overrides" do - include CompilerTesting + describe Puppet::Parser::Compiler, "when managing resource overrides" do - before do - @override = stub 'override', :ref => "My[ref]" - @resource = stub 'resource', :ref => "My[ref]", :builtin? => true - end + before do + @override = stub 'override', :ref => "My[ref]" + @resource = stub 'resource', :ref => "My[ref]", :builtin? => true + end - it "should be able to store overrides" do - lambda { @compiler.add_override(@override) }.should_not raise_error - end + it "should be able to store overrides" do + lambda { @compiler.add_override(@override) }.should_not raise_error + end - it "should apply overrides to the appropriate resources" do - @compiler.add_resource(@scope, @resource) - @resource.expects(:merge).with(@override) + it "should apply overrides to the appropriate resources" do + @compiler.add_resource(@scope, @resource) + @resource.expects(:merge).with(@override) - @compiler.add_override(@override) + @compiler.add_override(@override) - @compiler.compile - end + @compiler.compile + end - it "should accept overrides before the related resource has been created" do - @resource.expects(:merge).with(@override) + it "should accept overrides before the related resource has been created" do + @resource.expects(:merge).with(@override) - # First store the override - @compiler.add_override(@override) + # First store the override + @compiler.add_override(@override) - # Then the resource - @compiler.add_resource(@scope, @resource) + # Then the resource + @compiler.add_resource(@scope, @resource) - # And compile, so they get resolved - @compiler.compile - end + # And compile, so they get resolved + @compiler.compile + end - it "should fail if the compile is finished and resource overrides have not been applied" do - @compiler.add_override(@override) + it "should fail if the compile is finished and resource overrides have not been applied" do + @compiler.add_override(@override) - lambda { @compiler.compile }.should raise_error(Puppet::ParseError) + lambda { @compiler.compile }.should raise_error(Puppet::ParseError) + end end -end -# #620 - Nodes and classes should conflict, else classes don't get evaluated -describe Puppet::Parser::Compiler, "when evaluating nodes and classes with the same name (#620)" do - include CompilerTesting + # #620 - Nodes and classes should conflict, else classes don't get evaluated + describe Puppet::Parser::Compiler, "when evaluating nodes and classes with the same name (#620)" do - before do - @node = stub :nodescope? => true - @class = stub :nodescope? => false - end + before do + @node = stub :nodescope? => true + @class = stub :nodescope? => false + end - it "should fail if a node already exists with the same name as the class being evaluated" do - @compiler.class_set("one", @node) - lambda { @compiler.class_set("one", @class) }.should raise_error(Puppet::ParseError) - end + it "should fail if a node already exists with the same name as the class being evaluated" do + @compiler.class_set("one", @node) + lambda { @compiler.class_set("one", @class) }.should raise_error(Puppet::ParseError) + end - it "should fail if a class already exists with the same name as the node being evaluated" do - @compiler.class_set("one", @class) - lambda { @compiler.class_set("one", @node) }.should raise_error(Puppet::ParseError) + it "should fail if a class already exists with the same name as the node being evaluated" do + @compiler.class_set("one", @class) + lambda { @compiler.class_set("one", @node) }.should raise_error(Puppet::ParseError) + end end -end +end
\ No newline at end of file diff --git a/spec/unit/ral/provider/mount.rb b/spec/unit/ral/provider/mount.rb index 65aaf7053..0b90d53c9 100755 --- a/spec/unit/ral/provider/mount.rb +++ b/spec/unit/ral/provider/mount.rb @@ -4,8 +4,8 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/provider/mount' -module MountModuleTesting - def setup +describe Puppet::Provider::Mount do + before :each do @mounter = Object.new @mounter.extend(Puppet::Provider::Mount) @@ -16,119 +16,115 @@ module MountModuleTesting @mounter.stubs(:resource).returns(@resource) end -end -describe Puppet::Provider::Mount, " when mounting" do - include MountModuleTesting + describe Puppet::Provider::Mount, " when mounting" do - it "should use the 'mountcmd' method to mount" do - @mounter.stubs(:options).returns(nil) - @mounter.expects(:mountcmd) + it "should use the 'mountcmd' method to mount" do + @mounter.stubs(:options).returns(nil) + @mounter.expects(:mountcmd) - @mounter.mount - end + @mounter.mount + end - it "should flush before mounting if a flush method exists" do - @mounter.meta_def(:flush) { } - @mounter.expects(:flush) - @mounter.stubs(:mountcmd) - @mounter.stubs(:options).returns(nil) + it "should flush before mounting if a flush method exists" do + @mounter.meta_def(:flush) { } + @mounter.expects(:flush) + @mounter.stubs(:mountcmd) + @mounter.stubs(:options).returns(nil) - @mounter.mount - end + @mounter.mount + end - it "should add the options following '-o' if they exist and are not set to :absent" do - @mounter.stubs(:options).returns("ro") - @mounter.expects(:mountcmd).with { |*ary| ary[0] == "-o" and ary[1] == "ro" } + it "should add the options following '-o' if they exist and are not set to :absent" do + @mounter.stubs(:options).returns("ro") + @mounter.expects(:mountcmd).with { |*ary| ary[0] == "-o" and ary[1] == "ro" } - @mounter.mount - end + @mounter.mount + end - it "should specify the filesystem name to the mount command" do - @mounter.stubs(:options).returns(nil) - @mounter.expects(:mountcmd).with { |*ary| ary[-1] == @name } + it "should specify the filesystem name to the mount command" do + @mounter.stubs(:options).returns(nil) + @mounter.expects(:mountcmd).with { |*ary| ary[-1] == @name } - @mounter.mount + @mounter.mount + end end -end -describe Puppet::Provider::Mount, " when remounting" do - include MountModuleTesting - - it "should use '-o remount' if the resource specifies it supports remounting" do - @mounter.stubs(:info) - @resource.stubs(:[]).with(:remounts).returns(:true) - @mounter.expects(:mountcmd).with("-o", "remount", @name) - @mounter.remount + describe Puppet::Provider::Mount, " when remounting" do + + it "should use '-o remount' if the resource specifies it supports remounting" do + @mounter.stubs(:info) + @resource.stubs(:[]).with(:remounts).returns(:true) + @mounter.expects(:mountcmd).with("-o", "remount", @name) + @mounter.remount + end + + it "should unmount and mount if the resource does not specify it supports remounting" do + @mounter.stubs(:info) + @resource.stubs(:[]).with(:remounts).returns(false) + @mounter.expects(:unmount) + @mounter.expects(:mount) + @mounter.remount + end + + it "should log that it is remounting" do + @resource.stubs(:[]).with(:remounts).returns(:true) + @mounter.stubs(:mountcmd) + @mounter.expects(:info).with("Remounting") + @mounter.remount + end end - it "should unmount and mount if the resource does not specify it supports remounting" do - @mounter.stubs(:info) - @resource.stubs(:[]).with(:remounts).returns(false) - @mounter.expects(:unmount) - @mounter.expects(:mount) - @mounter.remount - end + describe Puppet::Provider::Mount, " when unmounting" do - it "should log that it is remounting" do - @resource.stubs(:[]).with(:remounts).returns(:true) - @mounter.stubs(:mountcmd) - @mounter.expects(:info).with("Remounting") - @mounter.remount + it "should call the :umount command with the resource name" do + @mounter.expects(:umount).with(@name) + @mounter.unmount + end end -end -describe Puppet::Provider::Mount, " when unmounting" do - include MountModuleTesting + describe Puppet::Provider::Mount, " when determining if it is mounted" do - it "should call the :umount command with the resource name" do - @mounter.expects(:umount).with(@name) - @mounter.unmount - end -end - -describe Puppet::Provider::Mount, " when determining if it is mounted" do - include MountModuleTesting + it "should parse the results of running the mount command with no arguments" do + Facter.stubs(:value).returns("whatever") + @mounter.expects(:mountcmd).returns("") - it "should parse the results of running the mount command with no arguments" do - Facter.stubs(:value).returns("whatever") - @mounter.expects(:mountcmd).returns("") - - @mounter.mounted? - end + @mounter.mounted? + end - it "should match ' on /private/var/automount<name>' if the operating system is Darwin" do - Facter.stubs(:value).with("operatingsystem").returns("Darwin") - @mounter.expects(:mountcmd).returns("/dev/whatever on /private/var/automount/\ndevfs on /dev") + it "should match ' on /private/var/automount<name>' if the operating system is Darwin" do + Facter.stubs(:value).with("operatingsystem").returns("Darwin") + @mounter.expects(:mountcmd).returns("/dev/whatever on /private/var/automount/\ndevfs on /dev") - @mounter.should be_mounted - end + @mounter.should be_mounted + end - it "should match ' on <name>' if the operating system is Darwin" do - Facter.stubs(:value).with("operatingsystem").returns("Darwin") - @mounter.expects(:mountcmd).returns("/dev/disk03 on / (local, journaled)\ndevfs on /dev") + it "should match ' on <name>' if the operating system is Darwin" do + Facter.stubs(:value).with("operatingsystem").returns("Darwin") + @mounter.expects(:mountcmd).returns("/dev/disk03 on / (local, journaled)\ndevfs on /dev") - @mounter.should be_mounted - end + @mounter.should be_mounted + end - it "should match '^<name> on' if the operating system is Solaris" do - Facter.stubs(:value).with("operatingsystem").returns("Solaris") - @mounter.expects(:mountcmd).returns("/ on /dev/dsk/whatever\n/var on /dev/dsk/other") + it "should match '^<name> on' if the operating system is Solaris" do + Facter.stubs(:value).with("operatingsystem").returns("Solaris") + @mounter.expects(:mountcmd).returns("/ on /dev/dsk/whatever\n/var on /dev/dsk/other") - @mounter.should be_mounted - end + @mounter.should be_mounted + end - it "should match ' on <name>' if the operating system is not Darwin or Solaris" do - Facter.stubs(:value).with("operatingsystem").returns("Debian") - @mounter.expects(:mountcmd).returns("/dev/dsk/whatever on / and stuff\n/dev/other/disk on /var and stuff") + it "should match ' on <name>' if the operating system is not Darwin or Solaris" do + Facter.stubs(:value).with("operatingsystem").returns("Debian") + @mounter.expects(:mountcmd).returns("/dev/dsk/whatever on / and stuff\n/dev/other/disk on /var and stuff") - @mounter.should be_mounted - end + @mounter.should be_mounted + end - it "should not be considered mounted if it did not match the mount output" do - Facter.stubs(:value).with("operatingsystem").returns("Debian") - @mounter.expects(:mountcmd).returns("/dev/dsk/whatever on /something/else and stuff\n/dev/other/disk on /var and stuff") + it "should not be considered mounted if it did not match the mount output" do + Facter.stubs(:value).with("operatingsystem").returns("Debian") + @mounter.expects(:mountcmd).returns("/dev/dsk/whatever on /something/else and stuff\n/dev/other/disk on /var and stuff") - @mounter.should_not be_mounted + @mounter.should_not be_mounted + end end end diff --git a/spec/unit/ral/provider/mount/parsed.rb b/spec/unit/ral/provider/mount/parsed.rb index 89928891a..b2ca22987 100755 --- a/spec/unit/ral/provider/mount/parsed.rb +++ b/spec/unit/ral/provider/mount/parsed.rb @@ -12,11 +12,6 @@ module ParsedMountTesting include PuppetTest::Support::Utils include PuppetTest::FileParsing - def setup - @mount_class = Puppet.type(:mount) - @provider_class = @mount_class.provider(:parsed) - end - def fake_fstab os = Facter['operatingsystem'] if os == "Solaris" @@ -79,104 +74,112 @@ end provider_class = Puppet::Type.type(:mount).provider(:parsed) describe provider_class do - include ParsedMountTesting + before :each do + @mount_class = Puppet.type(:mount) + @provider_class = @mount_class.provider(:parsed) + end - it "should be able to parse all of the example mount tabs" do - tab = fake_fstab - @provider = @provider_class - # LAK:FIXME Again, a relatively bad test, but I don't know how to rspec-ify this. - # I suppose this is more of an integration test? I dunno. - fakedataparse(tab) do - # Now just make we've got some mounts we know will be there - hashes = @provider_class.target_records(tab).find_all { |i| i.is_a? Hash } - (hashes.length > 0).should be_true - root = hashes.find { |i| i[:name] == "/" } + describe provider_class do + include ParsedMountTesting - proc { @provider_class.to_file(hashes) }.should_not raise_error - end - end + it "should be able to parse all of the example mount tabs" do + tab = fake_fstab + @provider = @provider_class + + # LAK:FIXME Again, a relatively bad test, but I don't know how to rspec-ify this. + # I suppose this is more of an integration test? I dunno. + fakedataparse(tab) do + # Now just make we've got some mounts we know will be there + hashes = @provider_class.target_records(tab).find_all { |i| i.is_a? Hash } + (hashes.length > 0).should be_true + root = hashes.find { |i| i[:name] == "/" } - # LAK:FIXME I can't mock Facter because this test happens at parse-time. - it "should default to /etc/vfstab on Solaris and /etc/fstab everywhere else" do - should = case Facter.value(:operatingsystem) - when "Solaris": "/etc/vfstab" - else - "/etc/fstab" + proc { @provider_class.to_file(hashes) }.should_not raise_error end - Puppet::Type.type(:mount).provider(:parsed).default_target.should == should + end + + # LAK:FIXME I can't mock Facter because this test happens at parse-time. + it "should default to /etc/vfstab on Solaris and /etc/fstab everywhere else" do + should = case Facter.value(:operatingsystem) + when "Solaris": "/etc/vfstab" + else + "/etc/fstab" + end + Puppet::Type.type(:mount).provider(:parsed).default_target.should == should + end end -end -describe provider_class, " when mounting an absent filesystem" do - include ParsedMountTesting + describe provider_class, " when mounting an absent filesystem" do + include ParsedMountTesting - # #730 - Make sure 'flush' is called when a mount is moving from absent to mounted - it "should flush the fstab to disk" do - mount = mkmount + # #730 - Make sure 'flush' is called when a mount is moving from absent to mounted + it "should flush the fstab to disk" do + mount = mkmount - # Mark the mount as absent - mount.property_hash[:ensure] = :absent + # Mark the mount as absent + mount.property_hash[:ensure] = :absent - mount.stubs(:mountcmd) # just so we don't actually try to mount anything + mount.stubs(:mountcmd) # just so we don't actually try to mount anything - mount.expects(:flush) - mount.mount + mount.expects(:flush) + mount.mount + end end -end -describe provider_class, " when modifying the filesystem tab" do - include ParsedMountTesting - before do - @mount = mkmount - @target = @provider_class.default_target + describe provider_class, " when modifying the filesystem tab" do + include ParsedMountTesting + before do + @mount = mkmount + @target = @provider_class.default_target - # Never write to disk, only to RAM. - @provider_class.stubs(:filetype).returns(Puppet::Util::FileType.filetype(:ram)) - end + # Never write to disk, only to RAM. + @provider_class.stubs(:filetype).returns(Puppet::Util::FileType.filetype(:ram)) + end - it "should write the mount to disk when :flush is called" do - @mount.flush + it "should write the mount to disk when :flush is called" do + @mount.flush - text = @provider_class.target_object(@provider_class.default_target).read - text.should == @mount.class.to_line(@mount.property_hash) + "\n" + text = @provider_class.target_object(@provider_class.default_target).read + text.should == @mount.class.to_line(@mount.property_hash) + "\n" + end end -end -describe provider_class, " when parsing information about the root filesystem" do - confine "Mount type not tested on Darwin" => Facter["operatingsystem"].value != "Darwin" - include ParsedMountTesting + describe provider_class, " when parsing information about the root filesystem" do + confine "Mount type not tested on Darwin" => Facter["operatingsystem"].value != "Darwin" + include ParsedMountTesting - before do - @mount = @mount_class.create :name => "/" - @provider = @mount.provider - end + before do + @mount = @mount_class.create :name => "/" + @provider = @mount.provider + end - it "should have a filesystem tab" do - FileTest.should be_exist(@provider_class.default_target) - end + it "should have a filesystem tab" do + FileTest.should be_exist(@provider_class.default_target) + end - it "should find the root filesystem" do - @provider_class.prefetch("/" => @mount) - @mount.provider.property_hash[:ensure].should == :present - end + it "should find the root filesystem" do + @provider_class.prefetch("/" => @mount) + @mount.provider.property_hash[:ensure].should == :present + end - it "should determine that the root fs is mounted" do - @provider_class.prefetch("/" => @mount) - @mount.provider.should be_mounted - end + it "should determine that the root fs is mounted" do + @provider_class.prefetch("/" => @mount) + @mount.provider.should be_mounted + end - after do - Puppet::Type.allclear + after do + Puppet::Type.allclear + end end -end -describe provider_class, " when mounting and unmounting" do - include ParsedMountTesting + describe provider_class, " when mounting and unmounting" do + include ParsedMountTesting - it "should call the 'mount' command to mount the filesystem" + it "should call the 'mount' command to mount the filesystem" - it "should call the 'unmount' command to unmount the filesystem" + it "should call the 'unmount' command to unmount the filesystem" - it "should specify the filesystem when remounting a filesystem" -end + it "should specify the filesystem when remounting a filesystem" + end +end
\ No newline at end of file diff --git a/spec/unit/ral/types/file.rb b/spec/unit/ral/types/file.rb index 7202e23e5..62fe2f677 100755 --- a/spec/unit/ral/types/file.rb +++ b/spec/unit/ral/types/file.rb @@ -4,26 +4,56 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/type/file' -describe Puppet::Type::File, " when used with replace=>false and content" do +describe Puppet::Type::File do before do @path = Tempfile.new("puppetspec") @path.close!() @path = @path.path - @file = Puppet::Type::File.create( { :name => @path, :content => "foo", :replace => :false } ) + @file = Puppet::Type::File.create(:name => @path) end - it "should be insync if the file exists and the content is different" do - File.open(@path, "w") do |f| f.puts "bar" end - @file.property(:content).insync?("bar").should be_true - end + describe "when used with content and replace=>false" do + before do + @file[:content] = "foo" + @file[:replace] = false + end + + it "should be insync if the file exists and the content is different" do + File.open(@path, "w") do |f| f.puts "bar" end + @file.property(:content).insync?("bar").should be_true + end - it "should be insync if the file exists and the content is right" do - File.open(@path, "w") do |f| f.puts "foo" end - @file.property(:content).insync?("foo").should be_true + it "should be insync if the file exists and the content is right" do + File.open(@path, "w") do |f| f.puts "foo" end + @file.property(:content).insync?("foo").should be_true + end + + it "should not be insync if the file does not exist" do + @file.property(:content).insync?(:nil).should be_false + end end - it "should not be insync if the file doesnot exist" do - @file.property(:content).insync?(:nil).should be_false + describe "when retrieving remote files" do + before do + @filesource = Puppet::Type::File::FileSource.new + @filesource.server = mock 'fileserver' + + @file.stubs(:uri2obj).returns(@filesource) + + @file[:source] = "puppet:///test" + end + + it "should fail without writing if it cannot retrieve remote contents" do + # create the file, because we only get the problem when it starts + # out absent. + File.open(@file[:path], "w") { |f| f.puts "a" } + @file.expects(:write).never + + @filesource.server.stubs(:describe).returns("493\tfile\t100\t0\t{md5}3f5fef3bddbc4398c46a7bd7ba7b3af7") + @filesource.server.stubs(:retrieve).raises(RuntimeError) + @file.property(:source).retrieve + lambda { @file.property(:source).sync }.should raise_error(Puppet::Error) + end end after do diff --git a/spec/unit/ral/types/mount.rb b/spec/unit/ral/types/mount.rb index 7d01022b5..9247601e6 100755 --- a/spec/unit/ral/types/mount.rb +++ b/spec/unit/ral/types/mount.rb @@ -57,8 +57,8 @@ describe Puppet::Type::Mount::Ensure, "when validating values" do after { Puppet::Type::Mount.clear } end -module MountEvaluationTesting - def setup +describe Puppet::Type::Mount::Ensure do + before :each do @provider = stub 'provider', :class => Puppet::Type::Mount.defaultprovider, :clear => nil, :satisfies? => true, :name => :mock Puppet::Type::Mount.defaultprovider.stubs(:new).returns(@provider) @mount = Puppet::Type::Mount.create(:name => "yay", :check => :ensure) @@ -66,6 +66,10 @@ module MountEvaluationTesting @ensure = @mount.property(:ensure) end + after :each do + Puppet::Type::Mount.clear + end + def mount_stub(params) Puppet::Type::Mount.validproperties.each do |prop| unless params[prop] @@ -79,117 +83,110 @@ module MountEvaluationTesting end end - def teardown - Puppet::Type::Mount.clear - end -end - -describe Puppet::Type::Mount::Ensure, "when retrieving its current state" do - include MountEvaluationTesting + describe Puppet::Type::Mount::Ensure, "when retrieving its current state" do - it "should return the provider's value if it is :absent" do - @provider.expects(:ensure).returns(:absent) - @ensure.retrieve.should == :absent - end + it "should return the provider's value if it is :absent" do + @provider.expects(:ensure).returns(:absent) + @ensure.retrieve.should == :absent + end - it "should return :mounted if the provider indicates it is mounted and the value is not :absent" do - @provider.expects(:ensure).returns(:present) - @provider.expects(:mounted?).returns(true) - @ensure.retrieve.should == :mounted - end + it "should return :mounted if the provider indicates it is mounted and the value is not :absent" do + @provider.expects(:ensure).returns(:present) + @provider.expects(:mounted?).returns(true) + @ensure.retrieve.should == :mounted + end - it "should return :present if the provider indicates it is not mounted and the value is not :absent" do - @provider.expects(:ensure).returns(:present) - @provider.expects(:mounted?).returns(false) - @ensure.retrieve.should == :present + it "should return :present if the provider indicates it is not mounted and the value is not :absent" do + @provider.expects(:ensure).returns(:present) + @provider.expects(:mounted?).returns(false) + @ensure.retrieve.should == :present + end end -end -describe Puppet::Type::Mount::Ensure, "when changing the host" do - include MountEvaluationTesting + describe Puppet::Type::Mount::Ensure, "when changing the host" do - it "should destroy itself if it should be absent" do - @provider.stubs(:mounted?).returns(false) - @provider.expects(:destroy) - @ensure.should = :absent - @ensure.sync - end + it "should destroy itself if it should be absent" do + @provider.stubs(:mounted?).returns(false) + @provider.expects(:destroy) + @ensure.should = :absent + @ensure.sync + end - it "should unmount itself before destroying if it is mounted and should be absent" do - @provider.expects(:mounted?).returns(true) - @provider.expects(:unmount) - @provider.expects(:destroy) - @ensure.should = :absent - @ensure.sync - end + it "should unmount itself before destroying if it is mounted and should be absent" do + @provider.expects(:mounted?).returns(true) + @provider.expects(:unmount) + @provider.expects(:destroy) + @ensure.should = :absent + @ensure.sync + end - it "should create itself if it is absent and should be present" do - @provider.stubs(:mounted?).returns(false) - @provider.expects(:create) - @ensure.should = :present - @ensure.sync - end + it "should create itself if it is absent and should be present" do + @provider.stubs(:mounted?).returns(false) + @provider.expects(:create) + @ensure.should = :present + @ensure.sync + end - it "should unmount itself if it is mounted and should be present" do - @provider.stubs(:mounted?).returns(true) + it "should unmount itself if it is mounted and should be present" do + @provider.stubs(:mounted?).returns(true) - # The interface here is just too much work to test right now. - @ensure.stubs(:syncothers) - @provider.expects(:unmount) - @ensure.should = :present - @ensure.sync - end + # The interface here is just too much work to test right now. + @ensure.stubs(:syncothers) + @provider.expects(:unmount) + @ensure.should = :present + @ensure.sync + end - it "should create and mount itself if it does not exist and should be mounted" do - @provider.stubs(:ensure).returns(:absent) - @provider.stubs(:mounted?).returns(false) - @provider.expects(:create) - @ensure.stubs(:syncothers) - @provider.expects(:mount) - @ensure.should = :mounted - @ensure.sync - end + it "should create and mount itself if it does not exist and should be mounted" do + @provider.stubs(:ensure).returns(:absent) + @provider.stubs(:mounted?).returns(false) + @provider.expects(:create) + @ensure.stubs(:syncothers) + @provider.expects(:mount) + @ensure.should = :mounted + @ensure.sync + end - it "should mount itself if it is present and should be mounted" do - @provider.stubs(:ensure).returns(:present) - @provider.stubs(:mounted?).returns(false) - @ensure.stubs(:syncothers) - @provider.expects(:mount) - @ensure.should = :mounted - @ensure.sync - end + it "should mount itself if it is present and should be mounted" do + @provider.stubs(:ensure).returns(:present) + @provider.stubs(:mounted?).returns(false) + @ensure.stubs(:syncothers) + @provider.expects(:mount) + @ensure.should = :mounted + @ensure.sync + end - it "should create but not mount itself if it is absent and mounted and should be mounted" do - @provider.stubs(:ensure).returns(:absent) - @provider.stubs(:mounted?).returns(true) - @ensure.stubs(:syncothers) - @provider.expects(:create) - @ensure.should = :mounted - @ensure.sync + it "should create but not mount itself if it is absent and mounted and should be mounted" do + @provider.stubs(:ensure).returns(:absent) + @provider.stubs(:mounted?).returns(true) + @ensure.stubs(:syncothers) + @provider.expects(:create) + @ensure.should = :mounted + @ensure.sync + end end -end -describe Puppet::Type::Mount, "when responding to events" do - include MountEvaluationTesting + describe Puppet::Type::Mount, "when responding to events" do - it "should remount if it is currently mounted" do - @provider.expects(:mounted?).returns(true) - @provider.expects(:remount) + it "should remount if it is currently mounted" do + @provider.expects(:mounted?).returns(true) + @provider.expects(:remount) - @mount.refresh - end + @mount.refresh + end - it "should not remount if it is not currently mounted" do - @provider.expects(:mounted?).returns(false) - @provider.expects(:remount).never + it "should not remount if it is not currently mounted" do + @provider.expects(:mounted?).returns(false) + @provider.expects(:remount).never - @mount.refresh - end + @mount.refresh + end - it "should not remount swap filesystems" do - @mount[:fstype] = "swap" - @provider.expects(:remount).never + it "should not remount swap filesystems" do + @mount[:fstype] = "swap" + @provider.expects(:remount).never - @mount.refresh + @mount.refresh + end end -end +end
\ No newline at end of file diff --git a/spec/unit/ral/types/package.rb b/spec/unit/ral/types/package.rb index f14a792b9..785d2eb37 100755 --- a/spec/unit/ral/types/package.rb +++ b/spec/unit/ral/types/package.rb @@ -102,7 +102,13 @@ describe Puppet::Type::Package, "when validating attribute values" do end module PackageEvaluationTesting - def setup + def setprops(properties) + @provider.stubs(:properties).returns(properties) + end +end + +describe Puppet::Type::Package do + before :each do @provider = stub 'provider', :class => Puppet::Type::Package.defaultprovider, :clear => nil, :satisfies? => true, :name => :mock Puppet::Type::Package.defaultprovider.stubs(:new).returns(@provider) @package = Puppet::Type::Package.create(:name => "yay") @@ -110,135 +116,132 @@ module PackageEvaluationTesting @catalog = Puppet::Node::Catalog.new @catalog.add_resource(@package) end - - def setprops(properties) - @provider.stubs(:properties).returns(properties) - end - - def teardown + + after :each do @catalog.clear(true) Puppet::Type::Package.clear end -end -describe Puppet::Type::Package, "when it should be purged" do - include PackageEvaluationTesting - before { @package[:ensure] = :purged } + describe Puppet::Type::Package, "when it should be purged" do + include PackageEvaluationTesting - it "should do nothing if it is :purged" do - @provider.expects(:properties).returns(:ensure => :purged) - @catalog.apply - end + before { @package[:ensure] = :purged } - [:absent, :installed, :present, :latest].each do |state| - it "should purge if it is #{state.to_s}" do - @provider.stubs(:properties).returns(:ensure => state) - @provider.expects(:purge) + it "should do nothing if it is :purged" do + @provider.expects(:properties).returns(:ensure => :purged) @catalog.apply end + + [:absent, :installed, :present, :latest].each do |state| + it "should purge if it is #{state.to_s}" do + @provider.stubs(:properties).returns(:ensure => state) + @provider.expects(:purge) + @catalog.apply + end + end end -end -describe Puppet::Type::Package, "when it should be absent" do - include PackageEvaluationTesting + describe Puppet::Type::Package, "when it should be absent" do + include PackageEvaluationTesting - before { @package[:ensure] = :absent } + before { @package[:ensure] = :absent } - [:purged, :absent].each do |state| - it "should do nothing if it is #{state.to_s}" do - @provider.expects(:properties).returns(:ensure => state) - @catalog.apply + [:purged, :absent].each do |state| + it "should do nothing if it is #{state.to_s}" do + @provider.expects(:properties).returns(:ensure => state) + @catalog.apply + end end - end - [:installed, :present, :latest].each do |state| - it "should uninstall if it is #{state.to_s}" do - @provider.stubs(:properties).returns(:ensure => state) - @provider.expects(:uninstall) - @catalog.apply + [:installed, :present, :latest].each do |state| + it "should uninstall if it is #{state.to_s}" do + @provider.stubs(:properties).returns(:ensure => state) + @provider.expects(:uninstall) + @catalog.apply + end end end -end -describe Puppet::Type::Package, "when it should be present" do - include PackageEvaluationTesting + describe Puppet::Type::Package, "when it should be present" do + include PackageEvaluationTesting - before { @package[:ensure] = :present } + before { @package[:ensure] = :present } - [:present, :latest, "1.0"].each do |state| - it "should do nothing if it is #{state.to_s}" do - @provider.expects(:properties).returns(:ensure => state) - @catalog.apply + [:present, :latest, "1.0"].each do |state| + it "should do nothing if it is #{state.to_s}" do + @provider.expects(:properties).returns(:ensure => state) + @catalog.apply + end end - end - [:purged, :absent].each do |state| - it "should install if it is #{state.to_s}" do - @provider.stubs(:properties).returns(:ensure => state) - @provider.expects(:install) - @catalog.apply + [:purged, :absent].each do |state| + it "should install if it is #{state.to_s}" do + @provider.stubs(:properties).returns(:ensure => state) + @provider.expects(:install) + @catalog.apply + end end end -end -describe Puppet::Type::Package, "when it should be latest" do - include PackageEvaluationTesting + describe Puppet::Type::Package, "when it should be latest" do + include PackageEvaluationTesting - before { @package[:ensure] = :latest } + before { @package[:ensure] = :latest } - [:purged, :absent].each do |state| - it "should upgrade if it is #{state.to_s}" do - @provider.stubs(:properties).returns(:ensure => state) + [:purged, :absent].each do |state| + it "should upgrade if it is #{state.to_s}" do + @provider.stubs(:properties).returns(:ensure => state) + @provider.expects(:update) + @catalog.apply + end + end + + it "should upgrade if the current version is not equal to the latest version" do + @provider.stubs(:properties).returns(:ensure => "1.0") + @provider.stubs(:latest).returns("2.0") @provider.expects(:update) @catalog.apply end - end - it "should upgrade if the current version is not equal to the latest version" do - @provider.stubs(:properties).returns(:ensure => "1.0") - @provider.stubs(:latest).returns("2.0") - @provider.expects(:update) - @catalog.apply - end + it "should do nothing if it is equal to the latest version" do + @provider.stubs(:properties).returns(:ensure => "1.0") + @provider.stubs(:latest).returns("1.0") + @provider.expects(:update).never + @catalog.apply + end - it "should do nothing if it is equal to the latest version" do - @provider.stubs(:properties).returns(:ensure => "1.0") - @provider.stubs(:latest).returns("1.0") - @provider.expects(:update).never - @catalog.apply + it "should do nothing if the provider returns :present as the latest version" do + @provider.stubs(:properties).returns(:ensure => :present) + @provider.stubs(:latest).returns("1.0") + @provider.expects(:update).never + @catalog.apply + end end - it "should do nothing if the provider returns :present as the latest version" do - @provider.stubs(:properties).returns(:ensure => :present) - @provider.stubs(:latest).returns("1.0") - @provider.expects(:update).never - @catalog.apply - end -end + describe Puppet::Type::Package, "when it should be a specific version" do + include PackageEvaluationTesting -describe Puppet::Type::Package, "when it should be a specific version" do - include PackageEvaluationTesting + before { @package[:ensure] = "1.0" } - before { @package[:ensure] = "1.0" } + [:purged, :absent].each do |state| + it "should install if it is #{state.to_s}" do + @provider.stubs(:properties).returns(:ensure => state) + @provider.expects(:install) + @catalog.apply + end + end - [:purged, :absent].each do |state| - it "should install if it is #{state.to_s}" do - @provider.stubs(:properties).returns(:ensure => state) - @provider.expects(:install) + it "should do nothing if the current version is equal to the desired version" do + @provider.stubs(:properties).returns(:ensure => "1.0") + @provider.expects(:install).never @catalog.apply end - end - it "should do nothing if the current version is equal to the desired version" do - @provider.stubs(:properties).returns(:ensure => "1.0") - @provider.expects(:install).never - @catalog.apply - end - - it "should install if the current version is not equal to the specified version" do - @provider.stubs(:properties).returns(:ensure => "2.0") - @provider.expects(:install) - @catalog.apply + it "should install if the current version is not equal to the specified version" do + @provider.stubs(:properties).returns(:ensure => "2.0") + @provider.expects(:install) + @catalog.apply + end end end diff --git a/spec/unit/ral/types/schedule.rb b/spec/unit/ral/types/schedule.rb index 73b3a0bd1..4e9840c34 100755 --- a/spec/unit/ral/types/schedule.rb +++ b/spec/unit/ral/types/schedule.rb @@ -5,11 +5,6 @@ require File.dirname(__FILE__) + '/../../../spec_helper' require 'puppet/type/schedule' module ScheduleTesting - def setup - Puppet.settings.stubs(:value).with(:ignoreschedules).returns(false) - - @schedule = Puppet::Type::Schedule.create(:name => "testing") - end def format(time) time.strftime("%H:%M:%S") @@ -44,331 +39,303 @@ module ScheduleTesting diff(:sec, 1, method, count) end - def teardown - Puppet::Type::Schedule.clear - end end describe Puppet::Type::Schedule do - include ScheduleTesting - - it "should default to :distance for period-matching" do - @schedule[:periodmatch].should == :distance - end - it "should default to a :repeat of 1" do - @schedule[:repeat].should == 1 - end - - it "should never match when the period is :never" do - @schedule[:period] = :never - @schedule.match?.should be_false - end -end - -describe Puppet::Type::Schedule, "when producing default schedules" do - include ScheduleTesting + before :each do + Puppet.settings.stubs(:value).with(:ignoreschedules).returns(false) - %w{hourly daily weekly monthly never}.each do |period| - period = period.to_sym - it "should produce a #{period} schedule with the period set appropriately" do - schedules = Puppet::Type::Schedule.mkdefaultschedules - schedules.find { |s| s[:name] == period.to_s and s[:period] == period }.should be_instance_of(Puppet::Type::Schedule) - end + @schedule = Puppet::Type::Schedule.create(:name => "testing") end - it "should produce a schedule named puppet with a period of hourly and a repeat of 2" do - schedules = Puppet::Type::Schedule.mkdefaultschedules - schedules.find { |s| - s[:name] == "puppet" and s[:period] == :hourly and s[:repeat] == 2 - }.should be_instance_of(Puppet::Type::Schedule) + after :each do + Puppet::Type::Schedule.clear end -end -describe Puppet::Type::Schedule, "when matching ranges" do - include ScheduleTesting - it "should match when the start time is before the current time and the end time is after the current time" do - @schedule[:range] = "%s - %s" % [format(Time.now - 10), format(Time.now + 10)] - @schedule.match?.should be_true - end - - it "should not match when the start time is after the current time" do - @schedule[:range] = "%s - %s" % [format(Time.now + 5), format(Time.now + 10)] - @schedule.match?.should be_false - end + describe Puppet::Type::Schedule do + include ScheduleTesting - it "should not match when the end time is previous to the current time" do - @schedule[:range] = "%s - %s" % [format(Time.now - 10), format(Time.now - 5)] - @schedule.match?.should be_false - end -end + it "should default to :distance for period-matching" do + @schedule[:periodmatch].should == :distance + end -describe Puppet::Type::Schedule, "when matching hourly by distance" do - include ScheduleTesting + it "should default to a :repeat of 1" do + @schedule[:repeat].should == 1 + end - before do - @schedule[:period] = :hourly - @schedule[:periodmatch] = :distance + it "should never match when the period is :never" do + @schedule[:period] = :never + @schedule.match?.should be_false + end end - it "should match an hour ago" do - @schedule.match?(hour("-", 1)).should be_true - end + describe Puppet::Type::Schedule, "when producing default schedules" do + include ScheduleTesting - it "should not match now" do - @schedule.match?(Time.now).should be_false - end + %w{hourly daily weekly monthly never}.each do |period| + period = period.to_sym + it "should produce a #{period} schedule with the period set appropriately" do + schedules = Puppet::Type::Schedule.mkdefaultschedules + schedules.find { |s| s[:name] == period.to_s and s[:period] == period }.should be_instance_of(Puppet::Type::Schedule) + end + end - it "should not match 59 minutes ago" do - @schedule.match?(min("-", 59)).should be_false + it "should produce a schedule named puppet with a period of hourly and a repeat of 2" do + schedules = Puppet::Type::Schedule.mkdefaultschedules + schedules.find { |s| + s[:name] == "puppet" and s[:period] == :hourly and s[:repeat] == 2 + }.should be_instance_of(Puppet::Type::Schedule) + end end -end - -describe Puppet::Type::Schedule, "when matching daily by distance" do - include ScheduleTesting - before do - @schedule[:period] = :daily - @schedule[:periodmatch] = :distance - end + describe Puppet::Type::Schedule, "when matching ranges" do + include ScheduleTesting - it "should match when the previous time was one day ago" do - @schedule.match?(day("-", 1)).should be_true - end + it "should match when the start time is before the current time and the end time is after the current time" do + @schedule[:range] = "%s - %s" % [format(Time.now - 10), format(Time.now + 10)] + @schedule.match?.should be_true + end - it "should not match when the previous time is now" do - @schedule.match?(Time.now).should be_false - end + it "should not match when the start time is after the current time" do + @schedule[:range] = "%s - %s" % [format(Time.now + 5), format(Time.now + 10)] + @schedule.match?.should be_false + end - it "should not match when the previous time was 23 hours ago" do - @schedule.match?(hour("-", 23)).should be_false + it "should not match when the end time is previous to the current time" do + @schedule[:range] = "%s - %s" % [format(Time.now - 10), format(Time.now - 5)] + @schedule.match?.should be_false + end end -end -describe Puppet::Type::Schedule, "when matching weekly by distance" do - include ScheduleTesting + describe Puppet::Type::Schedule, "when matching hourly by distance" do + include ScheduleTesting - before do - @schedule[:period] = :weekly - @schedule[:periodmatch] = :distance - end + before do + @schedule[:period] = :hourly + @schedule[:periodmatch] = :distance + end - it "should match seven days ago" do - @schedule.match?(day("-", 7)).should be_true - end + it "should match an hour ago" do + @schedule.match?(hour("-", 1)).should be_true + end - it "should not match now" do - @schedule.match?(Time.now).should be_false - end + it "should not match now" do + @schedule.match?(Time.now).should be_false + end - it "should not match six days ago" do - @schedule.match?(day("-", 6)).should be_false + it "should not match 59 minutes ago" do + @schedule.match?(min("-", 59)).should be_false + end end -end -describe Puppet::Type::Schedule, "when matching monthly by distance" do - include ScheduleTesting + describe Puppet::Type::Schedule, "when matching daily by distance" do + include ScheduleTesting - before do - @schedule[:period] = :monthly - @schedule[:periodmatch] = :distance - end + before do + @schedule[:period] = :daily + @schedule[:periodmatch] = :distance + end - it "should match 32 days ago" do - @schedule.match?(day("-", 32)).should be_true - end + it "should match when the previous time was one day ago" do + @schedule.match?(day("-", 1)).should be_true + end - it "should not match now" do - @schedule.match?(Time.now).should be_false - end + it "should not match when the previous time is now" do + @schedule.match?(Time.now).should be_false + end - it "should not match 27 days ago" do - @schedule.match?(day("-", 27)).should be_false + it "should not match when the previous time was 23 hours ago" do + @schedule.match?(hour("-", 23)).should be_false + end end -end -describe Puppet::Type::Schedule, "when matching hourly by number" do - include ScheduleTesting + describe Puppet::Type::Schedule, "when matching weekly by distance" do + include ScheduleTesting - before do - @schedule[:period] = :hourly - @schedule[:periodmatch] = :number - end + before do + @schedule[:period] = :weekly + @schedule[:periodmatch] = :distance + end - it "should match if the times are one minute apart and the current minute is 0" do - current = Time.now + it "should match seven days ago" do + @schedule.match?(day("-", 7)).should be_true + end - # Subtract an hour, reset the minute to zero, then add 59 minutes, so we're the previous hour plus 59 minutes. - previous = (current - 3600 - (current.min * 60) + (59 * 60)) + it "should not match now" do + @schedule.match?(Time.now).should be_false + end - # Now set the "current" time to the zero minute of the current hour. - now = (current - (current.min * 60)) - Time.stubs(:now).returns(now) - @schedule.match?(previous).should be_true + it "should not match six days ago" do + @schedule.match?(day("-", 6)).should be_false + end end - it "should not match if the times are 58 minutes apart and the current minute is 59" do - current = Time.now + describe Puppet::Type::Schedule, "when matching monthly by distance" do + include ScheduleTesting - # reset the minute to zero - previous = current - (current.min * 60) + before do + @schedule[:period] = :monthly + @schedule[:periodmatch] = :distance + end - # Now set the "current" time to the 59th minute of the current hour. - now = (current - (current.min * 60) + (59 * 60)) - Time.stubs(:now).returns(now) - @schedule.match?(previous).should be_false - end -end + it "should match 32 days ago" do + @schedule.match?(day("-", 32)).should be_true + end -describe Puppet::Type::Schedule, "when matching daily by number" do - include ScheduleTesting + it "should not match now" do + @schedule.match?(Time.now).should be_false + end - before do - @schedule[:period] = :daily - @schedule[:periodmatch] = :number + it "should not match 27 days ago" do + @schedule.match?(day("-", 27)).should be_false + end end - it "should match if the times are one minute apart and the current minute and hour are 0" do - zero = Time.now + describe Puppet::Type::Schedule, "when matching hourly by number" do + include ScheduleTesting - # Reset the current time to X:00:00 - current = zero - (zero.hour * 3600) - (zero.min * 60) - zero.sec + before do + @schedule[:period] = :hourly + @schedule[:periodmatch] = :number + end - # Now set the previous time to one minute before that - previous = current - 60 + it "should match if the times are one minute apart and the current minute is 0" do + current = Time.now - Time.stubs(:now).returns(current) - @schedule.match?(previous).should be_true - end + # Subtract an hour, reset the minute to zero, then add 59 minutes, so we're the previous hour plus 59 minutes. + previous = (current - 3600 - (current.min * 60) + (59 * 60)) - it "should not match if the times are 23 hours and 58 minutes apart and the current hour is 23 and the current minute is 59" do - zero = Time.now + # Now set the "current" time to the zero minute of the current hour. + now = (current - (current.min * 60)) + Time.stubs(:now).returns(now) + @schedule.match?(previous).should be_true + end - # Reset the previous time to 00:00:00 - previous = zero - (zero.hour * 3600) - (zero.min * 60) - zero.sec + it "should not match if the times are 58 minutes apart and the current minute is 59" do + current = Time.now - # Set the current time to 23:59 - now = previous + (23 * 3600) + (59 * 60) + # reset the minute to zero + previous = current - (current.min * 60) - Time.stubs(:now).returns(now) - @schedule.match?(previous).should be_false + # Now set the "current" time to the 59th minute of the current hour. + now = (current - (current.min * 60) + (59 * 60)) + Time.stubs(:now).returns(now) + @schedule.match?(previous).should be_false + end end -end -describe Puppet::Type::Schedule, "when matching weekly by number" do - include ScheduleTesting + describe Puppet::Type::Schedule, "when matching daily by number" do + include ScheduleTesting - before do - @schedule[:period] = :weekly - @schedule[:periodmatch] = :number - end + before do + @schedule[:period] = :daily + @schedule[:periodmatch] = :number + end - it "should match if the previous time is prior to the most recent Sunday" do - now = Time.now + it "should match if the times are one minute apart and the current minute and hour are 0" do + zero = Time.now - # Subtract the number days we've progressed into the week, plus one because we're zero-indexed. - previous = now - (3600 * 24 * (now.wday + 1)) + # Reset the current time to X:00:00 + current = zero - (zero.hour * 3600) - (zero.min * 60) - zero.sec - @schedule.match?(previous).should be_true - end + # Now set the previous time to one minute before that + previous = current - 60 - it "should not match if the previous time is after the most recent Saturday" do - now = Time.now + Time.stubs(:now).returns(current) + @schedule.match?(previous).should be_true + end - # Subtract the number days we've progressed into the week - previous = now - (3600 * 24 * now.wday) + it "should not match if the times are 23 hours and 58 minutes apart and the current hour is 23 and the current minute is 59" do + zero = Time.now - @schedule.match?(previous).should be_false - end -end + # Reset the previous time to 00:00:00 + previous = zero - (zero.hour * 3600) - (zero.min * 60) - zero.sec -describe Puppet::Type::Schedule, "when matching monthly by number" do - include ScheduleTesting + # Set the current time to 23:59 + now = previous + (23 * 3600) + (59 * 60) - before do - @schedule[:period] = :monthly - @schedule[:periodmatch] = :number + Time.stubs(:now).returns(now) + @schedule.match?(previous).should be_false + end end - it "should match when the previous time is prior to the first day of this month" do - now = Time.now + describe Puppet::Type::Schedule, "when matching weekly by number" do + include ScheduleTesting - # Subtract the number days we've progressed into the month - previous = now - (3600 * 24 * now.day) + before do + @schedule[:period] = :weekly + @schedule[:periodmatch] = :number + end - @schedule.match?(previous).should be_true - end + it "should match if the previous time is prior to the most recent Sunday" do + now = Time.now - it "should not match when the previous time is after the last day of last month" do - now = Time.now + # Subtract the number days we've progressed into the week, plus one because we're zero-indexed. + previous = now - (3600 * 24 * (now.wday + 1)) - # Subtract the number days we've progressed into the month, minus one - previous = now - (3600 * 24 * (now.day - 1)) + @schedule.match?(previous).should be_true + end - @schedule.match?(previous).should be_false - end -end + it "should not match if the previous time is after the most recent Saturday" do + now = Time.now -describe Puppet::Type::Schedule, "when matching with a repeat greater than one" do - include ScheduleTesting + # Subtract the number days we've progressed into the week + previous = now - (3600 * 24 * now.wday) - before do - @schedule[:period] = :daily - @schedule[:repeat] = 2 + @schedule.match?(previous).should be_false + end end - it "should fail if the periodmatch is 'number'" do - @schedule[:periodmatch] = :number - proc { @schedule[:repeat] = 2 }.should raise_error(Puppet::Error) - end + describe Puppet::Type::Schedule, "when matching monthly by number" do + include ScheduleTesting - it "should match if the previous run was further away than the distance divided by the repeat" do - previous = Time.now - (3600 * 13) - @schedule.match?(previous).should be_true - end + before do + @schedule[:period] = :monthly + @schedule[:periodmatch] = :number + end - it "should not match if the previous run was closer than the distance divided by the repeat" do - previous = Time.now - (3600 * 11) - @schedule.match?(previous).should be_false - end -end + it "should match when the previous time is prior to the first day of this month" do + now = Time.now -module OldTesting - def mksched - @stype.create(:name => "testsched") - end + # Subtract the number days we've progressed into the month + previous = now - (3600 * 24 * now.day) + + @schedule.match?(previous).should be_true + end - def test_period_with_repeat - previous = @now + it "should not match when the previous time is after the last day of last month" do + now = Time.now - s = mksched - s[:period] = :hourly + # Subtract the number days we've progressed into the month, minus one + previous = now - (3600 * 24 * (now.day - 1)) - assert_nothing_raised("Was not able to set periodmatch") { - s[:periodmatch] = :number - } - assert_raise(Puppet::Error) { - s[:repeat] = 2 - } - assert_nothing_raised("Was not able to reset periodmatch") { - s[:periodmatch] = :distance - } + @schedule.match?(previous).should be_false + end + end - assert(! s.match?(min("-", 40)), "matched minus 40 minutes") + describe Puppet::Type::Schedule, "when matching with a repeat greater than one" do + include ScheduleTesting - assert_nothing_raised("Was not able to set period") { - s[:repeat] = 2 - } + before do + @schedule[:period] = :daily + @schedule[:repeat] = 2 + end - assert(! s.match?(min("-", 20)), "matched minus 20 minutes with half-hourly") - assert(s.match?(min("-", 40)), "Did not match minus 40 with half-hourly") + it "should fail if the periodmatch is 'number'" do + @schedule[:periodmatch] = :number + proc { @schedule[:repeat] = 2 }.should raise_error(Puppet::Error) + end - assert_nothing_raised("Was not able to set period") { - s[:repeat] = 3 - } + it "should match if the previous run was further away than the distance divided by the repeat" do + previous = Time.now - (3600 * 13) + @schedule.match?(previous).should be_true + end - assert(! s.match?(min("-", 15)), "matched minus 15 minutes with half-hourly") - assert(s.match?(min("-", 25)), "Did not match minus 25 with half-hourly") + it "should not match if the previous run was closer than the distance divided by the repeat" do + previous = Time.now - (3600 * 11) + @schedule.match?(previous).should be_false + end end end |