summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Evans <jordane@osuosl.org>2014-08-13 08:54:06 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2014-08-21 12:37:11 -0700
commita6dd0a74386d535b757369bf6d31ba0d6b027dbe (patch)
tree34c08f2129c04b71c14e8f2cfd2dd0e09e2fc0f5
parent6e6a79e2c09d82cfe1878256a19001727a2c2387 (diff)
downloadchef-a6dd0a74386d535b757369bf6d31ba0d6b027dbe.tar.gz
raise error if multiple constraints are met
-rw-r--r--lib/chef/dsl/platform_introspection.rb19
-rw-r--r--spec/support/shared/unit/platform_introspector.rb49
2 files changed, 36 insertions, 32 deletions
diff --git a/lib/chef/dsl/platform_introspection.rb b/lib/chef/dsl/platform_introspection.rb
index 2e22662ca2..ff48cc16d2 100644
--- a/lib/chef/dsl/platform_introspection.rb
+++ b/lib/chef/dsl/platform_introspection.rb
@@ -70,20 +70,29 @@ class Chef
def match_versions(node)
begin
platform, version = node[:platform].to_s, node[:platform_version].to_s
- return unless @values.key?(platform)
+ return nil unless @values.key?(platform)
node_version = Chef::Version::Platform.new(version)
+ key_matches = []
keys = @values[platform].keys
keys.each do |k|
begin
- if Chef::VersionConstraint.new(k).include?(node_version)
- return @values[platform][k]
- end
+ if Chef::VersionConstraint.new(k).include?(node_version)
+ key_matches << k
+ end
rescue Chef::Exceptions::InvalidVersionConstraint => e
Chef::Log.debug "Caught InvalidVersionConstraint. This means that a key in value_for_platform cannot be interpreted as a Chef::VersionConstraint."
Chef::Log.debug(e)
end
end
- return nil
+ return @values[platform][version] if key_matches.include?(version)
+ case key_matches.length
+ when 0
+ return nil
+ when 1
+ return @values[platform][key_matches.first]
+ else
+ fail "Multiple matches detected for #{platform} with values #{@values}. The matches are: #{key_matches}"
+ end
rescue Chef::Exceptions::InvalidCookbookVersion => e
# Lets not break because someone passes a weird string like 'default' :)
Chef::Log.debug(e)
diff --git a/spec/support/shared/unit/platform_introspector.rb b/spec/support/shared/unit/platform_introspector.rb
index 45dbff7dae..f76ddbdf9e 100644
--- a/spec/support/shared/unit/platform_introspector.rb
+++ b/spec/support/shared/unit/platform_introspector.rb
@@ -28,9 +28,11 @@ 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["pessimistic"] = {"~> 1.2.3" => "12", "~> 1.2" => "1", "default" => "2"}
@platform_hash["default"] = "default"
+ # The following @platform_hash keys are used for testing version constraints
+ @platform_hash['exact_match'] = { '1.2.3' => 'exact', '>= 1.0' => 'not exact'}
+ @platform_hash['multiple_matches'] = { '~> 2.3.4' => 'matched ~> 2.3.4', '>= 2.3' => 'matched >=2.3' }
+ @platform_hash['successful_matches'] = { '< 3.0' => 'matched < 3.0', '>= 3.0' => 'matched >= 3.0' }
@platform_family_hash = {
"debian" => "debian value",
@@ -81,6 +83,24 @@ shared_examples_for "a platform introspector" do
platform_introspector.value_for_platform(@platform_hash).should == "openbsd"
end
+ it 'returns the exact match' do
+ node.automatic_attrs[:platform] = 'exact_match'
+ node.automatic_attrs[:platform_version] = '1.2.3'
+ platform_introspector.value_for_platform(@platform_hash).should == 'exact'
+ end
+
+ it 'raises RuntimeError' do
+ node.automatic_attrs[:platform] = 'multiple_matches'
+ node.automatic_attrs[:platform_version] = '2.3.4'
+ expect {platform_introspector.value_for_platform(@platform_hash)}.to raise_error(RuntimeError)
+ end
+
+ it 'should return the value for that match' do
+ node.automatic_attrs[:platform] = 'successful_matches'
+ node.automatic_attrs[:platform_version] = '2.9'
+ platform_introspector.value_for_platform(@platform_hash).should == 'matched < 3.0'
+ end
+
describe "when platform versions is an array" do
it "returns a version-specific value based on the current platform" do
node.automatic_attrs[:platform] = "debian"
@@ -93,31 +113,6 @@ 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
- it "pessimistic depends on x.y.z looks at x.y" do
- node.automatic_attrs[:platform] = "pessimistic"
- node.automatic_attrs[:platform_version] = "1.2.17"
- platform_introspector.value_for_platform(@platform_hash).should == "12"
- end
- it "pessismistic depends on x.y looks at x" do
- node.automatic_attrs[:platform] = "pessimistic"
- node.automatic_attrs[:platform_version] = "1.8.14"
- platform_introspector.value_for_platform(@platform_hash).should == "1"
- end
- it "pessismistic depends on x.y won't match x+1" do
- node.automatic_attrs[:platform] = "pessimistic"
- node.automatic_attrs[:platform_version] = "2.8.14"
- platform_introspector.value_for_platform(@platform_hash).should == "2"
- end
end
describe "when checking platform?" do