spec: Added unit for cookiebutter and shared examples

This commit is contained in:
Sebastien Badia
2015-05-21 23:25:33 -07:00
parent f6b1f9294d
commit b8f04564eb
3 changed files with 94 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
shared_examples_for "a Puppet::Error" do |description|
it "with message matching #{description.inspect}" do
expect { is_expected.to have_class_count(1) }.to raise_error(Puppet::Error, description)
end
end

View File

@@ -0,0 +1,37 @@
# these tests are a little concerning b/c they are hacking around the
# modulepath, so these tests will not catch issues that may eventually arise
# related to loading these plugins.
# I could not, for the life of me, figure out how to programatcally set the modulepath
$LOAD_PATH.push(
File.join(
File.dirname(__FILE__),
'..',
'..',
'..',
'fixtures',
'modules',
'inifile',
'lib')
)
require 'spec_helper'
provider_class = Puppet::Type.type(:{{cookiecutter.project_name}}_config).provider(:ini_setting)
describe provider_class do
it 'should default to the default setting when no other one is specified' do
resource = Puppet::Type::{{cookiecutter.project_name}}_config.new(
{:name => 'DEFAULT/foo', :value => 'bar'}
)
provider = provider_class.new(resource)
expect(provider.section).to eq('DEFAULT')
expect(provider.setting).to eq('foo')
end
it 'should allow setting to be set explicitly' do
resource = Puppet::Type::{{cookiecutter.project_name}}_config.new(
{:name => 'dude/foo', :value => 'bar'}
)
provider = provider_class.new(resource)
expect(provider.section).to eq('dude')
expect(provider.setting).to eq('foo')
end
end

View File

@@ -0,0 +1,52 @@
require 'puppet'
require 'puppet/type/{{cookiecutter.project_name}}_config'
describe 'Puppet::Type.type(:{{cookiecutter.project_name}}_config)' do
before :each do
@{{cookiecutter.project_name}}_config = Puppet::Type.type(:{{cookiecutter.project_name}}_config).new(:name => 'DEFAULT/foo', :value => 'bar')
end
it 'should require a name' do
expect {
Puppet::Type.type(:{{cookiecutter.project_name}}_config).new({})
}.to raise_error(Puppet::Error, 'Title or name must be provided')
end
it 'should not expect a name with whitespace' do
expect {
Puppet::Type.type(:{{cookiecutter.project_name}}_config).new(:name => 'f oo')
}.to raise_error(Puppet::Error, /Parameter name failed/)
end
it 'should fail when there is no section' do
expect {
Puppet::Type.type(:{{cookiecutter.project_name}}_config).new(:name => 'foo')
}.to raise_error(Puppet::Error, /Parameter name failed/)
end
it 'should not require a value when ensure is absent' do
Puppet::Type.type(:{{cookiecutter.project_name}}_config).new(:name => 'DEFAULT/foo', :ensure => :absent)
end
it 'should accept a valid value' do
@{{cookiecutter.project_name}}_config[:value] = 'bar'
expect(@{{cookiecutter.project_name}}_config[:value]).to eq('bar')
end
it 'should not accept a value with whitespace' do
@{{cookiecutter.project_name}}_config[:value] = 'b ar'
expect(@{{cookiecutter.project_name}}_config[:value]).to eq('b ar')
end
it 'should accept valid ensure values' do
@{{cookiecutter.project_name}}_config[:ensure] = :present
expect(@{{cookiecutter.project_name}}_config[:ensure]).to eq(:present)
@{{cookiecutter.project_name}}_config[:ensure] = :absent
expect(@{{cookiecutter.project_name}}_config[:ensure]).to eq(:absent)
end
it 'should not accept invalid ensure values' do
expect {
@{{cookiecutter.project_name}}_config[:ensure] = :latest
}.to raise_error(Puppet::Error, /Invalid value/)
end
end