diff options
81 files changed, 1222 insertions, 540 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 46eaea92a8..d3b4d82d21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ ## Unreleased: 12.0.0 +* [**Nolan Davidson**](https://github.com/nsdavidson): + The chef-apply command now prints usage information when called without arguments +* [**Kazuki Saito**](https://github.com/sakazuki): + CHEF-4933: idempotency fixes for ifconfig provider +* [**Kirill Shirinkin**](https://github.com/Fodoj): + The knife bootstrap command expands the path of the secret-file * [**Malte Swart**](https://github.com/mswart): [CHEF-4101] DeepMerge - support overwriting hash values with nil * [**James Belchamber**](https://github.com/JamesBelchamber): diff --git a/CHEF_MVPS.md b/CHEF_MVPS.md index 620a516080..28dbe3c8e8 100644 --- a/CHEF_MVPS.md +++ b/CHEF_MVPS.md @@ -18,6 +18,7 @@ After receiving three MVP awards, we add someone to the hall of fame. We want to | Release | Date | MVP | |---------|------|-----| +| [Client 11.16.0](http://www.getchef.com/blog/2014/09/08/release-chef-client-11-16-0-ohai-7-4-0/) | 2014-09-08 | Jesse Hu | | [Client 11.14.2](http://www.getchef.com/blog/2014/08/01/release-chef-client-11-14-2/) | 2014-08-01 | Nikhil Benesch | | [Client 11.12.0](http://www.getchef.com/blog/2014/04/08/release-chef-client-11-12-0-10-32-2/) | 2014-04-08 | Chris Bandy | | [Client 11.10.4](http://www.getchef.com/blog/2014/02/20/chef-client-patch-release-11-10-4/) | 2014-02-20 | Jon Cowie | diff --git a/chef.gemspec b/chef.gemspec index 89b4ac2665..481b120fe6 100644 --- a/chef.gemspec +++ b/chef.gemspec @@ -19,8 +19,8 @@ Gem::Specification.new do |s| s.add_dependency "mixlib-cli", "~> 1.4" s.add_dependency "mixlib-log", "~> 1.3" s.add_dependency "mixlib-authentication", "~> 1.3" - s.add_dependency "mixlib-shellout", "~> 1.4" - s.add_dependency "ohai", "~> 7.2" + s.add_dependency "mixlib-shellout", ">= 2.0.0.rc.0", "< 3.0" + s.add_dependency "ohai", ">= 7.6.0.rc.0" s.add_dependency "ffi-yajl", "~> 1.0", ">= 1.0.2" s.add_dependency "net-ssh", "~> 2.6" diff --git a/lib/chef/application.rb b/lib/chef/application.rb index 5b1d53d741..0430d4acfa 100644 --- a/lib/chef/application.rb +++ b/lib/chef/application.rb @@ -46,6 +46,7 @@ class Chef::Application configure_chef configure_logging configure_proxy_environment_variables + configure_encoding end # Get this party started @@ -175,6 +176,11 @@ class Chef::Application configure_no_proxy end + # Sets the default external encoding to UTF-8 (users can change this, but they shouldn't) + def configure_encoding + Encoding.default_external = Chef::Config[:ruby_encoding] + end + # Called prior to starting the application, by the run method def setup_application raise Chef::Exceptions::Application, "#{self.to_s}: you must override setup_application" diff --git a/lib/chef/application/apply.rb b/lib/chef/application/apply.rb index ab35b35389..ea9154c6f2 100644 --- a/lib/chef/application/apply.rb +++ b/lib/chef/application/apply.rb @@ -134,6 +134,10 @@ class Chef::Application::Apply < Chef::Application @recipe_text = STDIN.read temp_recipe_file else + if !ARGV[0] + puts opt_parser + Chef::Application.exit! "No recipe file provided", 1 + end @recipe_filename = ARGV[0] @recipe_text,@recipe_fh = read_recipe_file @recipe_filename end diff --git a/lib/chef/chef_fs/chef_fs_data_store.rb b/lib/chef/chef_fs/chef_fs_data_store.rb index 09a66a9ab2..484ab07390 100644 --- a/lib/chef/chef_fs/chef_fs_data_store.rb +++ b/lib/chef/chef_fs/chef_fs_data_store.rb @@ -27,7 +27,61 @@ require 'fileutils' class Chef module ChefFS + # + # Translation layer between chef-zero's DataStore (a place where it expects + # files to be stored) and ChefFS (the user's repository directory layout). + # + # chef-zero expects the data store to store files *its* way--for example, it + # expects get("nodes/blah") to return the JSON text for the blah node, and + # it expects get("cookbooks/blah/1.0.0") to return the JSON definition of + # the blah cookbook version 1.0.0. + # + # The repository is defined the way the *user* wants their layout. These + # two things are very similar in layout (for example, nodes are stored under + # the nodes/ directory and their filename is the name of the node). + # + # However, there are a few differences that make this more than just a raw + # file store: + # + # 1. Cookbooks are stored much differently. + # - chef-zero places JSON text with the checksums for the cookbook at + # /cookbooks/NAME/VERSION, and expects the JSON to contain URLs to the + # actual files, which are stored elsewhere. + # - The repository contains an actual directory with just the cookbook + # files and a metadata.rb containing a version #. There is no JSON to + # be found. + # - Further, if versioned_cookbooks is false, that directory is named + # /cookbooks/NAME and only one version exists. If versioned_cookbooks + # is true, the directory is named /cookbooks/NAME-VERSION. + # - Therefore, ChefFSDataStore calculates the cookbook JSON by looking at + # the files in the cookbook and checksumming them, and reading metadata.rb + # for the version and dependency information. + # - ChefFSDataStore also modifies the cookbook file URLs so that they point + # to /file_store/repo/<filename> (the path to the actual file under the + # repository root). For example, /file_store/repo/apache2/metadata.rb or + # /file_store/repo/cookbooks/apache2/recipes/default.rb). + # + # 2. Sandboxes don't exist in the repository. + # - ChefFSDataStore lets cookbooks be uploaded into a temporary memory + # storage, and when the cookbook is committed, copies the files onto the + # disk in the correct place (/cookbooks/apache2/recipes/default.rb). + # 3. Data bags: + # - The Chef server expects data bags in /data/BAG/ITEM + # - The repository stores data bags in /data_bags/BAG/ITEM + # + # 4. JSON filenames are generally NAME.json in the repository (e.g. /nodes/foo.json). + # class ChefFSDataStore + # + # Create a new ChefFSDataStore + # + # ==== Arguments + # + # [chef_fs] + # A +ChefFS::FileSystem+ object representing the repository root. + # Generally will be a +ChefFS::FileSystem::ChefRepositoryFileSystemRoot+ + # object, created from +ChefFS::Config.local_fs+. + # def initialize(chef_fs) @chef_fs = chef_fs @memory_store = ChefZero::DataStore::MemoryStore.new diff --git a/lib/chef/chef_fs/config.rb b/lib/chef/chef_fs/config.rb index ca273b2cca..fcad6c919f 100644 --- a/lib/chef/chef_fs/config.rb +++ b/lib/chef/chef_fs/config.rb @@ -22,9 +22,74 @@ require 'chef/chef_fs/path_utils' class Chef module ChefFS # - # Helpers to take Chef::Config and create chef_fs and local_fs from it + # Helpers to take Chef::Config and create chef_fs and local_fs (ChefFS + # objects representing the server and local repository, respectively). # class Config + # + # Create a new Config object which can produce a chef_fs and local_fs. + # + # ==== Arguments + # + # [chef_config] + # A hash that looks suspiciously like +Chef::Config+. These hash keys + # include: + # + # :chef_repo_path:: + # The root where all local chef object data is stored. Mirrors + # +Chef::Config.chef_repo_path+ + # :cookbook_path, node_path, ...:: + # Paths to cookbooks/, nodes/, data_bags/, etc. Mirrors + # +Chef::Config.cookbook_path+, etc. Defaults to + # +<chef_repo_path>/cookbooks+, etc. + # :repo_mode:: + # The directory format on disk. 'everything', 'hosted_everything' and + # 'static'. Default: autodetected based on whether the URL has + # "/organizations/NAME." + # :versioned_cookbooks:: + # If true, the repository contains cookbooks with versions in their + # name (apache2-1.0.0). If false, the repository just has one version + # of each cookbook and the directory has the cookbook name (apache2). + # Default: +false+ + # :chef_server_url:: + # The URL to the Chef server, e.g. https://api.opscode.com/organizations/foo. + # Used as the server for the remote chef_fs, and to "guess" repo_mode + # if not specified. + # :node_name:: The username to authenticate to the Chef server with. + # :client_key:: The private key for the user for authentication + # :environment:: The environment in which you are presently working + # :repo_mode:: + # The repository mode, :hosted_everything, :everything or :static. + # This determines the set of subdirectories the Chef server will offer + # up. + # :versioned_cookbooks:: Whether or not to include versions in cookbook names + # + # [cwd] + # The current working directory to base relative Chef paths from. + # Defaults to +Dir.pwd+. + # + # [options] + # A hash of other, not-suspiciously-like-chef-config options: + # :cookbook_version:: + # When downloading cookbooks, download this cookbook version instead + # of the latest. + # + # [ui] + # The object to print output to, with "output", "warn" and "error" + # (looks a little like a Chef::Knife::UI object, obtainable from + # Chef::Knife.ui). + # + # ==== Example + # + # require 'chef/chef_fs/config' + # config = Chef::ChefFS::Config.new + # config.chef_fs.child('cookbooks').children.each do |cookbook| + # puts "Cookbook on server: #{cookbook.name}" + # end + # config.local_fs.child('cookbooks').children.each do |cookbook| + # puts "Local cookbook: #{cookbook.name}" + # end + # def initialize(chef_config = Chef::Config, cwd = Dir.pwd, options = {}, ui = nil) @chef_config = chef_config @cwd = cwd diff --git a/lib/chef/chef_fs/file_system/chef_repository_file_system_root_dir.rb b/lib/chef/chef_fs/file_system/chef_repository_file_system_root_dir.rb index 050da60389..ac272d4c1a 100644 --- a/lib/chef/chef_fs/file_system/chef_repository_file_system_root_dir.rb +++ b/lib/chef/chef_fs/file_system/chef_repository_file_system_root_dir.rb @@ -42,15 +42,17 @@ class Chef # Create a new Chef Repository File System root. # # == Parameters - # - child_paths - a hash of child paths, e.g.: - # "nodes" => [ '/var/nodes', '/home/jkeiser/nodes' ], - # "roles" => [ '/var/roles' ], - # ... - # - root_paths - an array of paths representing the top level, where - # org.json, members.json, and invites.json will be stored. - # - chef_config - a hash of options that looks suspiciously like the ones + # [child_paths] + # A hash of child paths, e.g.: + # "nodes" => [ '/var/nodes', '/home/jkeiser/nodes' ], + # "roles" => [ '/var/roles' ], + # ... + # [root_paths] + # An array of paths representing the top level, where + # +org.json+, +members.json+, and +invites.json+ will be stored. + # [chef_config] - a hash of options that looks suspiciously like the ones # stored in Chef::Config, containing at least these keys: - # - :versioned_cookbooks - whether to include versions in cookbook names + # :versioned_cookbooks:: whether to include versions in cookbook names def initialize(child_paths, root_paths=[], chef_config=Chef::Config) super("", nil) @child_paths = child_paths diff --git a/lib/chef/chef_fs/file_system/chef_server_root_dir.rb b/lib/chef/chef_fs/file_system/chef_server_root_dir.rb index 61a224c307..370308ee0a 100644 --- a/lib/chef/chef_fs/file_system/chef_server_root_dir.rb +++ b/lib/chef/chef_fs/file_system/chef_server_root_dir.rb @@ -46,19 +46,24 @@ class Chef # # == Parameters # - # - root_name - a friendly name for the root, for printing--like "remote" or "chef_central". - # - chef_config - a hash with options that look suspiciously like Chef::Config, including the - # following keys: - # - :chef_server_url - the URL to the Chef server or top of the organization - # - :node_name - the username to authenticate to the Chef server with - # - :client_key - the private key for the user for authentication - # - :environment - the environment in which you are presently working - # - :repo_mode - the repository mode, :hosted_everything, :everything or :static. - # This determines the set of subdirectories the Chef server - # will offer up. - # - :versioned_cookbooks - whether or not to include versions in cookbook names - # - options - other options: - # - :cookbook_version - when cookbooks are retrieved, grab this version for them. + # [root_name] + # A friendly name for the root, for printing--like "remote" or "chef_central". + # [chef_config] + # A hash with options that look suspiciously like Chef::Config, including the + # following keys: + # :chef_server_url:: The URL to the Chef server or top of the organization + # :node_name:: The username to authenticate to the Chef server with + # :client_key:: The private key for the user for authentication + # :environment:: The environment in which you are presently working + # :repo_mode:: + # The repository mode, :hosted_everything, :everything or :static. + # This determines the set of subdirectories the Chef server will + # offer up. + # :versioned_cookbooks:: whether or not to include versions in cookbook names + # [options] + # Other options: + # :cookbook_version:: when cookbooks are retrieved, grab this version for them. + # :freeze:: freeze cookbooks on upload # def initialize(root_name, chef_config, options = {}) super("", nil) diff --git a/lib/chef/client.rb b/lib/chef/client.rb index 2de3ca3e64..161ecddb0f 100644 --- a/lib/chef/client.rb +++ b/lib/chef/client.rb @@ -44,6 +44,7 @@ require 'chef/resource_reporter' require 'chef/run_lock' require 'chef/policy_builder' require 'chef/request_id' +require 'chef/platform/rebooter' require 'ohai' require 'rbconfig' @@ -427,7 +428,9 @@ class Chef run_context = setup_run_context - converge(run_context) + catch (:end_client_run_early) do + converge(run_context) + end save_updated_node @@ -435,6 +438,10 @@ class Chef Chef::Log.info("Chef Run complete in #{run_status.elapsed_time} seconds") run_completed_successfully @events.run_completed(node) + + # rebooting has to be the last thing we do, no exceptions. + Chef::Platform::Rebooter.reboot_if_needed!(node) + true rescue Exception => e # CHEF-3336: Send the error first in case something goes wrong below and we don't know why diff --git a/lib/chef/config.rb b/lib/chef/config.rb index 4e71645dcc..1963a95aab 100644 --- a/lib/chef/config.rb +++ b/lib/chef/config.rb @@ -587,6 +587,51 @@ class Chef default :normal_attribute_whitelist, nil default :override_attribute_whitelist, nil + # Chef requires an English-language UTF-8 locale to function properly. We attempt + # to use the 'locale -a' command and search through a list of preferences until we + # find one that we can use. On Ubuntu systems we should find 'C.UTF-8' and be + # able to use that even if there is no English locale on the server, but Mac, Solaris, + # AIX, etc do not have that locale. We then try to find an English locale and fall + # back to 'C' if we do not. The choice of fallback is pick-your-poison. If we try + # to do the work to return a non-US UTF-8 locale then we fail inside of providers when + # things like 'svn info' return Japanese and we can't parse them. OTOH, if we pick 'C' then + # we will blow up on UTF-8 characters. Between the warn we throw and the Encoding + # exception that ruby will throw it is more obvious what is broken if we drop UTF-8 by + # default rather than drop English. + # + # If there is no 'locale -a' then we return 'en_US.UTF-8' since that is the most commonly + # available English UTF-8 locale. However, all modern POSIXen should support 'locale -a'. + default :internal_locale do + begin + locales = `locale -a`.split + case + when locales.include?('C.UTF-8') + 'C.UTF-8' + when locales.include?('en_US.UTF-8') + 'en_US.UTF-8' + when locales.include?('en.UTF-8') + 'en.UTF-8' + when guesses = locales.select { |l| l =~ /^en_.*UTF-8$'/ } + guesses.first + else + Chef::Log.warn "Please install an English UTF-8 locale for Chef to use, falling back to C locale and disabling UTF-8 support." + 'C' + end + rescue + Chef::Log.warn "No usable locale -a command found, assuming you have en_US.UTF-8 installed." + 'en_US.UTF-8' + end + end + + # Force UTF-8 Encoding, for when we fire up in the 'C' locale or other strange locales (e.g. + # japanese windows encodings). If we do not do this, then knife upload will fail when a cookbook's + # README.md has UTF-8 characters that do not encode in whatever surrounding encoding we have been + # passed. Effectively, the Chef Ecosystem is globally UTF-8 by default. Anyone who wants to be + # able to upload Shift_JIS or ISO-8859-1 files needs to mark *those* files explicitly with + # magic tags to make ruby correctly identify the encoding being used. Changing this default will + # break Chef community cookbooks and is very highly discouraged. + default :ruby_encoding, Encoding::UTF_8 + # If installed via an omnibus installer, this gives the path to the # "embedded" directory which contains all of the software packaged with # omnibus. This is used to locate the cacert.pem file on windows. diff --git a/lib/chef/dsl/reboot_pending.rb b/lib/chef/dsl/reboot_pending.rb index 9f80d38c61..a81debce99 100644 --- a/lib/chef/dsl/reboot_pending.rb +++ b/lib/chef/dsl/reboot_pending.rb @@ -27,10 +27,13 @@ class Chef include Chef::DSL::PlatformIntrospection # Returns true if the system needs a reboot or is expected to reboot - # Raises UnsupportedPlatform if this functionality isn't provided yet + # Note that we will silently miss any other platform-specific reboot notices besides Windows+Ubuntu. def reboot_pending? - if platform?("windows") + # don't break when used as a mixin in contexts without #node (e.g. specs). + if self.respond_to?(:node, true) && node.run_context.reboot_requested? + true + elsif platform?("windows") # PendingFileRenameOperations contains pairs (REG_MULTI_SZ) of filenames that cannot be updated # due to a file being in use (usually a temporary file and a system file) # \??\c:\temp\test.sys!\??\c:\winnt\system32\test.sys @@ -53,7 +56,7 @@ class Chef # This should work for Debian as well if update-notifier-common happens to be installed. We need an API for that. File.exists?('/var/run/reboot-required') else - raise Chef::Exceptions::UnsupportedPlatform.new(node[:platform]) + false end end end diff --git a/lib/chef/knife/core/bootstrap_context.rb b/lib/chef/knife/core/bootstrap_context.rb index 6bc48636c5..87c25ca160 100644 --- a/lib/chef/knife/core/bootstrap_context.rb +++ b/lib/chef/knife/core/bootstrap_context.rb @@ -44,8 +44,10 @@ class Chef def encrypted_data_bag_secret knife_config[:secret] || begin - if knife_config[:secret_file] && File.exist?(knife_config[:secret_file]) - IO.read(File.expand_path(knife_config[:secret_file])) + secret_file_path = knife_config[:secret_file] + expanded_secret_file_path = File.expand_path(secret_file_path.to_s) + if secret_file_path && File.exist?(expanded_secret_file_path) + IO.read(expanded_secret_file_path) else nil end diff --git a/lib/chef/mixin/shell_out.rb b/lib/chef/mixin/shell_out.rb index 881c94b862..82772b584a 100644 --- a/lib/chef/mixin/shell_out.rb +++ b/lib/chef/mixin/shell_out.rb @@ -30,32 +30,39 @@ class Chef # Generally speaking, 'extend Chef::Mixin::ShellOut' in your recipes and include 'Chef::Mixin::ShellOut' in your LWRPs # You can also call Mixlib::Shellout.new directly, but you lose all of the above functionality + # we use 'en_US.UTF-8' by default because we parse localized strings in English as an API and + # generally must support UTF-8 unicode. def shell_out(*command_args) - cmd = Mixlib::ShellOut.new(*run_command_compatible_options(command_args)) - cmd.live_stream ||= io_for_live_stream - cmd.run_command - cmd + args = command_args.dup + if args.last.is_a?(Hash) + options = args.pop.dup + env_key = options.has_key?(:env) ? :env : :environment + options[env_key] ||= {} + options[env_key] = options[env_key].dup + options[env_key]['LC_ALL'] ||= Chef::Config[:internal_locale] unless options[env_key].has_key?('LC_ALL') + args << options + else + args << { :environment => { 'LC_ALL' => Chef::Config[:internal_locale] } } + end + + shell_out_command(*args) end + # call shell_out (using en_US.UTF-8) and raise errors def shell_out!(*command_args) - cmd= shell_out(*command_args) + cmd = shell_out(*command_args) cmd.error! cmd end - # environment['LC_ALL'] should be nil or what the user specified def shell_out_with_systems_locale(*command_args) - args = command_args.dup - if args.last.is_a?(Hash) - options = args.last - env_key = options.has_key?(:env) ? :env : :environment - options[env_key] ||= {} - options[env_key]['LC_ALL'] ||= nil - else - args << { :environment => { 'LC_ALL' => nil } } - end + shell_out_command(*command_args) + end - shell_out(*args) + def shell_out_with_systems_locale!(*command_args) + cmd = shell_out_with_systems_locale(*command_args) + cmd.error! + cmd end DEPRECATED_OPTIONS = @@ -82,6 +89,13 @@ class Chef private + def shell_out_command(*command_args) + cmd = Mixlib::ShellOut.new(*run_command_compatible_options(command_args)) + cmd.live_stream ||= io_for_live_stream + cmd.run_command + cmd + end + def deprecate_option(old_option, new_option) Chef::Log.logger.warn "DEPRECATION: Chef::Mixin::ShellOut option :#{old_option} is deprecated. Use :#{new_option}" end diff --git a/lib/chef/platform/rebooter.rb b/lib/chef/platform/rebooter.rb new file mode 100644 index 0000000000..b46f0e394c --- /dev/null +++ b/lib/chef/platform/rebooter.rb @@ -0,0 +1,54 @@ +# +# Author:: Chris Doherty <cdoherty@getchef.com>) +# Copyright:: Copyright (c) 2014 Chef, 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 'chef/dsl/reboot_pending' +require 'chef/log' +require 'chef/platform' + +class Chef + class Platform + module Rebooter + extend Chef::Mixin::ShellOut + + class << self + + def reboot!(node) + reboot_info = node.run_context.reboot_info + + cmd = if Chef::Platform.windows? + # should this do /f as well? do we then need a minimum delay to let apps quit? + "shutdown /r /t #{reboot_info[:delay_mins]} /c \"#{reboot_info[:reason]}\"" + else + # probably Linux-only. + "shutdown -r +#{reboot_info[:delay_mins]} \"#{reboot_info[:reason]}\"" + end + + Chef::Log.warn "Rebooting server at a recipe's request. Details: #{reboot_info.inspect}" + shell_out!(cmd) + end + + # this is a wrapper function so Chef::Client only needs a single line of code. + def reboot_if_needed!(node) + if node.run_context.reboot_requested? + reboot!(node) + end + end + end + end + end +end diff --git a/lib/chef/provider/ifconfig.rb b/lib/chef/provider/ifconfig.rb index 31f88e5406..ac52100b56 100644 --- a/lib/chef/provider/ifconfig.rb +++ b/lib/chef/provider/ifconfig.rb @@ -19,6 +19,7 @@ require 'chef/log' require 'chef/mixin/command' require 'chef/provider' +require 'chef/resource/file' require 'chef/exceptions' require 'erb' @@ -109,11 +110,11 @@ class Chef :command => command ) Chef::Log.info("#{@new_resource} added") - # Write out the config files - generate_config end end end + # Write out the config files + generate_config end def action_enable @@ -140,12 +141,12 @@ class Chef run_command( :command => command ) - delete_config Chef::Log.info("#{@new_resource} deleted") end else Chef::Log.debug("#{@new_resource} does not exist - nothing to do") end + delete_config end def action_disable @@ -168,27 +169,25 @@ class Chef ! @config_template.nil? and ! @config_path.nil? end + def resource_for_config(path) + Chef::Resource::File.new(path, run_context) + end + def generate_config return unless can_generate_config? b = binding template = ::ERB.new(@config_template) - converge_by ("generate configuration file : #{@config_path}") do - network_file = ::File.new(@config_path, "w") - network_file.puts(template.result(b)) - network_file.close - end - Chef::Log.info("#{@new_resource} created configuration file") + config = resource_for_config(@config_path) + config.content(template.result(b)) + config.run_action(:create) + @new_resource.updated_by_last_action(true) if config.updated? end def delete_config return unless can_generate_config? - require 'fileutils' - if ::File.exist?(@config_path) - converge_by ("delete the #{@config_path}") do - FileUtils.rm_f(@config_path, :verbose => false) - end - end - Chef::Log.info("#{@new_resource} deleted configuration file") + config = resource_for_config(@config_path) + config.run_action(:delete) + @new_resource.updated_by_last_action(true) if config.updated? end private diff --git a/lib/chef/provider/link.rb b/lib/chef/provider/link.rb index d6602c2e03..af2fe4a84f 100644 --- a/lib/chef/provider/link.rb +++ b/lib/chef/provider/link.rb @@ -83,7 +83,7 @@ class Chef end def canonicalize(path) - Chef::Platform.windows? ? path.gsub('/', '\\') : path + Chef::Util::PathHelper.canonical_path(path) end def action_create diff --git a/lib/chef/provider/package/aix.rb b/lib/chef/provider/package/aix.rb index 9fb87d6ea0..da3e6d1684 100644 --- a/lib/chef/provider/package/aix.rb +++ b/lib/chef/provider/package/aix.rb @@ -112,14 +112,10 @@ class Chef def install_package(name, version) Chef::Log.debug("#{@new_resource} package install options: #{@new_resource.options}") if @new_resource.options.nil? - run_command_with_systems_locale( - :command => "installp -aYF -d #{@new_resource.source} #{@new_resource.package_name}" - ) + shell_out!( "installp -aYF -d #{@new_resource.source} #{@new_resource.package_name}" ) Chef::Log.debug("#{@new_resource} installed version #{@new_resource.version} from: #{@new_resource.source}") else - run_command_with_systems_locale( - :command => "installp -aYF #{expand_options(@new_resource.options)} -d #{@new_resource.source} #{@new_resource.package_name}" - ) + shell_out!( "installp -aYF #{expand_options(@new_resource.options)} -d #{@new_resource.source} #{@new_resource.package_name}" ) Chef::Log.debug("#{@new_resource} installed version #{@new_resource.version} from: #{@new_resource.source}") end end @@ -128,14 +124,10 @@ class Chef def remove_package(name, version) if @new_resource.options.nil? - run_command_with_systems_locale( - :command => "installp -u #{name}" - ) + shell_out!( "installp -u #{name}" ) Chef::Log.debug("#{@new_resource} removed version #{@new_resource.version}") else - run_command_with_systems_locale( - :command => "installp -u #{expand_options(@new_resource.options)} #{name}" - ) + shell_out!( "installp -u #{expand_options(@new_resource.options)} #{name}" ) Chef::Log.debug("#{@new_resource} removed version #{@new_resource.version}") end end diff --git a/lib/chef/provider/package/ips.rb b/lib/chef/provider/package/ips.rb index 92b41b3627..4090507303 100644 --- a/lib/chef/provider/package/ips.rb +++ b/lib/chef/provider/package/ips.rb @@ -65,15 +65,13 @@ class Chef def install_package(name, version) package_name = "#{name}@#{version}" normal_command = "pkg#{expand_options(@new_resource.options)} install -q #{package_name}" - if @new_resource.respond_to?(:accept_license) and @new_resource.accept_license - command = normal_command.gsub('-q', '-q --accept') - else - command = normal_command - end - begin - run_command_with_systems_locale(:command => command) - rescue - end + command = + if @new_resource.respond_to?(:accept_license) and @new_resource.accept_license + normal_command.gsub('-q', '-q --accept') + else + normal_command + end + shell_out(command) end def upgrade_package(name, version) @@ -82,9 +80,7 @@ class Chef def remove_package(name, version) package_name = "#{name}@#{version}" - run_command_with_systems_locale( - :command => "pkg#{expand_options(@new_resource.options)} uninstall -q #{package_name}" - ) + shell_out!( "pkg#{expand_options(@new_resource.options)} uninstall -q #{package_name}" ) end end end diff --git a/lib/chef/provider/package/macports.rb b/lib/chef/provider/package/macports.rb index 6ef303ee4f..05247e6d31 100644 --- a/lib/chef/provider/package/macports.rb +++ b/lib/chef/provider/package/macports.rb @@ -45,27 +45,21 @@ class Chef unless @current_resource.version == version command = "port#{expand_options(@new_resource.options)} install #{name}" command << " @#{version}" if version and !version.empty? - run_command_with_systems_locale( - :command => command - ) + shell_out!(command) end end def purge_package(name, version) command = "port#{expand_options(@new_resource.options)} uninstall #{name}" command << " @#{version}" if version and !version.empty? - run_command_with_systems_locale( - :command => command - ) + shell_out!(command) end def remove_package(name, version) command = "port#{expand_options(@new_resource.options)} deactivate #{name}" command << " @#{version}" if version and !version.empty? - run_command_with_systems_locale( - :command => command - ) + shell_out!(command) end def upgrade_package(name, version) @@ -78,9 +72,7 @@ class Chef # that hasn't been installed. install_package(name, version) elsif current_version != version - run_command_with_systems_locale( - :command => "port#{expand_options(@new_resource.options)} upgrade #{name} @#{version}" - ) + shell_out!( "port#{expand_options(@new_resource.options)} upgrade #{name} @#{version}" ) end end diff --git a/lib/chef/provider/package/pacman.rb b/lib/chef/provider/package/pacman.rb index 2e8bb7850b..1014ebcaa5 100644 --- a/lib/chef/provider/package/pacman.rb +++ b/lib/chef/provider/package/pacman.rb @@ -86,9 +86,7 @@ class Chef end def install_package(name, version) - run_command_with_systems_locale( - :command => "pacman --sync --noconfirm --noprogressbar#{expand_options(@new_resource.options)} #{name}" - ) + shell_out!( "pacman --sync --noconfirm --noprogressbar#{expand_options(@new_resource.options)} #{name}" ) end def upgrade_package(name, version) @@ -96,9 +94,7 @@ class Chef end def remove_package(name, version) - run_command_with_systems_locale( - :command => "pacman --remove --noconfirm --noprogressbar#{expand_options(@new_resource.options)} #{name}" - ) + shell_out!( "pacman --remove --noconfirm --noprogressbar#{expand_options(@new_resource.options)} #{name}" ) end def purge_package(name, version) diff --git a/lib/chef/provider/package/portage.rb b/lib/chef/provider/package/portage.rb index 6a3587558a..7e0eebd0d9 100644 --- a/lib/chef/provider/package/portage.rb +++ b/lib/chef/provider/package/portage.rb @@ -110,9 +110,7 @@ class Chef pkg = "~#{name}-#{$1}" end - run_command_with_systems_locale( - :command => "emerge -g --color n --nospinner --quiet#{expand_options(@new_resource.options)} #{pkg}" - ) + shell_out!( "emerge -g --color n --nospinner --quiet#{expand_options(@new_resource.options)} #{pkg}" ) end def upgrade_package(name, version) @@ -126,9 +124,7 @@ class Chef pkg = "#{@new_resource.package_name}" end - run_command_with_systems_locale( - :command => "emerge --unmerge --color n --nospinner --quiet#{expand_options(@new_resource.options)} #{pkg}" - ) + shell_out!( "emerge --unmerge --color n --nospinner --quiet#{expand_options(@new_resource.options)} #{pkg}" ) end def purge_package(name, version) diff --git a/lib/chef/provider/package/rpm.rb b/lib/chef/provider/package/rpm.rb index bbb561bd15..c0a6444252 100644 --- a/lib/chef/provider/package/rpm.rb +++ b/lib/chef/provider/package/rpm.rb @@ -90,13 +90,9 @@ class Chef def install_package(name, version) unless @current_resource.version - run_command_with_systems_locale( - :command => "rpm #{@new_resource.options} -i #{@new_resource.source}" - ) + shell_out!( "rpm #{@new_resource.options} -i #{@new_resource.source}" ) else - run_command_with_systems_locale( - :command => "rpm #{@new_resource.options} -U #{@new_resource.source}" - ) + shell_out!( "rpm #{@new_resource.options} -U #{@new_resource.source}" ) end end @@ -104,13 +100,9 @@ class Chef def remove_package(name, version) if version - run_command_with_systems_locale( - :command => "rpm #{@new_resource.options} -e #{name}-#{version}" - ) + shell_out!( "rpm #{@new_resource.options} -e #{name}-#{version}" ) else - run_command_with_systems_locale( - :command => "rpm #{@new_resource.options} -e #{name}" - ) + shell_out!( "rpm #{@new_resource.options} -e #{name}" ) end end diff --git a/lib/chef/provider/package/solaris.rb b/lib/chef/provider/package/solaris.rb index 0f45b61e18..19f844b66a 100644 --- a/lib/chef/provider/package/solaris.rb +++ b/lib/chef/provider/package/solaris.rb @@ -112,9 +112,7 @@ class Chef else command = "pkgadd -n -d #{@new_resource.source} all" end - run_command_with_systems_locale( - :command => command - ) + shell_out!(command) Chef::Log.debug("#{@new_resource} installed version #{@new_resource.version} from: #{@new_resource.source}") else if ::File.directory?(@new_resource.source) # CHEF-4469 @@ -122,23 +120,17 @@ class Chef else command = "pkgadd -n#{expand_options(@new_resource.options)} -d #{@new_resource.source} all" end - run_command_with_systems_locale( - :command => command - ) + shell_out!(command) Chef::Log.debug("#{@new_resource} installed version #{@new_resource.version} from: #{@new_resource.source}") end end def remove_package(name, version) if @new_resource.options.nil? - run_command_with_systems_locale( - :command => "pkgrm -n #{name}" - ) + shell_out!( "pkgrm -n #{name}" ) Chef::Log.debug("#{@new_resource} removed version #{@new_resource.version}") else - run_command_with_systems_locale( - :command => "pkgrm -n#{expand_options(@new_resource.options)} #{name}" - ) + shell_out!( "pkgrm -n#{expand_options(@new_resource.options)} #{name}" ) Chef::Log.debug("#{@new_resource} removed version #{@new_resource.version}") end end diff --git a/lib/chef/provider/reboot.rb b/lib/chef/provider/reboot.rb new file mode 100644 index 0000000000..8dde4653ec --- /dev/null +++ b/lib/chef/provider/reboot.rb @@ -0,0 +1,69 @@ +# +# Author:: Chris Doherty <cdoherty@getchef.com>) +# Copyright:: Copyright (c) 2014 Chef, 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 'chef/log' +require 'chef/provider' + +class Chef + class Provider + class Reboot < Chef::Provider + + def whyrun_supported? + true + end + + def load_current_resource + @current_resource ||= Chef::Resource::Reboot.new(@new_resource.name) + @current_resource.reason(@new_resource.reason) + @current_resource.delay_mins(@new_resource.delay_mins) + @current_resource + end + + def request_reboot + node.run_context.request_reboot( + :delay_mins => @new_resource.delay_mins, + :reason => @new_resource.reason, + :timestamp => Time.now, + :requested_by => @new_resource.name + ) + end + + def action_request_reboot + converge_by("request a system reboot to occur if the run succeeds") do + Chef::Log.warn "Reboot requested:'#{@new_resource.name}'" + request_reboot + end + end + + def action_reboot_now + converge_by("rebooting the system immediately") do + Chef::Log.warn "Rebooting system immediately, requested by '#{@new_resource.name}'" + request_reboot + throw :end_client_run_early + end + end + + def action_cancel + converge_by("cancel any existing end-of-run reboot request") do + Chef::Log.warn "Reboot canceled: '#{@new_resource.name}'" + node.run_context.cancel_reboot + end + end + end + end +end diff --git a/lib/chef/provider/service/debian.rb b/lib/chef/provider/service/debian.rb index 06fe7fc480..1ebef90349 100644 --- a/lib/chef/provider/service/debian.rb +++ b/lib/chef/provider/service/debian.rb @@ -130,15 +130,15 @@ class Chef def enable_service if @new_resource.priority.is_a? Integer - run_command(:command => "/usr/sbin/update-rc.d -f #{@new_resource.service_name} remove") - run_command(:command => "/usr/sbin/update-rc.d #{@new_resource.service_name} defaults #{@new_resource.priority} #{100 - @new_resource.priority}") + shell_out!("/usr/sbin/update-rc.d -f #{@new_resource.service_name} remove") + shell_out!("/usr/sbin/update-rc.d #{@new_resource.service_name} defaults #{@new_resource.priority} #{100 - @new_resource.priority}") elsif @new_resource.priority.is_a? Hash # we call the same command regardless of we're enabling or disabling # users passing a Hash are responsible for setting their own start priorities set_priority else # No priority, go with update-rc.d defaults - run_command(:command => "/usr/sbin/update-rc.d -f #{@new_resource.service_name} remove") - run_command(:command => "/usr/sbin/update-rc.d #{@new_resource.service_name} defaults") + shell_out!("/usr/sbin/update-rc.d -f #{@new_resource.service_name} remove") + shell_out!("/usr/sbin/update-rc.d #{@new_resource.service_name} defaults") end end @@ -146,16 +146,16 @@ class Chef def disable_service if @new_resource.priority.is_a? Integer # Stop processes in reverse order of start using '100 - start_priority' - run_command(:command => "/usr/sbin/update-rc.d -f #{@new_resource.service_name} remove") - run_command(:command => "/usr/sbin/update-rc.d -f #{@new_resource.service_name} stop #{100 - @new_resource.priority} 2 3 4 5 .") + shell_out!("/usr/sbin/update-rc.d -f #{@new_resource.service_name} remove") + shell_out!("/usr/sbin/update-rc.d -f #{@new_resource.service_name} stop #{100 - @new_resource.priority} 2 3 4 5 .") elsif @new_resource.priority.is_a? Hash # we call the same command regardless of we're enabling or disabling # users passing a Hash are responsible for setting their own stop priorities set_priority else # no priority, using '100 - 20 (update-rc.d default)' to stop in reverse order of start - run_command(:command => "/usr/sbin/update-rc.d -f #{@new_resource.service_name} remove") - run_command(:command => "/usr/sbin/update-rc.d -f #{@new_resource.service_name} stop 80 2 3 4 5 .") + shell_out!("/usr/sbin/update-rc.d -f #{@new_resource.service_name} remove") + shell_out!("/usr/sbin/update-rc.d -f #{@new_resource.service_name} stop 80 2 3 4 5 .") end end @@ -166,8 +166,8 @@ class Chef priority = o[1] args += "#{action} #{priority} #{level} . " end - run_command(:command => "/usr/sbin/update-rc.d -f #{@new_resource.service_name} remove") - run_command(:command => "/usr/sbin/update-rc.d #{@new_resource.service_name} #{args}") + shell_out!("/usr/sbin/update-rc.d -f #{@new_resource.service_name} remove") + shell_out!("/usr/sbin/update-rc.d #{@new_resource.service_name} #{args}") end end end diff --git a/lib/chef/provider/service/freebsd.rb b/lib/chef/provider/service/freebsd.rb index 7b69957cc6..08d58232e1 100644 --- a/lib/chef/provider/service/freebsd.rb +++ b/lib/chef/provider/service/freebsd.rb @@ -78,7 +78,7 @@ class Chef if new_resource.start_command super else - shell_out!("#{init_command} faststart") + shell_out_with_systems_locale!("#{init_command} faststart") end end @@ -86,7 +86,7 @@ class Chef if new_resource.stop_command super else - shell_out!("#{init_command} faststop") + shell_out_with_systems_locale!("#{init_command} faststop") end end @@ -94,7 +94,7 @@ class Chef if new_resource.restart_command super elsif new_resource.supports[:restart] - shell_out!("#{init_command} fastrestart") + shell_out_with_systems_locale!("#{init_command} fastrestart") else stop_service sleep 1 diff --git a/lib/chef/provider/service/gentoo.rb b/lib/chef/provider/service/gentoo.rb index 1559c7893f..a68abfebc9 100644 --- a/lib/chef/provider/service/gentoo.rb +++ b/lib/chef/provider/service/gentoo.rb @@ -58,10 +58,10 @@ class Chef::Provider::Service::Gentoo < Chef::Provider::Service::Init end def enable_service() - run_command(:command => "/sbin/rc-update add #{@new_resource.service_name} default") + shell_out!("/sbin/rc-update add #{@new_resource.service_name} default") end def disable_service() - run_command(:command => "/sbin/rc-update del #{@new_resource.service_name} default") + shell_out!("/sbin/rc-update del #{@new_resource.service_name} default") end end diff --git a/lib/chef/provider/service/init.rb b/lib/chef/provider/service/init.rb index 23d9dde80a..5d8bb5bb38 100644 --- a/lib/chef/provider/service/init.rb +++ b/lib/chef/provider/service/init.rb @@ -50,7 +50,7 @@ class Chef if @new_resource.start_command super else - shell_out!("#{default_init_command} start") + shell_out_with_systems_locale!("#{default_init_command} start") end end @@ -58,7 +58,7 @@ class Chef if @new_resource.stop_command super else - shell_out!("#{default_init_command} stop") + shell_out_with_systems_locale!("#{default_init_command} stop") end end @@ -66,7 +66,7 @@ class Chef if @new_resource.restart_command super elsif @new_resource.supports[:restart] - shell_out!("#{default_init_command} restart") + shell_out_with_systems_locale!("#{default_init_command} restart") else stop_service sleep 1 @@ -78,7 +78,7 @@ class Chef if @new_resource.reload_command super elsif @new_resource.supports[:reload] - shell_out!("#{default_init_command} reload") + shell_out_with_systems_locale!("#{default_init_command} reload") end end end diff --git a/lib/chef/provider/service/insserv.rb b/lib/chef/provider/service/insserv.rb index 35767ee7b9..f4c85dd9d3 100644 --- a/lib/chef/provider/service/insserv.rb +++ b/lib/chef/provider/service/insserv.rb @@ -37,12 +37,12 @@ class Chef end def enable_service() - run_command(:command => "/sbin/insserv -r -f #{@new_resource.service_name}") - run_command(:command => "/sbin/insserv -d -f #{@new_resource.service_name}") + shell_out!("/sbin/insserv -r -f #{@new_resource.service_name}") + shell_out!("/sbin/insserv -d -f #{@new_resource.service_name}") end def disable_service() - run_command(:command => "/sbin/insserv -r -f #{@new_resource.service_name}") + shell_out!("/sbin/insserv -r -f #{@new_resource.service_name}") end end end diff --git a/lib/chef/provider/service/macosx.rb b/lib/chef/provider/service/macosx.rb index 36930ee4ac..cf5e554559 100644 --- a/lib/chef/provider/service/macosx.rb +++ b/lib/chef/provider/service/macosx.rb @@ -83,7 +83,7 @@ class Chef if @new_resource.start_command super else - shell_out!("launchctl load -w '#{@plist}'", :user => @owner_uid, :group => @owner_gid) + shell_out_with_systems_locale!("launchctl load -w '#{@plist}'", :user => @owner_uid, :group => @owner_gid) end end end @@ -95,7 +95,7 @@ class Chef if @new_resource.stop_command super else - shell_out!("launchctl unload '#{@plist}'", :user => @owner_uid, :group => @owner_gid) + shell_out_with_systems_locale!("launchctl unload '#{@plist}'", :user => @owner_uid, :group => @owner_gid) end end end diff --git a/lib/chef/provider/service/simple.rb b/lib/chef/provider/service/simple.rb index 0eb983a0bf..bd51d15f84 100644 --- a/lib/chef/provider/service/simple.rb +++ b/lib/chef/provider/service/simple.rb @@ -85,16 +85,16 @@ class Chef end def start_service - shell_out!(@new_resource.start_command) + shell_out_with_systems_locale!(@new_resource.start_command) end def stop_service - shell_out!(@new_resource.stop_command) + shell_out_with_systems_locale!(@new_resource.stop_command) end def restart_service if @new_resource.restart_command - shell_out!(@new_resource.restart_command) + shell_out_with_systems_locale!(@new_resource.restart_command) else stop_service sleep 1 @@ -103,7 +103,7 @@ class Chef end def reload_service - shell_out!(@new_resource.reload_command) + shell_out_with_systems_locale!(@new_resource.reload_command) end protected diff --git a/lib/chef/provider/service/solaris.rb b/lib/chef/provider/service/solaris.rb index 0c47a3462b..f0584dcf6d 100644 --- a/lib/chef/provider/service/solaris.rb +++ b/lib/chef/provider/service/solaris.rb @@ -56,7 +56,7 @@ class Chef alias_method :start_service, :enable_service def reload_service - shell_out!("#{default_init_command} refresh #{@new_resource.service_name}") + shell_out_with_systems_locale!("#{default_init_command} refresh #{@new_resource.service_name}") end def restart_service diff --git a/lib/chef/provider/service/systemd.rb b/lib/chef/provider/service/systemd.rb index 6231603d03..31feee65d4 100644 --- a/lib/chef/provider/service/systemd.rb +++ b/lib/chef/provider/service/systemd.rb @@ -28,7 +28,7 @@ class Chef::Provider::Service::Systemd < Chef::Provider::Service::Simple if @new_resource.status_command Chef::Log.debug("#{@new_resource} you have specified a status command, running..") - unless shell_out_with_systems_locale(@new_resource.status_command).error? + unless shell_out(@new_resource.status_command).error? @current_resource.running(true) else @status_check_success = false @@ -61,7 +61,7 @@ class Chef::Provider::Service::Systemd < Chef::Provider::Service::Simple if @new_resource.start_command super else - shell_out_with_systems_locale("/bin/systemctl start #{@new_resource.service_name}") + shell_out_with_systems_locale!("/bin/systemctl start #{@new_resource.service_name}") end end end @@ -73,7 +73,7 @@ class Chef::Provider::Service::Systemd < Chef::Provider::Service::Simple if @new_resource.stop_command super else - shell_out_with_systems_locale("/bin/systemctl stop #{@new_resource.service_name}") + shell_out_with_systems_locale!("/bin/systemctl stop #{@new_resource.service_name}") end end end @@ -82,7 +82,7 @@ class Chef::Provider::Service::Systemd < Chef::Provider::Service::Simple if @new_resource.restart_command super else - shell_out_with_systems_locale("/bin/systemctl restart #{@new_resource.service_name}") + shell_out_with_systems_locale!("/bin/systemctl restart #{@new_resource.service_name}") end end @@ -91,7 +91,7 @@ class Chef::Provider::Service::Systemd < Chef::Provider::Service::Simple super else if @current_resource.running - shell_out_with_systems_locale("/bin/systemctl reload #{@new_resource.service_name}") + shell_out_with_systems_locale!("/bin/systemctl reload #{@new_resource.service_name}") else start_service end @@ -99,18 +99,18 @@ class Chef::Provider::Service::Systemd < Chef::Provider::Service::Simple end def enable_service - shell_out_with_systems_locale("/bin/systemctl enable #{@new_resource.service_name}") + shell_out!("/bin/systemctl enable #{@new_resource.service_name}") end def disable_service - shell_out_with_systems_locale("/bin/systemctl disable #{@new_resource.service_name}") + shell_out!("/bin/systemctl disable #{@new_resource.service_name}") end def is_active? - shell_out_with_systems_locale("/bin/systemctl is-active #{@new_resource.service_name} --quiet").exitstatus == 0 + shell_out("/bin/systemctl is-active #{@new_resource.service_name} --quiet").exitstatus == 0 end def is_enabled? - shell_out_with_systems_locale("/bin/systemctl is-enabled #{@new_resource.service_name} --quiet").exitstatus == 0 + shell_out("/bin/systemctl is-enabled #{@new_resource.service_name} --quiet").exitstatus == 0 end end diff --git a/lib/chef/provider/service/upstart.rb b/lib/chef/provider/service/upstart.rb index c81a8a50dc..670bf9e5f8 100644 --- a/lib/chef/provider/service/upstart.rb +++ b/lib/chef/provider/service/upstart.rb @@ -97,10 +97,10 @@ class Chef Chef::Log.debug("#{@new_resource} you have specified a status command, running..") begin - if run_command_with_systems_locale(:command => @new_resource.status_command) == 0 + if shell_out!(@new_resource.status_command) == 0 @current_resource.running true end - rescue Chef::Exceptions::Exec + rescue @command_success = false @current_resource.running false nil @@ -153,7 +153,7 @@ class Chef if @new_resource.start_command super else - run_command_with_systems_locale(:command => "/sbin/start #{@job}") + shell_out_with_systems_locale!("/sbin/start #{@job}") end end end @@ -167,7 +167,7 @@ class Chef if @new_resource.stop_command super else - run_command_with_systems_locale(:command => "/sbin/stop #{@job}") + shell_out_with_systems_locale!("/sbin/stop #{@job}") end end end @@ -179,7 +179,7 @@ class Chef # Older versions of upstart would fail on restart if the service was currently stopped, check for that. LP:430883 else if @current_resource.running - run_command_with_systems_locale(:command => "/sbin/restart #{@job}") + shell_out_with_systems_locale!("/sbin/restart #{@job}") else start_service end @@ -191,7 +191,7 @@ class Chef super else # upstart >= 0.6.3-4 supports reload (HUP) - run_command_with_systems_locale(:command => "/sbin/reload #{@job}") + shell_out_with_systems_locale!("/sbin/reload #{@job}") end end diff --git a/lib/chef/provider/subversion.rb b/lib/chef/provider/subversion.rb index 81ed639c53..6cf31c8ec8 100644 --- a/lib/chef/provider/subversion.rb +++ b/lib/chef/provider/subversion.rb @@ -60,7 +60,7 @@ class Chef def action_checkout if target_dir_non_existent_or_empty? converge_by("perform checkout of #{@new_resource.repository} into #{@new_resource.destination}") do - run_command(run_options(:command => checkout_command)) + shell_out!(run_options(command: checkout_command)) end else Chef::Log.debug "#{@new_resource} checkout destination #{@new_resource.destination} already exists or is a non-empty directory - nothing to do" @@ -77,7 +77,7 @@ class Chef def action_force_export converge_by("export #{@new_resource.repository} into #{@new_resource.destination}") do - run_command(run_options(:command => export_command)) + shell_out!(run_options(command: export_command)) end end @@ -88,7 +88,7 @@ class Chef Chef::Log.debug "#{@new_resource} current revision: #{current_rev} target revision: #{revision_int}" unless current_revision_matches_target_revision? converge_by("sync #{@new_resource.destination} from #{@new_resource.repository}") do - run_command(run_options(:command => sync_command)) + shell_out!(run_options(command: sync_command)) Chef::Log.info "#{@new_resource} updated to revision: #{revision_int}" end end @@ -100,14 +100,14 @@ class Chef def sync_command c = scm :update, @new_resource.svn_arguments, verbose, authentication, "-r#{revision_int}", @new_resource.destination Chef::Log.debug "#{@new_resource} updated working copy #{@new_resource.destination} to revision #{@new_resource.revision}" - c + c end def checkout_command c = scm :checkout, @new_resource.svn_arguments, verbose, authentication, "-r#{revision_int}", @new_resource.repository, @new_resource.destination Chef::Log.info "#{@new_resource} checked out #{@new_resource.repository} at revision #{@new_resource.revision} to #{@new_resource.destination}" - c + c end def export_command @@ -116,7 +116,7 @@ class Chef "-r#{revision_int}" << @new_resource.repository << @new_resource.destination c = scm :export, *args Chef::Log.info "#{@new_resource} exported #{@new_resource.repository} at revision #{@new_resource.revision} to #{@new_resource.destination}" - c + c end # If the specified revision isn't an integer ("HEAD" for example), look diff --git a/lib/chef/provider/whyrun_safe_ruby_block.rb b/lib/chef/provider/whyrun_safe_ruby_block.rb index 4b491a4f60..e5f35debd7 100644 --- a/lib/chef/provider/whyrun_safe_ruby_block.rb +++ b/lib/chef/provider/whyrun_safe_ruby_block.rb @@ -19,7 +19,7 @@ class Chef class Provider class WhyrunSafeRubyBlock < Chef::Provider::RubyBlock - def action_create + def action_run @new_resource.block.call @new_resource.updated_by_last_action(true) @run_context.events.resource_update_applied(@new_resource, :create, "execute the whyrun_safe_ruby_block #{@new_resource.name}") diff --git a/lib/chef/providers.rb b/lib/chef/providers.rb index 3c9e94e6f7..1b0ff3ffff 100644 --- a/lib/chef/providers.rb +++ b/lib/chef/providers.rb @@ -39,6 +39,7 @@ require 'chef/provider/mdadm' require 'chef/provider/mount' require 'chef/provider/package' require 'chef/provider/powershell_script' +require 'chef/provider/reboot' require 'chef/provider/remote_directory' require 'chef/provider/remote_file' require 'chef/provider/route' diff --git a/lib/chef/resource/reboot.rb b/lib/chef/resource/reboot.rb new file mode 100644 index 0000000000..d6caafdea8 --- /dev/null +++ b/lib/chef/resource/reboot.rb @@ -0,0 +1,48 @@ +# +# Author:: Chris Doherty <cdoherty@getchef.com>) +# Copyright:: Copyright (c) 2014 Chef, 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 '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 + class Reboot < Chef::Resource + def initialize(name, run_context=nil) + super + @resource_name = :reboot + @provider = Chef::Provider::Reboot + @allowed_actions = [:request_reboot, :reboot_now, :cancel] + + @reason = "Reboot by Chef" + @delay_mins = 0 + + # no default action. + end + + def reason(arg=nil) + set_or_return(:reason, arg, :kind_of => String) + end + + def delay_mins(arg=nil) + set_or_return(:delay_mins, arg, :kind_of => Fixnum) + end + end + end +end diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb index 93ff682288..8c2f71bd30 100644 --- a/lib/chef/resources.rb +++ b/lib/chef/resources.rb @@ -53,6 +53,7 @@ require 'chef/resource/perl' require 'chef/resource/portage_package' require 'chef/resource/powershell_script' require 'chef/resource/python' +require 'chef/resource/reboot' require 'chef/resource/registry_key' require 'chef/resource/remote_directory' require 'chef/resource/remote_file' diff --git a/lib/chef/run_context.rb b/lib/chef/run_context.rb index 3dd53f0f8f..bbe2f9eba0 100644 --- a/lib/chef/run_context.rb +++ b/lib/chef/run_context.rb @@ -61,6 +61,9 @@ class Chef # Event dispatcher for this run. attr_reader :events + # Hash of factoids for a reboot request. + attr_reader :reboot_info + # Creates a new Chef::RunContext object and populates its fields. This object gets # used by the Chef Server to generate a fully compiled recipe list for a node. # @@ -76,6 +79,7 @@ class Chef @loaded_recipes = {} @loaded_attributes = {} @events = events + @reboot_info = {} @node.run_context = self @@ -271,6 +275,27 @@ ERROR_MESSAGE end end + # there are options for how to handle multiple calls to these functions: + # 1. first call always wins (never change @reboot_info once set). + # 2. last call always wins (happily change @reboot_info whenever). + # 3. raise an exception on the first conflict. + # 4. disable reboot after this run if anyone ever calls :cancel. + # 5. raise an exception on any second call. + # 6. ? + def request_reboot(reboot_info) + Chef::Log::info "Changing reboot status from #{@reboot_info.inspect} to #{reboot_info.inspect}" + @reboot_info = reboot_info + end + + def cancel_reboot + Chef::Log::info "Changing reboot status from #{@reboot_info.inspect} to {}" + @reboot_info = {} + end + + def reboot_requested? + @reboot_info.size > 0 + end + private def loaded_recipe(cookbook, recipe) diff --git a/lib/chef/win32/api/file.rb b/lib/chef/win32/api/file.rb index 7a8dafd8b5..da9713e119 100644 --- a/lib/chef/win32/api/file.rb +++ b/lib/chef/win32/api/file.rb @@ -459,11 +459,7 @@ BOOL WINAPI DeviceIoControl( # to be passed to the *W vesion of WinAPI File # functions def encode_path(path) - (path_prepender << path.gsub(::File::SEPARATOR, ::File::ALT_SEPARATOR)).to_wstring - end - - def path_prepender - "\\\\?\\" + Chef::Util::PathHelper.canonical_path(path).to_wstring end # retrieves a file search handle and passes it diff --git a/pedant.gemfile b/pedant.gemfile index 7b135bdef4..d4ac849707 100644 --- a/pedant.gemfile +++ b/pedant.gemfile @@ -7,6 +7,9 @@ gem 'chef-pedant', :github => 'opscode/chef-pedant', :branch => "metadata-name-f # TODO figure out how to grab this stuff from the main Gemfile gem "activesupport", "< 4.0.0", :group => :compat_testing, :platform => "ruby" +gem "mixlib-shellout", github: "opscode/mixlib-shellout", branch: "master" +gem "ohai", github: "opscode/ohai", branch: "master" + group(:docgen) do gem "yard" end diff --git a/spec/functional/mixin/shell_out_spec.rb b/spec/functional/mixin/shell_out_spec.rb index 92492fcf6f..35e5b30eae 100644 --- a/spec/functional/mixin/shell_out_spec.rb +++ b/spec/functional/mixin/shell_out_spec.rb @@ -29,7 +29,7 @@ describe Chef::Mixin::ShellOut do shell_out_with_systems_locale('echo $LC_ALL') end - cmd.stdout.chomp.should match_environment_variable('LC_ALL') + expect(cmd.stdout.chomp).to match_environment_variable('LC_ALL') end end @@ -41,7 +41,7 @@ describe Chef::Mixin::ShellOut do shell_out_with_systems_locale('echo $LC_ALL', :environment => {'LC_ALL' => 'POSIX'}) end - cmd.stdout.chomp.should eq 'POSIX' + expect(cmd.stdout.chomp).to eq 'POSIX' end end end diff --git a/spec/functional/provider/whyrun_safe_ruby_block_spec.rb b/spec/functional/provider/whyrun_safe_ruby_block_spec.rb new file mode 100644 index 0000000000..150d46d384 --- /dev/null +++ b/spec/functional/provider/whyrun_safe_ruby_block_spec.rb @@ -0,0 +1,51 @@ +# +# 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' + +describe Chef::Resource::WhyrunSafeRubyBlock do + let(:node) { Chef::Node.new } + + let(:run_context) { + events = Chef::EventDispatch::Dispatcher.new + Chef::RunContext.new(node, {}, events) + } + + before do + $evil_global_evil_laugh = :wahwah + Chef::Config[:why_run] = true + end + + after do + Chef::Config[:why_run] = false + end + + describe "when testing the resource" do + let(:new_resource) do + r = Chef::Resource::WhyrunSafeRubyBlock.new("reload all", run_context) + r.block { $evil_global_evil_laugh = :mwahahaha } + r + end + + it "updates the evil laugh, even in why-run mode" do + new_resource.run_action(new_resource.action) + $evil_global_evil_laugh.should == :mwahahaha + new_resource.should be_updated + end + end +end diff --git a/spec/functional/rebooter_spec.rb b/spec/functional/rebooter_spec.rb new file mode 100644 index 0000000000..8006580d5c --- /dev/null +++ b/spec/functional/rebooter_spec.rb @@ -0,0 +1,105 @@ +# +# Author:: Chris Doherty <cdoherty@getchef.com>) +# Copyright:: Copyright (c) 2014 Chef, 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::Platform::Rebooter do + + let(:reboot_info) do + { + :delay_mins => 5, + :requested_by => "reboot resource functional test", + :reason => "rebooter spec test" + } + end + + def create_resource + resource = Chef::Resource::Reboot.new(expected[:requested_by], run_context) + resource.delay_mins(expected[:delay_mins]) + resource.reason(expected[:reason]) + resource + end + + let(:run_context) do + node = Chef::Node.new + events = Chef::EventDispatch::Dispatcher.new + Chef::RunContext.new(node, {}, events) + end + + let(:expected) do + { + :windows => 'shutdown /r /t 5 /c "rebooter spec test"', + :linux => 'shutdown -r +5 "rebooter spec test"' + } + end + + let(:rebooter) { Chef::Platform::Rebooter } + + describe '#reboot_if_needed!' do + + it 'should not call #shell_out! when reboot has not been requested' do + expect(rebooter).to receive(:shell_out!).exactly(0).times + expect(rebooter).to receive(:reboot_if_needed!).once.and_call_original + rebooter.reboot_if_needed!(run_context.node) + end + + describe 'calling #shell_out! to reboot' do + + before(:each) do + run_context.request_reboot(reboot_info) + end + + after(:each) do + run_context.cancel_reboot + end + + shared_context 'test a reboot method' do + def test_rebooter_method(method_sym, is_windows, expected_reboot_str) + Chef::Platform.stub(:windows?).and_return(is_windows) + expect(rebooter).to receive(:shell_out!).once.with(expected_reboot_str) + expect(rebooter).to receive(method_sym).once.and_call_original + rebooter.send(method_sym, run_context.node) + end + end + + describe 'when using #reboot_if_needed!' do + include_context 'test a reboot method' + + it 'should produce the correct string on Windows' do + test_rebooter_method(:reboot_if_needed!, true, expected[:windows]) + end + + it 'should produce the correct (Linux-specific) string on non-Windows' do + test_rebooter_method(:reboot_if_needed!, false, expected[:linux]) + end + end + + describe 'when using #reboot!' do + include_context 'test a reboot method' + + it 'should produce the correct string on Windows' do + test_rebooter_method(:reboot!, true, expected[:windows]) + end + + it 'should produce the correct (Linux-specific) string on non-Windows' do + test_rebooter_method(:reboot!, false, expected[:linux]) + end + end + end + end +end diff --git a/spec/functional/resource/deploy_revision_spec.rb b/spec/functional/resource/deploy_revision_spec.rb index 9ff1391e35..eae422ac1d 100644 --- a/spec/functional/resource/deploy_revision_spec.rb +++ b/spec/functional/resource/deploy_revision_spec.rb @@ -59,7 +59,6 @@ describe Chef::Resource::DeployRevision, :unix_only => true do let(:event_dispatch) { Chef::EventDispatch::Dispatcher.new } let(:run_context) { Chef::RunContext.new(node, {}, event_dispatch) } - # These tests use git's bundle feature, which is a way to export an entire # git repo (or subset of commits) as a single file. # @@ -799,7 +798,6 @@ describe Chef::Resource::DeployRevision, :unix_only => true do deploy_to_latest_with_callback_tracking.run_action(:deploy) end - let(:deploy_that_fails) do resource = deploy_to_latest_rev.dup errant_callback = lambda {|x| raise Exception, "I am a failed deploy" } @@ -882,5 +880,3 @@ describe Chef::Resource::DeployRevision, :unix_only => true do end end - - diff --git a/spec/functional/resource/file_spec.rb b/spec/functional/resource/file_spec.rb index d6f56db3e9..99966f85c8 100644 --- a/spec/functional/resource/file_spec.rb +++ b/spec/functional/resource/file_spec.rb @@ -24,12 +24,18 @@ describe Chef::Resource::File do let(:file_base) { "file_spec" } let(:expected_content) { "Don't fear the ruby." } - def create_resource + def create_resource(opts={}) events = Chef::EventDispatch::Dispatcher.new node = Chef::Node.new run_context = Chef::RunContext.new(node, {}, events) - resource = Chef::Resource::File.new(path, run_context) - resource + + use_path = if opts[:use_relative_path] + File.basename(path) + else + path + end + + Chef::Resource::File.new(use_path, run_context) end let(:resource) do @@ -42,6 +48,10 @@ describe Chef::Resource::File do create_resource end + let(:resource_with_relative_path) do + create_resource(:use_relative_path => true) + end + let(:unmanaged_content) do "This is file content that is not managed by chef" end @@ -74,6 +84,19 @@ describe Chef::Resource::File do end end + # github issue 1842. + describe "when running action :create on a relative path" do + before do + resource_with_relative_path.run_action(:create) + end + + context "and the file exists" do + it "should run without an exception" do + resource_with_relative_path.run_action(:create) + end + end + end + describe "when running action :touch" do context "and the target file does not exist" do before do diff --git a/spec/functional/resource/link_spec.rb b/spec/functional/resource/link_spec.rb index 8e630d84f2..2220e973cf 100644 --- a/spec/functional/resource/link_spec.rb +++ b/spec/functional/resource/link_spec.rb @@ -72,8 +72,8 @@ describe Chef::Resource::Link do end end - def canonicalize(path) - windows? ? path.gsub('/', '\\') : path + def paths_eql?(path1, path2) + Chef::Util::PathHelper.paths_eql?(path1, path2) end def symlink(a, b) @@ -180,7 +180,7 @@ describe Chef::Resource::Link do it 'links to the target file' do symlink?(target_file).should be_true - readlink(target_file).should == canonicalize(to) + paths_eql?(readlink(target_file), to).should be_true end it 'marks the resource updated' do resource.should be_updated @@ -201,7 +201,7 @@ describe Chef::Resource::Link do it 'leaves the file linked' do symlink?(target_file).should be_true - readlink(target_file).should == canonicalize(to) + paths_eql?(readlink(target_file), to).should be_true end it 'does not mark the resource updated' do resource.should_not be_updated @@ -279,7 +279,7 @@ describe Chef::Resource::Link do before(:each) do symlink(to, target_file) symlink?(target_file).should be_true - readlink(target_file).should == canonicalize(to) + paths_eql?(readlink(target_file), to).should be_true end include_context 'create symbolic link is noop' include_context 'delete succeeds' @@ -294,7 +294,7 @@ describe Chef::Resource::Link do File.open(@other_target, 'w') { |file| file.write('eek') } symlink(@other_target, target_file) symlink?(target_file).should be_true - readlink(target_file).should == canonicalize(@other_target) + paths_eql?(readlink(target_file), @other_target).should be_true end after(:each) do File.delete(@other_target) @@ -311,7 +311,7 @@ describe Chef::Resource::Link do nonexistent = File.join(test_file_dir, make_tmpname('nonexistent_spec')) symlink(nonexistent, target_file) symlink?(target_file).should be_true - readlink(target_file).should == canonicalize(nonexistent) + paths_eql?(readlink(target_file), nonexistent).should be_true end include_context 'create symbolic link succeeds' include_context 'delete succeeds' @@ -393,7 +393,7 @@ describe Chef::Resource::Link do File.open(@other_target, "w") { |file| file.write("eek") } symlink(@other_target, to) symlink?(to).should be_true - readlink(to).should == canonicalize(@other_target) + paths_eql?(readlink(to), @other_target).should be_true end after(:each) do File.delete(@other_target) @@ -408,7 +408,7 @@ describe Chef::Resource::Link do @other_target = File.join(test_file_dir, make_tmpname("other_spec")) symlink(@other_target, to) symlink?(to).should be_true - readlink(to).should == canonicalize(@other_target) + paths_eql?(readlink(to), @other_target).should be_true end context 'and the link does not yet exist' do include_context 'create symbolic link succeeds' @@ -441,7 +441,7 @@ describe Chef::Resource::Link do before(:each) do symlink(to, target_file) symlink?(target_file).should be_true - readlink(target_file).should == canonicalize(to) + paths_eql?(readlink(target_file), to).should be_true end include_context 'create symbolic link is noop' include_context 'delete succeeds' @@ -450,7 +450,7 @@ describe Chef::Resource::Link do before(:each) do symlink(absolute_to, target_file) symlink?(target_file).should be_true - readlink(target_file).should == canonicalize(absolute_to) + paths_eql?(readlink(target_file), absolute_to).should be_true end include_context 'create symbolic link succeeds' include_context 'delete succeeds' @@ -478,7 +478,7 @@ describe Chef::Resource::Link do before(:each) do symlink(to, target_file) symlink?(target_file).should be_true - readlink(target_file).should == canonicalize(to) + paths_eql?(readlink(target_file), to).should be_true end include_context 'create hard link succeeds' it_behaves_like 'delete errors out' @@ -552,7 +552,7 @@ describe Chef::Resource::Link do File.open(@other_target, "w") { |file| file.write("eek") } symlink(@other_target, to) symlink?(to).should be_true - readlink(to).should == canonicalize(@other_target) + paths_eql?(readlink(to), @other_target).should be_true end after(:each) do File.delete(@other_target) @@ -564,7 +564,7 @@ describe Chef::Resource::Link do # OS X gets angry about this sort of link. Bug in OS X, IMO. pending('OS X/FreeBSD/AIX symlink? and readlink working on hard links to symlinks', :if => (os_x? or freebsd? or aix?)) do symlink?(target_file).should be_true - readlink(target_file).should == canonicalize(@other_target) + paths_eql?(readlink(target_file), @other_target).should be_true end end include_context 'delete is noop' @@ -575,7 +575,7 @@ describe Chef::Resource::Link do @other_target = File.join(test_file_dir, make_tmpname("other_spec")) symlink(@other_target, to) symlink?(to).should be_true - readlink(to).should == canonicalize(@other_target) + paths_eql?(readlink(to), @other_target).should be_true end context 'and the link does not yet exist' do it 'links to the target file' do @@ -588,7 +588,7 @@ describe Chef::Resource::Link do File.exists?(target_file).should be_false end symlink?(target_file).should be_true - readlink(target_file).should == canonicalize(@other_target) + paths_eql?(readlink(target_file), @other_target).should be_true end end include_context 'delete is noop' diff --git a/spec/functional/resource/reboot_spec.rb b/spec/functional/resource/reboot_spec.rb new file mode 100644 index 0000000000..735ca994c8 --- /dev/null +++ b/spec/functional/resource/reboot_spec.rb @@ -0,0 +1,103 @@ +# +# Author:: Chris Doherty <cdoherty@getchef.com>) +# Copyright:: Copyright (c) 2014 Chef, 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 + + let(:expected) do + { + :delay_mins => 5, + :requested_by => "reboot resource functional test", + :reason => "reboot resource spec test" + } + end + + def create_resource + node = Chef::Node.new + events = Chef::EventDispatch::Dispatcher.new + run_context = Chef::RunContext.new(node, {}, events) + resource = Chef::Resource::Reboot.new(expected[:requested_by], run_context) + resource.delay_mins(expected[:delay_mins]) + resource.reason(expected[:reason]) + resource + end + + let(:resource) do + create_resource + end + + shared_context 'testing run context modification' do + def test_reboot_action(resource) + reboot_info = resource.run_context.reboot_info + expect(reboot_info.keys.sort).to eq([:delay_mins, :reason, :requested_by, :timestamp]) + expect(reboot_info[:delay_mins]).to eq(expected[:delay_mins]) + expect(reboot_info[:reason]).to eq(expected[:reason]) + expect(reboot_info[:requested_by]).to eq(expected[:requested_by]) + + expect(resource.run_context.reboot_requested?).to be_true + end + end + + # the currently defined behavior for multiple calls to this resource is "last one wins." + describe 'the request_reboot_on_successful_run action' do + include_context 'testing run context modification' + + before do + resource.run_action(:request_reboot) + end + + after do + resource.run_context.cancel_reboot + end + + it 'should have modified the run context correctly' do + test_reboot_action(resource) + end + end + + describe 'the reboot_interrupt_run action' do + include_context 'testing run context modification' + + after do + resource.run_context.cancel_reboot + end + + it 'should have modified the run context correctly' do + # this doesn't actually test the flow of Chef::Client#do_run, unfortunately. + expect { + resource.run_action(:reboot_now) + }.to throw_symbol(:end_client_run_early) + + test_reboot_action(resource) + end + end + + describe "the cancel action" do + before do + resource.run_context.request_reboot(expected) + resource.run_action(:cancel) + end + + it 'should have cleared the reboot request' do + # arguably we shouldn't be querying RunContext's internal data directly. + expect(resource.run_context.reboot_info).to eq({}) + expect(resource.run_context.reboot_requested?).to be_false + end + end +end diff --git a/spec/unit/application/apply.rb b/spec/unit/application/apply.rb index 32c98c6ed6..62a53c2a31 100644 --- a/spec/unit/application/apply.rb +++ b/spec/unit/application/apply.rb @@ -20,7 +20,7 @@ require 'spec_helper' describe Chef::Application::Apply do before do - @app = Chef::Application::Recipe.new + @app = Chef::Application::Apply.new @app.stub(:configure_logging).and_return(true) @recipe_text = "package 'nyancat'" Chef::Config[:solo] = true @@ -73,4 +73,14 @@ describe Chef::Application::Apply do @recipe_fh.path.should == @app.instance_variable_get(:@recipe_filename) end end + describe "recipe_file_arg" do + before do + ARGV.clear + end + it "should exit and log message" do + Chef::Log.should_receive(:debug).with(/^No recipe file provided/) + lambda { @app.run }.should raise_error(SystemExit) { |e| e.status.should == 1 } + end + + end end diff --git a/spec/unit/dsl/reboot_pending_spec.rb b/spec/unit/dsl/reboot_pending_spec.rb index 8576ae168a..0d643514e0 100644 --- a/spec/unit/dsl/reboot_pending_spec.rb +++ b/spec/unit/dsl/reboot_pending_spec.rb @@ -21,7 +21,7 @@ require "spec_helper" describe Chef::DSL::RebootPending do describe "reboot_pending?" do - describe "in isoloation" do + describe "in isolation" do let(:recipe) { Object.new.extend(Chef::DSL::RebootPending) } before do @@ -74,12 +74,6 @@ describe Chef::DSL::RebootPending do end end - context "platform is not supported" do - it 'should raise an exception' do - recipe.stub_chain(:node, :[]).with(:platform).and_return('msdos') - expect { recipe.reboot_pending? }.to raise_error(Chef::Exceptions::UnsupportedPlatform) - end - end end # describe in isolation describe "in a recipe" do diff --git a/spec/unit/knife/core/bootstrap_context_spec.rb b/spec/unit/knife/core/bootstrap_context_spec.rb index 064f8c5621..6427071a6b 100644 --- a/spec/unit/knife/core/bootstrap_context_spec.rb +++ b/spec/unit/knife/core/bootstrap_context_spec.rb @@ -119,7 +119,20 @@ EXPECTED context "via config[:secret_file]" do let(:chef_config) do { - :knife => {:secret_file => secret_file} + :knife => {:secret_file => secret_file} + } + end + it "reads the encrypted_data_bag_secret" do + bootstrap_context.encrypted_data_bag_secret.should eq IO.read(secret_file) + end + end + + context "via config[:secret_file] with short home path" do + let(:chef_config) do + home_path = File.expand_path("~") + shorted_secret_file_path = secret_file.gsub(home_path, "~") + { + :knife => {:secret_file => shorted_secret_file_path} } end it "reads the encrypted_data_bag_secret" do diff --git a/spec/unit/mixin/shell_out_spec.rb b/spec/unit/mixin/shell_out_spec.rb index 1f85ec6bf1..38a63a32ee 100644 --- a/spec/unit/mixin/shell_out_spec.rb +++ b/spec/unit/mixin/shell_out_spec.rb @@ -32,7 +32,7 @@ describe Chef::Mixin::ShellOut do let(:output) { StringIO.new } let!(:capture_log_output) { Chef::Log.logger = Logger.new(output) } - let(:assume_deprecation_log_level) { Chef::Log.stub(:level).and_return(:warn) } + let(:assume_deprecation_log_level) { allow(Chef::Log).to receive(:level).and_return(:warn) } context 'without options' do let(:command_args) { [ cmd ] } @@ -55,9 +55,9 @@ describe Chef::Mixin::ShellOut do it 'should emit a deprecation warning' do assume_deprecation_log_level and capture_log_output subject - output.string.should match /DEPRECATION:/ - output.string.should match Regexp.escape(old_option.to_s) - output.string.should match Regexp.escape(new_option.to_s) + expect(output.string).to match /DEPRECATION:/ + expect(output.string).to match Regexp.escape(old_option.to_s) + expect(output.string).to match Regexp.escape(new_option.to_s) end end @@ -106,7 +106,7 @@ describe Chef::Mixin::ShellOut do end end - describe "#shell_out_with_systems_locale" do + context "when testing individual methods" do before(:each) do @original_env = ENV.to_hash ENV.clear @@ -120,82 +120,152 @@ describe Chef::Mixin::ShellOut do let(:shell_out) { Chef::Mixin::ShellOut } let(:cmd) { "echo '#{rand(1000)}'" } - describe "when the last argument is a Hash" do - describe "and environment is an option" do - it "should not change environment['LC_ALL'] when set to nil" do - options = { :environment => { 'LC_ALL' => nil } } - shell_out.should_receive(:shell_out).with(cmd, options).and_return(true) - shell_out.shell_out_with_systems_locale(cmd, options) + describe "#shell_out" do + + describe "when the last argument is a Hash" do + describe "and environment is an option" do + it "should not change environment['LC_ALL'] when set to nil" do + options = { :environment => { 'LC_ALL' => nil } } + expect(shell_out).to receive(:shell_out_command).with(cmd, options).and_return(true) + shell_out.shell_out(cmd, options) + end + + it "should not change environment['LC_ALL'] when set to non-nil" do + options = { :environment => { 'LC_ALL' => 'en_US.UTF-8' } } + expect(shell_out).to receive(:shell_out_command).with(cmd, options).and_return(true) + shell_out.shell_out(cmd, options) + end + + it "should set environment['LC_ALL'] to 'en_US.UTF-8' when 'LC_ALL' not present" do + options = { :environment => { 'HOME' => '/Users/morty' } } + expect(shell_out).to receive(:shell_out_command).with(cmd, { + :environment => { 'HOME' => '/Users/morty', 'LC_ALL' => Chef::Config[:internal_locale] }, + }).and_return(true) + shell_out.shell_out(cmd, options) + end + + it "should not mutate the options hash when it adds LC_ALL" do + options = { :environment => { 'HOME' => '/Users/morty' } } + expect(shell_out).to receive(:shell_out_command).with(cmd, { + :environment => { 'HOME' => '/Users/morty', 'LC_ALL' => Chef::Config[:internal_locale] }, + }).and_return(true) + shell_out.shell_out(cmd, options) + expect(options[:environment].has_key?('LC_ALL')).to be false + end end - it "should not change environment['LC_ALL'] when set to non-nil" do - options = { :environment => { 'LC_ALL' => 'en_US.UTF-8' } } - shell_out.should_receive(:shell_out).with(cmd, options).and_return(true) - shell_out.shell_out_with_systems_locale(cmd, options) + describe "and env is an option" do + it "should not change env when set to nil" do + options = { :env => { 'LC_ALL' => nil } } + expect(shell_out).to receive(:shell_out_command).with(cmd, options).and_return(true) + shell_out.shell_out(cmd, options) + end + + it "should not change env when set to non-nil" do + options = { :env => { 'LC_ALL' => 'de_DE.UTF-8'}} + expect(shell_out).to receive(:shell_out_command).with(cmd, options).and_return(true) + shell_out.shell_out(cmd, options) + end + + it "should set env['LC_ALL'] to 'en_US.UTF-8' when 'LC_ALL' not present" do + options = { :env => { 'HOME' => '/Users/morty' } } + expect(shell_out).to receive(:shell_out_command).with(cmd, { + :env => { 'HOME' => '/Users/morty', 'LC_ALL' => Chef::Config[:internal_locale] }, + }).and_return(true) + shell_out.shell_out(cmd, options) + end + + it "should not mutate the options hash when it adds LC_ALL" do + options = { :env => { 'HOME' => '/Users/morty' } } + expect(shell_out).to receive(:shell_out_command).with(cmd, { + :env => { 'HOME' => '/Users/morty', 'LC_ALL' => Chef::Config[:internal_locale] }, + }).and_return(true) + shell_out.shell_out(cmd, options) + expect(options[:env].has_key?('LC_ALL')).to be false + end end - it "should set environment['LC_ALL'] to nil when 'LC_ALL' not present" do - options = { :environment => { 'HOME' => '/Users/morty' } } - shell_out.should_receive(:shell_out).with( - cmd, - { :environment => { - 'HOME' => '/Users/morty', - 'LC_ALL' => nil } - } - ).and_return(true) - shell_out.shell_out_with_systems_locale(cmd, options) + describe "and no env/environment option is present" do + it "should add environment option and set environment['LC_ALL'] to 'en_US.UTF_8'" do + options = { :user => 'morty' } + expect(shell_out).to receive(:shell_out_command).with(cmd, { + :user => 'morty', :environment => { 'LC_ALL' => Chef::Config[:internal_locale] }, + }).and_return(true) + shell_out.shell_out(cmd, options) + end end end - describe "and env is an option" do - it "should not change env when set to nil" do - options = { :env => { 'LC_ALL' => nil } } - shell_out.should_receive(:shell_out).with(cmd, options).and_return(true) - shell_out.shell_out_with_systems_locale(cmd, options) + describe "when the last argument is not a Hash" do + it "should add environment options and set environment['LC_ALL'] to 'en_US.UTF-8'" do + expect(shell_out).to receive(:shell_out_command).with(cmd, { + :environment => { 'LC_ALL' => Chef::Config[:internal_locale] }, + }).and_return(true) + shell_out.shell_out(cmd) end + end + + end - it "should not change env when set to non-nil" do - options = { :env => { 'LC_ALL' => 'en_US.UTF-8'}} - shell_out.should_receive(:shell_out).with(cmd, options).and_return(true) - shell_out.shell_out_with_systems_locale(cmd, options) + describe "#shell_out_with_systems_locale" do + + describe "when the last argument is a Hash" do + describe "and environment is an option" do + it "should not change environment['LC_ALL'] when set to nil" do + options = { :environment => { 'LC_ALL' => nil } } + expect(shell_out).to receive(:shell_out_command).with(cmd, options).and_return(true) + shell_out.shell_out_with_systems_locale(cmd, options) + end + + it "should not change environment['LC_ALL'] when set to non-nil" do + options = { :environment => { 'LC_ALL' => 'en_US.UTF-8' } } + expect(shell_out).to receive(:shell_out_command).with(cmd, options).and_return(true) + shell_out.shell_out_with_systems_locale(cmd, options) + end + + it "should no longer set environment['LC_ALL'] to nil when 'LC_ALL' not present" do + options = { :environment => { 'HOME' => '/Users/morty' } } + expect(shell_out).to receive(:shell_out_command).with(cmd, options).and_return(true) + shell_out.shell_out_with_systems_locale(cmd, options) + end end - it "should set env['LC_ALL'] to nil when 'LC_ALL' not present" do - options = { :env => { 'HOME' => '/Users/morty' } } - shell_out.should_receive(:shell_out).with( - cmd, - { :env => { - 'HOME' => '/Users/morty', - 'LC_ALL' => nil } - } - ).and_return(true) - shell_out.shell_out_with_systems_locale(cmd, options) + describe "and env is an option" do + it "should not change env when set to nil" do + options = { :env => { 'LC_ALL' => nil } } + expect(shell_out).to receive(:shell_out_command).with(cmd, options).and_return(true) + shell_out.shell_out_with_systems_locale(cmd, options) + end + + it "should not change env when set to non-nil" do + options = { :env => { 'LC_ALL' => 'en_US.UTF-8'}} + expect(shell_out).to receive(:shell_out_command).with(cmd, options).and_return(true) + shell_out.shell_out_with_systems_locale(cmd, options) + end + + it "should no longer set env['LC_ALL'] to nil when 'LC_ALL' not present" do + options = { :env => { 'HOME' => '/Users/morty' } } + expect(shell_out).to receive(:shell_out_command).with(cmd, options).and_return(true) + shell_out.shell_out_with_systems_locale(cmd, options) + end end - end - describe "and no env/environment option is present" do - it "should add environment option and set environment['LC_ALL'] to nil" do - options = { :user => 'morty' } - shell_out.should_receive(:shell_out).with( - cmd, - { :environment => { 'LC_ALL' => nil }, - :user => 'morty' - } - ).and_return(true) - shell_out.shell_out_with_systems_locale(cmd, options) + describe "and no env/environment option is present" do + it "should no longer add environment option and set environment['LC_ALL'] to nil" do + options = { :user => 'morty' } + expect(shell_out).to receive(:shell_out_command).with(cmd, options).and_return(true) + shell_out.shell_out_with_systems_locale(cmd, options) + end end end - end - describe "when the last argument is not a Hash" do - it "should add environment options and set environment['LC_ALL'] to nil" do - shell_out.should_receive(:shell_out).with( - cmd, - { :environment => { 'LC_ALL' => nil } } - ).and_return(true) - shell_out.shell_out_with_systems_locale(cmd) + describe "when the last argument is not a Hash" do + it "should no longer add environment options and set environment['LC_ALL'] to nil" do + expect(shell_out).to receive(:shell_out_command).with(cmd).and_return(true) + shell_out.shell_out_with_systems_locale(cmd) + end end end - end + end end diff --git a/spec/unit/provider/ifconfig/debian_spec.rb b/spec/unit/provider/ifconfig/debian_spec.rb index c6a37fdd5b..f4fd1480e0 100644 --- a/spec/unit/provider/ifconfig/debian_spec.rb +++ b/spec/unit/provider/ifconfig/debian_spec.rb @@ -56,8 +56,6 @@ describe Chef::Provider::Ifconfig::Debian do describe "generate_config" do context "when writing a file" do - let(:config_file_ifcfg) { StringIO.new } - let(:tempfile) { Tempfile.new("rspec-chef-ifconfig-debian") } let(:tempdir_path) { Dir.mktmpdir("rspec-chef-ifconfig-debian-dir") } @@ -67,14 +65,13 @@ describe Chef::Provider::Ifconfig::Debian do before do stub_const("Chef::Provider::Ifconfig::Debian::INTERFACES_FILE", tempfile.path) stub_const("Chef::Provider::Ifconfig::Debian::INTERFACES_DOT_D_DIR", tempdir_path) - expect(File).to receive(:new).with(config_filename_ifcfg, "w").and_return(config_file_ifcfg) end it "should write a network-script" do provider.run_action(:add) - expect(config_file_ifcfg.string).to match(/^iface eth0 inet static\s*$/) - expect(config_file_ifcfg.string).to match(/^\s+address 10\.0\.0\.1\s*$/) - expect(config_file_ifcfg.string).to match(/^\s+netmask 255\.255\.254\.0\s*$/) + expect(File.read(config_filename_ifcfg)).to match(/^iface eth0 inet static\s*$/) + expect(File.read(config_filename_ifcfg)).to match(/^\s+address 10\.0\.0\.1\s*$/) + expect(File.read(config_filename_ifcfg)).to match(/^\s+netmask 255\.255\.254\.0\s*$/) end context "when the interface_dot_d directory does not exist" do @@ -123,7 +120,6 @@ iface eth0 inet static netmask 255.255.254.0 EOF ) - expect(File).to receive(:new).with(config_filename_ifcfg, "w").and_return(config_file_ifcfg) expect(File.exists?(tempdir_path)).to be_true # since the file exists, the enclosing dir must also exist end @@ -139,6 +135,8 @@ EOF before do tempfile.write(expected_string) tempfile.close + + expect(provider).not_to receive(:converge_by).with(/modifying #{tempfile.path} to source #{tempdir_path}/) end it "should preserve all the contents" do @@ -165,6 +163,9 @@ EOF before do tempfile.write("a line\nanother line\n") tempfile.close + + allow(provider).to receive(:converge_by).and_call_original + expect(provider).to receive(:converge_by).with(/modifying #{tempfile.path} to source #{tempdir_path}/).and_call_original end it "should preserve the original contents and add the source line" do @@ -318,8 +319,17 @@ source #{tempdir_path}/* it "should delete network-script if it exists" do current_resource.device new_resource.device - expect(File).to receive(:exist?).with(config_filename_ifcfg).and_return(true) - expect(FileUtils).to receive(:rm_f).with(config_filename_ifcfg, :verbose => false) + + [:exist?, :exists?, :writable?].each do |cmd| + # need to stub :writable? to make why_run? happy + allow(File).to receive(cmd).and_call_original + allow(File).to receive(cmd).with(config_filename_ifcfg).and_return(true) + end + + # stub for Chef::Util::Backup#do_backup + expect(FileUtils).to receive(:cp) + .with(config_filename_ifcfg, /#{Chef::Config[:file_backup_path]}/, :preserve => true) + expect(File).to receive(:delete).with(config_filename_ifcfg) provider.run_action(:delete) end diff --git a/spec/unit/provider/ifconfig/redhat_spec.rb b/spec/unit/provider/ifconfig/redhat_spec.rb index f4b98dfc32..138c2a389d 100644 --- a/spec/unit/provider/ifconfig/redhat_spec.rb +++ b/spec/unit/provider/ifconfig/redhat_spec.rb @@ -37,22 +37,26 @@ describe Chef::Provider::Ifconfig::Redhat do status = double("Status", :exitstatus => 0) @provider.instance_variable_set("@status", status) @provider.current_resource = @current_resource - end + + config_filename = "/etc/sysconfig/network-scripts/ifcfg-#{@new_resource.device}" + @config = double("chef-resource-file") + @provider.should_receive(:resource_for_config).with(config_filename).and_return(@config) + end describe "generate_config for action_add" do - it "should write network-script for centos" do + it "should write network-script for centos" do @provider.stub(:load_current_resource) @provider.stub(:run_command) - config_filename = "/etc/sysconfig/network-scripts/ifcfg-#{@new_resource.device}" - config_file = StringIO.new - File.should_receive(:new).with(config_filename, "w").and_return(config_file) - + @config.should_receive(:content) do |arg| + arg.should match(/^\s*DEVICE=eth0\s*$/) + arg.should match(/^\s*IPADDR=10\.0\.0\.1\s*$/) + arg.should match(/^\s*NETMASK=255\.255\.254\.0\s*$/) + end + @config.should_receive(:run_action).with(:create) + @config.should_receive(:updated?).and_return(true) @provider.run_action(:add) - config_file.string.should match(/^\s*DEVICE=eth0\s*$/) - config_file.string.should match(/^\s*IPADDR=10\.0\.0\.1\s*$/) - config_file.string.should match(/^\s*NETMASK=255\.255\.254\.0\s*$/) - end + end end describe "delete_config for action_delete" do @@ -61,10 +65,8 @@ describe Chef::Provider::Ifconfig::Redhat do @current_resource.device @new_resource.device @provider.stub(:load_current_resource) @provider.stub(:run_command) - config_filename = "/etc/sysconfig/network-scripts/ifcfg-#{@new_resource.device}" - File.should_receive(:exist?).with(config_filename).and_return(true) - FileUtils.should_receive(:rm_f).with(config_filename, :verbose => false) - + @config.should_receive(:run_action).with(:delete) + @config.should_receive(:updated?).and_return(true) @provider.run_action(:delete) end end diff --git a/spec/unit/provider/ifconfig_spec.rb b/spec/unit/provider/ifconfig_spec.rb index fb8d0956d7..b09e365c65 100644 --- a/spec/unit/provider/ifconfig_spec.rb +++ b/spec/unit/provider/ifconfig_spec.rb @@ -72,7 +72,7 @@ describe Chef::Provider::Ifconfig do @provider.stub(:load_current_resource) @provider.should_not_receive(:run_command) @current_resource.inet_addr "10.0.0.1" - @provider.should_not_receive(:generate_config) + @provider.should_receive(:generate_config) @provider.run_action(:add) @new_resource.should_not be_updated @@ -123,7 +123,7 @@ describe Chef::Provider::Ifconfig do it "should not delete interface if it does not exist" do @provider.stub(:load_current_resource) @provider.should_not_receive(:run_command) - @provider.should_not_receive(:delete_config) + @provider.should_receive(:delete_config) @provider.run_action(:delete) @new_resource.should_not be_updated @@ -171,7 +171,7 @@ describe Chef::Provider::Ifconfig do @provider.stub(:load_current_resource) # This is so that nothing actually runs @provider.should_not_receive(:run_command) - @provider.should_not_receive(:delete_config) + @provider.should_receive(:delete_config) @provider.run_action(:delete) @new_resource.should_not be_updated diff --git a/spec/unit/provider/link_spec.rb b/spec/unit/provider/link_spec.rb index 6052f5dd3b..2f0a5f2020 100644 --- a/spec/unit/provider/link_spec.rb +++ b/spec/unit/provider/link_spec.rb @@ -38,8 +38,8 @@ describe Chef::Resource::Link, :not_supported_on_win2k3 do result end - def canonicalize(path) - Chef::Platform.windows? ? path.gsub('/', '\\') : path + def paths_eql?(path1, path2) + Chef::Util::PathHelper.paths_eql?(path1, path2) end describe "when the target is a symlink" do @@ -68,7 +68,7 @@ describe Chef::Resource::Link, :not_supported_on_win2k3 do provider.current_resource.link_type.should == :symbolic end it "should update the source of the existing link with the links target" do - provider.current_resource.to.should == canonicalize("#{CHEF_SPEC_DATA}/fofile") + paths_eql?(provider.current_resource.to, "#{CHEF_SPEC_DATA}/fofile").should be_true end it "should set the owner" do provider.current_resource.owner.should == 501 @@ -110,7 +110,7 @@ describe Chef::Resource::Link, :not_supported_on_win2k3 do provider.current_resource.link_type.should == :symbolic end it "should update the source of the existing link to the link's target" do - provider.current_resource.to.should == canonicalize("#{CHEF_SPEC_DATA}/fofile") + paths_eql?(provider.current_resource.to, "#{CHEF_SPEC_DATA}/fofile").should be_true end it "should not set the owner" do provider.current_resource.owner.should be_nil @@ -221,7 +221,7 @@ describe Chef::Resource::Link, :not_supported_on_win2k3 do provider.current_resource.link_type.should == :hard end it "should update the source of the existing link to the link's target" do - provider.current_resource.to.should == canonicalize("#{CHEF_SPEC_DATA}/fofile") + paths_eql?(provider.current_resource.to, "#{CHEF_SPEC_DATA}/fofile").should be_true end it "should not set the owner" do provider.current_resource.owner.should == nil diff --git a/spec/unit/provider/package/aix_spec.rb b/spec/unit/provider/package/aix_spec.rb index 35f85b628f..5d6e23302f 100644 --- a/spec/unit/provider/package/aix_spec.rb +++ b/spec/unit/provider/package/aix_spec.rb @@ -59,7 +59,6 @@ describe Chef::Provider::Package::Aix do lambda { @provider.process_resource_requirements }.should raise_error(Chef::Exceptions::Package) end - it "should get the source package version from lslpp if provided" do @stdout = StringIO.new(@bffinfo) @stdin, @stderr = StringIO.new, StringIO.new @@ -125,9 +124,7 @@ describe Chef::Provider::Package::Aix do describe "install and upgrade" do it "should run installp -aYF -d with the package source to install" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "installp -aYF -d /tmp/samba.base samba.base" - }) + @provider.should_receive(:shell_out!).with("installp -aYF -d /tmp/samba.base samba.base") @provider.install_package("samba.base", "3.3.12.0") end @@ -135,37 +132,28 @@ describe Chef::Provider::Package::Aix do @new_resource = Chef::Resource::Package.new("/tmp/samba.base") @provider = Chef::Provider::Package::Aix.new(@new_resource, @run_context) @new_resource.source.should == "/tmp/samba.base" - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "installp -aYF -d /tmp/samba.base /tmp/samba.base" - }) + @provider.should_receive(:shell_out!).with("installp -aYF -d /tmp/samba.base /tmp/samba.base") @provider.install_package("/tmp/samba.base", "3.3.12.0") end it "should run installp with -eLogfile option." do @new_resource.stub(:options).and_return("-e/tmp/installp.log") - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "installp -aYF -e/tmp/installp.log -d /tmp/samba.base samba.base" - }) + @provider.should_receive(:shell_out!).with("installp -aYF -e/tmp/installp.log -d /tmp/samba.base samba.base") @provider.install_package("samba.base", "3.3.12.0") end end describe "remove" do it "should run installp -u samba.base to remove the package" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "installp -u samba.base" - }) + @provider.should_receive(:shell_out!).with("installp -u samba.base") @provider.remove_package("samba.base", "3.3.12.0") end it "should run installp -u -e/tmp/installp.log with options -e/tmp/installp.log" do @new_resource.stub(:options).and_return("-e/tmp/installp.log") - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "installp -u -e/tmp/installp.log samba.base" - }) + @provider.should_receive(:shell_out!).with("installp -u -e/tmp/installp.log samba.base") @provider.remove_package("samba.base", "3.3.12.0") end end end - diff --git a/spec/unit/provider/package/ips_spec.rb b/spec/unit/provider/package/ips_spec.rb index c5941c2a91..07bca7f6d5 100644 --- a/spec/unit/provider/package/ips_spec.rb +++ b/spec/unit/provider/package/ips_spec.rb @@ -39,7 +39,7 @@ describe Chef::Provider::Package::Ips do pkg: info: no packages matching the following patterns you specified are installed on the system. Try specifying -r to query remotely: - crypto/gnupg + crypto/gnupg PKG_STATUS return OpenStruct.new(:stdout => stdout,:stdin => stdin,:stderr => stderr,:status => @status,:exitstatus => 1) end @@ -59,7 +59,7 @@ Packaging Date: April 1, 2012 05:55:52 PM FMRI: pkg://omnios/security/sudo@1.8.4.1,5.11-0.151002:20120401T175552Z PKG_STATUS stdin = StringIO.new - stderr = '' + stderr = '' return OpenStruct.new(:stdout => stdout,:stdin => stdin,:stderr => stderr,:status => @status,:exitstatus => 0) end @@ -123,17 +123,12 @@ INSTALLED context "when installing a package" do it "should run pkg install with the package name and version" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "pkg install -q crypto/gnupg@2.0.17" - }) + @provider.should_receive(:shell_out).with("pkg install -q crypto/gnupg@2.0.17") @provider.install_package("crypto/gnupg", "2.0.17") end - it "should run pkg install with the package name and version and options if specified" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "pkg --no-refresh install -q crypto/gnupg@2.0.17" - }) + @provider.should_receive(:shell_out).with("pkg --no-refresh install -q crypto/gnupg@2.0.17") @new_resource.stub(:options).and_return("--no-refresh") @provider.install_package("crypto/gnupg", "2.0.17") end @@ -206,9 +201,7 @@ REMOTE end it "should run pkg install with the --accept flag" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "pkg install -q --accept crypto/gnupg@2.0.17" - }) + @provider.should_receive(:shell_out).with("pkg install -q --accept crypto/gnupg@2.0.17") @provider.install_package("crypto/gnupg", "2.0.17") end end @@ -216,25 +209,19 @@ REMOTE context "when upgrading a package" do it "should run pkg install with the package name and version" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "pkg install -q crypto/gnupg@2.0.17" - }) + @provider.should_receive(:shell_out).with("pkg install -q crypto/gnupg@2.0.17") @provider.upgrade_package("crypto/gnupg", "2.0.17") end end context "when uninstalling a package" do it "should run pkg uninstall with the package name and version" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "pkg uninstall -q crypto/gnupg@2.0.17" - }) + @provider.should_receive(:shell_out!).with("pkg uninstall -q crypto/gnupg@2.0.17") @provider.remove_package("crypto/gnupg", "2.0.17") end it "should run pkg uninstall with the package name and version and options if specified" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "pkg --no-refresh uninstall -q crypto/gnupg@2.0.17" - }) + @provider.should_receive(:shell_out!).with("pkg --no-refresh uninstall -q crypto/gnupg@2.0.17") @new_resource.stub(:options).and_return("--no-refresh") @provider.remove_package("crypto/gnupg", "2.0.17") end diff --git a/spec/unit/provider/package/macports_spec.rb b/spec/unit/provider/package/macports_spec.rb index 9ebf23860d..535a5d2459 100644 --- a/spec/unit/provider/package/macports_spec.rb +++ b/spec/unit/provider/package/macports_spec.rb @@ -105,7 +105,7 @@ EOF it "should run the port install command with the correct version" do @current_resource.should_receive(:version).and_return("4.1.6") @provider.current_resource = @current_resource - @provider.should_receive(:run_command_with_systems_locale).with(:command => "port install zsh @4.2.7") + @provider.should_receive(:shell_out!).with("port install zsh @4.2.7") @provider.install_package("zsh", "4.2.7") end @@ -113,7 +113,7 @@ EOF it "should not do anything if a package already exists with the same version" do @current_resource.should_receive(:version).and_return("4.2.7") @provider.current_resource = @current_resource - @provider.should_not_receive(:run_command_with_systems_locale) + @provider.should_not_receive(:shell_out!) @provider.install_package("zsh", "4.2.7") end @@ -122,7 +122,7 @@ EOF @current_resource.should_receive(:version).and_return("4.1.6") @provider.current_resource = @current_resource @new_resource.stub(:options).and_return("-f") - @provider.should_receive(:run_command_with_systems_locale).with(:command => "port -f install zsh @4.2.7") + @provider.should_receive(:shell_out!).with("port -f install zsh @4.2.7") @provider.install_package("zsh", "4.2.7") end @@ -130,36 +130,36 @@ EOF describe "purge_package" do it "should run the port uninstall command with the correct version" do - @provider.should_receive(:run_command_with_systems_locale).with(:command => "port uninstall zsh @4.2.7") + @provider.should_receive(:shell_out!).with("port uninstall zsh @4.2.7") @provider.purge_package("zsh", "4.2.7") end it "should purge the currently active version if no explicit version is passed in" do - @provider.should_receive(:run_command_with_systems_locale).with(:command => "port uninstall zsh") + @provider.should_receive(:shell_out!).with("port uninstall zsh") @provider.purge_package("zsh", nil) end it "should add options to the port command when specified" do @new_resource.stub(:options).and_return("-f") - @provider.should_receive(:run_command_with_systems_locale).with(:command => "port -f uninstall zsh @4.2.7") + @provider.should_receive(:shell_out!).with("port -f uninstall zsh @4.2.7") @provider.purge_package("zsh", "4.2.7") end end describe "remove_package" do it "should run the port deactivate command with the correct version" do - @provider.should_receive(:run_command_with_systems_locale).with(:command => "port deactivate zsh @4.2.7") + @provider.should_receive(:shell_out!).with("port deactivate zsh @4.2.7") @provider.remove_package("zsh", "4.2.7") end it "should remove the currently active version if no explicit version is passed in" do - @provider.should_receive(:run_command_with_systems_locale).with(:command => "port deactivate zsh") + @provider.should_receive(:shell_out!).with("port deactivate zsh") @provider.remove_package("zsh", nil) end it "should add options to the port command when specified" do @new_resource.stub(:options).and_return("-f") - @provider.should_receive(:run_command_with_systems_locale).with(:command => "port -f deactivate zsh @4.2.7") + @provider.should_receive(:shell_out!).with("port -f deactivate zsh @4.2.7") @provider.remove_package("zsh", "4.2.7") end end @@ -169,7 +169,7 @@ EOF @current_resource.should_receive(:version).at_least(:once).and_return("4.1.6") @provider.current_resource = @current_resource - @provider.should_receive(:run_command_with_systems_locale).with(:command => "port upgrade zsh @4.2.7") + @provider.should_receive(:shell_out!).with("port upgrade zsh @4.2.7") @provider.upgrade_package("zsh", "4.2.7") end @@ -177,7 +177,7 @@ EOF it "should not run the port upgrade command if the version is already installed" do @current_resource.should_receive(:version).at_least(:once).and_return("4.2.7") @provider.current_resource = @current_resource - @provider.should_not_receive(:run_command_with_systems_locale) + @provider.should_not_receive(:shell_out!) @provider.upgrade_package("zsh", "4.2.7") end @@ -195,7 +195,7 @@ EOF @current_resource.should_receive(:version).at_least(:once).and_return("4.1.6") @provider.current_resource = @current_resource - @provider.should_receive(:run_command_with_systems_locale).with(:command => "port -f upgrade zsh @4.2.7") + @provider.should_receive(:shell_out!).with("port -f upgrade zsh @4.2.7") @provider.upgrade_package("zsh", "4.2.7") end diff --git a/spec/unit/provider/package/pacman_spec.rb b/spec/unit/provider/package/pacman_spec.rb index 528f7097e8..0c1c487980 100644 --- a/spec/unit/provider/package/pacman_spec.rb +++ b/spec/unit/provider/package/pacman_spec.rb @@ -155,16 +155,12 @@ PACMAN_CONF describe Chef::Provider::Package::Pacman, "install_package" do it "should run pacman install with the package name and version" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "pacman --sync --noconfirm --noprogressbar nano" - }) + @provider.should_receive(:shell_out!).with("pacman --sync --noconfirm --noprogressbar nano") @provider.install_package("nano", "1.0") end it "should run pacman install with the package name and version and options if specified" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "pacman --sync --noconfirm --noprogressbar --debug nano" - }) + @provider.should_receive(:shell_out!).with("pacman --sync --noconfirm --noprogressbar --debug nano") @new_resource.stub(:options).and_return("--debug") @provider.install_package("nano", "1.0") @@ -180,16 +176,12 @@ PACMAN_CONF describe Chef::Provider::Package::Pacman, "remove_package" do it "should run pacman remove with the package name" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "pacman --remove --noconfirm --noprogressbar nano" - }) + @provider.should_receive(:shell_out!).with("pacman --remove --noconfirm --noprogressbar nano") @provider.remove_package("nano", "1.0") end it "should run pacman remove with the package name and options if specified" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "pacman --remove --noconfirm --noprogressbar --debug nano" - }) + @provider.should_receive(:shell_out!).with("pacman --remove --noconfirm --noprogressbar --debug nano") @new_resource.stub(:options).and_return("--debug") @provider.remove_package("nano", "1.0") diff --git a/spec/unit/provider/package/portage_spec.rb b/spec/unit/provider/package/portage_spec.rb index 6f22952da2..570f123168 100644 --- a/spec/unit/provider/package/portage_spec.rb +++ b/spec/unit/provider/package/portage_spec.rb @@ -278,23 +278,17 @@ EOF describe Chef::Provider::Package::Portage, "install_package" do it "should install a normally versioned package using portage" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "emerge -g --color n --nospinner --quiet =dev-util/git-1.0.0" - }) + @provider.should_receive(:shell_out!).with("emerge -g --color n --nospinner --quiet =dev-util/git-1.0.0") @provider.install_package("dev-util/git", "1.0.0") end it "should install a tilde versioned package using portage" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "emerge -g --color n --nospinner --quiet ~dev-util/git-1.0.0" - }) + @provider.should_receive(:shell_out!).with("emerge -g --color n --nospinner --quiet ~dev-util/git-1.0.0") @provider.install_package("dev-util/git", "~1.0.0") end it "should add options to the emerge command when specified" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "emerge -g --color n --nospinner --quiet --oneshot =dev-util/git-1.0.0" - }) + @provider.should_receive(:shell_out!).with("emerge -g --color n --nospinner --quiet --oneshot =dev-util/git-1.0.0") @new_resource.stub(:options).and_return("--oneshot") @provider.install_package("dev-util/git", "1.0.0") @@ -303,16 +297,12 @@ EOF describe Chef::Provider::Package::Portage, "remove_package" do it "should un-emerge the package with no version specified" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "emerge --unmerge --color n --nospinner --quiet dev-util/git" - }) + @provider.should_receive(:shell_out!).with("emerge --unmerge --color n --nospinner --quiet dev-util/git") @provider.remove_package("dev-util/git", nil) end it "should un-emerge the package with a version specified" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "emerge --unmerge --color n --nospinner --quiet =dev-util/git-1.0.0" - }) + @provider.should_receive(:shell_out!).with("emerge --unmerge --color n --nospinner --quiet =dev-util/git-1.0.0") @provider.remove_package("dev-util/git", "1.0.0") end end diff --git a/spec/unit/provider/package/rpm_spec.rb b/spec/unit/provider/package/rpm_spec.rb index 6e46cf5e98..9a96d829b8 100644 --- a/spec/unit/provider/package/rpm_spec.rb +++ b/spec/unit/provider/package/rpm_spec.rb @@ -102,25 +102,19 @@ describe Chef::Provider::Package::Rpm do describe "when installing or upgrading" do it "should run rpm -i with the package source to install" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "rpm -i /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm" - }) + @provider.should_receive(:shell_out!).with("rpm -i /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm") @provider.install_package("ImageMagick-c++", "6.5.4.7-7.el6_5") end it "should run rpm -U with the package source to upgrade" do @current_resource.version("21.4-19.el5") - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "rpm -U /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm" - }) + @provider.should_receive(:shell_out!).with("rpm -U /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm") @provider.upgrade_package("ImageMagick-c++", "6.5.4.7-7.el6_5") end it "should install package if missing and set to upgrade" do @current_resource.version("ImageMagick-c++") - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "rpm -U /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm" - }) + @provider.should_receive(:shell_out!).with("rpm -U /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm") @provider.upgrade_package("ImageMagick-c++", "6.5.4.7-7.el6_5") end @@ -130,9 +124,7 @@ describe Chef::Provider::Package::Rpm do @new_resource.source.should == "/tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm" @current_resource = Chef::Resource::Package.new("ImageMagick-c++") @provider.current_resource = @current_resource - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "rpm -i /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm" - }) + @provider.should_receive(:shell_out!).with("rpm -i /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm") @provider.install_package("/tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm", "6.5.4.7-7.el6_5") end @@ -143,30 +135,23 @@ describe Chef::Provider::Package::Rpm do @current_resource = Chef::Resource::Package.new("ImageMagick-c++") @current_resource.version("21.4-19.el5") @provider.current_resource = @current_resource - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "rpm -U /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm" - }) + @provider.should_receive(:shell_out!).with("rpm -U /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm") @provider.upgrade_package("/tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm", "6.5.4.7-7.el6_5") end it "installs with custom options specified in the resource" do @provider.candidate_version = '11' @new_resource.options("--dbpath /var/lib/rpm") - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "rpm --dbpath /var/lib/rpm -i /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm" - }) + @provider.should_receive(:shell_out!).with("rpm --dbpath /var/lib/rpm -i /tmp/ImageMagick-c++-6.5.4.7-7.el6_5.x86_64.rpm") @provider.install_package(@new_resource.name, @provider.candidate_version) end end describe "when removing the package" do it "should run rpm -e to remove the package" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "rpm -e ImageMagick-c++-6.5.4.7-7.el6_5" - }) + @provider.should_receive(:shell_out!).with("rpm -e ImageMagick-c++-6.5.4.7-7.el6_5") @provider.remove_package("ImageMagick-c++", "6.5.4.7-7.el6_5") end end end end - diff --git a/spec/unit/provider/package/solaris_spec.rb b/spec/unit/provider/package/solaris_spec.rb index 086e327cce..d83ccbdf06 100644 --- a/spec/unit/provider/package/solaris_spec.rb +++ b/spec/unit/provider/package/solaris_spec.rb @@ -69,7 +69,6 @@ PKGINFO lambda { @provider.process_resource_requirements }.should raise_error(Chef::Exceptions::Package) end - it "should get the source package version from pkginfo if provided" do @stdout = StringIO.new(@pkginfo) @stdin, @stderr = StringIO.new, StringIO.new @@ -136,9 +135,7 @@ PKGINFO describe "install and upgrade" do it "should run pkgadd -n -d with the package source to install" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "pkgadd -n -d /tmp/bash.pkg all" - }) + @provider.should_receive(:shell_out!).with("pkgadd -n -d /tmp/bash.pkg all") @provider.install_package("SUNWbash", "11.10.0,REV=2005.01.08.05.16") end @@ -146,34 +143,26 @@ PKGINFO @new_resource = Chef::Resource::Package.new("/tmp/bash.pkg") @provider = Chef::Provider::Package::Solaris.new(@new_resource, @run_context) @new_resource.source.should == "/tmp/bash.pkg" - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "pkgadd -n -d /tmp/bash.pkg all" - }) + @provider.should_receive(:shell_out!).with("pkgadd -n -d /tmp/bash.pkg all") @provider.install_package("/tmp/bash.pkg", "11.10.0,REV=2005.01.08.05.16") end it "should run pkgadd -n -a /tmp/myadmin -d with the package options -a /tmp/myadmin" do @new_resource.stub(:options).and_return("-a /tmp/myadmin") - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "pkgadd -n -a /tmp/myadmin -d /tmp/bash.pkg all" - }) + @provider.should_receive(:shell_out!).with("pkgadd -n -a /tmp/myadmin -d /tmp/bash.pkg all") @provider.install_package("SUNWbash", "11.10.0,REV=2005.01.08.05.16") end end describe "remove" do it "should run pkgrm -n to remove the package" do - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "pkgrm -n SUNWbash" - }) + @provider.should_receive(:shell_out!).with("pkgrm -n SUNWbash") @provider.remove_package("SUNWbash", "11.10.0,REV=2005.01.08.05.16") end it "should run pkgrm -n -a /tmp/myadmin with options -a /tmp/myadmin" do @new_resource.stub(:options).and_return("-a /tmp/myadmin") - @provider.should_receive(:run_command_with_systems_locale).with({ - :command => "pkgrm -n -a /tmp/myadmin SUNWbash" - }) + @provider.should_receive(:shell_out!).with("pkgrm -n -a /tmp/myadmin SUNWbash") @provider.remove_package("SUNWbash", "11.10.0,REV=2005.01.08.05.16") end diff --git a/spec/unit/provider/service/arch_service_spec.rb b/spec/unit/provider/service/arch_service_spec.rb index b267915e44..38ed74cdee 100644 --- a/spec/unit/provider/service/arch_service_spec.rb +++ b/spec/unit/provider/service/arch_service_spec.rb @@ -20,7 +20,6 @@ require 'spec_helper' require 'ostruct' - # most of this code has been ripped from init_service_spec.rb # and is only slightly modified to match "arch" needs. @@ -36,7 +35,6 @@ describe Chef::Provider::Service::Arch, "load_current_resource" do @new_resource.pattern("chef") @new_resource.supports({:status => false}) - @provider = Chef::Provider::Service::Arch.new(@new_resource, @run_context) ::File.stub(:exists?).with("/etc/rc.conf").and_return(true) @@ -51,7 +49,6 @@ describe Chef::Provider::Service::Arch, "load_current_resource" do end end - describe "when the service supports status" do before do @new_resource.supports({:status => true}) @@ -82,7 +79,6 @@ describe Chef::Provider::Service::Arch, "load_current_resource" do end - describe "when a status command has been specified" do before do @new_resource.status_command("/etc/rc.d/chefhasmonkeypants status") @@ -109,7 +105,6 @@ describe Chef::Provider::Service::Arch, "load_current_resource" do lambda { @provider.process_resource_requirements }.should raise_error(Chef::Exceptions::Service) end - it "should fail if file /etc/rc.conf does not exist" do ::File.stub(:exists?).with("/etc/rc.conf").and_return(false) lambda { @provider.load_current_resource }.should raise_error(Chef::Exceptions::Service) @@ -211,7 +206,6 @@ RUNNING_PS end end - describe Chef::Provider::Service::Arch, "start_service" do # before(:each) do # @new_resource = double("Chef::Resource::Service", @@ -228,12 +222,12 @@ RUNNING_PS it "should call the start command if one is specified" do @new_resource.stub(:start_command).and_return("/etc/rc.d/chef startyousillysally") - @provider.should_receive(:shell_out!).with("/etc/rc.d/chef startyousillysally") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/rc.d/chef startyousillysally") @provider.start_service() end it "should call '/etc/rc.d/service_name start' if no start command is specified" do - @provider.should_receive(:shell_out!).with("/etc/rc.d/#{@new_resource.service_name} start") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/rc.d/#{@new_resource.service_name} start") @provider.start_service() end end @@ -254,12 +248,12 @@ RUNNING_PS it "should call the stop command if one is specified" do @new_resource.stub(:stop_command).and_return("/etc/rc.d/chef itoldyoutostop") - @provider.should_receive(:shell_out!).with("/etc/rc.d/chef itoldyoutostop") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/rc.d/chef itoldyoutostop") @provider.stop_service() end it "should call '/etc/rc.d/service_name stop' if no stop command is specified" do - @provider.should_receive(:shell_out!).with("/etc/rc.d/#{@new_resource.service_name} stop") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/rc.d/#{@new_resource.service_name} stop") @provider.stop_service() end end @@ -281,13 +275,13 @@ RUNNING_PS it "should call 'restart' on the service_name if the resource supports it" do @new_resource.stub(:supports).and_return({:restart => true}) - @provider.should_receive(:shell_out!).with("/etc/rc.d/#{@new_resource.service_name} restart") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/rc.d/#{@new_resource.service_name} restart") @provider.restart_service() end it "should call the restart_command if one has been specified" do @new_resource.stub(:restart_command).and_return("/etc/rc.d/chef restartinafire") - @provider.should_receive(:shell_out!).with("/etc/rc.d/#{@new_resource.service_name} restartinafire") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/rc.d/#{@new_resource.service_name} restartinafire") @provider.restart_service() end @@ -316,13 +310,13 @@ RUNNING_PS it "should call 'reload' on the service if it supports it" do @new_resource.stub(:supports).and_return({:reload => true}) - @provider.should_receive(:shell_out!).with("/etc/rc.d/#{@new_resource.service_name} reload") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/rc.d/#{@new_resource.service_name} reload") @provider.reload_service() end it "should should run the user specified reload command if one is specified and the service doesn't support reload" do @new_resource.stub(:reload_command).and_return("/etc/rc.d/chef lollerpants") - @provider.should_receive(:shell_out!).with("/etc/rc.d/#{@new_resource.service_name} lollerpants") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/rc.d/#{@new_resource.service_name} lollerpants") @provider.reload_service() end end diff --git a/spec/unit/provider/service/debian_service_spec.rb b/spec/unit/provider/service/debian_service_spec.rb index 567eb6744a..3e60857cbf 100644 --- a/spec/unit/provider/service/debian_service_spec.rb +++ b/spec/unit/provider/service/debian_service_spec.rb @@ -301,7 +301,7 @@ insserv: dryrun, not creating .depend.boot, .depend.start, and .depend.stop def expect_commands(provider, commands) commands.each do |command| - provider.should_receive(:run_command).with({:command => command}) + provider.should_receive(:shell_out!).with(command) end end diff --git a/spec/unit/provider/service/freebsd_service_spec.rb b/spec/unit/provider/service/freebsd_service_spec.rb index 6e58e82b97..eb55fac820 100644 --- a/spec/unit/provider/service/freebsd_service_spec.rb +++ b/spec/unit/provider/service/freebsd_service_spec.rb @@ -452,12 +452,12 @@ EOF describe Chef::Provider::Service::Freebsd, "start_service" do it "should call the start command if one is specified" do new_resource.start_command("/etc/rc.d/chef startyousillysally") - expect(provider).to receive(:shell_out!).with("/etc/rc.d/chef startyousillysally") + expect(provider).to receive(:shell_out_with_systems_locale!).with("/etc/rc.d/chef startyousillysally") provider.start_service() end it "should call '/usr/local/etc/rc.d/service_name faststart' if no start command is specified" do - expect(provider).to receive(:shell_out!).with("/usr/local/etc/rc.d/#{new_resource.service_name} faststart") + expect(provider).to receive(:shell_out_with_systems_locale!).with("/usr/local/etc/rc.d/#{new_resource.service_name} faststart") provider.start_service() end end @@ -465,12 +465,12 @@ EOF describe Chef::Provider::Service::Freebsd, "stop_service" do it "should call the stop command if one is specified" do new_resource.stop_command("/etc/init.d/chef itoldyoutostop") - expect(provider).to receive(:shell_out!).with("/etc/init.d/chef itoldyoutostop") + expect(provider).to receive(:shell_out_with_systems_locale!).with("/etc/init.d/chef itoldyoutostop") provider.stop_service() end it "should call '/usr/local/etc/rc.d/service_name faststop' if no stop command is specified" do - expect(provider).to receive(:shell_out!).with("/usr/local/etc/rc.d/#{new_resource.service_name} faststop") + expect(provider).to receive(:shell_out_with_systems_locale!).with("/usr/local/etc/rc.d/#{new_resource.service_name} faststop") provider.stop_service() end end @@ -478,13 +478,13 @@ EOF describe Chef::Provider::Service::Freebsd, "restart_service" do it "should call 'restart' on the service_name if the resource supports it" do new_resource.supports({:restart => true}) - expect(provider).to receive(:shell_out!).with("/usr/local/etc/rc.d/#{new_resource.service_name} fastrestart") + expect(provider).to receive(:shell_out_with_systems_locale!).with("/usr/local/etc/rc.d/#{new_resource.service_name} fastrestart") provider.restart_service() end it "should call the restart_command if one has been specified" do new_resource.restart_command("/etc/init.d/chef restartinafire") - expect(provider).to receive(:shell_out!).with("/etc/init.d/chef restartinafire") + expect(provider).to receive(:shell_out_with_systems_locale!).with("/etc/init.d/chef restartinafire") provider.restart_service() end diff --git a/spec/unit/provider/service/gentoo_service_spec.rb b/spec/unit/provider/service/gentoo_service_spec.rb index 95dc04108d..022a73cc9a 100644 --- a/spec/unit/provider/service/gentoo_service_spec.rb +++ b/spec/unit/provider/service/gentoo_service_spec.rb @@ -128,14 +128,14 @@ describe Chef::Provider::Service::Gentoo do describe Chef::Provider::Service::Gentoo, "enable_service" do it "should call rc-update add *service* default" do - @provider.should_receive(:run_command).with({:command => "/sbin/rc-update add chef default"}) + @provider.should_receive(:shell_out!).with("/sbin/rc-update add chef default") @provider.enable_service() end end describe Chef::Provider::Service::Gentoo, "disable_service" do it "should call rc-update del *service* default" do - @provider.should_receive(:run_command).with({:command => "/sbin/rc-update del chef default"}) + @provider.should_receive(:shell_out!).with("/sbin/rc-update del chef default") @provider.disable_service() end end diff --git a/spec/unit/provider/service/init_service_spec.rb b/spec/unit/provider/service/init_service_spec.rb index ad887c84a5..b523f6d3a9 100644 --- a/spec/unit/provider/service/init_service_spec.rb +++ b/spec/unit/provider/service/init_service_spec.rb @@ -100,7 +100,7 @@ PS end it "should use the init_command if one has been specified" do - @provider.should_receive(:shell_out!).with("/opt/chef-server/service/erchef start") + @provider.should_receive(:shell_out_with_systems_locale!).with("/opt/chef-server/service/erchef start") @provider.start_service end @@ -126,7 +126,6 @@ PS end - describe "when we have a 'ps' attribute" do it "should shell_out! the node's ps command" do @provider.should_receive(:shell_out!).and_return(@status) @@ -165,12 +164,12 @@ RUNNING_PS describe "when starting the service" do it "should call the start command if one is specified" do @new_resource.start_command("/etc/init.d/chef startyousillysally") - @provider.should_receive(:shell_out!).with("/etc/init.d/chef startyousillysally") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/init.d/chef startyousillysally") @provider.start_service() end it "should call '/etc/init.d/service_name start' if no start command is specified" do - @provider.should_receive(:shell_out!).with("/etc/init.d/#{@new_resource.service_name} start") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/init.d/#{@new_resource.service_name} start") @provider.start_service() end end @@ -178,12 +177,12 @@ RUNNING_PS describe Chef::Provider::Service::Init, "stop_service" do it "should call the stop command if one is specified" do @new_resource.stop_command("/etc/init.d/chef itoldyoutostop") - @provider.should_receive(:shell_out!).with("/etc/init.d/chef itoldyoutostop") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/init.d/chef itoldyoutostop") @provider.stop_service() end it "should call '/etc/init.d/service_name stop' if no stop command is specified" do - @provider.should_receive(:shell_out!).with("/etc/init.d/#{@new_resource.service_name} stop") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/init.d/#{@new_resource.service_name} stop") @provider.stop_service() end end @@ -191,13 +190,13 @@ RUNNING_PS describe "when restarting a service" do it "should call 'restart' on the service_name if the resource supports it" do @new_resource.supports({:restart => true}) - @provider.should_receive(:shell_out!).with("/etc/init.d/#{@new_resource.service_name} restart") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/init.d/#{@new_resource.service_name} restart") @provider.restart_service() end it "should call the restart_command if one has been specified" do @new_resource.restart_command("/etc/init.d/chef restartinafire") - @provider.should_receive(:shell_out!).with("/etc/init.d/#{@new_resource.service_name} restartinafire") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/init.d/#{@new_resource.service_name} restartinafire") @provider.restart_service() end @@ -212,13 +211,13 @@ RUNNING_PS describe "when reloading a service" do it "should call 'reload' on the service if it supports it" do @new_resource.supports({:reload => true}) - @provider.should_receive(:shell_out!).with("/etc/init.d/chef reload") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/init.d/chef reload") @provider.reload_service() end it "should should run the user specified reload command if one is specified and the service doesn't support reload" do @new_resource.reload_command("/etc/init.d/chef lollerpants") - @provider.should_receive(:shell_out!).with("/etc/init.d/chef lollerpants") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/init.d/chef lollerpants") @provider.reload_service() end end @@ -226,6 +225,7 @@ RUNNING_PS describe "when a custom command has been specified" do before do @new_resource.start_command("/etc/init.d/chef startyousillysally") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/init.d/chef startyousillysally") end it "should still pass all why run assertions" do diff --git a/spec/unit/provider/service/insserv_service_spec.rb b/spec/unit/provider/service/insserv_service_spec.rb index 04e63458a0..9ed03b519f 100644 --- a/spec/unit/provider/service/insserv_service_spec.rb +++ b/spec/unit/provider/service/insserv_service_spec.rb @@ -60,17 +60,16 @@ describe Chef::Provider::Service::Insserv do describe "enable_service" do it "should call insserv and create the default links" do - @provider.should_receive(:run_command).with({:command=>"/sbin/insserv -r -f #{@new_resource.service_name}"}) - @provider.should_receive(:run_command).with({:command=>"/sbin/insserv -d -f #{@new_resource.service_name}"}) + @provider.should_receive(:shell_out!).with("/sbin/insserv -r -f #{@new_resource.service_name}") + @provider.should_receive(:shell_out!).with("/sbin/insserv -d -f #{@new_resource.service_name}") @provider.enable_service end end describe "disable_service" do it "should call insserv and remove the links" do - @provider.should_receive(:run_command).with({:command=>"/sbin/insserv -r -f #{@new_resource.service_name}"}) + @provider.should_receive(:shell_out!).with("/sbin/insserv -r -f #{@new_resource.service_name}") @provider.disable_service end end end - diff --git a/spec/unit/provider/service/invokercd_service_spec.rb b/spec/unit/provider/service/invokercd_service_spec.rb index b638b08b72..d8a9851837 100644 --- a/spec/unit/provider/service/invokercd_service_spec.rb +++ b/spec/unit/provider/service/invokercd_service_spec.rb @@ -110,7 +110,6 @@ PS end - describe "when we have a 'ps' attribute" do it "should shell_out! the node's ps command" do @status = double("Status", :exitstatus => 0, :stdout => @stdout) @@ -152,12 +151,12 @@ RUNNING_PS describe "when starting the service" do it "should call the start command if one is specified" do @new_resource.start_command("/usr/sbin/invoke-rc.d chef startyousillysally") - @provider.should_receive(:shell_out!).with("/usr/sbin/invoke-rc.d chef startyousillysally") + @provider.should_receive(:shell_out_with_systems_locale!).with("/usr/sbin/invoke-rc.d chef startyousillysally") @provider.start_service() end it "should call '/usr/sbin/invoke-rc.d service_name start' if no start command is specified" do - @provider.should_receive(:shell_out!).with("/usr/sbin/invoke-rc.d #{@new_resource.service_name} start") + @provider.should_receive(:shell_out_with_systems_locale!).with("/usr/sbin/invoke-rc.d #{@new_resource.service_name} start") @provider.start_service() end end @@ -165,12 +164,12 @@ RUNNING_PS describe Chef::Provider::Service::Invokercd, "stop_service" do it "should call the stop command if one is specified" do @new_resource.stop_command("/usr/sbin/invoke-rc.d chef itoldyoutostop") - @provider.should_receive(:shell_out!).with("/usr/sbin/invoke-rc.d chef itoldyoutostop") + @provider.should_receive(:shell_out_with_systems_locale!).with("/usr/sbin/invoke-rc.d chef itoldyoutostop") @provider.stop_service() end it "should call '/usr/sbin/invoke-rc.d service_name stop' if no stop command is specified" do - @provider.should_receive(:shell_out!).with("/usr/sbin/invoke-rc.d #{@new_resource.service_name} stop") + @provider.should_receive(:shell_out_with_systems_locale!).with("/usr/sbin/invoke-rc.d #{@new_resource.service_name} stop") @provider.stop_service() end end @@ -178,13 +177,13 @@ RUNNING_PS describe "when restarting a service" do it "should call 'restart' on the service_name if the resource supports it" do @new_resource.supports({:restart => true}) - @provider.should_receive(:shell_out!).with("/usr/sbin/invoke-rc.d #{@new_resource.service_name} restart") + @provider.should_receive(:shell_out_with_systems_locale!).with("/usr/sbin/invoke-rc.d #{@new_resource.service_name} restart") @provider.restart_service() end it "should call the restart_command if one has been specified" do @new_resource.restart_command("/usr/sbin/invoke-rc.d chef restartinafire") - @provider.should_receive(:shell_out!).with("/usr/sbin/invoke-rc.d #{@new_resource.service_name} restartinafire") + @provider.should_receive(:shell_out_with_systems_locale!).with("/usr/sbin/invoke-rc.d #{@new_resource.service_name} restartinafire") @provider.restart_service() end @@ -199,13 +198,13 @@ RUNNING_PS describe "when reloading a service" do it "should call 'reload' on the service if it supports it" do @new_resource.supports({:reload => true}) - @provider.should_receive(:shell_out!).with("/usr/sbin/invoke-rc.d chef reload") + @provider.should_receive(:shell_out_with_systems_locale!).with("/usr/sbin/invoke-rc.d chef reload") @provider.reload_service() end it "should should run the user specified reload command if one is specified and the service doesn't support reload" do @new_resource.reload_command("/usr/sbin/invoke-rc.d chef lollerpants") - @provider.should_receive(:shell_out!).with("/usr/sbin/invoke-rc.d chef lollerpants") + @provider.should_receive(:shell_out_with_systems_locale!).with("/usr/sbin/invoke-rc.d chef lollerpants") @provider.reload_service() end end diff --git a/spec/unit/provider/service/macosx_spec.rb b/spec/unit/provider/service/macosx_spec.rb index 1202d80873..c5df1e0637 100644 --- a/spec/unit/provider/service/macosx_spec.rb +++ b/spec/unit/provider/service/macosx_spec.rb @@ -226,7 +226,7 @@ SVC_LIST it "calls the start command if one is specified and service is not running" do new_resource.stub(:start_command).and_return("cowsay dirty") - provider.should_receive(:shell_out!).with("cowsay dirty") + provider.should_receive(:shell_out_with_systems_locale!).with("cowsay dirty") provider.start_service end @@ -238,7 +238,7 @@ SVC_LIST end it "starts service via launchctl if service found" do - provider.should_receive(:shell_out!). + provider.should_receive(:shell_out_with_systems_locale!). with("launchctl load -w '/Users/igor/Library/LaunchAgents/io.redis.redis-server.plist'", :group => 1001, :user => 101). and_return(0) @@ -258,7 +258,7 @@ SVC_LIST it "calls the stop command if one is specified and service is running" do new_resource.stub(:stop_command).and_return("kill -9 123") - provider.should_receive(:shell_out!).with("kill -9 123") + provider.should_receive(:shell_out_with_systems_locale!).with("kill -9 123") provider.stop_service end @@ -270,7 +270,7 @@ SVC_LIST end it "stops the service via launchctl if service found" do - provider.should_receive(:shell_out!). + provider.should_receive(:shell_out_with_systems_locale!). with("launchctl unload '/Users/igor/Library/LaunchAgents/io.redis.redis-server.plist'", :group => 1001, :user => 101). and_return(0) @@ -291,7 +291,7 @@ SVC_LIST it "issues a command if given" do new_resource.stub(:restart_command).and_return("reload that thing") - provider.should_receive(:shell_out!).with("reload that thing") + provider.should_receive(:shell_out_with_systems_locale!).with("reload that thing") provider.restart_service end diff --git a/spec/unit/provider/service/simple_service_spec.rb b/spec/unit/provider/service/simple_service_spec.rb index 61fb30fe13..11ebf74725 100644 --- a/spec/unit/provider/service/simple_service_spec.rb +++ b/spec/unit/provider/service/simple_service_spec.rb @@ -107,7 +107,7 @@ NOMOCKINGSTRINGSPLZ describe "when starting the service" do it "should call the start command if one is specified" do @new_resource.stub(:start_command).and_return("#{@new_resource.start_command}") - @provider.should_receive(:shell_out!).with("#{@new_resource.start_command}") + @provider.should_receive(:shell_out_with_systems_locale!).with("#{@new_resource.start_command}") @provider.start_service() end @@ -121,7 +121,7 @@ NOMOCKINGSTRINGSPLZ describe "when stopping a service" do it "should call the stop command if one is specified" do @new_resource.stop_command("/etc/init.d/themadness stop") - @provider.should_receive(:shell_out!).with("/etc/init.d/themadness stop") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/init.d/themadness stop") @provider.stop_service() end @@ -135,7 +135,7 @@ NOMOCKINGSTRINGSPLZ describe Chef::Provider::Service::Simple, "restart_service" do it "should call the restart command if one has been specified" do @new_resource.restart_command("/etc/init.d/foo restart") - @provider.should_receive(:shell_out!).with("/etc/init.d/foo restart") + @provider.should_receive(:shell_out_with_systems_locale!).with("/etc/init.d/foo restart") @provider.restart_service() end @@ -162,7 +162,7 @@ NOMOCKINGSTRINGSPLZ it "should should run the user specified reload command if one is specified" do @new_resource.reload_command("kill -9 1") - @provider.should_receive(:shell_out!).with("kill -9 1") + @provider.should_receive(:shell_out_with_systems_locale!).with("kill -9 1") @provider.reload_service() end end diff --git a/spec/unit/provider/service/solaris_smf_service_spec.rb b/spec/unit/provider/service/solaris_smf_service_spec.rb index 978f149258..8df22efa7e 100644 --- a/spec/unit/provider/service/solaris_smf_service_spec.rb +++ b/spec/unit/provider/service/solaris_smf_service_spec.rb @@ -59,7 +59,6 @@ describe Chef::Provider::Service::Solaris do @provider.load_current_resource end - it "should return the current resource" do @provider.stub(:shell_out!).with("/bin/svcs -l chef").and_return(@status) @provider.load_current_resource.should eql(@current_resource) @@ -131,7 +130,6 @@ describe Chef::Provider::Service::Solaris do end end - describe "when disabling the service" do before(:each) do @provider.current_resource = @current_resource @@ -159,7 +157,7 @@ describe Chef::Provider::Service::Solaris do end it "should call svcadm refresh chef" do - @provider.should_receive(:shell_out!).with("/usr/sbin/svcadm refresh chef").and_return(@status) + @provider.should_receive(:shell_out_with_systems_locale!).with("/usr/sbin/svcadm refresh chef").and_return(@status) @provider.reload_service end diff --git a/spec/unit/provider/service/systemd_service_spec.rb b/spec/unit/provider/service/systemd_service_spec.rb index 2aa7b539f2..7358f63b5e 100644 --- a/spec/unit/provider/service/systemd_service_spec.rb +++ b/spec/unit/provider/service/systemd_service_spec.rb @@ -74,25 +74,25 @@ describe Chef::Provider::Service::Systemd do end it "should run the services status command if one has been specified" do - @provider.stub(:shell_out_with_systems_locale).and_return(@shell_out_success) + @provider.stub(:shell_out).and_return(@shell_out_success) @current_resource.should_receive(:running).with(true) @provider.load_current_resource end it "should run the services status command if one has been specified and properly set status check state" do - @provider.stub(:shell_out_with_systems_locale).with("/bin/chefhasmonkeypants status").and_return(@shell_out_success) + @provider.stub(:shell_out).with("/bin/chefhasmonkeypants status").and_return(@shell_out_success) @provider.load_current_resource @provider.instance_variable_get("@status_check_success").should be_true end it "should set running to false if a status command fails" do - @provider.stub(:shell_out_with_systems_locale).and_return(@shell_out_failure) + @provider.stub(:shell_out).and_return(@shell_out_failure) @current_resource.should_receive(:running).with(false) @provider.load_current_resource end it "should update state to indicate status check failed when a status command fails" do - @provider.stub(:shell_out_with_systems_locale).and_return(@shell_out_failure) + @provider.stub(:shell_out).and_return(@shell_out_failure) @provider.load_current_resource @provider.instance_variable_get("@status_check_success").should be_false end @@ -129,31 +129,31 @@ describe Chef::Provider::Service::Systemd do it "should call the start command if one is specified" do @new_resource.stub(:start_command).and_return("/sbin/rsyslog startyousillysally") - @provider.should_receive(:shell_out!).with("/sbin/rsyslog startyousillysally") + @provider.should_receive(:shell_out_with_systems_locale!).with("/sbin/rsyslog startyousillysally") @provider.start_service end it "should call '/bin/systemctl start service_name' if no start command is specified" do - @provider.should_receive(:shell_out_with_systems_locale).with("/bin/systemctl start #{@new_resource.service_name}").and_return(@shell_out_success) + @provider.should_receive(:shell_out_with_systems_locale!).with("/bin/systemctl start #{@new_resource.service_name}").and_return(@shell_out_success) @provider.start_service end it "should not call '/bin/systemctl start service_name' if it is already running" do @current_resource.stub(:running).and_return(true) - @provider.should_not_receive(:shell_out_with_systems_locale).with("/bin/systemctl start #{@new_resource.service_name}") + @provider.should_not_receive(:shell_out_with_systems_locale!).with("/bin/systemctl start #{@new_resource.service_name}") @provider.start_service end it "should call the restart command if one is specified" do @current_resource.stub(:running).and_return(true) @new_resource.stub(:restart_command).and_return("/sbin/rsyslog restartyousillysally") - @provider.should_receive(:shell_out!).with("/sbin/rsyslog restartyousillysally") + @provider.should_receive(:shell_out_with_systems_locale!).with("/sbin/rsyslog restartyousillysally") @provider.restart_service end it "should call '/bin/systemctl restart service_name' if no restart command is specified" do @current_resource.stub(:running).and_return(true) - @provider.should_receive(:shell_out_with_systems_locale).with("/bin/systemctl restart #{@new_resource.service_name}").and_return(@shell_out_success) + @provider.should_receive(:shell_out_with_systems_locale!).with("/bin/systemctl restart #{@new_resource.service_name}").and_return(@shell_out_success) @provider.restart_service end @@ -162,7 +162,7 @@ describe Chef::Provider::Service::Systemd do it "should call the reload command" do @current_resource.stub(:running).and_return(true) @new_resource.stub(:reload_command).and_return("/sbin/rsyslog reloadyousillysally") - @provider.should_receive(:shell_out!).with("/sbin/rsyslog reloadyousillysally") + @provider.should_receive(:shell_out_with_systems_locale!).with("/sbin/rsyslog reloadyousillysally") @provider.reload_service end end @@ -170,7 +170,7 @@ describe Chef::Provider::Service::Systemd do context "when a reload command is not specified" do it "should call '/bin/systemctl reload service_name' if the service is running" do @current_resource.stub(:running).and_return(true) - @provider.should_receive(:shell_out_with_systems_locale).with("/bin/systemctl reload #{@new_resource.service_name}").and_return(@shell_out_success) + @provider.should_receive(:shell_out_with_systems_locale!).with("/bin/systemctl reload #{@new_resource.service_name}").and_return(@shell_out_success) @provider.reload_service end @@ -185,19 +185,19 @@ describe Chef::Provider::Service::Systemd do it "should call the stop command if one is specified" do @current_resource.stub(:running).and_return(true) @new_resource.stub(:stop_command).and_return("/sbin/rsyslog stopyousillysally") - @provider.should_receive(:shell_out!).with("/sbin/rsyslog stopyousillysally") + @provider.should_receive(:shell_out_with_systems_locale!).with("/sbin/rsyslog stopyousillysally") @provider.stop_service end it "should call '/bin/systemctl stop service_name' if no stop command is specified" do @current_resource.stub(:running).and_return(true) - @provider.should_receive(:shell_out_with_systems_locale).with("/bin/systemctl stop #{@new_resource.service_name}").and_return(@shell_out_success) + @provider.should_receive(:shell_out_with_systems_locale!).with("/bin/systemctl stop #{@new_resource.service_name}").and_return(@shell_out_success) @provider.stop_service end it "should not call '/bin/systemctl stop service_name' if it is already stopped" do @current_resource.stub(:running).and_return(false) - @provider.should_not_receive(:shell_out_with_systems_locale).with("/bin/systemctl stop #{@new_resource.service_name}") + @provider.should_not_receive(:shell_out_with_systems_locale!).with("/bin/systemctl stop #{@new_resource.service_name}") @provider.stop_service end end @@ -210,12 +210,12 @@ describe Chef::Provider::Service::Systemd do end it "should call '/bin/systemctl enable service_name' to enable the service" do - @provider.should_receive(:shell_out_with_systems_locale).with("/bin/systemctl enable #{@new_resource.service_name}").and_return(@shell_out_success) + @provider.should_receive(:shell_out!).with("/bin/systemctl enable #{@new_resource.service_name}").and_return(@shell_out_success) @provider.enable_service end it "should call '/bin/systemctl disable service_name' to disable the service" do - @provider.should_receive(:shell_out_with_systems_locale).with("/bin/systemctl disable #{@new_resource.service_name}").and_return(@shell_out_success) + @provider.should_receive(:shell_out!).with("/bin/systemctl disable #{@new_resource.service_name}").and_return(@shell_out_success) @provider.disable_service end end @@ -227,12 +227,12 @@ describe Chef::Provider::Service::Systemd do end it "should return true if '/bin/systemctl is-active service_name' returns 0" do - @provider.should_receive(:shell_out_with_systems_locale).with('/bin/systemctl is-active rsyslog.service --quiet').and_return(@shell_out_success) + @provider.should_receive(:shell_out).with('/bin/systemctl is-active rsyslog.service --quiet').and_return(@shell_out_success) @provider.is_active?.should be_true end it "should return false if '/bin/systemctl is-active service_name' returns anything except 0" do - @provider.should_receive(:shell_out_with_systems_locale).with('/bin/systemctl is-active rsyslog.service --quiet').and_return(@shell_out_failure) + @provider.should_receive(:shell_out).with('/bin/systemctl is-active rsyslog.service --quiet').and_return(@shell_out_failure) @provider.is_active?.should be_false end end @@ -244,12 +244,12 @@ describe Chef::Provider::Service::Systemd do end it "should return true if '/bin/systemctl is-enabled service_name' returns 0" do - @provider.should_receive(:shell_out_with_systems_locale).with('/bin/systemctl is-enabled rsyslog.service --quiet').and_return(@shell_out_success) + @provider.should_receive(:shell_out).with('/bin/systemctl is-enabled rsyslog.service --quiet').and_return(@shell_out_success) @provider.is_enabled?.should be_true end it "should return false if '/bin/systemctl is-enabled service_name' returns anything except 0" do - @provider.should_receive(:shell_out_with_systems_locale).with('/bin/systemctl is-enabled rsyslog.service --quiet').and_return(@shell_out_failure) + @provider.should_receive(:shell_out).with('/bin/systemctl is-enabled rsyslog.service --quiet').and_return(@shell_out_failure) @provider.is_enabled?.should be_false end end diff --git a/spec/unit/provider/service/upstart_service_spec.rb b/spec/unit/provider/service/upstart_service_spec.rb index efe4e0481f..499a794ff4 100644 --- a/spec/unit/provider/service/upstart_service_spec.rb +++ b/spec/unit/provider/service/upstart_service_spec.rb @@ -161,7 +161,6 @@ describe Chef::Provider::Service::Upstart do @provider.load_current_resource end - it "should track state when the upstart configuration file fails to load" do File.should_receive(:exists?).and_return false @provider.load_current_resource @@ -174,19 +173,19 @@ describe Chef::Provider::Service::Upstart do end it "should run the services status command if one has been specified" do - @provider.stub(:run_command_with_systems_locale).with({:command => "/bin/chefhasmonkeypants status"}).and_return(0) + @provider.stub(:shell_out!).with("/bin/chefhasmonkeypants status").and_return(0) @current_resource.should_receive(:running).with(true) @provider.load_current_resource end it "should track state when the user-provided status command fails" do - @provider.stub(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_raise(Chef::Exceptions::Exec) + @provider.stub(:shell_out!).and_raise(Errno::ENOENT) @provider.load_current_resource @provider.instance_variable_get("@command_success").should == false end it "should set running to false if it catches a Chef::Exceptions::Exec when using a status command" do - @provider.stub(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_raise(Chef::Exceptions::Exec) + @provider.stub(:shell_out!).and_raise(Errno::ENOENT) @current_resource.should_receive(:running).with(false) @provider.load_current_resource end @@ -202,7 +201,6 @@ describe Chef::Provider::Service::Upstart do @provider.load_current_resource.should eql(@current_resource) end - end describe "enable and disable service" do @@ -243,18 +241,18 @@ describe Chef::Provider::Service::Upstart do it "should call the start command if one is specified" do @new_resource.stub(:start_command).and_return("/sbin/rsyslog startyousillysally") - @provider.should_receive(:shell_out!).with("/sbin/rsyslog startyousillysally") + @provider.should_receive(:shell_out_with_systems_locale!).with("/sbin/rsyslog startyousillysally") @provider.start_service() end it "should call '/sbin/start service_name' if no start command is specified" do - @provider.should_receive(:run_command_with_systems_locale).with({:command => "/sbin/start #{@new_resource.service_name}"}).and_return(0) + @provider.should_receive(:shell_out_with_systems_locale!).with("/sbin/start #{@new_resource.service_name}").and_return(0) @provider.start_service() end it "should not call '/sbin/start service_name' if it is already running" do @current_resource.stub(:running).and_return(true) - @provider.should_not_receive(:run_command_with_systems_locale).with({:command => "/sbin/start #{@new_resource.service_name}"}) + @provider.should_not_receive(:shell_out_with_systems_locale!) @provider.start_service() end @@ -263,58 +261,58 @@ describe Chef::Provider::Service::Upstart do @new_resource.parameters({ "OSD_ID" => "2" }) @provider = Chef::Provider::Service::Upstart.new(@new_resource, @run_context) @provider.current_resource = @current_resource - @provider.should_receive(:run_command_with_systems_locale).with({:command => "/sbin/start rsyslog OSD_ID=2"}).and_return(0) + @provider.should_receive(:shell_out_with_systems_locale!).with("/sbin/start rsyslog OSD_ID=2").and_return(0) @provider.start_service() end it "should call the restart command if one is specified" do @current_resource.stub(:running).and_return(true) @new_resource.stub(:restart_command).and_return("/sbin/rsyslog restartyousillysally") - @provider.should_receive(:shell_out!).with("/sbin/rsyslog restartyousillysally") + @provider.should_receive(:shell_out_with_systems_locale!).with("/sbin/rsyslog restartyousillysally") @provider.restart_service() end it "should call '/sbin/restart service_name' if no restart command is specified" do @current_resource.stub(:running).and_return(true) - @provider.should_receive(:run_command_with_systems_locale).with({:command => "/sbin/restart #{@new_resource.service_name}"}).and_return(0) + @provider.should_receive(:shell_out_with_systems_locale!).with("/sbin/restart #{@new_resource.service_name}").and_return(0) @provider.restart_service() end it "should call '/sbin/start service_name' if restart_service is called for a stopped service" do @current_resource.stub(:running).and_return(false) - @provider.should_receive(:run_command_with_systems_locale).with({:command => "/sbin/start #{@new_resource.service_name}"}).and_return(0) + @provider.should_receive(:shell_out_with_systems_locale!).with("/sbin/start #{@new_resource.service_name}").and_return(0) @provider.restart_service() end it "should call the reload command if one is specified" do @current_resource.stub(:running).and_return(true) @new_resource.stub(:reload_command).and_return("/sbin/rsyslog reloadyousillysally") - @provider.should_receive(:shell_out!).with("/sbin/rsyslog reloadyousillysally") + @provider.should_receive(:shell_out_with_systems_locale!).with("/sbin/rsyslog reloadyousillysally") @provider.reload_service() end it "should call '/sbin/reload service_name' if no reload command is specified" do @current_resource.stub(:running).and_return(true) - @provider.should_receive(:run_command_with_systems_locale).with({:command => "/sbin/reload #{@new_resource.service_name}"}).and_return(0) + @provider.should_receive(:shell_out_with_systems_locale!).with("/sbin/reload #{@new_resource.service_name}").and_return(0) @provider.reload_service() end it "should call the stop command if one is specified" do @current_resource.stub(:running).and_return(true) @new_resource.stub(:stop_command).and_return("/sbin/rsyslog stopyousillysally") - @provider.should_receive(:shell_out!).with("/sbin/rsyslog stopyousillysally") + @provider.should_receive(:shell_out_with_systems_locale!).with("/sbin/rsyslog stopyousillysally") @provider.stop_service() end it "should call '/sbin/stop service_name' if no stop command is specified" do @current_resource.stub(:running).and_return(true) - @provider.should_receive(:run_command_with_systems_locale).with({:command => "/sbin/stop #{@new_resource.service_name}"}).and_return(0) + @provider.should_receive(:shell_out_with_systems_locale!).with("/sbin/stop #{@new_resource.service_name}").and_return(0) @provider.stop_service() end it "should not call '/sbin/stop service_name' if it is already stopped" do @current_resource.stub(:running).and_return(false) - @provider.should_not_receive(:run_command_with_systems_locale).with({:command => "/sbin/stop #{@new_resource.service_name}"}) + @provider.should_not_receive(:shell_out_with_systems_locale!).with("/sbin/stop #{@new_resource.service_name}") @provider.stop_service() end end diff --git a/spec/unit/provider/subversion_spec.rb b/spec/unit/provider/subversion_spec.rb index f37a42d235..5d9d1cec1e 100644 --- a/spec/unit/provider/subversion_spec.rb +++ b/spec/unit/provider/subversion_spec.rb @@ -16,7 +16,6 @@ # limitations under the License. # - require 'spec_helper' describe Chef::Provider::Subversion do @@ -199,7 +198,7 @@ describe Chef::Provider::Subversion do it "runs an export with the --force option" do ::File.stub(:directory?).with("/my/deploy").and_return(true) expected_cmd = "svn export --force -q -r12345 http://svn.example.org/trunk/ /my/deploy/dir" - @provider.should_receive(:run_command).with(:command => expected_cmd) + @provider.should_receive(:shell_out!).with(command: expected_cmd) @provider.run_action(:force_export) @resource.should be_updated end @@ -207,7 +206,7 @@ describe Chef::Provider::Subversion do it "runs the checkout command for action_checkout" do ::File.stub(:directory?).with("/my/deploy").and_return(true) expected_cmd = "svn checkout -q -r12345 http://svn.example.org/trunk/ /my/deploy/dir" - @provider.should_receive(:run_command).with(:command => expected_cmd) + @provider.should_receive(:shell_out!).with(command: expected_cmd) @provider.run_action(:checkout) @resource.should be_updated end @@ -231,7 +230,7 @@ describe Chef::Provider::Subversion do @resource.user "whois" @resource.group "thisis" expected_cmd = "svn checkout -q -r12345 http://svn.example.org/trunk/ /my/deploy/dir" - @provider.should_receive(:run_command).with(:command => expected_cmd, :user => "whois", :group => "thisis") + @provider.should_receive(:shell_out!).with(command: expected_cmd, user: "whois", group: "thisis") @provider.run_action(:checkout) @resource.should be_updated end @@ -256,7 +255,7 @@ describe Chef::Provider::Subversion do @provider.stub(:find_current_revision).and_return("11410") @provider.stub(:current_revision_matches_target_revision?).and_return(false) expected_cmd = "svn update -q -r12345 /my/deploy/dir" - @provider.should_receive(:run_command).with(:command => expected_cmd) + @provider.should_receive(:shell_out!).with(command: expected_cmd) @provider.run_action(:sync) @resource.should be_updated end @@ -273,7 +272,7 @@ describe Chef::Provider::Subversion do it "runs the export_command on action_export" do ::File.stub(:directory?).with("/my/deploy").and_return(true) expected_cmd = "svn export --force -q -r12345 http://svn.example.org/trunk/ /my/deploy/dir" - @provider.should_receive(:run_command).with(:command => expected_cmd) + @provider.should_receive(:shell_out!).with(command: expected_cmd) @provider.run_action(:export) @resource.should be_updated end diff --git a/spec/unit/provider/whyrun_safe_ruby_block_spec.rb b/spec/unit/provider/whyrun_safe_ruby_block_spec.rb index 5a17aacbd9..d5209248b3 100644 --- a/spec/unit/provider/whyrun_safe_ruby_block_spec.rb +++ b/spec/unit/provider/whyrun_safe_ruby_block_spec.rb @@ -30,14 +30,14 @@ describe Chef::Provider::WhyrunSafeRubyBlock, "initialize" do end it "should call the block and flag the resource as updated" do - @provider.run_action(:create) + @provider.run_action(:run) $evil_global_evil_laugh.should == :mwahahaha @new_resource.should be_updated end it "should call the block and flat the resource as updated - even in whyrun" do Chef::Config[:why_run] = true - @provider.run_action(:create) + @provider.run_action(:run) $evil_global_evil_laugh.should == :mwahahaha @new_resource.should be_updated Chef::Config[:why_run] = false diff --git a/spec/unit/run_context_spec.rb b/spec/unit/run_context_spec.rb index 1def10faf5..21ece2abaa 100644 --- a/spec/unit/run_context_spec.rb +++ b/spec/unit/run_context_spec.rb @@ -134,4 +134,19 @@ describe Chef::RunContext do end end + describe "handling reboot requests" do + let(:expected) do + { :reason => "spec tests require a reboot" } + end + + it "stores and deletes the reboot request" do + @run_context.request_reboot(expected) + expect(@run_context.reboot_info).to eq(expected) + expect(@run_context.reboot_requested?).to be_true + + @run_context.cancel_reboot + expect(@run_context.reboot_info).to eq({}) + expect(@run_context.reboot_requested?).to be_false + end + end end diff --git a/spec/unit/workstation_config_loader_spec.rb b/spec/unit/workstation_config_loader_spec.rb index 78313aec37..d1e352602d 100644 --- a/spec/unit/workstation_config_loader_spec.rb +++ b/spec/unit/workstation_config_loader_spec.rb @@ -88,7 +88,12 @@ describe Chef::WorkstationConfigLoader do let(:env_pwd) { "/path/to/cwd" } before do - env["PWD"] = env_pwd + if Chef::Platform.windows? + env["CD"] = env_pwd + else + env["PWD"] = env_pwd + end + allow(config_loader).to receive(:path_exists?).with("#{env_pwd}/.chef/knife.rb").and_return(true) allow(File).to receive(:exist?).with("#{env_pwd}/.chef").and_return(true) allow(File).to receive(:directory?).with("#{env_pwd}/.chef").and_return(true) @@ -229,7 +234,8 @@ describe Chef::WorkstationConfigLoader do let(:config_content) { "" } let(:explicit_config_location) do - t = Tempfile.new("#{described_class}-rspec-test") + # could use described_class, but remove all ':' from the path if so. + t = Tempfile.new("Chef-WorkstationConfigLoader-rspec-test") t.print(config_content) t.close t.path |