Continued work on rabbit host methods

Did not increase the version, b/c 1.3.0 is still under development.
We should not migrate to this version, until we complete this work.
We are using the new ["openstack"]["mq"] attributes.  It is up
to the operator to install rabbit, and make these ["openstack"]["mq"]
attributes available.
* The #rabbit_servers method will return the ["openstack"]["mq"]["servers"]
  value if defined.  Otherwise, it will perform a search, and construct
  a comma-delimited list of servers.
* The #rabbit_server method will return the ["openstack"]["mq"]["server"]
  value if defined.  Otherwise, will perform a search, and return the first
  rabbit server found.

Change-Id: Ia3771d70ea7eeb367b1fa17c5258f719528b5d22
This commit is contained in:
John Dewey
2013-06-21 21:30:29 -07:00
parent 0c771cfab3
commit 294e65458f
3 changed files with 96 additions and 50 deletions

View File

@@ -3,7 +3,10 @@
This file is used to list changes made in each version of cookbook-openstack-common.
## 0.3.0:
* Added `#rabbit_servers` method, which returns a list of rabbit servers.
* Added `#rabbit_servers` method, which returns a comma-delimited string of rabbit
servers in the format of host:port.
* Added `#rabbit_server` method, which returns a single rabbit server in the format
of host:port.
* The `#memcached_servers` method no longer accepts an environment.
* Re-factored methods which search to a generic `#search_for`.
* Added `#address_for` method, which returns the IPv4 (default) address of the given

View File

@@ -28,9 +28,8 @@ module ::Openstack
search(:node, query, &block).first
end
# Search for memcache servers. Will return the value for
# ["openstack"]["memcached_servers"] when set, otherwise
# will perform the search.
# Returns the value for ["openstack"]["memcached_servers"] when
# set, otherwise will perform a search.
#
# @param [String] role The role to be found (optional).
# @return [Array] A list of memcached servers in format
@@ -49,17 +48,46 @@ module ::Openstack
end
end
# Search for rabbit servers.
# Returns all rabbit servers.
# Uses the value for ["openstack"]["mq"]["servers"] when set, otherwise
# will perform a search.
#
# @param [String] role The role to be found (optional).
# @return [Array] A list of rabbit servers in format
# '<ip>:<port>'.
def rabbit_servers role="infra-messaging"
search_for(role).map do |n|
address = n['rabbitmq']['address']
port = n['rabbitmq']['port'] || "5672"
# @return [String] Rabbit servers joined by a comma in
# the format of '<ip>:<port>'.
def rabbit_servers
if node["openstack"]["mq"]["servers"]
servers = node["openstack"]["mq"]["servers"]
port = node["openstack"]["mq"]["port"]
"#{address}:#{port}"
end.sort
servers.map { |s| "#{s}:#{port}" }.join ","
else
role = node["openstack"]["mq"]["server_role"]
search_for(role).map do |n|
# The listen attribute should be saved to the node
# in the wrapper cookbook. See the reference cookbook
# openstack-ops-messaging.
address = n["openstack"]["mq"]["listen"]
port = n["openstack"]["mq"]["port"]
"#{address}:#{port}"
end.sort.join ","
end
end
# Returns a single rabbit server.
# Uses the value for ["openstack"]["mq"]["host"] when set, otherwise
# will perform a search.
#
# @return [String] Rabbit server in the format of '<ip>:<port>'.
def rabbit_server
if node["openstack"]["mq"]["host"]
host = node["openstack"]["mq"]["host"]
port = node["openstack"]["mq"]["port"]
"#{host}:#{port}"
else
node.override["openstack"]["mq"]["servers"] = nil # safe
rabbit_servers.split(",")[0]
end
end
end

View File

@@ -3,7 +3,12 @@ require ::File.join ::File.dirname(__FILE__), "..", "libraries", "search"
describe ::Openstack do
before do
@chef_run = ::ChefSpec::ChefRunner.new ::CHEFSPEC_OPTS
@chef_run = ::ChefSpec::ChefRunner.new(::CHEFSPEC_OPTS) do |n|
n.set["openstack"]["mq"] = {
"server_role" => "openstack-ops-mq",
"port" => 5672
}
end
@chef_run.converge "openstack-common::default"
@subject = ::Object.new.extend ::Openstack
end
@@ -60,19 +65,6 @@ describe ::Openstack do
expect(resp).to eq ["1.1.1.1:11211", "2.2.2.2:11211", "3.3.3.3:11211"]
end
it "adds default memcached port when missing" do
nodes = [
{ "memcached" => { "listen" => "1.1.1.1" }},
]
@subject.stub(:node).and_return @chef_run.node
@subject.stub(:search_for).
with("role").
and_return nodes
resp = @subject.memcached_servers("role")
expect(resp[0]).to eq "1.1.1.1:11211"
end
it "returns memcached servers as defined by attributes" do
nodes = {
"openstack" => {
@@ -99,46 +91,69 @@ describe ::Openstack do
end
describe "#rabbit_servers" do
it "returns rabbit list" do
it "returns rabbit servers" do
nodes = [
{ "rabbitmq" => { "address" => "1.1.1.1", "port" => "5672" }},
{ "rabbitmq" => { "address" => "2.2.2.2", "port" => "5672" }}
{ "openstack" => { "mq" => { "listen" => "1.1.1.1", "port" => "5672" }}},
{ "openstack" => { "mq" => { "listen" => "2.2.2.2", "port" => "5672" }}},
]
@subject.stub(:node).and_return @chef_run.node
@subject.stub(:search_for).
with("role").
and_return nodes
resp = @subject.rabbit_servers("role")
resp = @subject.rabbit_servers
expect(resp).to eq ["1.1.1.1:5672", "2.2.2.2:5672"]
expect(resp).to eq "1.1.1.1:5672,2.2.2.2:5672"
end
it "returns sorted rabbit list" do
it "returns sorted rabbit servers" do
nodes = [
{ "rabbitmq" => { "address" => "3.3.3.3", "port" => "5672" }},
{ "rabbitmq" => { "address" => "1.1.1.1", "port" => "5672" }},
{ "rabbitmq" => { "address" => "2.2.2.2", "port" => "5672" }}
{ "openstack" => { "mq" => { "listen" => "3.3.3.3", "port" => "5672" }}},
{ "openstack" => { "mq" => { "listen" => "1.1.1.1", "port" => "5672" }}},
{ "openstack" => { "mq" => { "listen" => "2.2.2.2", "port" => "5672" }}}
]
@subject.stub(:node).and_return @chef_run.node
@subject.stub(:search_for).
with("role").
and_return nodes
resp = @subject.rabbit_servers("role")
resp = @subject.rabbit_servers
expect(resp).to eq ["1.1.1.1:5672", "2.2.2.2:5672", "3.3.3.3:5672"]
expect(resp).to eq "1.1.1.1:5672,2.2.2.2:5672,3.3.3.3:5672"
end
it "adds default rabbit port when missing" do
nodes = [
{ "rabbitmq" => { "address" => "1.1.1.1" }}
]
it "returns rabbit servers when not searching" do
node = @chef_run.node
node.set["openstack"]["mq"]["servers"] = ["1.1.1.1", "2.2.2.2"]
@subject.stub(:node).and_return @chef_run.node
@subject.stub(:search_for).
with("role").
and_return nodes
resp = @subject.rabbit_servers("role")
resp = @subject.rabbit_servers
expect(resp[0]).to eq "1.1.1.1:5672"
expect(resp).to eq "1.1.1.1:5672,2.2.2.2:5672"
end
end
describe "#rabbit_server" do
it "returns a rabbit server" do
@subject.stub(:node).and_return @chef_run.node
@subject.stub(:rabbit_servers).
and_return "1.1.1.1:5672"
resp = @subject.rabbit_server
expect(resp).to eq "1.1.1.1:5672"
end
it "returns a rabbit server when multiple servers returned" do
@subject.stub(:node).and_return @chef_run.node
@subject.stub(:rabbit_servers).
and_return "1.1.1.1:5672,2.2.2.2:5672"
resp = @subject.rabbit_server
expect(resp).to eq "1.1.1.1:5672"
end
it "returns a rabbit server when not searching" do
node = @chef_run.node
node.set["openstack"]["mq"]["host"] = "1.1.1.1"
@subject.stub(:node).and_return @chef_run.node
resp = @subject.rabbit_server
expect(resp).to eq "1.1.1.1:5672"
end
end
end