From 09262b439bf20764264c6404c2d5415ceaea1ffc Mon Sep 17 00:00:00 2001 From: Mark Maglana Date: Mon, 26 Aug 2013 18:26:18 -0700 Subject: [PATCH] Rewrite test for create_tenant --- lib/aviator/core/service.rb | 9 +- .../identity/v2/admin/create_tenant_test.rb | 140 +++++++++++++----- ...ow_to_extract_and_use_the_session_data.yml | 97 ------------ test/support/openstack_request_test_helper.rb | 50 +++++++ test/test_helper.rb | 3 + 5 files changed, 159 insertions(+), 140 deletions(-) delete mode 100644 test/cassettes/openstack/identity/v2/admin/create_tenant/i_request/knows_how_to_extract_and_use_the_session_data.yml create mode 100644 test/support/openstack_request_test_helper.rb diff --git a/lib/aviator/core/service.rb b/lib/aviator/core/service.rb index 921104e..2adaf1a 100644 --- a/lib/aviator/core/service.rb +++ b/lib/aviator/core/service.rb @@ -37,14 +37,17 @@ module Aviator # This method gets called by the request file eval'd in self.build below def define_request(request_name, &block) klass = Class.new(Aviator::Request, &block) - return request_name, klass + return klass, request_name end def self.build(path_to_request_file) - clean_room = self.new + clean_room = new clean_room.instance_eval(File.read(path_to_request_file)) end + + + private_class_method :new end @@ -128,7 +131,7 @@ module Aviator @requests ||= {} request_file_paths.each do |path_to_file| - request_name, klass = RequestBuilder.build(path_to_file) + klass, request_name = RequestBuilder.build(path_to_file) api_version = @requests[klass.api_version] ||= {} endpoint_type = api_version[klass.endpoint_type] ||= {} diff --git a/test/aviator/openstack/identity/v2/admin/create_tenant_test.rb b/test/aviator/openstack/identity/v2/admin/create_tenant_test.rb index 1d6b642..e8b01f3 100644 --- a/test/aviator/openstack/identity/v2/admin/create_tenant_test.rb +++ b/test/aviator/openstack/identity/v2/admin/create_tenant_test.rb @@ -3,58 +3,118 @@ require 'test_helper' class Aviator::Test describe 'aviator/openstack/identity/v2/admin/create_tenant' do - - def klass - Aviator::Service - end - - def do_auth_request - config = Environment.admin - - service = klass.new( - provider: config[:provider], - service: config[:auth_service][:name] - ) - - request_name = config[:auth_service][:request].to_sym - - bootstrap = { - auth_service: config[:auth_service] - } - - service.request request_name, bootstrap do |params| - config[:auth_credentials].each do |k,v| - params[k] = v - end + def create_request + klass.new(helper.admin_session_data) do |params| + params[:name] = 'Project' + params[:description] = 'My Project' + params[:enabled] = true end end - - def service - klass.new( - provider: 'openstack', - service: 'identity' - ) + + def helper + Aviator::Test::RequestHelper end - describe '#request' do - - it 'knows how to extract and use the session data' do - session_data = do_auth_request.body + def klass + path = helper.request_path('identity', 'v2', 'admin', 'create_tenant.rb') + klass, request_name = Aviator::Service::RequestBuilder.build(path) + klass + end + + + describe '::endpoint_type' do + + it 'returns :admin' do + klass.endpoint_type.must_equal :admin + end + + end + + + describe '::api_version' do + + it 'returns :v2' do + klass.api_version.must_equal :v2 + end + + end - response = service.request :create_tenant, session_data do |params| - params.name = 'Test Project' - params.description = 'This is a test' - params.enabled = true - end - - response.status.must_equal 200 + + describe '::http_method' do + + it 'returns :post' do + klass.http_method.must_equal :post end end + + describe '::required_params' do + + it 'returns the list of required params' do + klass.required_params.must_equal [:name, :description, :enabled] + end + + end + + + describe '#url' do + + it 'returns the correct value' do + session_data = helper.admin_session_data + service_spec = session_data[:access][:serviceCatalog].find{|s| s[:type] == 'identity' } + url = "#{ service_spec[:endpoints][0][:adminURL] }/tenants" + + request = create_request + + request.url.must_equal url + end + + end + + + describe '#headers' do + + it 'returns valid headers' do + headers = { + 'X-Auth-Token' => helper.admin_session_data[:access][:token][:id] + } + + request = create_request + + request.headers.must_equal headers + end + + end + + + describe '#body' do + + it 'returns a valid body' do + params = { + name: 'Project', + description: 'My Project', + enabled: true + } + + body = { + tenant: params + } + + request = klass.new(helper.admin_session_data) do |p| + params.each do |k,v| + p[k] = params[k] + end + end + + request.body.must_equal body + end + + end + end end \ No newline at end of file diff --git a/test/cassettes/openstack/identity/v2/admin/create_tenant/i_request/knows_how_to_extract_and_use_the_session_data.yml b/test/cassettes/openstack/identity/v2/admin/create_tenant/i_request/knows_how_to_extract_and_use_the_session_data.yml deleted file mode 100644 index dd7fe05..0000000 --- a/test/cassettes/openstack/identity/v2/admin/create_tenant/i_request/knows_how_to_extract_and_use_the_session_data.yml +++ /dev/null @@ -1,97 +0,0 @@ ---- -http_interactions: -- request: - method: post - uri: :5000/v2.0/tokens - body: - encoding: UTF-8 - string: ! '{"auth":{"passwordCredentials":{"username":"admin","password":""},"tenantName":"admin"}}' - headers: - Content-Type: - - application/json - User-Agent: - - Faraday v0.8.8 - response: - status: - code: 200 - message: - headers: - vary: - - X-Auth-Token - content-type: - - application/json - content-length: - - '2648' - date: - - Mon, 26 Aug 2013 22:27:13 GMT - connection: - - close - body: - encoding: US-ASCII - string: ! '{"access": {"token": {"issued_at": "2013-08-26T22:27:13.886315", - "expires": "2013-08-27T22:27:13Z", "id": "3396443734194600ba8b976415fc8b7a", - "tenant": {"description": null, "enabled": true, "id": "3cab25130620477b8b03f1bfa8741603", - "name": "admin"}}, "serviceCatalog": [{"endpoints": [{"adminURL": "http://192.168.56.11:8774/v2/3cab25130620477b8b03f1bfa8741603", - "region": "RegionOne", "internalURL": "http://192.168.56.11:8774/v2/3cab25130620477b8b03f1bfa8741603", - "id": "3b72a66bf2f0491bb8dba827cade0d48", "publicURL": "http://192.168.56.11:8774/v2/3cab25130620477b8b03f1bfa8741603"}], - "endpoints_links": [], "type": "compute", "name": "nova"}, {"endpoints": [{"adminURL": - "http://192.168.56.11:3333", "region": "RegionOne", "internalURL": "http://192.168.56.11:3333", - "id": "482f749b370c40eab8788d6d0bc47f48", "publicURL": "http://192.168.56.11:3333"}], - "endpoints_links": [], "type": "s3", "name": "s3"}, {"endpoints": [{"adminURL": - "http://192.168.56.11:9292", "region": "RegionOne", "internalURL": "http://192.168.56.11:9292", - "id": "0cd5d5d5a0c24721a0392b47c89e3640", "publicURL": "http://192.168.56.11:9292"}], - "endpoints_links": [], "type": "image", "name": "glance"}, {"endpoints": [{"adminURL": - "http://192.168.56.11:8777", "region": "RegionOne", "internalURL": "http://192.168.56.11:8777", - "id": "4eb4edec1d2647bfb8ba4f9a5757169d", "publicURL": "http://192.168.56.11:8777"}], - "endpoints_links": [], "type": "metering", "name": "ceilometer"}, {"endpoints": - [{"adminURL": "http://192.168.56.11:8776/v1/3cab25130620477b8b03f1bfa8741603", - "region": "RegionOne", "internalURL": "http://192.168.56.11:8776/v1/3cab25130620477b8b03f1bfa8741603", - "id": "009e8a41953d439f845b2a0c0dc28b73", "publicURL": "http://192.168.56.11:8776/v1/3cab25130620477b8b03f1bfa8741603"}], - "endpoints_links": [], "type": "volume", "name": "cinder"}, {"endpoints": - [{"adminURL": "http://192.168.56.11:8773/services/Admin", "region": "RegionOne", - "internalURL": "http://192.168.56.11:8773/services/Cloud", "id": "6820836ec6834548bf7b54da0271dded", - "publicURL": "http://192.168.56.11:8773/services/Cloud"}], "endpoints_links": - [], "type": "ec2", "name": "ec2"}, {"endpoints": [{"adminURL": "http://192.168.56.11:35357/v2.0", - "region": "RegionOne", "internalURL": "http://192.168.56.11:5000/v2.0", "id": - "24a95f51f67949e784971e97463ee4d8", "publicURL": "http://192.168.56.11:5000/v2.0"}], - "endpoints_links": [], "type": "identity", "name": "keystone"}], "user": {"username": - "admin", "roles_links": [], "id": "cbbcc4f7aef6435fa2da7e5f0b2f1e97", "roles": - [{"name": "admin"}], "name": "admin"}, "metadata": {"is_admin": 0, "roles": - ["01a81f2dbb3441f1aaa8fe68a7c6f546"]}}}' - http_version: - recorded_at: Mon, 26 Aug 2013 22:23:39 GMT -- request: - method: post - uri: http://192.168.56.11:35357/v2.0/tenants - body: - encoding: UTF-8 - string: ! '{"tenant":{"name":"Test Project","description":"This is a test","enabled":true}}' - headers: - Content-Type: - - application/json - User-Agent: - - Faraday v0.8.8 - X-Auth-Token: - - 3396443734194600ba8b976415fc8b7a - response: - status: - code: 200 - message: - headers: - vary: - - X-Auth-Token - content-type: - - application/json - content-length: - - '128' - date: - - Mon, 26 Aug 2013 22:27:14 GMT - connection: - - close - body: - encoding: US-ASCII - string: ! '{"tenant": {"description": "This is a test", "enabled": true, "id": - "d14f977f7b4342f39ab3907e07978162", "name": "Test Project"}}' - http_version: - recorded_at: Mon, 26 Aug 2013 22:23:39 GMT -recorded_with: VCR 2.5.0 diff --git a/test/support/openstack_request_test_helper.rb b/test/support/openstack_request_test_helper.rb new file mode 100644 index 0000000..e51bd19 --- /dev/null +++ b/test/support/openstack_request_test_helper.rb @@ -0,0 +1,50 @@ +module Aviator +class Test + + module RequestHelper + + class << self + + def admin_session_data + @admin_session_data ||= JSON.parse('{"access": {"token": {"issued_at": "2013-08-26T22:27:13.886315", + "expires": "2013-08-27T22:27:13Z", "id": "3396443734194600ba8b976415fc8b7a", + "tenant": {"description": null, "enabled": true, "id": "3cab25130620477b8b03f1bfa8741603", + "name": "admin"}}, "serviceCatalog": [{"endpoints": [{"adminURL": "http://192.168.56.11:8774/v2/3cab25130620477b8b03f1bfa8741603", + "region": "RegionOne", "internalURL": "http://192.168.56.11:8774/v2/3cab25130620477b8b03f1bfa8741603", + "id": "3b72a66bf2f0491bb8dba827cade0d48", "publicURL": "http://192.168.56.11:8774/v2/3cab25130620477b8b03f1bfa8741603"}], + "endpoints_links": [], "type": "compute", "name": "nova"}, {"endpoints": [{"adminURL": + "http://192.168.56.11:3333", "region": "RegionOne", "internalURL": "http://192.168.56.11:3333", + "id": "482f749b370c40eab8788d6d0bc47f48", "publicURL": "http://192.168.56.11:3333"}], + "endpoints_links": [], "type": "s3", "name": "s3"}, {"endpoints": [{"adminURL": + "http://192.168.56.11:9292", "region": "RegionOne", "internalURL": "http://192.168.56.11:9292", + "id": "0cd5d5d5a0c24721a0392b47c89e3640", "publicURL": "http://192.168.56.11:9292"}], + "endpoints_links": [], "type": "image", "name": "glance"}, {"endpoints": [{"adminURL": + "http://192.168.56.11:8777", "region": "RegionOne", "internalURL": "http://192.168.56.11:8777", + "id": "4eb4edec1d2647bfb8ba4f9a5757169d", "publicURL": "http://192.168.56.11:8777"}], + "endpoints_links": [], "type": "metering", "name": "ceilometer"}, {"endpoints": + [{"adminURL": "http://192.168.56.11:8776/v1/3cab25130620477b8b03f1bfa8741603", + "region": "RegionOne", "internalURL": "http://192.168.56.11:8776/v1/3cab25130620477b8b03f1bfa8741603", + "id": "009e8a41953d439f845b2a0c0dc28b73", "publicURL": "http://192.168.56.11:8776/v1/3cab25130620477b8b03f1bfa8741603"}], + "endpoints_links": [], "type": "volume", "name": "cinder"}, {"endpoints": + [{"adminURL": "http://192.168.56.11:8773/services/Admin", "region": "RegionOne", + "internalURL": "http://192.168.56.11:8773/services/Cloud", "id": "6820836ec6834548bf7b54da0271dded", + "publicURL": "http://192.168.56.11:8773/services/Cloud"}], "endpoints_links": + [], "type": "ec2", "name": "ec2"}, {"endpoints": [{"adminURL": "http://192.168.56.11:35357/v2.0", + "region": "RegionOne", "internalURL": "http://192.168.56.11:5000/v2.0", "id": + "24a95f51f67949e784971e97463ee4d8", "publicURL": "http://192.168.56.11:5000/v2.0"}], + "endpoints_links": [], "type": "identity", "name": "keystone"}], "user": {"username": + "admin", "roles_links": [], "id": "cbbcc4f7aef6435fa2da7e5f0b2f1e97", "roles": + [{"name": "admin"}], "name": "admin"}, "metadata": {"is_admin": 0, "roles": + ["01a81f2dbb3441f1aaa8fe68a7c6f546"]}}}').with_indifferent_access + end + + + def request_path(*path) + Pathname.new(__FILE__).join('..', '..', '..', 'lib', 'aviator', 'openstack').expand_path.join(*path) + end + + end + end + +end +end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 56e070c..8c339d2 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -21,6 +21,9 @@ unless ENV['CI'] || ENV['TRAVIS'] require 'pry' end +# Make sure this loads first +require Pathname.new(__FILE__).join('..', 'support', 'test_base_class.rb').expand_path + # Load all helpers in test/support Dir[Pathname.new(__FILE__).join('..', 'support', '*.rb')].each do |f| require f