Bit of cleanup in the tests and library

Tend to avoid () where necessary when writing ruby.
I know a nit pick.
This commit is contained in:
John Dewey
2012-11-20 16:32:30 -08:00
parent 0f5ef59f5f
commit bf7822b391
2 changed files with 42 additions and 41 deletions

View File

@@ -20,7 +20,7 @@
module Openstack
# Instead of specifying the verbose node["openstack"]["endpoints"][name],
# this shortcut allows the simpler and shorter endpoint(name)
def endpoint(name)
def endpoint name
@node['openstack']['endpoints'][name]
rescue
nil
@@ -30,7 +30,7 @@ module Openstack
# set in the endpoint hash, we use the Openstack::get_uri_from_mash
# library routine from the openstack-utils cookbook to grab a URI object
# and construct the URI object from the endpoint parts.
def endpoint_uri(name)
def endpoint_uri name
ep = endpoint(name)
if ep && ep.has_key?("uri")
ep["uri"]
@@ -40,7 +40,7 @@ module Openstack
end
# Useful for iterating over the OpenStack endpoints
def endpoints(&block)
def endpoints &block
@node['openstack']['endpoints'].each do | name, info |
block.call(name, info)
end
@@ -52,27 +52,27 @@ module Openstack
# this shortcut allows the simpler and shorter db(service), where
# service is one of 'compute', 'image', 'identity', 'network',
# and 'volume'
def db(service)
def db service
@node['openstack']['db'][service]
rescue
nil
end
# Shortcut to get the SQLAlchemy DB URI for a named service
def db_uri(service, user, pass)
def db_uri service, user, pass
info = db(service)
if info
host = info['host']
port = info['port'].to_s
type = info['db_type']
name = info['db_name']
if type == 'postgresql'
if type == "postgresql"
# Normalize to the SQLAlchemy standard db type identifier
type = 'pgsql'
type = "pgsql"
end
if type == 'mysql' or type == 'pgsql'
if type == "mysql" or type == "pgsql"
result = "#{type}://#{user}:#{pass}@#{host}:#{port}/#{name}"
elsif type == 'sqlite'
elsif type == "sqlite"
# SQLite uses filepaths not db name
path = info['path']
result = "sqlite://#{path}"
@@ -88,28 +88,28 @@ module Openstack
# underlying database cookbooks. For instance, if a MySQL database
# is used, the node["mysql"]["server_root_password"] is used along
# with the "root" (super)user.
def db_create_with_user(service, user, pass)
info = db(service)
def db_create_with_user service, user, pass
info = db service
if info
host = info['host']
port = info['port'].to_s
type = info['db_type']
db_name = info['db_name']
case type
when 'postgresql', 'pgsql'
when "postgresql", "pgsql"
db_prov = Chef::Provider::Database::Postgresql
user_prov = Chef::Provider::Database::PostgresqlUser
# See https://github.com/opscode-cookbooks/postgresql/blob/master/recipes/server.rb#L41
super_user = 'postgres'
super_user = "postgres"
super_password = @node['postgresql']['password']['postgres']
when 'mysql'
when "mysql"
db_prov = Chef::Provider::Database::Mysql
user_prov = Chef::Provider::Database::MysqlUser
# See https://github.com/opscode-cookbooks/mysql/blob/master/recipes/server.rb#L40
super_user = 'root'
super_user = "root"
# For some reason, setting this to anything other than localhost fails miserably :(
host = 'localhost'
host = "localhost"
super_password = @node['mysql']['server_root_password']
else
Chef::Log.error("Unsupported database type #{type}")

View File

@@ -5,22 +5,23 @@ describe ::Openstack do
before do
@chef_run = ::ChefSpec::ChefRunner.new do |n|
n.set['mysql'] = {}
n.set['mysql']['server_root_password'] = "pass"
end.converge "openstack-common::default"
n.set['mysql']['server_root_password'] = "password"
end
@chef_run.converge "openstack-common::default"
@subject = ::Object.new.extend(::Openstack)
end
describe "#endpoint" do
it "returns nil when no openstack.endpoints not in node attrs" do
@subject.instance_variable_set(:@node, {})
@subject.instance_variable_set :@node, {}
@subject.endpoint("nonexisting").should be_nil
end
it "returns nil when no such endpoint was found" do
@subject.instance_variable_set(:@node, @chef_run.node)
@subject.instance_variable_set :@node, @chef_run.node
@subject.endpoint("nonexisting").should be_nil
end
it "returns endpoint hash when found" do
@subject.instance_variable_set(:@node, @chef_run.node)
@subject.instance_variable_set :@node, @chef_run.node
@subject.endpoint("compute-api")['host'].should == "127.0.0.1"
@subject.endpoint("compute-api").has_key?("uri").should be_false
end
@@ -28,11 +29,11 @@ describe ::Openstack do
describe "#endpoint_uri" do
it "returns nil when no openstack.endpoints not in node attrs" do
@subject.instance_variable_set(:@node, {})
@subject.instance_variable_set :@node, {}
@subject.endpoint_uri("nonexisting").should be_nil
end
it "returns nil when no such endpoint was found" do
@subject.instance_variable_set(:@node, @chef_run.node)
@subject.instance_variable_set :@node, @chef_run.node
@subject.endpoint_uri("nonexisting").should be_nil
end
it "returns endpoint URI string when uri key in endpoint hash" do
@@ -45,7 +46,7 @@ describe ::Openstack do
}
}
}
@subject.instance_variable_set(:@node, uri_hash)
@subject.instance_variable_set :@node, uri_hash
@subject.endpoint_uri("compute-api").should eq "http://localhost"
end
it "returns endpoint URI string when uri key in endpoint hash and host also in hash" do
@@ -59,7 +60,7 @@ describe ::Openstack do
}
}
}
@subject.instance_variable_set(:@node, uri_hash)
@subject.instance_variable_set :@node, uri_hash
@subject.endpoint_uri("compute-api").should eq "http://localhost"
end
it "returns endpoint URI string when uri key not in endpoint hash but host is in hash" do
@@ -73,18 +74,18 @@ describe ::Openstack do
}
}
::Openstack.stub(:uri_from_hash).and_return "http://localhost"
@subject.instance_variable_set(:@node, uri_hash)
@subject.instance_variable_set :@node, uri_hash
@subject.endpoint_uri("compute-api").should eq "http://localhost"
end
end
describe "#endpoints" do
it "does nothing when no endpoints" do
@subject.instance_variable_set(:@node, {})
@subject.instance_variable_set :@node, {}
@subject.endpoints.should be_nil
end
it "does nothing when empty endpoints" do
@subject.instance_variable_set(:@node, {"openstack" => { "endpoints" => {}}})
@subject.instance_variable_set :@node, {"openstack" => { "endpoints" => {}}}
@count = 0
@subject.endpoints do | ep |
@count += 1
@@ -92,9 +93,9 @@ describe ::Openstack do
@count.should eq 0
end
it "executes block count when have endpoints" do
@subject.instance_variable_set(:@node, @chef_run.node)
@subject.instance_variable_set :@node, @chef_run.node
@count = 0
@subject.endpoints do | ep |
@subject.endpoints do |ep|
@count += 1
end
@count.should >= 1
@@ -103,15 +104,15 @@ describe ::Openstack do
describe "#db" do
it "returns nil when no openstack.db not in node attrs" do
@subject.instance_variable_set(:@node, {})
@subject.instance_variable_set :@node, {}
@subject.db("nonexisting").should be_nil
end
it "returns nil when no such service was found" do
@subject.instance_variable_set(:@node, @chef_run.node)
@subject.instance_variable_set :@node, @chef_run.node
@subject.db("nonexisting").should be_nil
end
it "returns db info hash when service found" do
@subject.instance_variable_set(:@node, @chef_run.node)
@subject.instance_variable_set :@node, @chef_run.node
@subject.db("compute")['host'].should == "127.0.0.1"
@subject.db("compute").has_key?("uri").should be_false
end
@@ -119,15 +120,15 @@ describe ::Openstack do
describe "#db_uri" do
it "returns nil when no openstack.db not in node attrs" do
@subject.instance_variable_set(:@node, {})
@subject.instance_variable_set :@node, {}
@subject.db_uri("nonexisting", "user", "pass").should be_nil
end
it "returns nil when no such service was found" do
@subject.instance_variable_set(:@node, @chef_run.node)
@subject.instance_variable_set :@node, @chef_run.node
@subject.db_uri("nonexisting", "user", "pass").should be_nil
end
it "returns db info hash when service found" do
@subject.instance_variable_set(:@node, @chef_run.node)
@subject.instance_variable_set :@node, @chef_run.node
expect = "mysql://user:pass@127.0.0.1:3306/nova"
@subject.db_uri("compute", "user", "pass").should eq expect
end
@@ -135,17 +136,17 @@ describe ::Openstack do
describe "#db_create_with_user" do
it "returns nil when no openstack.db not in node attrs" do
@subject.instance_variable_set(:@node, {})
@subject.instance_variable_set :@node, {}
@subject.db_create_with_user("nonexisting", "user", "pass").should be_nil
end
it "returns nil when no such service was found" do
@subject.instance_variable_set(:@node, @chef_run.node)
@subject.instance_variable_set :@node, @chef_run.node
@subject.db_create_with_user("nonexisting", "user", "pass").should be_nil
end
it "returns db info and creates database with user when service found" do
@subject.stub(:database).and_return(Hash.new)
@subject.stub(:database_user).and_return(Hash.new)
@subject.instance_variable_set(:@node, @chef_run.node)
@subject.stub(:database).and_return {}
@subject.stub(:database_user).and_return {}
@subject.instance_variable_set :@node, @chef_run.node
result = @subject.db_create_with_user("compute", "user", "pass")
result['host'].should eq "127.0.0.1"
result['port'].should eq "3306"