summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2017-12-15 15:44:12 -0800
committerTim Smith <tsmith@chef.io>2017-12-15 15:51:40 -0800
commit0d1072d74f711bb180b4c8d075309c3934eb06a6 (patch)
tree5a3d2da5d944a7d1b4f5bb00e72f9ca21e729324
parent10252cc8f4f855d5de65efb645583f89bf8771f2 (diff)
downloadchef-0d1072d74f711bb180b4c8d075309c3934eb06a6.tar.gz
Modernize reboot resource and add spec
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/provider/reboot.rb12
-rw-r--r--lib/chef/resource/reboot.rb34
-rw-r--r--spec/unit/resource/reboot_spec.rb52
3 files changed, 78 insertions, 20 deletions
diff --git a/lib/chef/provider/reboot.rb b/lib/chef/provider/reboot.rb
index 32dc38f788..c09fbaa2c0 100644
--- a/lib/chef/provider/reboot.rb
+++ b/lib/chef/provider/reboot.rb
@@ -21,9 +21,19 @@ require "chef/provider"
class Chef
class Provider
+ # Use the reboot resource to reboot a node, a necessary step with some
+ # installations on certain platforms. This resource is supported for use on
+ # the Microsoft Windows, macOS, and Linux platforms.
+ #
+ # In using this resource via notifications, it's important to *only* use
+ # immediate notifications. Delayed notifications produce unintuitive and
+ # probably undesired results.
+ #
+ # @since 12.0.0
class Reboot < Chef::Provider
provides :reboot
+ # @return [void]
def load_current_resource
@current_resource ||= Chef::Resource::Reboot.new(new_resource.name)
current_resource.reason(new_resource.reason)
@@ -31,6 +41,8 @@ class Chef
current_resource
end
+ # add a reboot to the node run_context
+ # @return [void]
def request_reboot
node.run_context.request_reboot(
:delay_mins => new_resource.delay_mins,
diff --git a/lib/chef/resource/reboot.rb b/lib/chef/resource/reboot.rb
index 519defa602..bdaa755533 100644
--- a/lib/chef/resource/reboot.rb
+++ b/lib/chef/resource/reboot.rb
@@ -18,31 +18,25 @@
require "chef/resource"
-# In using this resource via notifications, it's important to *only* use
-# immediate notifications. Delayed notifications produce unintuitive and
-# probably undesired results.
class Chef
class Resource
+ # Use the reboot resource to reboot a node, a necessary step with some
+ # installations on certain platforms. This resource is supported for use on
+ # the Microsoft Windows, macOS, and Linux platforms.
+ #
+ # In using this resource via notifications, it's important to *only* use
+ # immediate notifications. Delayed notifications produce unintuitive and
+ # probably undesired results.
+ #
+ # @since 12.0.0
class Reboot < Chef::Resource
- allowed_actions :request_reboot, :reboot_now, :cancel
-
- def initialize(name, run_context = nil)
- super
- @provider = Chef::Provider::Reboot
-
- @reason = "Reboot by Chef"
- @delay_mins = 0
+ resource_name :reboot
- # no default action.
- end
-
- def reason(arg = nil)
- set_or_return(:reason, arg, :kind_of => String)
- end
+ allowed_actions :request_reboot, :reboot_now, :cancel
+ default_action :nothing # make sure people are quite clear what they want
- def delay_mins(arg = nil)
- set_or_return(:delay_mins, arg, :kind_of => Integer)
- end
+ property :reason, String, default: "Reboot by Chef"
+ property :delay_mins, Integer, default: 0
end
end
end
diff --git a/spec/unit/resource/reboot_spec.rb b/spec/unit/resource/reboot_spec.rb
new file mode 100644
index 0000000000..36e9f705a1
--- /dev/null
+++ b/spec/unit/resource/reboot_spec.rb
@@ -0,0 +1,52 @@
+#
+# Copyright:: Copyright 2017, 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::Reboot do
+
+ before(:each) do
+ @resource = Chef::Resource::Reboot.new("reboot me!")
+ end
+
+ it "should create a new Chef::Resource::Reboot" do
+ expect(@resource).to be_a_kind_of(Chef::Resource)
+ expect(@resource).to be_a_kind_of(Chef::Resource::Reboot)
+ end
+
+ it "should have a default action of :nothing" do
+ expect(@resource.action).to eql([:nothing])
+ end
+
+ it "supports the :nothing, :request_reboot, :reboot_now, and :cancel actions" do
+ expect(@resource.allowed_actions).to include(:nothing, :request_reboot, :reboot_now, :cancel)
+ end
+
+ it "should have a resource_name of :reboot" do
+ expect(@resource.resource_name).to eq(:reboot)
+ end
+
+ it "should accept a String for the reboot reason" do
+ @resource.reason "reasons"
+ expect(@resource.reason).to eq("reasons")
+ end
+
+ it "should accept an Integer for delay_mins" do
+ @resource.delay_mins 100
+ expect { @resource.delay_mins "100" }.to raise_error(ArgumentError)
+ end
+end