summaryrefslogtreecommitdiff
path: root/spec/functions/validate_cmd_spec.rb
blob: 7cb9782d60ec9303c577c981f57a0fee6e655780 (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
#! /usr/bin/env ruby -S rspec
require 'spec_helper'

TESTEXE = File.exists?('/usr/bin/test') ? '/usr/bin/test' : '/bin/test'
TOUCHEXE = File.exists?('/usr/bin/touch') ? '/usr/bin/touch' : '/bin/touch'

describe Puppet::Parser::Functions.function(:validate_cmd) do
  let(:scope) { PuppetlabsSpec::PuppetInternals.scope }

  subject do
    function_name = Puppet::Parser::Functions.function(:validate_cmd)
    scope.method(function_name)
  end

  context 'with no % placeholder' do
    describe "with an explicit failure message" do
      it "prints the failure message on error" do
        expect {
          subject.call ['', '/bin/false', 'failure message!']
        }.to raise_error Puppet::ParseError, /failure message!/
      end
    end

    describe "on validation failure" do
      it "includes the command error output" do
        expect {
          subject.call ['', "#{TOUCHEXE} /cant/touch/this"]
        }.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/
      end

      it "includes the command return value" do
        expect {
          subject.call ['', '/cant/run/this']
        }.to raise_error Puppet::ParseError, /returned 1\b/
      end
    end

    describe "when performing actual validation" do
      it "can positively validate file content" do
        expect { subject.call ["non-empty", "#{TESTEXE} -s"] }.to_not raise_error
      end

      it "can negatively validate file content" do
        expect {
          subject.call ["", "#{TESTEXE} -s"]
        }.to raise_error Puppet::ParseError, /failed to validate.*test -s/
      end
    end
  end

  context 'with % placeholder' do
    describe "with an explicit failure message" do
      it "prints the failure message on error" do
        expect {
          subject.call ['', '/bin/false % -f', 'failure message!']
        }.to raise_error Puppet::ParseError, /failure message!/
      end
    end
    describe "on validation failure" do
      it "includes the command error output" do
        expect {
          subject.call ['', "#{TOUCHEXE} /cant/touch/this"]
        }.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/
      end

      it "includes the command return value" do
        expect {
          subject.call ['', '/cant/run/this % -z']
        }.to raise_error Puppet::ParseError, /Execution of '\/cant\/run\/this .+ -z' returned 1/
      end
    end

    describe "when performing actual validation" do
      it "can positively validate file content" do
        expect { subject.call ["non-empty", "#{TESTEXE} -s %"] }.to_not raise_error
      end

      it "can negatively validate file content" do
        expect {
          subject.call ["", "#{TESTEXE} -s %"]
        }.to raise_error Puppet::ParseError, /failed to validate.*test -s/
      end
    end
  end
end