From f0b24db7b45c2398f40c7a9baee44b81b972b140 Mon Sep 17 00:00:00 2001 From: John Dewey Date: Thu, 11 Jul 2013 13:10:35 -0700 Subject: [PATCH] Should always return an array I thought search would return an empty array. Apparently it it is not, and not a bad thing to make sure that it always does. http://paste.openstack.org/show/40125/ Change-Id: Ia39a092ffae69f94619ac9c640c1ffcc4d70510f --- CHANGELOG.md | 3 +++ libraries/search.rb | 3 ++- spec/search_spec.rb | 10 ++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23e75638..2602ebe9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ This file is used to list changes made in each version of cookbook-openstack-common. +## 0.3.4: +* Allow `#search_for` to always returns an array. + ## 0.3.3: * Incorrectly mocked search results, as a result `#search_for` was performing unnecessary actions to an array. diff --git a/libraries/search.rb b/libraries/search.rb index a316dc2a..fc8a1e53 100644 --- a/libraries/search.rb +++ b/libraries/search.rb @@ -25,7 +25,8 @@ module ::Openstack def search_for role, &block query = "chef_environment:#{node.chef_environment} AND roles:#{role}" - search(:node, query, &block) + resp = search(:node, query, &block) + resp ? resp : [] end # Returns the value for ["openstack"]["memcached_servers"] when diff --git a/spec/search_spec.rb b/spec/search_spec.rb index 40bac3c9..7bcfe7f4 100644 --- a/spec/search_spec.rb +++ b/spec/search_spec.rb @@ -33,6 +33,16 @@ describe ::Openstack do expect(resp).to eq [] end + + it "always returns empty results" do + @subject.stub(:node).and_return @chef_run.node + @subject.stub(:search). + with(:node, "chef_environment:_default AND roles:empty-role"). + and_return nil + resp = @subject.search_for("empty-role") + + expect(resp).to eq [] + end end describe "#memcached_servers" do