diff options
Diffstat (limited to 'spec/functions/ensure_resource_spec.rb')
-rwxr-xr-x[-rw-r--r--] | spec/functions/ensure_resource_spec.rb | 123 |
1 files changed, 86 insertions, 37 deletions
diff --git a/spec/functions/ensure_resource_spec.rb b/spec/functions/ensure_resource_spec.rb index 2e8aefc..33bcac0 100644..100755 --- a/spec/functions/ensure_resource_spec.rb +++ b/spec/functions/ensure_resource_spec.rb @@ -1,64 +1,113 @@ #! /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 { should run.with_params().and_raise_error(ArgumentError) } - it { should run.with_params(['type']).and_raise_error(ArgumentError) } + 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 "should contain the the ensured resources" do - subject.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}' + 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 - # these first three should not fail - it { should run.with_params('User', 'dan', {}) } - it { should run.with_params('User', 'dan', '') } - it { should run.with_params('User', 'dan', {'ensure' => 'present'}) } - it { should run.with_params('User', 'dan', {'ensure' => 'present', 'managehome' => false}) } - # test that this fails - it { should run.with_params('User', 'dan', {'ensure' => 'absent', 'managehome' => false}).and_raise_error(Puppet::Error) } + 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 describe 'when an array of new resources are passed in' do - it "should contain the ensured resources" do - subject.should run.with_params('User', ['dan', 'alex'], {}) - compiler.catalog.resource('User[dan]').to_s.should == 'User[dan]' - compiler.catalog.resource('User[alex]').to_s.should == 'User[alex]' + let :catalog do + compile_to_catalog("ensure_resource('User', ['dan', 'alex'], {})") + end + + it 'should contain the ensured resources' do + expect(catalog.resource('User[dan]').to_s).to eq('User[dan]') + expect(catalog.resource('User[alex]').to_s).to eq('User[alex]') end end describe 'when an array of existing resources is compared against existing resources' do - let :pre_condition do - 'user { "dan": ensure => present; "alex": ensure => present }' + pp = <<-EOS + user { 'dan': ensure => present; 'alex': ensure => present } + ensure_resource('User', ['dan', 'alex'], {}) + EOS + + let :catalog do + compile_to_catalog(pp) end - it "should return the existing resources" do - subject.should run.with_params('User', ['dan', 'alex'], {}) - compiler.catalog.resource('User[dan]').to_s.should == 'User[dan]' - compiler.catalog.resource('User[alex]').to_s.should == 'User[alex]' + + it 'should return the existing resources' do + expect(catalog.resource('User[dan]').to_s).to eq('User[dan]') + expect(catalog.resource('User[alex]').to_s).to eq('User[alex]') end end - describe 'when compared against existing resources with attributes' do - let :pre_condition do - 'user { "dan": ensure => present; "alex": ensure => present }' + describe 'works when compared against existing resources with attributes' do + [ + "ensure_resource('User', ['dan', 'alex'], {})", + "ensure_resource('User', ['dan', 'alex'], '')", + "ensure_resource('User', ['dan', 'alex'], {'ensure' => 'present'})", + ].each do |ensure_resource| + pp = <<-EOS + user { 'dan': ensure => present; 'alex': ensure => present } + #{ensure_resource} + EOS + + it { expect { compile_to_catalog(pp) }.to_not raise_error } end - # These should not fail - it { should run.with_params('User', ['dan', 'alex'], {}) } - it { should run.with_params('User', ['dan', 'alex'], '') } - it { should run.with_params('User', ['dan', 'alex'], {'ensure' => 'present'}) } - # This should fail - it { should run.with_params('User', ['dan', 'alex'], {'ensure' => 'absent'}).and_raise_error(Puppet::Error) } end + + describe 'fails when compared against existing resources with conflicting attributes' do + pp = <<-EOS + user { 'dan': ensure => present; 'alex': ensure => present } + ensure_resource('User', ['dan', 'alex'], {'ensure' => 'absent'}) + EOS + + it { expect { compile_to_catalog(pp) }.to raise_error } + end + end |