summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerdar Sutay <serdar@opscode.com>2014-10-13 15:57:56 -0700
committerSerdar Sutay <serdar@opscode.com>2014-10-14 14:07:05 -0700
commitd251fbfd199625b71656e82268417a87c49dbfe3 (patch)
treeb77daa97b95a7f82e65e913f8716771889a90cbb
parent55d9aae70f9f135bf0d110623da2fdffc34c58e9 (diff)
downloadchef-d251fbfd199625b71656e82268417a87c49dbfe3.tar.gz
Enable guard_interpreters for the execute resource and set the default interpreter for the execute resource to be :execute.
This ensures that attributes of the resource like :environment & :cwd can be inherited by the guard when guard is specified as a string.
-rw-r--r--lib/chef/guard_interpreter/resource_guard_interpreter.rb19
-rw-r--r--lib/chef/resource/conditional.rb34
-rw-r--r--lib/chef/resource/execute.rb25
-rw-r--r--lib/chef/resource/script.rb25
-rw-r--r--spec/functional/resource/execute_spec.rb113
-rw-r--r--spec/unit/resource/execute_spec.rb5
6 files changed, 177 insertions, 44 deletions
diff --git a/lib/chef/guard_interpreter/resource_guard_interpreter.rb b/lib/chef/guard_interpreter/resource_guard_interpreter.rb
index 229a8502c7..8eaa82c0a5 100644
--- a/lib/chef/guard_interpreter/resource_guard_interpreter.rb
+++ b/lib/chef/guard_interpreter/resource_guard_interpreter.rb
@@ -33,10 +33,19 @@ class Chef
# to the resource
merge_inherited_attributes
- # Script resources have a code attribute, which is
- # what is used to execute the command, so include
- # that with attributes specified by caller in opts
- block_attributes = @command_opts.merge({:code => @command})
+ # Only execute and script resources and use guard attributes.
+ # The command to be executed on them are passed via different attributes.
+ # Script resources use code attribute and execute resources use
+ # command attribute. Moreover script resources are also execute
+ # resources. Here we make sure @command is assigned to the right
+ # attribute by checking the type of the resources.
+ # We need to make sure we check for Script first because any resource
+ # that can get to here is an Execute resource.
+ if @parent_resource.is_a? Chef::Resource::Script
+ block_attributes = @command_opts.merge({:code => @command})
+ else
+ block_attributes = @command_opts.merge({:command => @command})
+ end
# Handles cases like powershell_script where default
# attributes are different when used in a guard vs. not. For
@@ -79,7 +88,7 @@ class Chef
raise ArgumentError, "Specified guard_interpreter resource #{parent_resource.guard_interpreter.to_s} unknown for this platform"
end
- if ! resource_class.ancestors.include?(Chef::Resource::Script)
+ if ! resource_class.ancestors.include?(Chef::Resource::Execute)
raise ArgumentError, "Specified guard interpreter class #{resource_class} must be a kind of Chef::Resource::Script resource"
end
diff --git a/lib/chef/resource/conditional.rb b/lib/chef/resource/conditional.rb
index 324c5a4676..6a987d3086 100644
--- a/lib/chef/resource/conditional.rb
+++ b/lib/chef/resource/conditional.rb
@@ -54,23 +54,29 @@ class Chef
end
def configure
- case @command
- when String
- @guard_interpreter = new_guard_interpreter(@parent_resource, @command, @command_opts, &@block)
- @block = nil
- when nil
- # we should have a block if we get here
- if @parent_resource.guard_interpreter != :default
- msg = "#{@parent_resource.name} was given a guard_interpreter of #{@parent_resource.guard_interpreter}, "
- msg << "but not given a command as a string. guard_interpreter does not support blocks (because they just contain ruby)."
- raise ArgumentError, msg
- end
-
+ if @block_given
+ # If a block is given, we will not interpret the block with a guard interpreter.
@guard_interpreter = nil
@command, @command_opts = nil, nil
else
- # command was passed, but it wasn't a String
- raise ArgumentError, "Invalid only_if/not_if command, expected a string: #{command.inspect} (#{command.class})"
+ case @command
+ when String
+ @guard_interpreter = new_guard_interpreter(@parent_resource, @command, @command_opts, &@block)
+ @block = nil
+ when nil
+ # We should have a block if we get here
+ if @parent_resource.guard_interpreter != :default
+ msg = "#{@parent_resource.name} was given a guard_interpreter of #{@parent_resource.guard_interpreter}, "
+ msg << "but not given a command as a string. guard_interpreter does not support blocks (because they just contain ruby)."
+ raise ArgumentError, msg
+ end
+
+ @guard_interpreter = nil
+ @command, @command_opts = nil, nil
+ else
+ # command was passed, but it wasn't a String
+ raise ArgumentError, "Invalid only_if/not_if command, expected a string: #{command.inspect} (#{command.class})"
+ end
end
end
diff --git a/lib/chef/resource/execute.rb b/lib/chef/resource/execute.rb
index 80ee16c5ec..e0a1bbb8c0 100644
--- a/lib/chef/resource/execute.rb
+++ b/lib/chef/resource/execute.rb
@@ -40,6 +40,7 @@ class Chef
@user = nil
@allowed_actions.push(:run)
@umask = nil
+ @guard_interpreter = :execute
end
def umask(arg=nil)
@@ -116,6 +117,30 @@ class Chef
)
end
+ def self.set_guard_inherited_attributes(*inherited_attributes)
+ @class_inherited_attributes = inherited_attributes
+ end
+
+ def self.guard_inherited_attributes(*inherited_attributes)
+ # Similar to patterns elsewhere, return attributes from this
+ # class and superclasses as a form of inheritance
+ ancestor_attributes = []
+
+ if superclass.respond_to?(:guard_inherited_attributes)
+ ancestor_attributes = superclass.guard_inherited_attributes
+ end
+
+ ancestor_attributes.concat(@class_inherited_attributes ? @class_inherited_attributes : []).uniq
+ end
+
+ set_guard_inherited_attributes(
+ :cwd,
+ :environment,
+ :group,
+ :user,
+ :umask
+ )
+
end
end
end
diff --git a/lib/chef/resource/script.rb b/lib/chef/resource/script.rb
index 6f66fb9094..8cc9c6f0c5 100644
--- a/lib/chef/resource/script.rb
+++ b/lib/chef/resource/script.rb
@@ -58,31 +58,6 @@ class Chef
)
end
- def self.set_guard_inherited_attributes(*inherited_attributes)
- @class_inherited_attributes = inherited_attributes
- end
-
- def self.guard_inherited_attributes(*inherited_attributes)
- # Similar to patterns elsewhere, return attributes from this
- # class and superclasses as a form of inheritance
- ancestor_attributes = []
-
- if superclass.respond_to?(:guard_inherited_attributes)
- ancestor_attributes = superclass.guard_inherited_attributes
- end
-
- ancestor_attributes.concat(@class_inherited_attributes ? @class_inherited_attributes : []).uniq
- end
-
- set_guard_inherited_attributes(
- :cwd,
- :environment,
- :group,
- :path,
- :user,
- :umask
- )
-
end
end
end
diff --git a/spec/functional/resource/execute_spec.rb b/spec/functional/resource/execute_spec.rb
new file mode 100644
index 0000000000..ff358fe045
--- /dev/null
+++ b/spec/functional/resource/execute_spec.rb
@@ -0,0 +1,113 @@
+#
+# 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(:execute_resource) {
+ exec_resource = Chef::Resource::Execute.new("foo_resource", run_context)
+
+ exec_resource.environment(resource_environment) if resource_environment
+ exec_resource.cwd(resource_cwd) if resource_cwd
+ exec_resource.command("echo hello")
+ if guard
+ if guard_options
+ exec_resource.only_if(guard, guard_options)
+ else
+ exec_resource.only_if(guard)
+ end
+ end
+ exec_resource
+ }
+
+ let(:resource_environment) { nil }
+ let(:resource_cwd) { nil }
+ let(:guard) { nil }
+ let(:guard_options) { nil }
+
+ describe "when guard is ruby block" do
+ it "guard can still run" do
+ execute_resource.only_if do
+ true
+ end
+ execute_resource.run_action(:run)
+ execute_resource.should be_updated_by_last_action
+ end
+ end
+
+ describe "when parent resource sets :cwd" do
+ let(:resource_cwd) { CHEF_SPEC_DATA }
+
+ let(:guard) { %{ruby -e 'exit 1 unless File.exists?("./big_json_plus_one.json")'} }
+
+ it "guard inherits :cwd from resource" do
+ execute_resource.run_action(:run)
+ execute_resource.should be_updated_by_last_action
+ end
+ end
+
+ describe "when parent resource sets :environment" do
+ let(:resource_environment) do
+ {
+ "SAWS_SECRET" => "supersecret",
+ "SAWS_KEY" => "qwerty"
+ }
+ 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
+ let(:guard) { %{ruby -e 'exit 1 if ENV["SAWS_SECRET"] != "supersecret"'} }
+
+ it "guard inherits :environment value from resource" do
+ execute_resource.run_action(:run)
+ execute_resource.should be_updated_by_last_action
+ end
+
+ describe "when guard sets additional values in the :environment" do
+ let(:guard) { %{ruby -e 'exit 1 if ENV["SGCE_SECRET"] != "regularsecret"'} }
+
+ let(:guard_options) do
+ {
+ :environment => { 'SGCE_SECRET' => "regularsecret" }
+ }
+ end
+
+ it "guard sees merged value for in its ENV" do
+ execute_resource.run_action(:run)
+ execute_resource.should be_updated_by_last_action
+ end
+ end
+
+ describe "when guard sets same value in the :environment" do
+ let(:guard) { %{ruby -e 'exit 1 if ENV["SAWS_SECRET"] != "regularsecret"'} }
+
+ let(:guard_options) do
+ {
+ :environment => { 'SAWS_SECRET' => "regularsecret" }
+ }
+ end
+
+ it "guard sees value from guard options in its ENV" do
+ execute_resource.run_action(:run)
+ execute_resource.should be_updated_by_last_action
+ end
+ end
+ end
+end
diff --git a/spec/unit/resource/execute_spec.rb b/spec/unit/resource/execute_spec.rb
index 8c8dcfb6ca..e1728b7892 100644
--- a/spec/unit/resource/execute_spec.rb
+++ b/spec/unit/resource/execute_spec.rb
@@ -23,4 +23,9 @@ describe Chef::Resource::Execute do
let(:resource_instance_name) { "some command" }
let(:execute_resource) { Chef::Resource::Execute.new(resource_instance_name) }
it_behaves_like "an execute resource"
+
+ it "default guard interpreter should be :execute interpreter" do
+ execute_resource.guard_interpreter.should be(:execute)
+ end
+
end