summaryrefslogtreecommitdiff
path: root/spec/functions/ensure_resource_spec.rb
blob: 5366205d572571d948b8613f66dfca9047b2fdc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
require 'spec_helper'

describe 'ensure_resource' do
  it { is_expected.not_to eq(nil) }
  it { is_expected.to run.with_params().and_raise_error(ArgumentError, /Must specify a type/) }
  it { is_expected.to run.with_params('type').and_raise_error(ArgumentError, /Must specify a title/) }
  if Puppet::Util::Package.versioncmp(Puppet.version, '4.6.0') >= 0
    it { is_expected.to run.with_params('type', 'title', {}, 'extras').and_raise_error(ArgumentError) }
  else
    it { is_expected.to run.with_params('type', 'title', {}, 'extras').and_raise_error(Puppet::ParseError) }
  end

  it {
    pending("should not accept numbers as arguments")
    is_expected.to run.with_params(1,2,3).and_raise_error(Puppet::ParseError)
  }

  context 'given an empty catalog' do
    describe 'after running ensure_resource("user", "username1", {})' do
      before { subject.call(['User', 'username1', {}]) }

      # this lambda is required due to strangeness within rspec-puppet's expectation handling
      it { expect(lambda { catalogue }).to contain_user('username1').without_ensure }
    end

    describe 'after running ensure_resource("user", "username1", { gid => undef })' do
      before { subject.call(['User', 'username1', { 'gid' => :undef }]) }

      # this lambda is required due to strangeness within rspec-puppet's expectation handling
      it { expect(lambda { catalogue }).to contain_user('username1').without_ensure }
      it { expect(lambda { catalogue }).to contain_user('username1').without_gid }
    end

    describe 'after running ensure_resource("user", "username1", { ensure => present, gid => undef })' do
      before { subject.call(['User', 'username1', { 'ensure' => 'present', 'gid' => :undef }]) }

      # this lambda is required due to strangeness within rspec-puppet's expectation handling
      it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
      it { expect(lambda { catalogue }).to contain_user('username1').without_gid }
    end
  end

  context 'given a catalog with UTF8 chars' do
    describe 'after running ensure_resource("user", "Şắოрŀễ Ţëם", {})' do
      before { subject.call(['User', 'Şắოрŀễ Ţëם', {}]) }

      # this lambda is required due to strangeness within rspec-puppet's expectation handling
      it { expect(lambda { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_ensure }
    end

    describe 'after running ensure_resource("user", "Şắოрŀễ Ţëם", { gid => undef })' do
      before { subject.call(['User', 'Şắოрŀễ Ţëם', { 'gid' => :undef }]) }

      # this lambda is required due to strangeness within rspec-puppet's expectation handling
      it { expect(lambda { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_ensure }
      it { expect(lambda { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_gid }
    end

    describe 'after running ensure_resource("user", "Şắოрŀễ Ţëם", { ensure => present, gid => undef })' do
      before { subject.call(['User', 'Şắოрŀễ Ţëם', { 'ensure' => 'present', 'gid' => :undef }]) }

      # this lambda is required due to strangeness within rspec-puppet's expectation handling
      it { expect(lambda { catalogue }).to contain_user('Şắოрŀễ Ţëם').with_ensure('present') }
      it { expect(lambda { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_gid }
    end
  end

  context 'given a catalog with "user { username1: ensure => present }"' do
    let(:pre_condition) { 'user { username1: ensure => present }' }

    describe 'after running ensure_resource("user", "username1", {})' do
      before { subject.call(['User', 'username1', {}]) }

      # this lambda is required due to strangeness within rspec-puppet's expectation handling
      it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
    end

    describe 'after running ensure_resource("user", "username2", {})' do
      before { subject.call(['User', 'username2', {}]) }

      # this lambda is required due to strangeness within rspec-puppet's expectation handling
      it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
      it { expect(lambda { catalogue }).to contain_user('username2').without_ensure }
    end

    describe 'after running ensure_resource("user", "username1", { gid => undef })' do
      before { subject.call(['User', 'username1', { 'gid' => :undef }]) }

      # this lambda is required due to strangeness within rspec-puppet's expectation handling
      it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
    end

    describe 'after running ensure_resource("user", ["username1", "username2"], {})' do
      before { subject.call(['User', ['username1', 'username2'], {}]) }

      # this lambda is required due to strangeness within rspec-puppet's expectation handling
      it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
      it { expect(lambda { catalogue }).to contain_user('username2').without_ensure }
    end

    describe 'when providing already set params' do
      let(:params) { { 'ensure' => 'present' } }
      before { subject.call(['User', ['username2', 'username3'], params]) }

      # this lambda is required due to strangeness within rspec-puppet's expectation handling
      it { expect(lambda { catalogue }).to contain_user('username1').with(params) }
      it { expect(lambda { catalogue }).to contain_user('username2').with(params) }
    end

    context 'when trying to add params' do
      it { is_expected.to run \
        .with_params('User', 'username1', { 'ensure' => 'present', 'shell' => true }) \
        .and_raise_error(Puppet::Resource::Catalog::DuplicateResourceError, /User\[username1\] is already declared/)
      }
    end
  end
end