diff options
Diffstat (limited to 'spec/functions')
-rw-r--r-- | spec/functions/ensure_packages_spec.rb | 60 | ||||
-rw-r--r-- | spec/functions/ensure_resource_spec.rb | 75 |
2 files changed, 90 insertions, 45 deletions
diff --git a/spec/functions/ensure_packages_spec.rb b/spec/functions/ensure_packages_spec.rb index 1c2a328..4163d69 100644 --- a/spec/functions/ensure_packages_spec.rb +++ b/spec/functions/ensure_packages_spec.rb @@ -2,41 +2,65 @@ require 'spec_helper' require 'rspec-puppet' +require 'puppet_spec/compiler' describe 'ensure_packages' do - let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + include PuppetSpec::Compiler + + before :each do + Puppet::Parser::Functions.autoloader.loadall + Puppet::Parser::Functions.function(:ensure_packages) + Puppet::Parser::Functions.function(:ensure_resource) + Puppet::Parser::Functions.function(:defined_with_params) + Puppet::Parser::Functions.function(:create_resources) + end + + let :node do Puppet::Node.new('localhost') end + let :compiler do Puppet::Parser::Compiler.new(node) end + let :scope do + if Puppet.version.to_f >= 3.0 + Puppet::Parser::Scope.new(compiler) + else + newscope = Puppet::Parser::Scope.new + newscope.compiler = compiler + newscope.source = Puppet::Resource::Type.new(:node, :localhost) + newscope + end + end describe 'argument handling' do it 'fails with no arguments' do - should run.with_params().and_raise_error(Puppet::ParseError) + expect { + scope.function_ensure_packages([]) + }.to raise_error(Puppet::ParseError, /0 for 1/) end - it 'requires an array' do - lambda { scope.function_ensure_packages([['foo']]) }.should_not raise_error - end - it 'fails when given a string' do - should run.with_params('foo').and_raise_error(Puppet::ParseError) + + it 'accepts an array of values' do + scope.function_ensure_packages([['foo']]) end end - context 'given a catalog containing Package[puppet]{ensure => absent}' do - let :pre_condition do - 'package { puppet: ensure => absent }' + context 'given a catalog with puppet package => absent' do + let :catalog do + compile_to_catalog(<<-EOS + ensure_packages(['facter']) + package { puppet: ensure => absent } + EOS + ) end - # NOTE: should run.with_params has the side effect of making the compiler - # available to the test harness. it 'has no effect on Package[puppet]' do - should run.with_params(['puppet']) - rsrc = compiler.catalog.resource('Package[puppet]') - rsrc.to_hash.should == {:ensure => "absent"} + expect(catalog.resource(:package, 'puppet')['ensure']).to eq('absent') end end context 'given a clean catalog' do + let :catalog do + compile_to_catalog('ensure_packages(["facter"])') + end + it 'declares package resources with ensure => present' do - should run.with_params(['facter']) - rsrc = compiler.catalog.resource('Package[facter]') - rsrc.to_hash.should == {:name => "facter", :ensure => "present"} + expect(catalog.resource(:package, 'facter')['ensure']).to eq('present') end end end diff --git a/spec/functions/ensure_resource_spec.rb b/spec/functions/ensure_resource_spec.rb index 611666e..430691c 100644 --- a/spec/functions/ensure_resource_spec.rb +++ b/spec/functions/ensure_resource_spec.rb @@ -1,40 +1,61 @@ -#! /usr/bin/env ruby -S rspec require 'spec_helper' - require 'rspec-puppet' +require 'puppet_spec/compiler' + describe 'ensure_resource' do + include PuppetSpec::Compiler + + before :all do + Puppet::Parser::Functions.autoloader.loadall + Puppet::Parser::Functions.function(:ensure_packages) + end + + let :node do Puppet::Node.new('localhost') end + let :compiler do Puppet::Parser::Compiler.new(node) end + let :scope do Puppet::Parser::Scope.new(compiler) end + describe 'when a type or title is not specified' do - it do - should run.with_params().and_raise_error(ArgumentError) - should run.with_params(['type']).and_raise_error(ArgumentError) - end + it { expect { scope.function_ensure_resource([]) }.to raise_error } + it { expect { scope.function_ensure_resource(['type']) }.to raise_error } end + describe 'when compared against a resource with no attributes' do - let :pre_condition do - 'user { "dan": }' + let :catalog do + compile_to_catalog(<<-EOS + user { "dan": } + ensure_resource('user', 'dan', {}) + EOS + ) end - it do - should run.with_params('user', 'dan', {}) - compiler.catalog.resource('User[dan]').to_s.should == 'User[dan]' + + it 'should contain the the ensured resources' do + expect(catalog.resource(:user, 'dan').to_s).to eq('User[dan]') end end - describe 'when compared against a resource with attributes' do - let :pre_condition do - 'user { "dan": ensure => present, shell => "/bin/csh", managehome => false}' - end - it do - # these first three should not fail - should run.with_params('User', 'dan', {}) - should run.with_params('User', 'dan', '') - should run.with_params('User', 'dan', {'ensure' => 'present'}) - should run.with_params('User', 'dan', - {'ensure' => 'present', 'managehome' => false} - ) - # test that this fails - should run.with_params('User', 'dan', - {'ensure' => 'absent', 'managehome' => false} - ).and_raise_error(Puppet::Error) + describe 'works when compared against a resource with non-conflicting attributes' do + [ + "ensure_resource('User', 'dan', {})", + "ensure_resource('User', 'dan', '')", + "ensure_resource('User', 'dan', {'ensure' => 'present'})", + "ensure_resource('User', 'dan', {'ensure' => 'present', 'managehome' => false})" + ].each do |ensure_resource| + pp = <<-EOS + user { "dan": ensure => present, shell => "/bin/csh", managehome => false} + #{ensure_resource} + EOS + + it { expect { compile_to_catalog(pp) }.to_not raise_error } end end + + describe 'fails when compared against a resource with conflicting attributes' do + pp = <<-EOS + user { "dan": ensure => present, shell => "/bin/csh", managehome => false} + ensure_resource('User', 'dan', {'ensure' => 'absent', 'managehome' => false}) + EOS + + it { expect { compile_to_catalog(pp) }.to raise_error } + end + end |