Skip to content

Commit aa9e71e

Browse files
committed
Fixes CHEF-703, making search iterate for you by default without a block
1 parent aaa9dcf commit aa9e71e

4 files changed

Lines changed: 57 additions & 2 deletions

File tree

‎chef/lib/chef/mixin/language.rb‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,18 @@ def platform?(*args)
7979
end
8080

8181
def search(*args, &block)
82-
Chef::Search::Query.new.search(*args, &block)
82+
# If you pass a block, or have at least the start argument, do raw result parsing
83+
#
84+
# Otherwise, do the iteration for the end user
85+
if Kernel.block_given? || args.length >= 4
86+
Chef::Search::Query.new.search(*args, &block)
87+
else
88+
results = Array.new
89+
Chef::Search::Query.new.search(*args) do |o|
90+
results << o
91+
end
92+
results
93+
end
8394
end
8495

8596
def data_bag(bag)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
# Cookbook Name:: search
3+
# Recipe:: default
4+
#
5+
# Copyright 2009, Opscode
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
# We have to sleep at least 10 seconds to confirm that the data has made it
21+
# into the index. We can only rely on this because we are in a test environment
22+
# in real-land Chef, the index is only eventually consistent.. and may take a
23+
# variable amount of time.
24+
25+
26+
sleep 10
27+
objects, start, total = search(:users, "*:*", nil, 0, 10)
28+
29+
objects.each do |entry|
30+
file "#{node[:tmpdir]}/#{entry["id"]}"
31+
end
32+
33+

‎features/data/cookbooks/search/recipes/search_data_noblock.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
sleep 10
27-
objects, start, rows = search(:users, "*:*")
27+
objects = search(:users, "*:*")
2828

2929
objects.each do |entry|
3030
file "#{node[:tmpdir]}/#{entry["id"]}"

‎features/search/search_data.feature‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,14 @@ Feature: Search Data
2626
And a file named 'francis' should exist
2727
And a file named 'axl_rose' should exist
2828

29+
Scenario: Search the user index without a block, with manual paging
30+
Given a validated node
31+
And it includes the recipe 'search::search_data_manual'
32+
And a 'data_bag' named 'users' exists
33+
And a 'data_bag_item' named 'francis' exists
34+
And a 'data_bag_item' named 'axl_rose' exists
35+
When I run the chef-client
36+
Then the run should exit '0'
37+
And a file named 'francis' should exist
38+
And a file named 'axl_rose' should exist
39+

0 commit comments

Comments
 (0)