summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Evans <jordane@osuosl.org>2014-07-31 19:17:54 +0200
committerLamont Granquist <lamont@scriptkiddie.org>2014-08-21 12:37:11 -0700
commit2f69b6f047b71f0cec450d37ca3edacfc882f2e7 (patch)
treea1d3b9f2b3b4bca0561eaf53c4161492d124cabc
parentaa773b4ebf83352f9587c0b8d3e2187e43f7735d (diff)
downloadchef-2f69b6f047b71f0cec450d37ca3edacfc882f2e7.tar.gz
initial pass at supporting version constraints in value_for_platform
-rw-r--r--lib/chef/dsl/platform_introspection.rb23
-rw-r--r--spec/support/shared/unit/platform_introspector.rb11
2 files changed, 34 insertions, 0 deletions
diff --git a/lib/chef/dsl/platform_introspection.rb b/lib/chef/dsl/platform_introspection.rb
index 33aa451f30..a093b933e5 100644
--- a/lib/chef/dsl/platform_introspection.rb
+++ b/lib/chef/dsl/platform_introspection.rb
@@ -50,8 +50,12 @@ class Chef
def value_for_node(node)
platform, version = node[:platform].to_s, node[:platform_version].to_s
+ # Check if we match a version constraint via Gem::Requirement and Gem::Version
+ matched_value = match_versions(node)
if @values.key?(platform) && @values[platform].key?(version)
@values[platform][version]
+ elsif matched_value
+ matched_value
elsif @values.key?(platform) && @values[platform].key?("default")
@values[platform]["default"]
elsif @values.key?("default")
@@ -63,6 +67,25 @@ class Chef
private
+ def match_versions(node)
+ begin
+ platform, version = node[:platform].to_s, node[:platform_version].to_s
+ return unless @values.key?(platform)
+ node_version = Gem::Version.new(version)
+ keys = @values[platform].keys
+ keys.each do |k|
+ if Gem::Requirement.new(k).satisfied_by?(node_version)
+ return @values[platform][k]
+ break
+ end
+ end
+ return nil
+ rescue ArgumentError
+ # Lets not break because someone passes a weird string like 'default' :)
+ return
+ end
+ end
+
def set(platforms, value)
if platforms.to_s == 'default'
@values["default"] = value
diff --git a/spec/support/shared/unit/platform_introspector.rb b/spec/support/shared/unit/platform_introspector.rb
index d596e2984e..255c10b804 100644
--- a/spec/support/shared/unit/platform_introspector.rb
+++ b/spec/support/shared/unit/platform_introspector.rb
@@ -28,6 +28,7 @@ shared_examples_for "a platform introspector" do
}
end
@platform_hash["debian"] = {["5", "6"] => "debian-5/6", "default" => "debian"}
+ @platform_hash["centos"] = {"~> 6.0" => "centos-6", ">= 7.0" => "centos-7", "6.5" => "centos-6.5" }
@platform_hash["default"] = "default"
@platform_family_hash = {
@@ -91,6 +92,16 @@ shared_examples_for "a platform introspector" do
node.automatic_attrs[:platform_version] = "0.0.0"
platform_introspector.value_for_platform(@platform_hash).should == "debian"
end
+ it "returns a value when given a version constraint key" do
+ node.automatic_attrs[:platform] = "centos"
+ node.automatic_attrs[:platform_version] = "7.0.1406"
+ platform_introspector.value_for_platform(@platform_hash).should == "centos-7"
+ end
+ it "returns the value for a specific key over a constrained key" do
+ node.automatic_attrs[:platform] = "centos"
+ node.automatic_attrs[:platform_version] = "6.5"
+ platform_introspector.value_for_platform(@platform_hash).should == "centos-6.5"
+ end
end
describe "when checking platform?" do