Update puppetlabs/stdlib module
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / validate_augeas_spec.rb
1 require 'spec_helper'
2
3 describe 'validate_augeas' do
4   if Puppet.features.augeas?
5     describe 'signature validation' do
6       it { is_expected.not_to eq(nil) }
7       it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
8       it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
9       it { is_expected.to run.with_params('', '', [], '', 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
10       it { is_expected.to run.with_params('one', 'one', 'MSG to User', '4th arg').and_raise_error(NoMethodError) }
11     end
12
13     describe 'valid inputs' do
14       inputs = [
15         ["root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns'],
16         ["proc /proc   proc    nodev,noexec,nosuid     0       0\n", 'Fstab.lns'],
17       ]
18
19       inputs.each do |input|
20         it { is_expected.to run.with_params(*input) }
21       end
22     end
23
24     describe 'valid inputs which fail augeas validation' do
25       # The intent here is to make sure valid inputs raise exceptions when they
26       # don't specify an error message to display.  This is the behvior in
27       # 2.2.x and prior.
28       inputs = [
29         ["root:x:0:0:root\n", 'Passwd.lns'],
30         ["127.0.1.1\n", 'Hosts.lns'],
31       ]
32
33       inputs.each do |input|
34         it { is_expected.to run.with_params(*input).and_raise_error(Puppet::ParseError, %r{validate_augeas.*?matched less than it should}) }
35       end
36     end
37
38     describe 'when specifying nice error messages' do
39       # The intent here is to make sure the function returns the 4th argument
40       # in the exception thrown
41       inputs = [
42         ["root:x:0:0:root\n", 'Passwd.lns', [], 'Failed to validate passwd content'],
43         ["127.0.1.1\n", 'Hosts.lns', [], 'Wrong hosts content'],
44       ]
45
46       inputs.each do |input|
47         it { is_expected.to run.with_params(*input).and_raise_error(Puppet::ParseError, %r{#{input[3]}}) }
48       end
49     end
50
51     describe 'matching additional tests' do
52       inputs = [
53         ["root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/foobar']],
54         ["root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/root/shell[.="/bin/sh"]', 'foobar']],
55       ]
56
57       inputs.each do |input|
58         it { is_expected.to run.with_params(*input) }
59       end
60     end
61
62     describe 'failing additional tests' do
63       inputs = [
64         ["foobar:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/foobar']],
65         ["root:x:0:0:root:/root:/bin/sh\n", 'Passwd.lns', ['$file/root/shell[.="/bin/sh"]', 'foobar']],
66       ]
67
68       inputs.each do |input|
69         it { is_expected.to run.with_params(*input).and_raise_error(Puppet::ParseError, %r{testing path}) }
70       end
71     end
72   else
73     skip 'ruby-augeas not installed'
74   end
75 end