cookbook-openstack-common/spec/endpoints_spec.rb
Samuel Cassiba 51ac5f8b9f Style and lint fixes to support newer ChefDK
- bumps ChefDK release to 0.15.15 in bootstrap

Change-Id: I6ce4587caa3ae68ddbd3ef1a521aaf46f4840b2c
2016-07-01 18:16:19 -07:00

205 lines
7.5 KiB
Ruby

# encoding: UTF-8
require_relative 'spec_helper'
require ::File.join ::File.dirname(__FILE__), '..', 'libraries', 'uri'
require ::File.join ::File.dirname(__FILE__), '..', 'libraries', 'endpoints'
describe 'openstack-common::default' do
describe 'Openstack endpoints' do
let(:runner) { ChefSpec::SoloRunner.new(CHEFSPEC_OPTS) }
let(:node) { runner.node }
let(:chef_run) { runner.converge(described_recipe) }
let(:subject) { Object.new.extend(Openstack) }
%w(public internal admin).each do |ep_type|
describe "#{ep_type}_endpoint" do
it 'fails with a NoMethodError when no openstack.endpoints in node attrs' do
allow(subject).to receive(:node).and_return({})
expect do
subject.send("#{ep_type}_endpoint", 'someservice')
end.to raise_error(NoMethodError)
end
it 'fails with a NoMethodError when no endpoint was found' do
allow(subject).to receive(:node).and_return(node)
expect do
subject.send("#{ep_type}_endpoint", 'someservice')
end.to raise_error(NoMethodError)
end
it 'handles a URI needing escaped' do
uri_hash = {
'openstack' => {
'endpoints' => {
ep_type => {
'compute-api' => {
'uri' => 'http://localhost:8080/v2/%(tenant_id)s'
}
}
}
}
}
allow(subject).to receive(:node).and_return(uri_hash)
expect(
subject.send("#{ep_type}_endpoint", 'compute-api').path
).to eq('/v2/%25(tenant_id)s')
end
it 'returns endpoint URI object when uri key in endpoint hash' do
uri_hash = {
'openstack' => {
'endpoints' => {
ep_type => {
'compute-api' => {
'uri' => 'http://localhost:1234/path'
}
}
}
}
}
allow(subject).to receive(:node).and_return(uri_hash)
expect(
subject.send("#{ep_type}_endpoint", 'compute-api').port
).to eq(1234)
end
it 'returns endpoint URI string when uri key in endpoint hash and host also in hash' do
uri_hash = {
'openstack' => {
'endpoints' => {
ep_type => {
'compute-api' => {
'uri' => 'http://localhost',
'host' => 'ignored'
}
}
}
}
}
allow(subject).to receive(:node).and_return(uri_hash)
expect(subject.send("#{ep_type}_endpoint", 'compute-api').to_s).to eq('http://localhost')
end
it 'returns endpoint URI object when uri key not in endpoint hash but host is in hash' do
expect(subject).to receive(:uri_from_hash).with('host' => 'localhost', 'port' => '1234')
uri_hash = {
'openstack' => {
'endpoints' => {
ep_type => {
'compute-api' => {
'host' => 'localhost',
'port' => '1234'
}
}
}
}
}
allow(subject).to receive(:node).and_return(uri_hash)
subject.send("#{ep_type}_endpoint", 'compute-api')
end
end
end
describe '#db' do
it 'returns nil when no openstack.db not in node attrs' do
allow(subject).to receive(:node).and_return({})
expect(subject.db('nonexisting')).to be_nil
end
it 'returns nil when no such service was found' do
allow(subject).to receive(:node).and_return(chef_run.node)
expect(subject.db('nonexisting')).to be_nil
end
it 'returns db info hash when service found' do
allow(subject).to receive(:node).and_return(chef_run.node)
expect(subject.db('compute')['host']).to eq('127.0.0.1')
expect(subject.db('compute').key?('uri')).to be_falsey
end
end
describe '#db_uri' do
it 'returns nil when no openstack.db not in node attrs' do
allow(subject).to receive(:node).and_return({})
expect(subject.db_uri('nonexisting', 'user', 'pass')).to be_nil
end
it 'returns nil when no such service was found' do
allow(subject).to receive(:node).and_return(chef_run.node)
expect(
subject.db_uri('nonexisting', 'user', 'pass')
).to be_nil
end
it 'returns compute db info hash when service found for default mysql' do
allow(subject).to receive(:node).and_return(chef_run.node)
expected = 'mysql://user:pass@127.0.0.1:3306/nova?charset=utf8'
expect(
subject.db_uri('compute', 'user', 'pass')
).to eq(expected)
end
it 'returns network db info hash when service found for sqlite with options' do
node.set['openstack']['db']['service_type'] = 'sqlite'
node.set['openstack']['db']['options'] = { 'sqlite' => '?options' }
node.set['openstack']['db']['network']['path'] = 'path'
allow(subject).to receive(:node).and_return(chef_run.node)
expected = 'sqlite:///path?options'
expect(
subject.db_uri('network', 'user', 'pass')
).to eq(expected)
end
it 'returns compute db info hash when service found for mariadb' do
node.set['openstack']['db']['service_type'] = 'mariadb'
allow(subject).to receive(:node).and_return(chef_run.node)
expected = 'mysql://user:pass@127.0.0.1:3306/nova?charset=utf8'
expect(
subject.db_uri('compute', 'user', 'pass')
).to eq(expected)
end
%w(galera percona-cluster).each do |db|
it "returns compute db info hash when service found for #{db}" do
node.set['openstack']['db']['service_type'] = db
allow(subject).to receive(:node).and_return(chef_run.node)
expected = 'mysql://user:pass@127.0.0.1:3306/nova?charset=utf8'
expect(
subject.db_uri('compute', 'user', 'pass')
).to eq(expected)
end
end
it 'returns compute slave db info hash when service found for default mysql' do
node.set['openstack']['endpoints']['db']['enabled_slave'] = true
allow(subject).to receive(:node).and_return(chef_run.node)
expected = 'mysql://user:pass@127.0.0.1:3316/nova?charset=utf8'
expect(
subject.db_uri('compute', 'user', 'pass', true)
).to eq(expected)
end
it 'returns image slave db info hash when service found for mariadb' do
node.set['openstack']['db']['service_type'] = 'mariadb'
node.set['openstack']['endpoints']['db']['enabled_slave'] = true
allow(subject).to receive(:node).and_return(chef_run.node)
expected = 'mysql://user:pass@127.0.0.1:3316/glance?charset=utf8'
expect(
subject.db_uri('image', 'user', 'pass', true)
).to eq(expected)
end
%w(galera percona-cluster).each do |db|
it "returns network slave db info hash when service found for #{db}" do
node.set['openstack']['db']['service_type'] = db
node.set['openstack']['endpoints']['db']['enabled_slave'] = true
allow(subject).to receive(:node).and_return(chef_run.node)
expected = 'mysql://user:pass@127.0.0.1:3316/neutron?charset=utf8'
expect(
subject.db_uri('network', 'user', 'pass', true)
).to eq(expected)
end
end
end
end
end