Browse Source

improved native nova_config type

- tightened input validation
- removed constraint that /etc/nova/nova.conf should already exist
- added unit tests
changes/27/26227/1
Dan Bode 10 years ago
parent
commit
a294248c78
  1. 4
      nova/lib/puppet/provider/nova_config/parsed.rb
  2. 10
      nova/lib/puppet/type/nova_config.rb
  3. 14
      nova/spec/find_spec_utils.rb
  4. 36
      nova/spec/unit/provider/nova_config/parsed_spec.rb
  5. 36
      nova/spec/unit/type/nova_config_spec.rb

4
nova/lib/puppet/provider/nova_config/parsed.rb

@ -9,7 +9,7 @@ Puppet::Type.type(:nova_config).provide(
:filetype => :flat
) do
confine :exists => novaconf
#confine :exists => novaconf
text_line :comment, :match => /^\s*#/;
text_line :blank, :match => /^\s*$/;
@ -18,7 +18,7 @@ Puppet::Type.type(:nova_config).provide(
:match => /--(.*)/ ,
:post_parse => proc { |hash|
Puppet.debug("nova config line:#{hash[:line]} has been parsed")
if hash[:line] =~ /(\S+)\s*=\s*(\S+)/
if hash[:line] =~ /^\s*(\S+)\s*=\s*(\S+)\s*$/
hash[:name]=$1
hash[:value]=$2
else

10
nova/lib/puppet/type/nova_config.rb

@ -3,11 +3,11 @@ Puppet::Type.newtype(:nova_config) do
ensurable
newparam(:name, :namevar => true) do
newvalues(/\S+/)
newvalues(/^\S+$/)
end
newproperty(:value) do
newvalues(/\S+/)
newvalues(/^\S+$/)
end
newproperty(:target) do
@ -21,4 +21,10 @@ Puppet::Type.newtype(:nova_config) do
}
end
validate do
if ! self[:value] and self[:ensure] == :present
raise Puppet::Error, 'Property value must be set when ensure is present'
end
end
end

14
nova/spec/find_spec_utils.rb

@ -0,0 +1,14 @@
def find_puppet_spec()
puppetdir = $LOAD_PATH.detect do |file|
File.directory?(File.join(file, 'puppet')) &&
File.directory?(File.join(file, '../spec/lib'))
end
raise Exception, "could not find puppet spec lib" unless puppetdir
$LOAD_PATH.unshift(File.join(puppetdir, '../spec/lib'))
$LOAD_PATH.unshift(File.join(puppetdir, '../spec'))
require File.join(puppetdir, '../spec/spec_helper')
end
find_puppet_spec
require 'puppet_spec/files'
include PuppetSpec
include PuppetSpec::Files

36
nova/spec/unit/provider/nova_config/parsed_spec.rb

@ -0,0 +1,36 @@
require 'puppet'
require 'mocha'
require 'tempfile'
RSpec.configure do |config|
config.mock_with :mocha
end
provider_class = Puppet::Type.type(:nova_config).provider(:parsed)
describe provider_class do
before :each do
@nova_config = Tempfile.new('nova.conf')
@resource = Puppet::Type::Nova_config.new(
{:target => @nova_config, :name => 'foo', :value => 'bar'}
)
@provider = provider_class.new(@resource)
end
it 'should be able to parse lines into records' do
record = @provider.class.parse('--foo = bar').first
record[:name].should == 'foo'
record[:value].should == 'bar'
record[:record_type].should == :parsed
end
it 'should fail when it parses an invalid record' do
expect { @provider.class.parse('--foo = bar baz') }.should raise_error(Puppet::Error, /Invalid line/)
end
it 'should be able to create a valid line from a resource' do
provider_class.to_line({:name => 'foo', :value => 'bar'}).should == '--foo=bar'
end
it 'should parse empty lines' do
record = @provider.class.parse(' ').first
record[:record_type].should == :blank
end
it 'should parse comment lines' do
record = @provider.class.parse(' #--foo = bar').first
record[:record_type].should == :comment
end
end

36
nova/spec/unit/type/nova_config_spec.rb

@ -0,0 +1,36 @@
require File.join(File.dirname(__FILE__), '../../find_spec_utils.rb')
require 'puppet'
require 'puppet/type/nova_config'
describe 'Puppet::Type.type(:nova_config)' do
before :each do
@nova_config = Puppet::Type.type(:nova_config).new(:name => 'foo', :value => 'bar')
end
it 'should require a name' do
expect { Puppet::Type.type(:nova_config).new({}) }.should raise_error(Puppet::Error, 'Title or name must be provided')
end
it 'should not expect a name with whitespace' do
expect { Puppet::Type.type(:nova_config).new(:name => 'f oo') }.should raise_error(Puppet::Error, /Invalid value/)
end
it 'should not require a value when ensure is absent' do
Puppet::Type.type(:nova_config).new(:name => 'foo', :ensure => :absent)
end
it 'should require a value when ensure is present' do
expect { Puppet::Type.type(:nova_config).new(:name => 'foo', :ensure => :present) }.should raise_error(Puppet::Error, 'Property value must be set when ensure is present')
end
it 'should accept a valid value' do
@nova_config[:value] = 'bar'
@nova_config[:value].should == 'bar'
end
it 'should not accept a value with whitespace' do
expect { @nova_config[:value] = 'b ar' }.should raise_error(Puppet::Error, /Invalid value/)
end
it 'should accept valid ensure values' do
@nova_config[:ensure] = :present
@nova_config[:ensure].should == :present
@nova_config[:ensure] = :absent
@nova_config[:ensure].should == :absent
end
it 'should not accept invalid ensure values' do
expect { @nova_config[:ensure] = :latest}.should raise_error(Puppet::Error, /Invalid value/)
end
end
Loading…
Cancel
Save