Skip to content

Commit c4bb9cd

Browse files
committed
Updating the script provider to set the ownership of the tempfile via a Chef::Resource::File; adding a Chef::Resource#run_action method. Closes CHEF-57
1 parent 4c2bfff commit c4bb9cd

4 files changed

Lines changed: 124 additions & 0 deletions

File tree

chef/lib/chef/provider/script.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ def action_run
2727
tf = Tempfile.new("chef-script")
2828
tf.puts(@new_resource.code)
2929
tf.close
30+
31+
fr = Chef::Resource::File.new(tf.path, nil, @node)
32+
fr.owner(@new_resource.user)
33+
fr.group(@new_resource.group)
34+
fr.run_action(:create)
35+
3036
@new_resource.command("#{@new_resource.interpreter} #{tf.path}")
3137
super
3238
end

chef/lib/chef/resource.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ def not_if(arg=nil, &blk)
221221
@not_if
222222
end
223223

224+
def run_action(action)
225+
provider_class = Chef::Platform.find_provider_for_node(@node, self)
226+
provider = provider_class.new(@node, self)
227+
provider.load_current_resource
228+
provider.send("action_#{action}")
229+
end
230+
224231
private
225232

226233
def check_timing(timing)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#
2+
# Author:: Adam Jacob (adam@opscode.com)
3+
# Copyright:: Copyright (c) 2009 Opscode
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
20+
21+
describe Chef::Provider::Script, "action_run" do
22+
before(:each) do
23+
@node = mock("Chef::Node", :null_object => true)
24+
@new_resource = mock("Chef::Resource::Script",
25+
:null_object => true,
26+
:code => "$| = 1; print 'i like beans'",
27+
:interpreter => 'perl',
28+
:user => nil,
29+
:group => nil
30+
)
31+
@tempfile = mock("Tempfile",
32+
:null_object => true,
33+
:puts => true,
34+
:close => true,
35+
:path => "/tmp/perlscript"
36+
)
37+
@fr = Chef::Resource::File.new(@tempfile.path, nil, @node)
38+
Chef::Resource::File.stub!(:new).and_return(@fr)
39+
@fr.stub!(:run_action).and_return(true)
40+
Tempfile.stub!(:new).with("chef-script").and_return(@tempfile)
41+
File.stub!(:chown).and_return(true)
42+
@provider = Chef::Provider::Script.new(@node, @new_resource)
43+
@provider.stub!(:run_command).and_return(true)
44+
end
45+
46+
it "should create a new temp file for the script" do
47+
Tempfile.should_receive(:new).with("chef-script").and_return(@tempfile)
48+
@provider.action_run
49+
end
50+
51+
it "should put the contents of the script in the temp file" do
52+
@tempfile.should_receive(:puts).with(@new_resource.code)
53+
@provider.action_run
54+
end
55+
56+
it "should close the tempfile" do
57+
@tempfile.should_receive(:close)
58+
@provider.action_run
59+
end
60+
61+
it "should create a new Chef::Resource::File for the tempfile" do
62+
Chef::Resource::File.should_receive(:new).with(@tempfile.path, nil, @node).and_return(@fr)
63+
@provider.action_run
64+
end
65+
66+
it "should set the owner attribute on the file resource from the script user" do
67+
@fr.should_receive(:owner).with(@new_resource.user)
68+
@provider.action_run
69+
end
70+
71+
it "should set the group attribute on the file resource from the script group" do
72+
@fr.should_receive(:group).with(@new_resource.group)
73+
@provider.action_run
74+
end
75+
76+
it "should run the create action on the file resouce" do
77+
@fr.should_receive(:run_action).with(:create)
78+
@provider.action_run
79+
end
80+
81+
it "should set the command to 'interpreter tempfile'" do
82+
@new_resource.should_receive(:command).with("#{@new_resource.interpreter} #{@tempfile.path}")
83+
@provider.action_run
84+
end
85+
86+
end
87+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# Cookbook Name:: CHEF-57
3+
# Recipe:: default
4+
#
5+
# Copyright 2009, Example Com
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
script "boo" do
21+
user 'adam'
22+
code "print 'I like cheese';"
23+
interpreter "perl"
24+
end

0 commit comments

Comments
 (0)