Skip to content

Commit

Permalink
BGBUILD-133: Support a consolidated configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
goldmann committed Jan 17, 2011
1 parent ba9b221 commit 58dbbe9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,5 +1,6 @@
v0.8.0

* [BGBUILD-133] Support a consolidated configuration file
* [BGBUILD-128] Allow to specify plugin configuration using CLI
* [BGBUILD-134] Replace rubygem-commander with rubygem-thor
* [BGBUILD-131] Check if OS is supported before executing the plugin
Expand Down
16 changes: 4 additions & 12 deletions lib/boxgrinder-build/plugins/base-plugin.rb
Expand Up @@ -51,8 +51,6 @@ def init(config, appliance_config, options = {})
@dir.base = "#{@appliance_config.path.build}/#{@plugin_info[:name]}-plugin"
@dir.tmp = "#{@dir.base}/tmp"

@config_file = "#{ENV['HOME']}/.boxgrinder/plugins/#{@plugin_info[:name]}"

read_plugin_config
merge_plugin_config

Expand Down Expand Up @@ -111,10 +109,8 @@ def current_platform
def validate_plugin_config(fields = [], doc = nil)
more_info = doc.nil? ? '' : "See #{doc} for more info"

raise "Not valid configuration file for #{info[:name]} plugin. Please create valid '#{@config_file}' file. #{more_info}" if @plugin_config.nil?

fields.each do |field|
raise "Please specify a valid '#{field}' key in plugin configuration file: '#{@config_file}'. #{more_info}" if @plugin_config[field].nil?
raise "Please specify a valid '#{field}' key in BoxGrinder configuration file: '#{@config.file}'. #{more_info}" if @plugin_config[field].nil?
end
end

Expand Down Expand Up @@ -174,15 +170,11 @@ def set_default_config_value(key, value)

# This reads the plugin config from file
def read_plugin_config
return unless File.exists?(@config_file)
return if @config[@plugin_info[:name].to_s].nil?

@log.debug "Reading configuration file for #{self.class.name}."
@log.debug "Reading configuration for #{@plugin_info[:full_name]} plugin."

begin
@plugin_config = YAML.load_file(@config_file)
rescue
@log.warn "An error occurred while reading configuration file '#{@config_file}' for #{self.class.name}. Is it a valid YAML file?"
end
@plugin_config = @config[@plugin_info[:name].to_s]
end

# This merges the plugin config with configuration provided in command line
Expand Down
1 change: 1 addition & 0 deletions rubygem-boxgrinder-build.spec
Expand Up @@ -94,6 +94,7 @@ popd
- [BGBUILD-68] Global .boxgrinder/config or rc style file for config
- [BGBUILD-131] Check if OS is supported before executing the plugin
- [BGBUILD-72] Add support for growing (not pre-allocated) disks for KVM/Xen
- [BGBUILD-133] Support a consolidated configuration file

* Mon Dec 20 2010 <mgoldman@redhat.com> - 0.7.1-1
- Upstream release: 0.7.1
Expand Down
2 changes: 1 addition & 1 deletion spec/appliance-spec.rb
Expand Up @@ -65,7 +65,7 @@ def prepare_appliance_config
it "should create @config object without log" do
config = Appliance.new("file", {:platform => :ec2}, :log => "ALOG").instance_variable_get(:@config)

config.size.should == 9
config.size.should == 10
config[:log].should == nil
end

Expand Down
37 changes: 13 additions & 24 deletions spec/plugins/base-plugin-spec.rb
Expand Up @@ -27,6 +27,8 @@ module BoxGrinder
@config = mock('Config')
@config.stub!(:name).and_return('BoxGrinder')
@config.stub!(:version_with_release).and_return('0.1.2')
@config.stub!(:[]).with('plugin_name').and_return({})
@config.stub!(:file).and_return('/home/abc/boxgrinder_config_file')

@appliance_config = mock('ApplianceConfig')

Expand Down Expand Up @@ -166,30 +168,17 @@ module BoxGrinder
@plugin.instance_variable_get(:@plugin_config)['key'].should == 'avalue'
end

it "should read plugin config" do
@plugin.instance_variable_set(:@config_file, "configfile")

File.should_receive(:exists?).with('configfile').and_return(true)
YAML.should_receive(:load_file).with('configfile').and_return('abcdef')

@plugin.read_plugin_config

@plugin.instance_variable_get(:@plugin_config).should == 'abcdef'
end

it "should read plugin config and log warning an exception" do
log = mock("Log")

log.should_receive(:debug).with("Reading configuration file for BoxGrinder::BasePlugin.")
log.should_receive(:warn).with("An error occurred while reading configuration file 'configfile' for BoxGrinder::BasePlugin. Is it a valid YAML file?")

@plugin.instance_variable_set(:@log, log)
@plugin.instance_variable_set(:@config_file, "configfile")

File.should_receive(:exists?).with('configfile').and_return(true)
YAML.should_receive(:load_file).with('configfile').and_raise('something')
describe ".read_plugin_config" do
it "should read plugin config" do
@config.stub!(:[]).with('plugin_name').and_return({'abc' => 'def'})
@plugin.read_plugin_config
@plugin.instance_variable_get(:@plugin_config)['abc'].should == 'def'
end

@plugin.read_plugin_config
it "should read plugin config and exit early" do
@config.stub!(:[]).with(:plugin_name).and_return(nil)
@plugin.read_plugin_config
end
end

describe ".current_platform" do
Expand All @@ -216,7 +205,7 @@ module BoxGrinder

lambda {
@plugin.validate_plugin_config(['one', 'two', 'three'])
}.should raise_error(RuntimeError, /^Please specify a valid 'three' key in plugin configuration file:/)
}.should raise_error(RuntimeError, "Please specify a valid 'three' key in BoxGrinder configuration file: '/home/abc/boxgrinder_config_file'. ")
end
end

Expand Down

0 comments on commit 58dbbe9

Please sign in to comment.