Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: chef/chef
base: 251cac6fe7
...
head fork: chef/chef
compare: e1152ab817
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
View
92 chef/lib/chef/mixin/language.rb
@@ -93,6 +93,8 @@ def assert_valid_platform_values!(platforms, value)
end
end
+
+
# Given a hash similar to the one we use for Platforms, select a value from the hash. Supports
# per platform defaults, along with a single base default. Arrays may be passed as hash keys and
# will be expanded.
@@ -104,13 +106,13 @@ def assert_valid_platform_values!(platforms, value)
# value:: Whatever the most specific value of the hash is.
def value_for_platform(platform_hash)
PlatformDependentValue.new(platform_hash).value_for_node(node)
- end
+ end
# Given a list of platforms, returns true if the current recipe is being run on a node with
# that platform, false otherwise.
#
# === Parameters
- # args:: A list of platforms
+ # args:: A list of platforms. Each platform can be in string or symbol format.
#
# === Returns
# true:: If the current platform is in the list
@@ -119,12 +121,96 @@ def platform?(*args)
has_platform = false
args.flatten.each do |platform|
- has_platform = true if platform == node[:platform]
+ has_platform = true if platform.to_s == node[:platform]
end
has_platform
end
+
+
+ # Implementation class for determining platform family dependent values
+ class PlatformFamilyDependentValue
+
+ # Create a platform family dependent value object.
+ # === Arguments
+ # platform_family_hash (Hash) a map of platform families to values.
+ # like this:
+ # {
+ # :rhel => "value for all EL variants"
+ # :fedora => "value for fedora variants fedora and amazon" ,
+ # [:fedora, :rhel] => "value for all known redhat variants"
+ # :debian => "value for debian variants including debian, ubuntu, mint" ,
+ # :default => "the default when nothing else matches"
+ # }
+ # * platform families can be specified as Symbols or Strings
+ # * multiple platform families can be grouped by using an Array as the key
+ # * values for platform families can be any object, with no restrictions. Some examples:
+ # - [:stop, :start]
+ # - "mysql-devel"
+ # - { :key => "value" }
+ def initialize(platform_family_hash)
+ @values = {}
+ @values["default"] = nil
+ platform_family_hash.each { |platform_families, value| set(platform_families, value)}
+ end
+
+ def value_for_node(node)
+ if node.key?(:platform_family)
+ platform_family = node[:platform_family].to_s
+ if @values.key?(platform_family)
+ @values[platform_family]
+ else
+ @values["default"]
+ end
+ else
+ @values["default"]
+ end
+ end
+
+ private
+
+ def set(platform_family, value)
+ if platform_family.to_s == 'default'
+ @values["default"] = value
+ else
+ Array(platform_family).each { |family| @values[family.to_s] = value }
+ value
+ end
+ end
+ end
+
+
+ # Given a hash mapping platform families to values, select a value from the hash. Supports a single
+ # base default if platform family is not in the map. Arrays may be passed as hash keys and will be
+ # expanded.
+ #
+ # === Parameters
+ # platform_family_hash:: A hash in the form { platform_family_name => value }
+ #
+ # === Returns
+ # value:: Whatever the most specific value of the hash is.
+ def value_for_platform_family(platform_family_hash)
+ PlatformFamilyDependentValue.new(platform_family_hash).value_for_node(node)
+ end
+
+ # Given a list of platform families, returns true if the current recipe is being run on a
+ # node within that platform family, false otherwise.
+ #
+ # === Parameters
+ # args:: A list of platform families. Each platform family can be in string or symbol format.
+ #
+ # === Returns
+ # true:: if the current node platform family is in the list.
+ # false:: if the current node platform family is not in the list.
+ def platform_family?(*args)
+ has_pf = false
+ args.flatten.each do |platform_family|
+ has_pf = true if platform_family.to_s == node[:platform_family]
+ end
+ has_pf
+ end
+
def search(*args, &block)
# If you pass a block, or have at least the start argument, do raw result parsing
#
View
121 chef/spec/unit/mixin/language_spec.rb
@@ -37,6 +37,13 @@ class LanguageTester
end
@platform_hash["debian"] = {["5", "6"] => "debian-5/6", "default" => "debian"}
@platform_hash["default"] = "default"
+
+ @platform_family_hash = {
+ "debian" => "debian value",
+ [:rhel, :fedora] => "redhatty value",
+ "suse" => "suse value",
+ :default => "default value"
+ }
end
it "returns a default value when there is no known platform" do
@@ -44,16 +51,30 @@ class LanguageTester
@language.value_for_platform(@platform_hash).should == "default"
end
+ it "returns a default value when there is no known platform family" do
+ @language.value_for_platform_family(@platform_family_hash).should == "default value"
+ end
+
it "returns a default value when the current platform doesn't match" do
@node[:platform] = "not-a-known-platform"
@language.value_for_platform(@platform_hash).should == "default"
end
+ it "returns a default value when current platform_family doesn't match" do
+ @node[:platform_family] = "ultra-derived-linux"
+ @language.value_for_platform_family(@platform_family_hash).should == "default value"
+ end
+
it "returns a value based on the current platform" do
@node[:platform] = "openbsd"
@language.value_for_platform(@platform_hash).should == "openbsd"
end
+ it "returns a value based on the current platform family" do
+ @node[:platform_family] = "debian"
+ @language.value_for_platform_family(@platform_family_hash).should == "debian value"
+ end
+
it "returns a version-specific value based on the current platform" do
@node[:platform] = "openbsd"
@node[:platform_version] = "1.2.3"
@@ -80,6 +101,56 @@ class LanguageTester
end
end
+ describe "when checking platform?" do
+ before(:each) do
+ @language = LanguageTester.new
+ @node = Hash.new
+ @language.stub!(:node).and_return(@node)
+ end
+
+ it "returns true if the node is a provided platform and platforms are provided as symbols" do
+ @node[:platform] = 'ubuntu'
+ @language.platform?([:redhat, :ubuntu]).should == true
+ end
+
+ it "returns true if the node is a provided platform and platforms are provided as strings" do
+ @node[:platform] = 'ubuntu'
+ @language.platform?(["redhat", "ubuntu"]).should == true
+ end
+
+ it "returns false if the node is not of the provided platforms" do
+ @node[:platform] = 'ubuntu'
+ @language.platform?(:splatlinux).should == false
+ end
+ end
+
+ describe "when checking platform_family?" do
+ before(:each) do
+ @language = LanguageTester.new
+ @node = Hash.new
+ @language.stub!(:node).and_return(@node)
+ end
+
+ it "returns true if the node is in a provided platform family and families are provided as symbols" do
+ @node[:platform_family] = 'debian'
+ @language.platform_family?([:rhel, :debian]).should == true
+ end
+
+ it "returns true if the node is a provided platform and platforms are provided as strings" do
+ @node[:platform_family] = 'rhel'
+ @language.platform_family?(["rhel", "debian"]).should == true
+ end
+
+ it "returns false if the node is not of the provided platforms" do
+ @node[:platform_family] = 'suse'
+ @language.platform_family?(:splatlinux).should == false
+ end
+
+ it "returns false if the node is not of the provided platforms and platform_family is not set" do
+ @language.platform_family?(:splatlinux).should == false
+ end
+
+ end
# NOTE: this is a regression test for bug CHEF-1514
describe "when the value is an array" do
before do
@@ -102,8 +173,7 @@ class LanguageTester
@node[:platform] = "debian"
@node[:platform_version] = '4.0'
@language.value_for_platform(@platform_hash).should == [:restart, :reload]
- end
-
+ end
end
describe "when loading data bags and items" do
@@ -186,3 +256,50 @@ class LanguageTester
end
end
+describe Chef::Mixin::Language::PlatformFamilyDependentValue do
+ before do
+ @array_values = [:stop, :start, :reload]
+
+ @platform_family_hash = {
+ "debian" => "debian value",
+ [:rhel, "fedora"] => "redhatty value",
+ "suse" => @array_values,
+ :gentoo => "gentoo value",
+ :default => "default value"
+ }
+
+ @platform_family_value = Chef::Mixin::Language::PlatformFamilyDependentValue.new(@platform_family_hash)
+ end
+
+ it "returns the default value when the platform family doesn't match" do
+ @platform_family_value.value_for_node(:platform_family => :os2).should == 'default value'
+ end
+
+
+ it "returns a value for the platform family when it was set as a string but fetched as a symbol" do
+ @platform_family_value.value_for_node(:platform_family => :debian).should == "debian value"
+ end
+
+
+ it "returns a value for the platform family when it was set as a symbol but fetched as a string" do
+ @platform_family_value.value_for_node(:platform_family => "gentoo").should == "gentoo value"
+ end
+
+ it "returns an array value stored for a platform family" do
+ @platform_family_value.value_for_node(:platform_family => "suse").should == @array_values
+ end
+
+ it "returns a value for the platform family when it was set within an array hash key as a symbol" do
+ @platform_family_value.value_for_node(:platform_family => :rhel).should == "redhatty value"
+ end
+
+ it "returns a value for the platform family when it was set within an array hash key as a string" do
+ @platform_family_value.value_for_node(:platform_family => "fedora").should == "redhatty value"
+ end
+
+ it "returns nil if there is no default and no platforms match" do
+ platform_specific_value = Chef::Mixin::Language::PlatformFamilyDependentValue.new({})
+ platform_specific_value.value_for_node(:platform_family => 'foo').should be_nil
+ end
+
+end

No commit comments for this range

Something went wrong with that request. Please try again.