Skip to content

Commit

Permalink
Move OpenStack-specific test to its own file
Browse files Browse the repository at this point in the history
The tests for service included OpenStack-aware logic. This moves
that out of the class and into its own test.

Change-Id: Ie130811fa3785a782f51b0300c7a24387441dd5a
  • Loading branch information
relaxdiego committed Jul 10, 2014
1 parent 095b201 commit fd7f489
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 71 deletions.
1 change: 1 addition & 0 deletions lib/aviator/openstack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Openstack
class << self

def find_request(service, name, session_data, options)
service = service.to_s
endpoint_type = options[:endpoint_type]
endpoint_types = if endpoint_type
[endpoint_type.to_s.camelize]
Expand Down
82 changes: 11 additions & 71 deletions test/aviator/core/service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,56 +51,6 @@ def service(default_session_data=nil)
end


it 'can find the correct request if api version is not defined but can be inferred from host_uri' do
request_name = config[:auth_service][:request].to_sym

bootstrap = {
:auth_service => {
:name => 'identity',
:host_uri => 'http://devstack:5000/v2.0',
:request => 'create_token'
}
}

response = service.request request_name, :session_data => bootstrap do |params|
config[:auth_credentials].each do |k,v|
params[k] = v
end
end

response.must_be_instance_of Aviator::Response
response.request.api_version.must_equal :v2
response.status.must_equal 200
end


it 'raises an error if session data does not have the endpoint information' do
request_name = config[:auth_service][:request].to_sym

bootstrap = Hashish.new(JSON.parse('{"access": {"token": {"issued_at": "2013-09-25T20:21:55.453783",
"expires": "2013-09-26T02:21:55Z", "id": "2f6bdec6cd0f49b4a60ede0cd4bf2c0d"},
"serviceCatalog": [], "user": {"username": "bogus",
"roles_links": [], "id": "447527294dae4a1788d36beb0db99c00", "roles": [],
"name": "bogus"}, "metadata": {"is_admin": 0, "roles":
[]}}}'))

s = service(bootstrap)

the_method = lambda { s.request request_name }

the_method.must_raise Aviator::Service::MissingServiceEndpointError
end


it 'can find the correct request based on non-bootstrapped session data' do
session_data = do_auth_request.body

response = service.request :list_tenants, :session_data => session_data

response.status.must_equal 200
end


it 'uses the default session data if session data is not provided' do
default_session_data = do_auth_request.body
s = service(default_session_data)
Expand All @@ -121,35 +71,25 @@ def service(default_session_data=nil)
error.message.wont_be_nil
end


it 'accepts an endpoint type option for selecting a specific request' do
default_session_data = do_auth_request.body
s = service(default_session_data)

response1 = s.request :list_tenants, :endpoint_type => 'admin'
response2 = s.request :list_tenants, :endpoint_type => 'public'

response1.request.url.wont_equal response2.request.url
end

end


describe '#request_classes' do

it 'returns an array of the request classes' do
provider_name = config[:provider]
service_name = config[:auth_service][:name]
service_path = Pathname.new(__FILE__).join(
'..', '..', '..', '..', 'lib', 'aviator', provider_name, service_name
).expand_path

request_files = Pathname.glob(service_path.join('**', '*.rb')) \
.map{|rf| rf.to_s.match(/#{provider_name}\/#{service_name}\/([\w\/]+)\.rb$/) } \
provider_name = config[:provider]
service_name = config[:auth_service][:name]
provider_module = "Aviator::#{ provider_name.camelize }".constantize

request_file_paths = provider_module.request_file_paths(service_name)
request_file_paths.each{ |path| require path }

constant_parts = request_file_paths \
.map{|rf| rf.to_s.match(/#{ provider_name }\/#{ service_name }\/([\w\/]+)\.rb$/) } \
.map{|rf| rf[1].split('/').map{|c| c.camelize }.join('::') }

classes = request_files.map do |rf|
"Aviator::#{provider_name.camelize}::#{service_name.camelize}::#{rf}".constantize
classes = constant_parts.map do |cp|
"Aviator::#{ provider_name.camelize }::#{ service_name.camelize }::#{ cp }".constantize
end

service.request_classes.must_equal classes
Expand Down
127 changes: 127 additions & 0 deletions test/aviator/openstack_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
require 'test_helper'

class Aviator::Test

describe 'aviator/openstack_rb' do

def config
Environment.openstack_admin
end


def do_auth_request
request_name = config[:auth_service][:request].to_sym

bootstrap = {
:auth_service => config[:auth_service]
}

load_service.request request_name, :session_data => bootstrap do |params|
config[:auth_credentials].each do |k,v|
params[k] = v
end
end
end


def load_service(default_session_data=nil)
options = {
:provider => config[:provider],
:service => config[:auth_service][:name]
}

options[:default_session_data] = default_session_data unless default_session_data.nil?

Aviator::Service.new(options)
end


def modyul
Aviator::Openstack
end


describe '#find_request' do

it 'can find the correct request class if api version is not defined but can be inferred from host_uri' do
load_service

bootstrap = {
:auth_service => {
:name => 'identity',
:host_uri => 'http://devstack:5000/v2.0',
:request => 'create_token'
}
}

request_class = modyul.find_request(
bootstrap[:auth_service][:name],
bootstrap[:auth_service][:request],
bootstrap,
{}
)
api_version = bootstrap[:auth_service][:host_uri].match(/(v\d+)\.?\d*/)[1].to_sym

request_class.api_version.must_equal api_version
end


it 'raises an error if session data does not have the endpoint information' do
load_service
request_name = config[:auth_service][:request].to_sym

bootstrap = Hashish.new(JSON.parse('{"access": {"token": {"issued_at": "2013-09-25T20:21:55.453783",
"expires": "2013-09-26T02:21:55Z", "id": "2f6bdec6cd0f49b4a60ede0cd4bf2c0d"},
"serviceCatalog": [], "user": {"username": "bogus",
"roles_links": [], "id": "447527294dae4a1788d36beb0db99c00", "roles": [],
"name": "bogus"}, "metadata": {"is_admin": 0, "roles":
[]}}}'))

the_method = lambda { modyul.find_request('identity', request_name, bootstrap, {}) }

the_method.must_raise Aviator::Service::MissingServiceEndpointError
end


it 'can find the correct request based on non-bootstrapped session data' do
session_data = do_auth_request.body
service_name = :identity
request_name = :list_tenants
service_spec = session_data[:access][:serviceCatalog].find{|s| s[:type] == service_name.to_s }
version = service_spec[:endpoints][0][:publicURL].match(/(v\d+)\.?\d*/)[1].to_sym

request = modyul.find_request(service_name, request_name, session_data, {})

request.service.must_equal service_name
request.api_version.must_equal version
end


it 'accepts an endpoint type option for selecting a specific request' do
load_service

bootstrap = {
:auth_service => config[:auth_service]
}

request1 = modyul.find_request(
:identity,
:list_tenants,
bootstrap,
{ :endpoint_type => :admin }
)
request2 = modyul.find_request(
:identity,
:list_tenants,
bootstrap,
{ :endpoint_type => :public }
)

request1.wont_equal request2
end

end

end

end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fd7f489

Please sign in to comment.