summaryrefslogtreecommitdiff
path: root/spec/unit/provider/execute_spec.rb
blob: cbcb9373c48cca22c78ddf3296f07d0e4d68d1de (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#
# Author:: Prajakta Purohit (<prajakta@opscode.com>)
# Copyright:: Copyright (c) 2008 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'

describe Chef::Provider::Execute do

  let(:node) { Chef::Node.new }
  let(:events) { Chef::EventDispatch::Dispatcher.new }
  let(:run_context) { Chef::RunContext.new(node, {}, events) }
  let(:provider) { Chef::Provider::Execute.new(new_resource, run_context) }
  let(:current_resource) { Chef::Resource::Ifconfig.new("foo_resource", run_context) }

  let(:opts) do
    {
      timeout:      3600,
      returns:      0,
      log_level:    :info,
      log_tag:      new_resource.to_s,
      live_stream:  STDOUT,
    }
  end

  let(:new_resource) { Chef::Resource::Execute.new("foo_resource", run_context) }

  before do
    allow(ChefConfig).to receive(:windows?) { false }
    @original_log_level = Chef::Log.level
    Chef::Log.level = :info
    allow(STDOUT).to receive(:tty?).and_return(true)
  end

  after do
    Chef::Log.level = @original_log_level
  end

  describe "#initialize" do
    it "should return a Chef::Provider::Execute provider" do
      expect(provider.class).to eql(Chef::Provider::Execute)
    end
  end

  describe "#load_current_resource" do
    before do
      expect(Chef::Resource::Execute).to receive(:new).with(new_resource.name).and_return(current_resource)
    end

    it "should return the current resource" do
      expect(provider.load_current_resource).to eql(current_resource)
    end

    it "our timeout should default to 3600" do
      provider.load_current_resource
      expect(provider.timeout).to eql(3600)
    end
  end

  describe "#action_run" do
    it "runs shell_out with the default options" do
      expect(provider).to receive(:shell_out!).with(new_resource.name, opts)
      expect(provider).to receive(:converge_by).with("execute foo_resource").and_call_original
      expect(Chef::Log).not_to receive(:warn)
      provider.run_action(:run)
      expect(new_resource).to be_updated
    end

    it "if you pass a command attribute, it runs the command" do
      new_resource.command "/usr/argelbargle/bin/oogachacka 12345"
      expect(provider).to receive(:shell_out!).with(new_resource.command, opts)
      expect(provider).to receive(:converge_by).with("execute #{new_resource.command}").and_call_original
      expect(Chef::Log).not_to receive(:warn)
      provider.run_action(:run)
      expect(new_resource).to be_updated
    end

    it "should honor sensitive attribute" do
      new_resource.sensitive true
      # Since the resource is sensitive, it should not have :live_stream set
      opts.delete(:live_stream)
      expect(provider).to receive(:shell_out!).with(new_resource.name, opts)
      expect(provider).to receive(:converge_by).with("execute sensitive resource").and_call_original
      expect(Chef::Log).not_to receive(:warn)
      provider.run_action(:run)
      expect(new_resource).to be_updated
    end

    it "should do nothing if the sentinel file exists" do
      new_resource.creates "/foo_resource"
      expect(FileTest).to receive(:exist?).with(new_resource.creates).and_return(true)
      expect(provider).not_to receive(:shell_out!)
      expect(Chef::Log).not_to receive(:warn)
      provider.run_action(:run)
      expect(new_resource).not_to be_updated
    end

    describe "when the user specifies a relative path without cwd" do
      before do
        expect(new_resource.cwd).to be_falsey
        new_resource.creates "foo_resource"
      end

      it "should warn in Chef-12", :chef_lt_13_only do
        expect(Chef::Log).to receive(:warn).with(/relative path/)
        expect(FileTest).to receive(:exist?).with(new_resource.creates).and_return(true)
        expect(provider).not_to receive(:shell_out!)
        provider.run_action(:run)
        expect(new_resource).not_to be_updated
      end

      it "should raise if user specified relative path without cwd for Chef-13", :chef_gte_13_only do
        expect(Chef::Log).to receive(:warn).with(/relative path/)
        expect(FileTest).to receive(:exist?).with(new_resource.creates).and_return(true)
        expect(provider).not_to receive(:shell_out!)
        expect { provider.run_action(:run) }.to raise_error  # @todo: add a real error for Chef-13
      end
    end

    it "should respect cwd options for 'creates'" do
      new_resource.cwd "/tmp"
      new_resource.creates "foo_resource"
      expect(FileTest).not_to receive(:exist?).with(new_resource.creates)
      expect(FileTest).to receive(:exist?).with(File.join("/tmp", new_resource.creates)).and_return(true)
      expect(Chef::Log).not_to receive(:warn)
      expect(provider).not_to receive(:shell_out!)

      provider.run_action(:run)
      expect(new_resource).not_to be_updated
    end

    it "should unset the live_stream if STDOUT is not a tty" do
      expect(STDOUT).to receive(:tty?).and_return(false)
      opts.delete(:live_stream)
      expect(provider).to receive(:shell_out!).with(new_resource.name, opts)
      expect(provider).to receive(:converge_by).with("execute foo_resource").and_call_original
      expect(Chef::Log).not_to receive(:warn)
      provider.run_action(:run)
      expect(new_resource).to be_updated
    end

    it "should unset the live_stream if chef is running as a daemon" do
      allow(Chef::Config).to receive(:[]).and_call_original
      expect(Chef::Config).to receive(:[]).with(:daemon).and_return(true)
      opts.delete(:live_stream)
      expect(provider).to receive(:shell_out!).with(new_resource.name, opts)
      expect(provider).to receive(:converge_by).with("execute foo_resource").and_call_original
      expect(Chef::Log).not_to receive(:warn)
      provider.run_action(:run)
      expect(new_resource).to be_updated
    end

    it "should unset the live_stream if we are not running with a log level of at least :info" do
      expect(Chef::Log).to receive(:info?).and_return(false)
      opts.delete(:live_stream)
      expect(provider).to receive(:shell_out!).with(new_resource.name, opts)
      expect(provider).to receive(:converge_by).with("execute foo_resource").and_call_original
      expect(Chef::Log).not_to receive(:warn)
      provider.run_action(:run)
      expect(new_resource).to be_updated
    end

    describe "when an alternate user identity is specified" do
      before do
        allow(provider).to receive(:shell_out!).and_return(nil)
      end

      context "when running on Windows" do
        before do
          allow(::Chef::Platform).to receive(:windows?).and_return(true)
        end

        context "when the username is specified" do
          before do
            new_resource.user('starchild')
          end

          context "when the domain is specified" do
            before do
              new_resource.domain('mydomain')
            end

            it "should raise an error if the password is not specified" do
              expect(new_resource).to receive(:password).at_least(1).times.and_return(nil)
              expect { provider.run_action(:run) }.to raise_error(ArgumentError)
            end

            it "should not raise an error if the password is specified" do
              expect(new_resource).to receive(:password).at_least(1).times.and_return('we.funk!')
              expect { provider.run_action(:run) }.not_to raise_error
            end
          end

          context "when the domain is not specified" do
            before do
              expect(new_resource).to receive(:domain).at_least(1).times.and_return(nil)
            end

            it "should raise an error if the password is not specified" do
              expect(new_resource).to receive(:password).at_least(1).times.and_return(nil)
              expect { provider.run_action(:run) }.to raise_error(ArgumentError)
            end

            it "should not raise an error if the password is specified" do
              expect(new_resource).to receive(:password).at_least(1).times.and_return('we.funk!')
              expect { provider.run_action(:run) }.not_to raise_error

            end
          end
        end

        context "when the username is not specified" do
          before do
            expect(new_resource).to receive(:user).at_least(1).times.and_return(nil)
          end

          it "should raise an error if the password is specified" do
            expect(new_resource).to receive(:password).at_least(1).times.and_return('we.funk!')
            expect { provider.run_action(:run) }.to raise_error(ArgumentError)
          end

          it "should raise an error if the domain is specified" do
            expect(new_resource).to receive(:domain).at_least(1).times.and_return('mothership')
            expect { provider.run_action(:run) }.to raise_error(ArgumentError)
          end

          it "should raise an error if the domain and password are specified" do
            expect(new_resource).to receive(:password).at_least(1).times.and_return('we.funk!')
            expect(new_resource).to receive(:domain).at_least(1).times.and_return('mothership')
            expect { provider.run_action(:run) }.to raise_error(ArgumentError)
          end
        end
      end

      context "when not running on Windows" do
        before do
          allow(::Chef::Platform).to receive(:windows?).and_return(false)
        end

        it "should not raise an error if the user is specified" do
          new_resource.user('starchild')
        end

        it "should raise an error if the password is specified" do
          expect(new_resource).to receive(:password).and_return('we.funk!')
          expect { provider.run_action(:run) }.to raise_error(Chef::Exceptions::UnsupportedPlatform)
        end

        it "should raise an error if the domain is specified" do
          expect(new_resource).to receive(:domain).and_return('we.funk!')
          expect { provider.run_action(:run) }.to raise_error(Chef::Exceptions::UnsupportedPlatform)
        end
      end
    end

  end
end