diff options
Diffstat (limited to 'spec/unit/node/environment_spec.rb')
-rwxr-xr-x | spec/unit/node/environment_spec.rb | 134 |
1 files changed, 76 insertions, 58 deletions
diff --git a/spec/unit/node/environment_spec.rb b/spec/unit/node/environment_spec.rb index 78d383440..d5d3068a1 100755 --- a/spec/unit/node/environment_spec.rb +++ b/spec/unit/node/environment_spec.rb @@ -7,6 +7,8 @@ require 'puppet/node/environment' require 'puppet/util/execution' describe Puppet::Node::Environment do + let(:env) { Puppet::Node::Environment.new("testing") } + include PuppetSpec::Files after do Puppet::Node::Environment.clear @@ -44,59 +46,57 @@ describe Puppet::Node::Environment do describe "when managing known resource types" do before do - @env = Puppet::Node::Environment.new("dev") - @collection = Puppet::Resource::TypeCollection.new(@env) - @env.stubs(:perform_initial_import).returns(Puppet::Parser::AST::Hostclass.new('')) + @collection = Puppet::Resource::TypeCollection.new(env) + env.stubs(:perform_initial_import).returns(Puppet::Parser::AST::Hostclass.new('')) Thread.current[:known_resource_types] = nil end it "should create a resource type collection if none exists" do - Puppet::Resource::TypeCollection.expects(:new).with(@env).returns @collection - @env.known_resource_types.should equal(@collection) + Puppet::Resource::TypeCollection.expects(:new).with(env).returns @collection + env.known_resource_types.should equal(@collection) end it "should reuse any existing resource type collection" do - @env.known_resource_types.should equal(@env.known_resource_types) + env.known_resource_types.should equal(env.known_resource_types) end it "should perform the initial import when creating a new collection" do - @env = Puppet::Node::Environment.new("dev") - @env.expects(:perform_initial_import).returns(Puppet::Parser::AST::Hostclass.new('')) - @env.known_resource_types + env.expects(:perform_initial_import).returns(Puppet::Parser::AST::Hostclass.new('')) + env.known_resource_types end it "should return the same collection even if stale if it's the same thread" do Puppet::Resource::TypeCollection.stubs(:new).returns @collection - @env.known_resource_types.stubs(:stale?).returns true + env.known_resource_types.stubs(:stale?).returns true - @env.known_resource_types.should equal(@collection) + env.known_resource_types.should equal(@collection) end it "should return the current thread associated collection if there is one" do Thread.current[:known_resource_types] = @collection - @env.known_resource_types.should equal(@collection) + env.known_resource_types.should equal(@collection) end it "should give to all threads using the same environment the same collection if the collection isn't stale" do - original_thread_type_collection = Puppet::Resource::TypeCollection.new(@env) - Puppet::Resource::TypeCollection.expects(:new).with(@env).returns original_thread_type_collection - @env.known_resource_types.should equal(original_thread_type_collection) + original_thread_type_collection = Puppet::Resource::TypeCollection.new(env) + Puppet::Resource::TypeCollection.expects(:new).with(env).returns original_thread_type_collection + env.known_resource_types.should equal(original_thread_type_collection) original_thread_type_collection.expects(:require_reparse?).returns(false) - Puppet::Resource::TypeCollection.stubs(:new).with(@env).returns @collection + Puppet::Resource::TypeCollection.stubs(:new).with(env).returns @collection t = Thread.new { - @env.known_resource_types.should equal(original_thread_type_collection) + env.known_resource_types.should equal(original_thread_type_collection) } t.join end it "should generate a new TypeCollection if the current one requires reparsing" do - old_type_collection = @env.known_resource_types + old_type_collection = env.known_resource_types old_type_collection.stubs(:require_reparse?).returns true Thread.current[:known_resource_types] = nil - new_type_collection = @env.known_resource_types + new_type_collection = env.known_resource_types new_type_collection.should be_a Puppet::Resource::TypeCollection new_type_collection.should_not equal(old_type_collection) @@ -109,14 +109,11 @@ describe Puppet::Node::Environment do Puppet[:modulepath] = path - env = Puppet::Node::Environment.new("testing") - env.modulepath.should == [real_file] end it "should prefix the value of the 'PUPPETLIB' environment variable to the module path if present" do Puppet::Util::Execution.withenv("PUPPETLIB" => %w{/l1 /l2}.join(File::PATH_SEPARATOR)) do - env = Puppet::Node::Environment.new("testing") module_path = %w{/one /two}.join(File::PATH_SEPARATOR) env.expects(:validate_dirs).with(%w{/l1 /l2 /one /two}).returns %w{/l1 /l2 /one /two} env.expects(:[]).with(:modulepath).returns module_path @@ -132,8 +129,6 @@ describe Puppet::Node::Environment do end it "should not return non-directories" do - env = Puppet::Node::Environment.new("testing") - FileTest.expects(:directory?).with(@path_one).returns true FileTest.expects(:directory?).with(@path_two).returns false @@ -142,7 +137,6 @@ describe Puppet::Node::Environment do it "should use the current working directory to fully-qualify unqualified paths" do FileTest.stubs(:directory?).returns true - env = Puppet::Node::Environment.new("testing") two = File.expand_path(File.join(Dir.getwd, "two")) env.validate_dirs([@path_one, 'two']).should == [@path_one, two] @@ -155,44 +149,75 @@ describe Puppet::Node::Environment do end it "should provide an array-like accessor method for returning any environment-specific setting" do - env = Puppet::Node::Environment.new("testing") env.should respond_to(:[]) end it "should ask the Puppet settings instance for the setting qualified with the environment name" do Puppet.settings.expects(:value).with("myvar", :testing).returns("myval") - env = Puppet::Node::Environment.new("testing") env["myvar"].should == "myval" end it "should be able to return an individual module that exists in its module path" do - env = Puppet::Node::Environment.new("testing") mod = mock 'module' - Puppet::Module.expects(:new).with("one", env).returns mod + Puppet::Module.expects(:new).with("one", :environment => env).returns mod mod.expects(:exist?).returns true env.module("one").should equal(mod) end it "should return nil if asked for a module that does not exist in its path" do - env = Puppet::Node::Environment.new("testing") mod = mock 'module' - Puppet::Module.expects(:new).with("one", env).returns mod + Puppet::Module.expects(:new).with("one", :environment => env).returns mod mod.expects(:exist?).returns false env.module("one").should be_nil end - it "should be able to return its modules" do - Puppet::Node::Environment.new("testing").should respond_to(:modules) + describe ".modules_by_path" do + before do + dir = tmpdir("deep_path") + + @first = File.join(dir, "first") + @second = File.join(dir, "second") + Puppet[:modulepath] = "#{@first}#{File::PATH_SEPARATOR}#{@second}" + + FileUtils.mkdir_p(@first) + FileUtils.mkdir_p(@second) + end + + it "should return an empty list if there are no modules" do + env.modules_by_path.should == { + @first => [], + @second => [] + } + end + + it "should include modules even if they exist in multiple dirs in the modulepath" do + modpath1 = File.join(@first, "foo") + FileUtils.mkdir_p(modpath1) + modpath2 = File.join(@second, "foo") + FileUtils.mkdir_p(modpath2) + + env.modules_by_path.should == { + @first => [Puppet::Module.new('foo', :environment => env, :path => modpath1)], + @second => [Puppet::Module.new('foo', :environment => env, :path => modpath2)] + } + end end describe ".modules" do + it "should return an empty list if there are no modules" do + env.modulepath = %w{/a /b} + Dir.expects(:entries).with("/a").returns [] + Dir.expects(:entries).with("/b").returns [] + + env.modules.should == [] + end + it "should return a module named for every directory in each module path" do - env = Puppet::Node::Environment.new("testing") - env.expects(:modulepath).at_least_once.returns %w{/a /b} + env.modulepath = %w{/a /b} Dir.expects(:entries).with("/a").returns %w{foo bar} Dir.expects(:entries).with("/b").returns %w{bee baz} @@ -200,8 +225,7 @@ describe Puppet::Node::Environment do end it "should remove duplicates" do - env = Puppet::Node::Environment.new("testing") - env.expects(:modulepath).returns( %w{/a /b} ).at_least_once + env.modulepath = %w{/a /b} Dir.expects(:entries).with("/a").returns %w{foo} Dir.expects(:entries).with("/b").returns %w{foo} @@ -209,8 +233,7 @@ describe Puppet::Node::Environment do end it "should ignore invalid modules" do - env = Puppet::Node::Environment.new("testing") - env.stubs(:modulepath).returns %w{/a} + env.modulepath = %w{/a} Dir.expects(:entries).with("/a").returns %w{foo bar} Puppet::Module.expects(:new).with { |name, env| name == "foo" }.returns mock("foomod", :name => "foo") @@ -220,16 +243,14 @@ describe Puppet::Node::Environment do end it "should create modules with the correct environment" do - env = Puppet::Node::Environment.new("testing") - env.expects(:modulepath).at_least_once.returns %w{/a} + env.modulepath = %w{/a} Dir.expects(:entries).with("/a").returns %w{foo} env.modules.each {|mod| mod.environment.should == env } end it "should cache the module list" do - env = Puppet::Node::Environment.new("testing") - env.expects(:modulepath).at_least_once.returns %w{/a} + env.modulepath = %w{/a} Dir.expects(:entries).once.with("/a").returns %w{foo} env.modules @@ -244,20 +265,18 @@ describe Puppet::Node::Environment do @helper.extend(Puppet::Node::Environment::Helper) end - it "should be able to set and retrieve the environment" do + it "should be able to set and retrieve the environment as a symbol" do @helper.environment = :foo @helper.environment.name.should == :foo end it "should accept an environment directly" do - env = Puppet::Node::Environment.new :foo - @helper.environment = env + @helper.environment = Puppet::Node::Environment.new(:foo) @helper.environment.name.should == :foo end it "should accept an environment as a string" do - env = Puppet::Node::Environment.new "foo" - @helper.environment = env + @helper.environment = 'foo' @helper.environment.name.should == :foo end end @@ -266,14 +285,13 @@ describe Puppet::Node::Environment do before do @parser = Puppet::Parser::Parser.new("test") Puppet::Parser::Parser.stubs(:new).returns @parser - @env = Puppet::Node::Environment.new("env") end it "should set the parser's string to the 'code' setting and parse if code is available" do Puppet.settings[:code] = "my code" @parser.expects(:string=).with "my code" @parser.expects(:parse) - @env.instance_eval { perform_initial_import } + env.instance_eval { perform_initial_import } end it "should set the parser's file to the 'manifest' setting and parse if no code is available and the manifest is available" do @@ -282,7 +300,7 @@ describe Puppet::Node::Environment do Puppet.settings[:manifest] = filename @parser.expects(:file=).with filename @parser.expects(:parse) - @env.instance_eval { perform_initial_import } + env.instance_eval { perform_initial_import } end it "should pass the manifest file to the parser even if it does not exist on disk" do @@ -291,15 +309,15 @@ describe Puppet::Node::Environment do Puppet.settings[:manifest] = filename @parser.expects(:file=).with(filename).once @parser.expects(:parse).once - @env.instance_eval { perform_initial_import } + env.instance_eval { perform_initial_import } end it "should fail helpfully if there is an error importing" do File.stubs(:exist?).returns true - @env.stubs(:known_resource_types).returns Puppet::Resource::TypeCollection.new(@env) + env.stubs(:known_resource_types).returns Puppet::Resource::TypeCollection.new(env) @parser.expects(:file=).once @parser.expects(:parse).raises ArgumentError - lambda { @env.instance_eval { perform_initial_import } }.should raise_error(Puppet::Error) + lambda { env.instance_eval { perform_initial_import } }.should raise_error(Puppet::Error) end it "should not do anything if the ignore_import settings is set" do @@ -307,15 +325,15 @@ describe Puppet::Node::Environment do @parser.expects(:string=).never @parser.expects(:file=).never @parser.expects(:parse).never - @env.instance_eval { perform_initial_import } + env.instance_eval { perform_initial_import } end it "should mark the type collection as needing a reparse when there is an error parsing" do @parser.expects(:parse).raises Puppet::ParseError.new("Syntax error at ...") - @env.stubs(:known_resource_types).returns Puppet::Resource::TypeCollection.new(@env) + env.stubs(:known_resource_types).returns Puppet::Resource::TypeCollection.new(env) - lambda { @env.instance_eval { perform_initial_import } }.should raise_error(Puppet::Error, /Syntax error at .../) - @env.known_resource_types.require_reparse?.should be_true + lambda { env.instance_eval { perform_initial_import } }.should raise_error(Puppet::Error, /Syntax error at .../) + env.known_resource_types.require_reparse?.should be_true end end end |