]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/spec/functions/validate_augeas_spec.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / validate_augeas_spec.rb
1 #! /usr/bin/env ruby -S rspec
2 require 'spec_helper'
3
4 describe Puppet::Parser::Functions.function(:validate_augeas), :if => Puppet.features.augeas? do
5   let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6
7   # The subject of these examplres is the method itself.
8   subject do
9     # This makes sure the function is loaded within each test
10     function_name = Puppet::Parser::Functions.function(:validate_augeas)
11     scope.method(function_name)
12   end
13
14   context 'Using Puppet::Parser::Scope.new' do
15
16     describe 'Garbage inputs' do
17       inputs = [
18         [ nil ],
19         [ [ nil ] ],
20         [ { 'foo' => 'bar' } ],
21         [ { } ],
22         [ '' ],
23         [ "one", "one", "MSG to User", "4th arg" ],
24       ]
25
26       inputs.each do |input|
27         it "validate_augeas(#{input.inspect}) should fail" do
28           expect { subject.call [input] }.to raise_error Puppet::ParseError
29         end
30       end
31     end
32
33     describe 'Valid inputs' do
34       inputs = [
35         [ "root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns' ],
36         [ "proc /proc   proc    nodev,noexec,nosuid     0       0\n", 'Fstab.lns'],
37       ]
38
39       inputs.each do |input|
40         it "validate_augeas(#{input.inspect}) should not fail" do
41           expect { subject.call input }.not_to raise_error
42         end
43       end
44     end
45
46     describe "Valid inputs which should raise an exception without a message" do
47       # The intent here is to make sure valid inputs raise exceptions when they
48       # don't specify an error message to display.  This is the behvior in
49       # 2.2.x and prior.
50       inputs = [
51         [ "root:x:0:0:root\n", 'Passwd.lns' ],
52         [ "127.0.1.1\n", 'Hosts.lns' ],
53       ]
54
55       inputs.each do |input|
56         it "validate_augeas(#{input.inspect}) should fail" do
57           expect { subject.call input }.to raise_error /validate_augeas.*?matched less than it should/
58         end
59       end
60     end
61
62     describe "Nicer Error Messages" do
63       # The intent here is to make sure the function returns the 4th argument
64       # in the exception thrown
65       inputs = [
66         [ "root:x:0:0:root\n", 'Passwd.lns', [], 'Failed to validate passwd content' ],
67         [ "127.0.1.1\n", 'Hosts.lns', [], 'Wrong hosts content' ],
68       ]
69
70       inputs.each do |input|
71         it "validate_augeas(#{input.inspect}) should fail" do
72           expect { subject.call input }.to raise_error /#{input[3]}/
73         end
74       end
75     end
76
77     describe "Passing simple unit tests" do
78       inputs = [
79         [ "root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/foobar']],
80         [ "root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/root/shell[.="/bin/sh"]', 'foobar']],
81       ]
82
83       inputs.each do |input|
84         it "validate_augeas(#{input.inspect}) should fail" do
85           expect { subject.call input }.not_to raise_error
86         end
87       end
88     end
89
90     describe "Failing simple unit tests" do
91       inputs = [
92         [ "foobar:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/foobar']],
93         [ "root:x:0:0:root:/root:/bin/sh\n", 'Passwd.lns', ['$file/root/shell[.="/bin/sh"]', 'foobar']],
94       ]
95
96       inputs.each do |input|
97         it "validate_augeas(#{input.inspect}) should fail" do
98           expect { subject.call input }.to raise_error /testing path/
99         end
100       end
101     end
102   end
103 end