diff --git a/.gitignore b/.gitignore index 1f98906..a42890e 100644 --- a/.gitignore +++ b/.gitignore @@ -14,7 +14,9 @@ rdoc spec/reports test/tmp test/version_tmp -tmp +tmp/*.* +!tmp/.gitignore .DS_Store test/environment.yml vcr.log + diff --git a/lib/aviator/core/service.rb b/lib/aviator/core/service.rb index a016a42..7cde75f 100644 --- a/lib/aviator/core/service.rb +++ b/lib/aviator/core/service.rb @@ -35,6 +35,16 @@ module Aviator end + class Logger < Faraday::Response::Logger + def initialize(app, logger=nil) + super(app) + @logger = logger || begin + require 'logger' + ::Logger.new(self.class::LOG_FILE_PATH) + end + end + end + # Because we define requests in a flattened scope, we want to make sure that when each # request is initialized it doesn't get polluted by instance variables and methods # of the containing class. This builder class makes that happen by being a @@ -68,6 +78,7 @@ module Aviator def initialize(opts={}) @provider = opts[:provider] || (raise ProviderNotDefinedError.new) @service = opts[:service] || (raise ServiceNameNotDefinedError.new) + @log_file = opts[:log_file] @default_session_data = opts[:default_session_data] @@ -102,6 +113,13 @@ module Aviator def http_connection @http_connection ||= Faraday.new do |conn| + if log_file + # Ugly hack to make logger configurable + const_name = 'LOG_FILE_PATH' + Logger.send(:remove_const, const_name) if Logger.const_defined?(const_name) + Logger.const_set(const_name, log_file) + conn.use Logger.dup + end conn.adapter Faraday.default_adapter conn.headers['Content-Type'] = 'application/json' end @@ -169,6 +187,11 @@ module Aviator endpoint_type[request_name] = klass end end + + + def log_file + @log_file + end def requests diff --git a/lib/aviator/core/session.rb b/lib/aviator/core/session.rb index 9bf959c..03fc078 100644 --- a/lib/aviator/core/session.rb +++ b/lib/aviator/core/session.rb @@ -35,6 +35,7 @@ module Aviator raise EnvironmentNotDefinedError.new(config_path, environment) unless config[environment] @environment = config[environment] + @log_file = opts[:log_file] end @@ -84,7 +85,8 @@ module Aviator @auth_service ||= Service.new( provider: environment[:provider], service: environment[:auth_service][:name], - default_session_data: { auth_service: environment[:auth_service] } + default_session_data: { auth_service: environment[:auth_service] }, + log_file: log_file ) end @@ -102,13 +104,19 @@ module Aviator @services[service_name] ||= Service.new( provider: environment[:provider], service: service_name, - default_session_data: auth_info + default_session_data: auth_info, + log_file: log_file ) @services[service_name] end + def log_file + @log_file + end + + def update_services_session_data return unless @services diff --git a/test/aviator/core/session_test.rb b/test/aviator/core/session_test.rb index 80e189e..521ffc3 100644 --- a/test/aviator/core/session_test.rb +++ b/test/aviator/core/session_test.rb @@ -9,16 +9,17 @@ class Aviator::Test end - def new_session - Aviator::Session.new( - config_file: config.path, - environment: 'test' - ) + def log_file_path + Pathname.new(__FILE__).expand_path.join('..', '..', '..', '..', 'tmp', 'aviator.log') end - def log_file_path - Pathname.new(__FILE__).expand_path.join('..', '..', '..', '..', 'tmp', 'aviator.log') + def new_session + Aviator::Session.new( + config_file: config.path, + environment: 'test', + log_file: log_file_path + ) end @@ -66,6 +67,20 @@ class Aviator::Test end # describe '#authenticate' + describe '::new' do + + it 'directs log entries to the given log file' do + log_file_path.delete if log_file_path.file? + + session = new_session + session.authenticate + + log_file_path.file?.must_equal true + end + + end + + describe '#xxx_service' do it 'returns an instance of the indicated service' do diff --git a/test/cassettes/core/session/c_new/directs_log_entries_to_the_given_log_file.yml b/test/cassettes/core/session/c_new/directs_log_entries_to_the_given_log_file.yml new file mode 100644 index 0000000..f7eac2b --- /dev/null +++ b/test/cassettes/core/session/c_new/directs_log_entries_to_the_given_log_file.yml @@ -0,0 +1,63 @@ +--- +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: + - Thu, 29 Aug 2013 03:33:05 GMT + connection: + - close + body: + encoding: US-ASCII + string: ! '{"access": {"token": {"issued_at": "2013-08-29T03:33:05.280254", + "expires": "2013-08-30T03:33:05Z", "id": "5673e8fdcfd644eab06ae0cc5e68d2f0", + "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: Thu, 29 Aug 2013 07:32:20 GMT +recorded_with: VCR 2.5.0 diff --git a/tmp/.gitignore b/tmp/.gitignore new file mode 100644 index 0000000..e69de29