File: exec_spec.rb

package info (click to toggle)
puppet-agent 8.10.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,404 kB
  • sloc: ruby: 286,820; sh: 492; xml: 116; makefile: 88; cs: 68
file content (114 lines) | stat: -rw-r--r-- 3,136 bytes parent folder | download | duplicates (2)
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
require 'spec_helper'

require 'puppet_spec/files'

describe Puppet::Type.type(:exec), unless: Puppet::Util::Platform.jruby? do
  include PuppetSpec::Files

  let(:catalog) { Puppet::Resource::Catalog.new }
  let(:path) { tmpfile('exec_provider') }

  before :each do
    catalog.host_config = false
  end

  shared_examples_for 'a valid exec resource' do
    it "should execute the command" do
      exec = described_class.new :command => command, :path => ENV['PATH']

      catalog.add_resource exec
      catalog.apply

      expect(File.read(path)).to eq('foo')
    end

    it "should not execute the command if onlyif returns non-zero" do
      exec = described_class.new(
        :command => command,
        :onlyif => "ruby -e 'exit 44'",
        :path => ENV['PATH']
      )

      catalog.add_resource exec
      catalog.apply

      expect(Puppet::FileSystem.exist?(path)).to be_falsey
    end

    it "should execute the command if onlyif returns zero" do
      exec = described_class.new(
        :command => command,
        :onlyif => "ruby -e 'exit 0'",
        :path => ENV['PATH']
      )

      catalog.add_resource exec
      catalog.apply

      expect(File.read(path)).to eq('foo')
    end

    it "should execute the command if unless returns non-zero" do
      exec = described_class.new(
        :command => command,
        :unless => "ruby -e 'exit 45'",
        :path => ENV['PATH']
      )

      catalog.add_resource exec
      catalog.apply

      expect(File.read(path)).to eq('foo')
    end

    it "should not execute the command if unless returns zero" do
      exec = described_class.new(
        :command => command,
        :unless => "ruby -e 'exit 0'",
        :path => ENV['PATH']
      )

      catalog.add_resource exec
      catalog.apply

      expect(Puppet::FileSystem.exist?(path)).to be_falsey
    end
  end

  context 'when an exec sends an EOF' do
    let(:command) { ["/bin/bash", "-c", "exec /bin/sleep 1 >/dev/null 2>&1"] }

    it 'should not take significant user time' do
      exec = described_class.new :command => command, :path => ENV['PATH']
      catalog.add_resource exec
      timed_apply = Benchmark.measure { catalog.apply }
      # In testing I found the user time before the patch in 4f35fd262e to be above
      # 0.3, after the patch it was consistently below 0.1 seconds.
      expect(timed_apply.utime).to be < 0.3
    end
  end

  context 'when command is a string' do
    let(:command) { "ruby -e 'File.open(\"#{path}\", \"w\") { |f| f.print \"foo\" }'" }

    it_behaves_like 'a valid exec resource'
  end

  context 'when command is an array' do
    let(:command) { ['ruby', '-e', "File.open(\"#{path}\", \"w\") { |f| f.print \"foo\" }"] }

    it_behaves_like 'a valid exec resource'

    context 'when is invalid' do
      let(:command) { [ "ruby -e 'puts 1'" ] }

      it 'logs error' do
        exec = described_class.new :command => command, :path => ENV['PATH']
        catalog.add_resource exec
        logs = catalog.apply.report.logs

        expect(logs[0].message).to eql("Could not find command 'ruby -e 'puts 1''")
      end
    end
  end
end