summaryrefslogtreecommitdiff
path: root/spec/functional/resource/execute_spec.rb
blob: cebcc52fcfea9d26a5590457a75e204114099190 (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
118
#
# Author:: Serdar Sutay (<serdar@opscode.com>)
# Copyright:: Copyright (c) 2014 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'spec_helper'
require 'functional/resource/base'

describe Chef::Resource::Execute do
  let(:resource) {
    resource = Chef::Resource::Execute.new("foo_resource", run_context)
    resource.command("echo hello")
    resource
  }

  describe "when guard is ruby block" do
    it "guard can still run" do
      resource.only_if { true }
      resource.run_action(:run)
      expect(resource).to be_updated_by_last_action
    end
  end

  describe "when parent resource sets :cwd" do
    let(:guard) { %{ruby -e 'exit 1 unless File.exists?("./big_json_plus_one.json")'} }

    it "guard inherits :cwd from resource and runs" do
      resource.cwd CHEF_SPEC_DATA
      resource.only_if guard
      resource.run_action(:run)
      expect(resource).to be_updated_by_last_action
    end

    it "guard inherits :cwd from resource and does not run" do
      resource.cwd CHEF_SPEC_DATA
      resource.not_if guard
      resource.run_action(:run)
      expect(resource).not_to be_updated_by_last_action
    end
  end

  # We use ruby command so that we don't need to deal with platform specific
  # commands while testing execute resource. We set it so that the resource
  # will be updated if the ENV variable is set to what we are intending
  #
  # FIXME: yeah, but invoking ruby is slow...
  describe "when parent resource sets :environment" do
    before do
      resource.environment({
        "SAWS_SECRET"  => "supersecret",
        "SAWS_KEY"     => "qwerty",
      })
    end

    it "guard inherits :environment value from resource and runs" do
      resource.only_if %{ruby -e 'exit 1 if ENV["SAWS_SECRET"] != "supersecret"'}
      resource.run_action(:run)
      expect(resource).to be_updated_by_last_action
    end

    it "guard inherits :environment value from resource and does not run" do
      resource.only_if %{ruby -e 'exit 1 if ENV["SAWS_SECRET"] == "supersecret"'}
      resource.run_action(:run)
      expect(resource).not_to be_updated_by_last_action
    end

    it "guard adds additional values in its :environment and runs" do
      resource.only_if %{ruby -e 'exit 1 if ENV["SGCE_SECRET"] != "regularsecret"'}, {
        :environment => { 'SGCE_SECRET' => "regularsecret" }
      }
      resource.run_action(:run)
      expect(resource).to be_updated_by_last_action
    end

    it "guard adds additional values in its :environment and does not run" do
      resource.only_if %{ruby -e 'exit 1 if ENV["SGCE_SECRET"] == "regularsecret"'}, {
        :environment => { 'SGCE_SECRET' => "regularsecret" }
      }
      resource.run_action(:run)
      expect(resource).not_to be_updated_by_last_action
    end

    it "guard overwrites value with its :environment and runs" do
      resource.only_if %{ruby -e 'exit 1 if ENV["SAWS_SECRET"] != "regularsecret"'}, {
        :environment => { 'SAWS_SECRET' => "regularsecret" }
      }
      resource.run_action(:run)
      expect(resource).to be_updated_by_last_action
    end

    it "guard overwrites value with its :environment and does not runs" do
      resource.only_if %{ruby -e 'exit 1 if ENV["SAWS_SECRET"] == "regularsecret"'}, {
        :environment => { 'SAWS_SECRET' => "regularsecret" }
      }
      resource.run_action(:run)
      expect(resource).not_to be_updated_by_last_action
    end
  end

  it "times out when a timeout is set on the resource" do
    resource.command %{ruby -e 'sleep 600'}
    resource.timeout 0.1
    expect { resource.run_action(:run) }.to raise_error(Mixlib::ShellOut::CommandTimeout)
  end
end