Permalink
Browse files

14595 - plugin packager / info doesn't handle absent DDL files well

Previously the plugin application's package/info actions raised
undescriptive exceptions if a ddl wasn't present.

The plugin application will now raise a descriptive error stating that
no ddl file was found.

Added tests for PluginPackager
  • Loading branch information...
1 parent 5420e6e commit 35ed09caee521ee55cbb61e0cc800d9dede3c812 @ploubser committed with ripienaar May 21, 2012
Showing with 134 additions and 1 deletion.
  1. +6 −1 lib/mcollective/pluginpackager.rb
  2. +128 −0 spec/unit/pluginpackager_spec.rb
@@ -16,7 +16,12 @@ def self.[](klass)
# Fetch and return metadata from plugin DDL
def self.get_metadata(path, type)
ddl = DDL.new("package", type.to_sym, false)
- ddl.instance_eval File.read(Dir.glob(File.join(path, type, "*.ddl")).first)
+ begin
+ ddl_file = File.read(Dir.glob(File.join(path, type, "*.ddl")).first)
+ rescue Exception
+ raise "failed to load ddl file in plugin directory : #{File.join(path, type)}"
+ end
+ ddl.instance_eval ddl_file
ddl.meta
end
@@ -0,0 +1,128 @@
+#!/usr/bin/enn rspec
+
+require 'spec_helper'
+
+module MCollective
+ describe PluginPackager do
+ describe "#load_packagers" do
+ it "should load all PluginPackager plugins" do
+ PluginManager.expects(:find_and_load).with("pluginpackager")
+ PluginPackager.load_packagers
+ end
+ end
+
+ describe "#[]" do
+ it "should return the correct class" do
+ PluginPackager.expects(:const_get).with("Foo").returns(:foo)
+ result = PluginPackager["Foo"]
+ result.should == :foo
+ end
+
+ it "should do something else" do
+ expect{
+ PluginPackager["Bar"]
+ }.to raise_error NameError, 'uninitialized constant MCollective::PluginPackager::Bar'
+ end
+ end
+
+ describe "#get_metadata" do
+ it "should raise an exception if the ddl file can't be loaded" do
+ DDL.expects(:new).with("package", :foo, false)
+ File.stubs(:join)
+ Dir.stubs(:glob).returns('')
+ expect{
+ PluginPackager.get_metadata("/tmp", "foo")
+ }.to raise_error RuntimeError, "failed to load ddl file in plugin directory"
+ end
+
+ it "should load the ddl file and return the metadata" do
+ ddl = mock
+ DDL.expects(:new).with("package", :foo, false).returns(ddl)
+ File.stubs(:join)
+ Dir.stubs(:glob).returns(["foo.ddl"])
+ File.expects(:read).with("foo.ddl").returns("foo_ddl")
+ ddl.expects(:instance_eval).with("foo_ddl")
+ ddl.expects(:meta)
+
+ PluginPackager.get_metadata("/tmp", "foo")
+ end
+ end
+
+ describe "#check_dir_present" do
+ it "should return true if the directory is present and not empty" do
+ File.expects(:directory?).with("/tmp").returns(true)
+ File.expects(:join).with("/tmp", "*")
+ Dir.expects(:glob).returns([1])
+ result = PluginPackager.check_dir_present("/tmp")
+ result.should == true
+ end
+
+ it "should return false if the directory is not present" do
+ File.expects(:directory?).with("/tmp").returns(false)
+ result = PluginPackager.check_dir_present("/tmp")
+ result.should == false
+ end
+
+ it "should return false if the direcotry is present but empty" do
+ File.expects(:directory?).with("/tmp").returns(true)
+ File.expects(:join).with("/tmp", "*")
+ Dir.expects(:glob).returns([])
+ result = PluginPackager.check_dir_present("/tmp")
+ result.should == false
+ end
+ end
+
+ describe "#do_quietly?" do
+ it "should call the block parameter if verbose is true" do
+ result = PluginPackager.do_quietly?(true) {:success}
+ result.should == :success
+ end
+
+ it "should call the block parameter quietly if verbose is false" do
+ std_out = Tempfile.new("mc_pluginpackager_spec")
+ File.expects(:new).with("/dev/null", "w").returns(std_out)
+ PluginPackager.do_quietly?(false) {puts "success"}
+ std_out.rewind
+ std_out.read.should == "success\n"
+ std_out.close
+ std_out.unlink
+ end
+
+ it "should raise an exception and reset stdout if the block raises an execption" do
+ expect{
+ PluginPackager.do_quietly?(false) {raise Exception, "exception"}
+ }.to raise_error Exception, "exception"
+ end
+ end
+
+ describe "#build_tool?" do
+ it "should return true if the given build tool is present on the system" do
+ File.expects(:join).returns("foo")
+ File.expects(:exists?).with("foo").returns(true)
+ result = PluginPackager.build_tool?("foo")
+ result.should == true
+ end
+
+ it "should return false if the given build tool is not present on the system" do
+ File.stubs(:join).returns("foo")
+ File.stubs(:exists?).with("foo").returns(false)
+ result = PluginPackager.build_tool?("foo")
+ result.should == false
+ end
+ end
+
+ describe "#safe_system" do
+ it "should not raise any exceptions if a command ran" do
+ PluginPackager.expects(:system).with("foo").returns(true)
+ lambda{PluginPackager.safe_system("foo")}.should_not raise_error
+ end
+
+ it "should raise a RuntimeError if command cannot be run" do
+ PluginPackager.expects(:system).with("foo").returns(false)
+ expect{
+ PluginPackager.safe_system("foo")
+ }.to raise_error RuntimeError, "Failed: foo"
+ end
+ end
+ end
+end

0 comments on commit 35ed09c

Please sign in to comment.