spec/unit/module.rb in puppet-0.24.9 vs spec/unit/module.rb in puppet-0.25.0

- old
+ new

@@ -1,247 +1,247 @@ #!/usr/bin/env ruby require File.dirname(__FILE__) + '/../spec_helper' -describe Puppet::Module, " when building its search path" do - include PuppetTest - - it "should fully qualify unqualified paths in the search path" do - Puppet[:modulepath] = "something:/my/something" - File.stubs(:directory?).returns(true) - Puppet::Module.modulepath.should == [File.join(Dir.getwd, 'something'), "/my/something"] +describe Puppet::Module do + before do + # This is necessary because of the extra checks we have for the deprecated + # 'plugins' directory + FileTest.stubs(:exist?).returns false end - it "should ignore paths that do not exist" do - Puppet[:modulepath] = "/yes:/no" - File.expects(:directory?).with("/yes").returns(true) - File.expects(:directory?).with("/no").returns(false) - Puppet::Module.modulepath.should == %w{/yes} + it "should have a class method that returns a named module from a given environment" do + env = mock 'module' + env.expects(:module).with("mymod").returns "yep" + Puppet::Node::Environment.expects(:new).with("myenv").returns env + + Puppet::Module.find("mymod", "myenv").should == "yep" end - it "should prepend PUPPETLIB in search path when set" do - Puppet[:modulepath] = "/my/mod:/other/mod" - ENV["PUPPETLIB"] = "/env/mod:/myenv/mod" - File.stubs(:directory?).returns(true) - Puppet::Module.modulepath.should == %w{/env/mod /myenv/mod /my/mod /other/mod} + it "should return nil if asked for a named module that doesn't exist" do + env = mock 'module' + env.expects(:module).with("mymod").returns nil + Puppet::Node::Environment.expects(:new).with("myenv").returns env + + Puppet::Module.find("mymod", "myenv").should be_nil end - it "should use the environment-specific search path when a node environment is provided" do - Puppet.settings.expects(:value).with(:modulepath, "myenv").returns("/mone:/mtwo") - File.stubs(:directory?).returns(true) - Puppet::Module.modulepath("myenv").should == %w{/mone /mtwo} + it "should return nil if asked for a module whose name is 'nil'" do + Puppet::Module.find(nil, "myenv").should be_nil end - after do - ENV["PUPPETLIB"] = nil + it "should provide support for logging" do + Puppet::Module.ancestors.should be_include(Puppet::Util::Logging) end -end -describe Puppet::Module, " when searching for modules" do - it "should find modules in the search path" do - path = %w{/dir/path} - Puppet::Module.stubs(:modulepath).returns(path) - File.stubs(:directory?).returns(true) - mod = Puppet::Module.find("mymod") - mod.should be_an_instance_of(Puppet::Module) - mod.path.should == "/dir/path/mymod" + it "should be able to be converted to a string" do + Puppet::Module.new("foo").to_s.should == "Module foo" end - it "should not search for fully qualified modules" do - path = %w{/dir/path} - Puppet::Module.expects(:modulepath).never - File.expects(:directory?).never - Puppet::Module.find("/mymod").should be_nil + it "should add the path to its string form if the module is found" do + mod = Puppet::Module.new("foo") + mod.stubs(:path).returns "/a" + mod.to_s.should == "Module foo(/a)" end - it "should search for modules in the order specified in the search path" do - Puppet[:modulepath] = "/one:/two:/three" - Puppet::Module.stubs(:modulepath).returns %w{/one /two /three} - File.expects(:directory?).with("/one/mod").returns(false) - File.expects(:directory?).with("/two/mod").returns(true) - File.expects(:directory?).with("/three/mod").never - mod = Puppet::Module.find("mod") - mod.path.should == "/two/mod" + it "should fail if its name is not alphanumeric" do + lambda { Puppet::Module.new(".something") }.should raise_error(Puppet::Module::InvalidName) end - it "should use a node environment if specified" do - Puppet::Module.expects(:modulepath).with("myenv").returns([]) - Puppet::Module.find("mymod", "myenv") + it "should require a name at initialization" do + lambda { Puppet::Module.new }.should raise_error(ArgumentError) end -end -describe Puppet::Module, " when searching for templates" do - it "should return fully-qualified templates directly" do - Puppet::Module.expects(:modulepath).never - Puppet::Module.find_template("/my/template").should == "/my/template" + it "should convert an environment name into an Environment instance" do + Puppet::Module.new("foo", "prod").environment.should be_instance_of(Puppet::Node::Environment) end - it "should return the template from the first found module" do - Puppet[:modulepath] = "/one:/two" - File.stubs(:directory?).returns(true) - Puppet::Module.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate" + it "should accept an environment at initialization" do + Puppet::Module.new("foo", :prod).environment.name.should == :prod end - - it "should return the file in the templatedir if it exists" do - Puppet.settings.expects(:value).with(:templatedir, nil).returns("/my/templates") - Puppet[:modulepath] = "/one:/two" - File.stubs(:directory?).returns(true) - File.stubs(:exists?).returns(true) - Puppet::Module.find_template("mymod/mytemplate").should == "/my/templates/mymod/mytemplate" - end - it "should raise an error if no valid templatedir exists" do - Puppet::Module.stubs(:templatepath).with(nil).returns(nil) - lambda { Puppet::Module.find_template("mytemplate") }.should raise_error + it "should use the default environment if none is provided" do + env = Puppet::Node::Environment.new + Puppet::Module.new("foo").environment.should equal(env) end - it "should not raise an error if no valid templatedir exists and the template exists in a module" do - Puppet::Module.stubs(:templatepath).with(nil).returns(nil) - Puppet[:modulepath] = "/one:/two" - File.stubs(:directory?).returns(true) - File.stubs(:exists?).returns(true) - Puppet::Module.find_template("mymod/mytemplate").should == "/one/mymod/templates/mytemplate" + it "should use any provided Environment instance" do + env = Puppet::Node::Environment.new + Puppet::Module.new("foo", env).environment.should equal(env) end - it "should use the main templatedir if no module is found" do - Puppet::Module.stubs(:templatepath).with(nil).returns(["/my/templates"]) - Puppet::Module.expects(:find).with("mymod", nil).returns(nil) - Puppet::Module.find_template("mymod/mytemplate").should == "/my/templates/mymod/mytemplate" + it "should return the path to the first found instance in its environment's module paths as its path" do + mod = Puppet::Module.new("foo") + env = mock 'environment' + mod.stubs(:environment).returns env + + env.expects(:modulepath).returns %w{/a /b /c} + + FileTest.expects(:exist?).with("/a/foo").returns false + FileTest.expects(:exist?).with("/b/foo").returns true + FileTest.expects(:exist?).with("/c/foo").never + + mod.path.should == "/b/foo" end - it "should return unqualified templates directly in the template dir" do - Puppet::Module.stubs(:templatepath).with(nil).returns(["/my/templates"]) - Puppet::Module.expects(:find).never - Puppet::Module.find_template("mytemplate").should == "/my/templates/mytemplate" + it "should be considered existent if it exists in at least one module path" do + mod = Puppet::Module.new("foo") + mod.expects(:path).returns "/a/foo" + mod.should be_exist end - it "should accept relative templatedirs" do - Puppet[:templatedir] = "my/templates" - File.expects(:directory?).with(File.join(Dir.getwd,"my/templates")).returns(true) - Puppet::Module.find_template("mytemplate").should == File.join(Dir.getwd,"my/templates/mytemplate") + it "should be considered nonexistent if it does not exist in any of the module paths" do + mod = Puppet::Module.new("foo") + mod.expects(:path).returns nil + mod.should_not be_exist end - it "should use the environment templatedir if no module is found and an environment is specified" do - Puppet::Module.stubs(:templatepath).with("myenv").returns(["/myenv/templates"]) - Puppet::Module.expects(:find).with("mymod", "myenv").returns(nil) - Puppet::Module.find_template("mymod/mytemplate", "myenv").should == "/myenv/templates/mymod/mytemplate" + [:plugins, :templates, :files, :manifests].each do |filetype| + dirname = filetype == :plugins ? "lib" : filetype.to_s + it "should be able to return individual #{filetype}" do + mod = Puppet::Module.new("foo") + mod.stubs(:path).returns "/a/foo" + path = File.join("/a/foo", dirname, "my/file") + FileTest.expects(:exist?).with(path).returns true + mod.send(filetype.to_s.sub(/s$/, ''), "my/file").should == path + end + + it "should consider #{filetype} to be present if their base directory exists" do + mod = Puppet::Module.new("foo") + mod.stubs(:path).returns "/a/foo" + path = File.join("/a/foo", dirname) + FileTest.expects(:exist?).with(path).returns true + mod.send(filetype.to_s + "?").should be_true + end + + it "should consider #{filetype} to be absent if their base directory does not exist" do + mod = Puppet::Module.new("foo") + mod.stubs(:path).returns "/a/foo" + path = File.join("/a/foo", dirname) + FileTest.expects(:exist?).with(path).returns false + mod.send(filetype.to_s + "?").should be_false + end + + it "should consider #{filetype} to be absent if the module base directory does not exist" do + mod = Puppet::Module.new("foo") + mod.stubs(:path).returns nil + mod.send(filetype.to_s + "?").should be_false + end + + it "should return nil if asked to return individual #{filetype} that don't exist" do + mod = Puppet::Module.new("foo") + mod.stubs(:path).returns "/a/foo" + path = File.join("/a/foo", dirname, "my/file") + FileTest.expects(:exist?).with(path).returns false + mod.send(filetype.to_s.sub(/s$/, ''), "my/file").should be_nil + end + + it "should return nil when asked for individual #{filetype} if the module does not exist" do + mod = Puppet::Module.new("foo") + mod.stubs(:path).returns nil + mod.send(filetype.to_s.sub(/s$/, ''), "my/file").should be_nil + end + + it "should return the base directory if asked for a nil path" do + mod = Puppet::Module.new("foo") + mod.stubs(:path).returns "/a/foo" + base = File.join("/a/foo", dirname) + FileTest.expects(:exist?).with(base).returns true + mod.send(filetype.to_s.sub(/s$/, ''), nil).should == base + end end - it "should use first dir from environment templatedir if no module is found and an environment is specified" do - Puppet::Module.stubs(:templatepath).with("myenv").returns(["/myenv/templates", "/two/templates"]) - Puppet::Module.expects(:find).with("mymod", "myenv").returns(nil) - Puppet::Module.find_template("mymod/mytemplate", "myenv").should == "/myenv/templates/mymod/mytemplate" + %w{plugins files}.each do |filetype| + short = filetype.sub(/s$/, '') + dirname = filetype == "plugins" ? "lib" : filetype.to_s + it "should be able to return the #{short} directory" do + Puppet::Module.new("foo").should respond_to(short + "_directory") + end + + it "should return the path to the #{short} directory" do + mod = Puppet::Module.new("foo") + mod.stubs(:path).returns "/a/foo" + + mod.send(short + "_directory").should == "/a/foo/#{dirname}" + end end - it "should use a valid dir when templatedir is a path for unqualified templates and the first dir contains template" do - Puppet::Module.stubs(:templatepath).returns(["/one/templates", "/two/templates"]) - File.expects(:exists?).with("/one/templates/mytemplate").returns(true) - Puppet::Module.expects(:find).never - Puppet::Module.find_template("mytemplate").should == "/one/templates/mytemplate" + it "should throw a warning if plugins are in a 'plugins' directory rather than a 'lib' directory" do + mod = Puppet::Module.new("foo") + mod.stubs(:path).returns "/a/foo" + FileTest.expects(:exist?).with("/a/foo/plugins").returns true + + mod.expects(:warning) + + mod.plugin_directory.should == "/a/foo/plugins" end - it "should use a valid dir when templatedir is a path for unqualified templates and only second dir contains template" do - Puppet::Module.stubs(:templatepath).returns(["/one/templates", "/two/templates"]) - File.expects(:exists?).with("/one/templates/mytemplate").returns(false) - File.expects(:exists?).with("/two/templates/mytemplate").returns(true) - Puppet::Module.expects(:find).never - Puppet::Module.find_template("mytemplate").should == "/two/templates/mytemplate" + it "should default to 'lib' for the plugins directory" do + mod = Puppet::Module.new("foo") + mod.stubs(:path).returns "/a/foo" + mod.plugin_directory.should == "/a/foo/lib" end +end - it "should use the node environment if specified" do - Puppet.settings.stubs(:value).returns.returns("/my/directory") - Puppet.settings.expects(:value).with(:modulepath, "myenv").returns("/my/modules") - File.stubs(:directory?).returns(true) - Puppet::Module.find_template("mymod/envtemplate", "myenv").should == "/my/modules/mymod/templates/envtemplate" +describe Puppet::Module, " when building its search path" do + it "should use the current environment's search path if no environment is specified" do + env = mock 'env' + env.expects(:modulepath).returns "eh" + Puppet::Node::Environment.expects(:new).with(nil).returns env + + Puppet::Module.modulepath.should == "eh" end - after { Puppet.settings.clear } + it "should use the specified environment's search path if an environment is specified" do + env = mock 'env' + env.expects(:modulepath).returns "eh" + Puppet::Node::Environment.expects(:new).with("foo").returns env + + Puppet::Module.modulepath("foo").should == "eh" + end end -describe Puppet::Module, " when searching for manifests when no module is found" do +describe Puppet::Module, "when finding matching manifests" do before do - File.stubs(:find).returns(nil) + @mod = Puppet::Module.new("mymod") + @mod.stubs(:path).returns "/a" end - it "should not look for modules when paths are fully qualified" do - Puppet.expects(:value).with(:modulepath).never - file = "/fully/qualified/file.pp" - Dir.stubs(:glob).with(file).returns([file]) - Puppet::Module.find_manifests(file) - end + it "should return all manifests matching the glob pattern" do + Dir.expects(:glob).with("/a/manifests/yay/*.pp").returns(%w{foo bar}) - it "should directly return fully qualified files" do - file = "/fully/qualified/file.pp" - Dir.stubs(:glob).with(file).returns([file]) - Puppet::Module.find_manifests(file).should == [file] + @mod.match_manifests("yay/*.pp").should == %w{foo bar} end - it "should match against provided fully qualified patterns" do - pattern = "/fully/qualified/pattern/*" - Dir.expects(:glob).with(pattern).returns(%w{my file list}) - Puppet::Module.find_manifests(pattern).should == %w{my file list} - end + it "should not return directories" do + Dir.expects(:glob).with("/a/manifests/yay/*.pp").returns(%w{foo bar}) - it "should look for files relative to the current directory" do - cwd = Dir.getwd - Dir.expects(:glob).with("#{cwd}/foobar/init.pp").returns(["#{cwd}/foobar/init.pp"]) - Puppet::Module.find_manifests("foobar/init.pp").should == ["#{cwd}/foobar/init.pp"] + FileTest.expects(:directory?).with("foo").returns false + FileTest.expects(:directory?).with("bar").returns true + @mod.match_manifests("yay/*.pp").should == %w{foo} end - it "should only return files, not directories" do - pattern = "/fully/qualified/pattern/*" - file = "/my/file" - dir = "/my/directory" - Dir.expects(:glob).with(pattern).returns([file, dir]) - FileTest.expects(:directory?).with(file).returns(false) - FileTest.expects(:directory?).with(dir).returns(true) - Puppet::Module.find_manifests(pattern).should == [file] - end -end + it "should default to the 'init.pp' file if no glob pattern is specified" do + FileTest.stubs(:exist?).returns true -describe Puppet::Module, " when searching for manifests in a found module" do - it "should return the manifests from the first found module" do - Puppet[:modulepath] = "/one:/two" - File.stubs(:directory?).returns(true) - Dir.expects(:glob).with("/one/mymod/manifests/init.pp").returns(%w{/one/mymod/manifests/init.pp}) - Puppet::Module.find_manifests("mymod/init.pp").should == ["/one/mymod/manifests/init.pp"] + @mod.match_manifests(nil).should == %w{/a/manifests/init.pp} end - it "should use the node environment if specified" do - Puppet.settings.expects(:value).with(:modulepath, "myenv").returns("/env/modules") - File.stubs(:directory?).returns(true) - Dir.expects(:glob).with("/env/modules/mymod/manifests/envmanifest.pp").returns(%w{/env/modules/mymod/manifests/envmanifest.pp}) - Puppet::Module.find_manifests("mymod/envmanifest.pp", :environment => "myenv").should == ["/env/modules/mymod/manifests/envmanifest.pp"] - end + it "should return all manifests matching the glob pattern in all existing paths" do + Dir.expects(:glob).with("/a/manifests/yay/*.pp").returns(%w{a b}) - it "should return all manifests matching the glob pattern" do - Puppet.settings.expects(:value).with(:modulepath, nil).returns("/my/modules") - File.stubs(:directory?).returns(true) - Dir.expects(:glob).with("/my/modules/mymod/manifests/yay/*.pp").returns(%w{/one /two}) - Puppet::Module.find_manifests("mymod/yay/*.pp").should == %w{/one /two} + @mod.match_manifests("yay/*.pp").should == %w{a b} end - it "should not return directories" do - Puppet.settings.expects(:value).with(:modulepath, nil).returns("/my/modules") - File.stubs(:directory?).returns(true) - Dir.expects(:glob).with("/my/modules/mymod/manifests/yay/*.pp").returns(%w{/one /two}) - FileTest.expects(:directory?).with("/one").returns false - FileTest.expects(:directory?).with("/two").returns true - Puppet::Module.find_manifests("mymod/yay/*.pp").should == %w{/one} - end + it "should match the glob pattern plus '.pp' if no results are found" do + Dir.expects(:glob).with("/a/manifests/yay/foo").returns([]) + Dir.expects(:glob).with("/a/manifests/yay/foo.pp").returns(%w{yay}) - it "should default to the 'init.pp' file in the manifests directory" do - Puppet.settings.expects(:value).with(:modulepath, nil).returns("/my/modules") - File.stubs(:directory?).returns(true) - Dir.expects(:glob).with("/my/modules/mymod/manifests/init.pp").returns(%w{my manifest}) - Puppet::Module.find_manifests("mymod").should == %w{my manifest} + @mod.match_manifests("yay/foo").should == %w{yay} end - after { Puppet.settings.clear } -end + it "should return an empty array if no manifests matched" do + Dir.expects(:glob).with("/a/manifests/yay/*.pp").returns([]) -describe Puppet::Module, " when returning files" do - it "should return the path to the module's 'files' directory" do - mod = Puppet::Module.send(:new, "mymod", "/my/mod") - mod.files.should == "/my/mod/files" + @mod.match_manifests("yay/*.pp").should == [] end end