|
| 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 | + |
0 commit comments