summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2019-11-08 16:04:06 -0800
committerGitHub <noreply@github.com>2019-11-08 16:04:06 -0800
commit698ef57d5d95070cdbe97836ad088bf2c99667a1 (patch)
tree42c59ce14e7644c60dcb29cf873aedea98cb5ba8
parent5ddcf287078a2e0c52a5149e5f4ee45199e419ed (diff)
parent1f11fa6e710dd018bdaecf499db8ed1e89e38e4d (diff)
downloadchef-698ef57d5d95070cdbe97836ad088bf2c99667a1.tar.gz
Merge pull request #9081 from chef/sleep
Add new chef_sleep resource
-rw-r--r--kitchen-tests/cookbooks/end_to_end/recipes/default.rb2
-rw-r--r--lib/chef/resource/chef_sleep.rb70
-rw-r--r--lib/chef/resources.rb1
-rw-r--r--spec/unit/resource/chef_sleep_spec.rb30
4 files changed, 103 insertions, 0 deletions
diff --git a/kitchen-tests/cookbooks/end_to_end/recipes/default.rb b/kitchen-tests/cookbooks/end_to_end/recipes/default.rb
index 901c0aa63d..f49cccd9a5 100644
--- a/kitchen-tests/cookbooks/end_to_end/recipes/default.rb
+++ b/kitchen-tests/cookbooks/end_to_end/recipes/default.rb
@@ -9,6 +9,8 @@ hostname "chef-bk-ci.chef.io"
apt_update
+chef_sleep "2"
+
timezone "UTC"
include_recipe "ubuntu" if platform?("ubuntu")
diff --git a/lib/chef/resource/chef_sleep.rb b/lib/chef/resource/chef_sleep.rb
new file mode 100644
index 0000000000..8bd7d2421d
--- /dev/null
+++ b/lib/chef/resource/chef_sleep.rb
@@ -0,0 +1,70 @@
+#
+# Copyright:: 2019, Chef Software Inc.
+#
+# 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_relative "../resource"
+require_relative "../dist"
+
+class Chef
+ class Resource
+ class ChefSleep < Chef::Resource
+ resource_name :chef_sleep
+ provides :chef_sleep
+
+ unified_mode true
+
+ description "Use the chef_sleep resource to set the number of seconds to sleep during a #{Chef::Dist::PRODUCT} run. Only use this resource when a command or service exits successfully but is not ready for the next step of the recipe."
+ introduced "15.5"
+ examples <<~DOC
+ Sleep for 10 seconds
+ ```ruby
+ chef_sleep '10'
+ ```
+
+ Sleep for 10 seconds with a descriptive resource name for logging
+ ```ruby
+ chef_sleep 'wait for the service to start' do
+ seconds 10
+ end
+ ````
+
+ Use a notification from another resource to sleep only when necessary
+ ```ruby
+ service 'Service that is slow to start and reports as started' do
+ service_name 'my_database'
+ action :start
+ notifies :sleep, chef_sleep['wait for service start']
+ end
+
+ chef_sleep 'wait for service start' do
+ seconds 30
+ action :nothing
+ end
+ ```
+ DOC
+
+ property :seconds, [String, Integer],
+ description: "The number of seconds to sleep.",
+ coerce: proc { |s| Integer(s) },
+ name_property: true
+
+ action :sleep do
+ converge_by("sleep #{new_resource.seconds} seconds") do
+ sleep(new_resource.seconds)
+ end
+ end
+ end
+ end
+end
diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb
index 235c1e41a1..d0344ceb9c 100644
--- a/lib/chef/resources.rb
+++ b/lib/chef/resources.rb
@@ -28,6 +28,7 @@ require_relative "resource/build_essential"
require_relative "resource/cookbook_file"
require_relative "resource/chef_gem"
require_relative "resource/chef_handler"
+require_relative "resource/chef_sleep"
require_relative "resource/chocolatey_config"
require_relative "resource/chocolatey_feature"
require_relative "resource/chocolatey_package"
diff --git a/spec/unit/resource/chef_sleep_spec.rb b/spec/unit/resource/chef_sleep_spec.rb
new file mode 100644
index 0000000000..bde29f4f1d
--- /dev/null
+++ b/spec/unit/resource/chef_sleep_spec.rb
@@ -0,0 +1,30 @@
+#
+# Copyright:: 2019, Chef Software, 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::Resource::ChefSleep do
+ let(:resource) { Chef::Resource::ChefSleep.new("30") }
+
+ it "sets the default action as :sleep" do
+ expect(resource.action).to eql([:sleep])
+ end
+
+ it "sleep is the name property and it coerces to an Integer" do
+ expect(resource.seconds).to eql(30)
+ end
+end