summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan McLellan <btm@loftninjas.org>2019-04-16 14:12:31 -0400
committerGitHub <noreply@github.com>2019-04-16 14:12:31 -0400
commitcd2ade61dfd241e2b5e6d0cfeca27a80eae47e4c (patch)
tree9770ac901d50d6ba7efbfb98011e0c370c629857
parent62ea7382021593d117a78a0d5f60e39722c2e4d4 (diff)
parentc248a1e9f7e5ef3c7d832c422ea887cb2841f101 (diff)
downloadchef-cd2ade61dfd241e2b5e6d0cfeca27a80eae47e4c.tar.gz
Merge pull request #8375 from chef/btm/locale-fix-custom
Limit locale resource to Linux
-rw-r--r--kitchen-tests/cookbooks/end_to_end/recipes/default.rb7
-rw-r--r--lib/chef/resource/locale.rb28
-rw-r--r--spec/functional/resource/locale_spec.rb109
3 files changed, 87 insertions, 57 deletions
diff --git a/kitchen-tests/cookbooks/end_to_end/recipes/default.rb b/kitchen-tests/cookbooks/end_to_end/recipes/default.rb
index 7c9856d909..6d95f4471b 100644
--- a/kitchen-tests/cookbooks/end_to_end/recipes/default.rb
+++ b/kitchen-tests/cookbooks/end_to_end/recipes/default.rb
@@ -17,6 +17,13 @@ if platform_family?("rhel", "fedora", "amazon")
include_recipe "selinux::disabled"
end
+bash "disable yum metadata caching" do
+ code <<-EOH
+ echo http_caching=packages >> /etc/yum.conf
+ EOH
+ only_if { File.exist?("/etc/yum.conf") && File.readlines("/etc/yum.conf").grep(/http_caching=packages/).empty? }
+end
+
yum_repository "epel" do
enabled true
description "Extra Packages for Enterprise Linux #{node['platform_version'].to_i} - $basearch"
diff --git a/lib/chef/resource/locale.rb b/lib/chef/resource/locale.rb
index b16cfe7e3c..52170e8765 100644
--- a/lib/chef/resource/locale.rb
+++ b/lib/chef/resource/locale.rb
@@ -28,6 +28,7 @@ class Chef
LC_VARIABLES = %w{LC_ADDRESS LC_COLLATE LC_CTYPE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE LC_TIME}.freeze
LOCALE_CONF = "/etc/locale.conf".freeze
LOCALE_REGEX = /\A\S+/.freeze
+ LOCALE_PLATFORM_FAMILIES = %w{debian}.freeze
property :lang, String,
description: "Sets the default system language.",
@@ -71,28 +72,31 @@ class Chef
update_locale
end
end
- rescue
- # It might affect debugging
- raise "#{node['platform']} platform is not supported by the chef locale resource. " +
- "If you believe this is in error please file an issue at https://github.com/chef/chef/issues"
end
end
action_class do
+ # Avoid running this resource on platforms that don't use /etc/locale.conf
+ #
+ def define_resource_requirements
+ requirements.assert(:all_actions) do |a|
+ a.assertion { LOCALE_PLATFORM_FAMILIES.include?(node[:platform_family]) }
+ a.failure_message(Chef::Exceptions::ProviderNotFound, "The locale resource is not supported on platform family: #{node[:platform_family]}")
+ end
+
+ requirements.assert(:all_actions) do |a|
+ # RHEL/CentOS type platforms don't have locale-gen
+ a.assertion { shell_out("locale-gen") }
+ a.failure_message(Chef::Exceptions::ProviderNotFound, "The locale resource requires the locale-gen tool")
+ end
+ end
# Generates the localisation files from templates using locale-gen.
# @see http://manpages.ubuntu.com/manpages/cosmic/man8/locale-gen.8.html
# @raise [Mixlib::ShellOut::ShellCommandFailed] not a supported language or locale
#
def generate_locales
- bash "Generating locales: #{unavailable_locales.join(' ')}" do
- code <<~CODE
- if type locale-gen >/dev/null 2>&1
- then
- locale-gen #{unavailable_locales.join(' ')}
- fi
- CODE
- end
+ shell_out!("locale-gen #{unavailable_locales.join(' ')}")
end
# Updates system locale by appropriately writing them in /etc/locale.conf
diff --git a/spec/functional/resource/locale_spec.rb b/spec/functional/resource/locale_spec.rb
index 61ef4630bc..83dfc2d007 100644
--- a/spec/functional/resource/locale_spec.rb
+++ b/spec/functional/resource/locale_spec.rb
@@ -18,61 +18,80 @@
require "spec_helper"
-describe Chef::Resource::Locale, :requires_root, :not_supported_on_windows do
-
- let(:node) { Chef::Node.new }
+describe Chef::Resource::Locale, :requires_root do
+ let(:node) do
+ n = Chef::Node.new
+ n.consume_external_attrs(OHAI_SYSTEM.data, {})
+ n
+ end
let(:events) { Chef::EventDispatch::Dispatcher.new }
let(:run_context) { Chef::RunContext.new(node, {}, events) }
let(:resource) { Chef::Resource::Locale.new("fakey_fakerton", run_context) }
- def sets_system_locale(*locales)
- system_locales = File.readlines("/etc/locale.conf")
- expect(system_locales.map(&:strip)).to eq(locales)
- end
+ context "on debian/ubuntu", :debian_family_only do
+ def sets_system_locale(*locales)
+ system_locales = File.readlines("/etc/locale.conf")
+ expect(system_locales.map(&:strip)).to eq(locales)
+ end
- def unsets_system_locale(*locales)
- system_locales = File.readlines("/etc/locale.conf")
- expect(system_locales.map(&:strip)).not_to eq(locales)
- end
+ def unsets_system_locale(*locales)
+ system_locales = File.readlines("/etc/locale.conf")
+ expect(system_locales.map(&:strip)).not_to eq(locales)
+ end
- describe "action: update" do
- context "Sets system variable" do
- it "when LC var is given" do
- resource.lc_env({ "LC_MESSAGES" => "en_US" })
- resource.run_action(:update)
- sets_system_locale("LC_MESSAGES=en_US")
+ describe "action: update" do
+ context "Sets system variable" do
+ it "when LC var is given" do
+ resource.lc_env({ "LC_MESSAGES" => "en_US" })
+ resource.run_action(:update)
+ sets_system_locale("LC_MESSAGES=en_US")
+ end
+ it "when lang is given" do
+ resource.lang("en_US")
+ resource.run_action(:update)
+ sets_system_locale("LANG=en_US")
+ end
+ it "when both lang & LC vars are given" do
+ resource.lang("en_US")
+ resource.lc_env({ "LC_TIME" => "en_IN" })
+ resource.run_action(:update)
+ sets_system_locale("LANG=en_US", "LC_TIME=en_IN")
+ end
end
- it "when lang is given" do
- resource.lang("en_US")
- resource.run_action(:update)
- sets_system_locale("LANG=en_US")
- end
- it "when both lang & LC vars are given" do
- resource.lang("en_US")
- resource.lc_env({ "LC_TIME" => "en_IN" })
- resource.run_action(:update)
- sets_system_locale("LANG=en_US", "LC_TIME=en_IN")
+
+ context "Unsets system variable" do
+ it "when LC var is not given" do
+ resource.lc_env()
+ resource.run_action(:update)
+ unsets_system_locale("LC_MESSAGES=en_US")
+ end
+ it "when lang is not given" do
+ resource.lang()
+ resource.run_action(:update)
+ unsets_system_locale("LANG=en_US")
+ end
+ it "when both lang & LC vars are not given" do
+ resource.lang()
+ resource.lc_env()
+ resource.run_action(:update)
+ unsets_system_locale("LANG=en_US", "LC_TIME=en_IN")
+ sets_system_locale("")
+ end
end
end
+ end
- context "Unsets system variable" do
- it "when LC var is not given" do
- resource.lc_env()
- resource.run_action(:update)
- unsets_system_locale("LC_MESSAGES=en_US")
- end
- it "when lang is not given" do
- resource.lang()
- resource.run_action(:update)
- unsets_system_locale("LANG=en_US")
- end
- it "when both lang & LC vars are not given" do
- resource.lang()
- resource.lc_env()
- resource.run_action(:update)
- unsets_system_locale("LANG=en_US", "LC_TIME=en_IN")
- sets_system_locale("")
- end
+ context "on rhel", :rhel do
+ it "raises an exception due lacking the locale-gen tool" do
+ resource.lang("en_US")
+ expect { resource.run_action(:update) }.to raise_error(Chef::Exceptions::ProviderNotFound)
+ end
+ end
+
+ context "on macos", :macos_only do
+ it "raises an exception due to being an unsupported platform" do
+ resource.lang("en_US")
+ expect { resource.run_action(:update) }.to raise_error(Chef::Exceptions::ProviderNotFound)
end
end
end