summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2017-03-31 12:24:00 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2017-03-31 14:09:07 -0700
commit1b0cc1b541a9015fc925fc5d1ada6f85934fc0c7 (patch)
tree8a6f2b2c99faa926c705216a47fb0d1f94cd1cf0
parent5dacd74ce5cccc4dbb651a45f9e591dd1921cc6e (diff)
downloadchef-1b0cc1b541a9015fc925fc5d1ada6f85934fc0c7.tar.gz
Chef-13: fix notifying array resources
this also deprecates the multi-resource notification syntax where `foo[a,b]` would attempt to notify both `foo[a]` and `foo[b]` because that is hella ambiguous. Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r--lib/chef/deprecated.rb10
-rw-r--r--lib/chef/dsl/declare_resource.rb2
-rw-r--r--lib/chef/resource_collection/resource_set.rb46
-rw-r--r--spec/integration/recipes/notifies_spec.rb29
-rw-r--r--spec/unit/resource_collection/resource_set_spec.rb16
-rw-r--r--spec/unit/resource_collection_spec.rb3
6 files changed, 87 insertions, 19 deletions
diff --git a/lib/chef/deprecated.rb b/lib/chef/deprecated.rb
index 461f65225b..d4db0813c7 100644
--- a/lib/chef/deprecated.rb
+++ b/lib/chef/deprecated.rb
@@ -218,6 +218,16 @@ class Chef
end
end
+ class MultiresourceMatch < Base
+ def id
+ 16
+ end
+
+ def target
+ "multiresource_match.html"
+ end
+ end
+
# id 3694 was deleted
class Generic < Base
diff --git a/lib/chef/dsl/declare_resource.rb b/lib/chef/dsl/declare_resource.rb
index ac3776c92f..6869e77eca 100644
--- a/lib/chef/dsl/declare_resource.rb
+++ b/lib/chef/dsl/declare_resource.rb
@@ -241,7 +241,7 @@ class Chef
resource = build_resource(type, name, created_at: created_at, &resource_attrs_block)
- run_context.resource_collection.insert(resource, resource_type: type, instance_name: name)
+ run_context.resource_collection.insert(resource, resource_type: resource.declared_type, instance_name: resource.name)
resource
end
diff --git a/lib/chef/resource_collection/resource_set.rb b/lib/chef/resource_collection/resource_set.rb
index 111d23dc09..6ff29247a0 100644
--- a/lib/chef/resource_collection/resource_set.rb
+++ b/lib/chef/resource_collection/resource_set.rb
@@ -161,27 +161,39 @@ class Chef
end
def find_resource_by_string(arg)
- results = Array.new
- case arg
- when MULTIPLE_RESOURCE_MATCH
- resource_type = $1
- arg =~ /^.+\[(.+)\]$/
- resource_list = $1
- resource_list.split(",").each do |instance_name|
- results << lookup(create_key(resource_type, instance_name))
- end
- when SINGLE_RESOURCE_MATCH
+ begin
+ if arg =~ SINGLE_RESOURCE_MATCH
resource_type = $1
name = $2
- results << lookup(create_key(resource_type, name))
- when NAMELESS_RESOURCE_MATCH
- resource_type = $1
- name = ""
- results << lookup(create_key(resource_type, name))
+ return [ lookup(create_key(resource_type, name)) ]
+ end
+ rescue Chef::Exceptions::ResourceNotFound => e
+ if arg =~ MULTIPLE_RESOURCE_MATCH
+ begin
+ results = Array.new
+ resource_type = $1
+ arg =~ /^.+\[(.+)\]$/
+ resource_list = $1
+ resource_list.split(",").each do |instance_name|
+ results << lookup(create_key(resource_type, instance_name))
+ end
+ Chef.deprecated(:multiresource_match, "The resource_collection multi-resource syntax is deprecated")
+ return results
+ rescue Chef::Exceptions::ResourceNotFound
+ raise e
+ end
else
- raise ArgumentError, "Bad string format #{arg}, you must have a string like resource_type[name]!"
+ raise e
+ end
end
- results
+
+ if arg =~ NAMELESS_RESOURCE_MATCH
+ resource_type = $1
+ name = ""
+ return [ lookup(create_key(resource_type, name)) ]
+ end
+
+ raise ArgumentError, "Bad string format #{arg}, you must have a string like resource_type[name]!"
end
end
end
diff --git a/spec/integration/recipes/notifies_spec.rb b/spec/integration/recipes/notifies_spec.rb
index 6f6a0f06b0..b008e4ade7 100644
--- a/spec/integration/recipes/notifies_spec.rb
+++ b/spec/integration/recipes/notifies_spec.rb
@@ -362,4 +362,33 @@ EOM
result.error!
end
end
+
+ when_the_repository "has resources that have arrays as the name" do
+ before do
+ directory "cookbooks/x" do
+ file "recipes/default.rb", <<-EOM
+ log [ "a", "b" ] do
+ action :nothing
+ end
+
+ log "doit" do
+ notifies :write, "log[a, b]"
+ end
+ EOM
+ end
+ end
+
+ it "notifying the resource should work" do
+ file "config/client.rb", <<EOM
+local_mode true
+cookbook_path "#{path_to('cookbooks')}"
+log_level :warn
+EOM
+
+ result = shell_out("#{chef_client} -c \"#{path_to('config/client.rb')}\" --no-color -F doc -o 'x::default'", :cwd => chef_dir)
+ expect(result.stdout).to match /\* log\[a, b\] action write/
+ result.error!
+ end
+
+ end
end
diff --git a/spec/unit/resource_collection/resource_set_spec.rb b/spec/unit/resource_collection/resource_set_spec.rb
index ecf3e2707f..9955bc2bea 100644
--- a/spec/unit/resource_collection/resource_set_spec.rb
+++ b/spec/unit/resource_collection/resource_set_spec.rb
@@ -27,6 +27,7 @@ describe Chef::ResourceCollection::ResourceSet do
let(:zen_master) { Chef::Resource::ZenMaster.new(zen_master_name) }
let(:zen_master2) { Chef::Resource::ZenMaster.new(zen_master2_name) }
let(:zen_follower) { Chef::Resource::ZenFollower.new(zen_follower_name) }
+ let(:zen_array) { Chef::Resource::ZenMaster.new( [ zen_master_name, zen_master2_name ]) }
describe "initialize" do
it "should return a Chef::ResourceSet" do
@@ -113,13 +114,27 @@ describe Chef::ResourceCollection::ResourceSet do
end
it "should find resources by strings of zen_master[a,b]" do
+ Chef::Config[:treat_deprecation_warnings_as_errors] = false
collection.insert_as(zen_master)
collection.insert_as(zen_master2)
check_by_names(collection.find("zen_master[#{zen_master_name},#{zen_master2_name}]"),
zen_master_name, zen_master2_name)
end
+ it "should find array names" do
+ collection.insert_as(zen_array)
+ expect(collection.find("zen_master[#{zen_master_name}, #{zen_master2_name}]")).to eql(zen_array)
+ end
+
+ it "should favor array names over multi resource syntax" do
+ collection.insert_as(zen_master)
+ collection.insert_as(zen_master2)
+ collection.insert_as(zen_array)
+ expect(collection.find("zen_master[#{zen_master_name}, #{zen_master2_name}]")).to eql(zen_array)
+ end
+
it "should find resources by strings of zen_master[a,b] with custom names" do
+ Chef::Config[:treat_deprecation_warnings_as_errors] = false
collection.insert_as(zen_master, :zzz, "name1")
collection.insert_as(zen_master2, :zzz, "name2")
check_by_names(collection.find("zzz[name1,name2]"),
@@ -134,6 +149,7 @@ describe Chef::ResourceCollection::ResourceSet do
end
it "should find resources of multiple types by strings of zen_master[a] with custom names" do
+ Chef::Config[:treat_deprecation_warnings_as_errors] = false
collection.insert_as(zen_master, :zzz, "name1")
collection.insert_as(zen_master2, :zzz, "name2")
collection.insert_as(zen_follower, :yyy, "name3")
diff --git a/spec/unit/resource_collection_spec.rb b/spec/unit/resource_collection_spec.rb
index 76038e51b9..c696572b13 100644
--- a/spec/unit/resource_collection_spec.rb
+++ b/spec/unit/resource_collection_spec.rb
@@ -1,7 +1,7 @@
#
# Author:: Adam Jacob (<adam@chef.io>)
# Author:: Christopher Walters (<cw@chef.io>)
-# Copyright:: Copyright 2008-2016, Chef Software Inc.
+# Copyright:: Copyright 2008-2017, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -219,6 +219,7 @@ describe Chef::ResourceCollection do
end
it "should find resources by strings of zen_master[a,b]" do
+ Chef::Config[:treat_deprecation_warnings_as_errors] = false
load_up_resources
results = rc.resources("zen_master[monkey,dog]")
expect(results.length).to eql(2)