From 7c72e5394fd99ee276ca2c046be64c55f93d21fe Mon Sep 17 00:00:00 2001 From: Nelvin Driz Date: Tue, 14 Aug 2012 16:32:23 +0800 Subject: [PATCH] [openstack] Authentication Mocks Signed-off-by: Nelvin Driz --- lib/fog/core/time.rb | 2 ++ lib/fog/openstack/compute.rb | 19 +++++++++++++++++- lib/fog/openstack/identity.rb | 37 ++++++++++++++++++++++++++++++----- lib/fog/openstack/image.rb | 10 ++++++++++ lib/fog/openstack/volume.rb | 10 ++++++++++ 5 files changed, 72 insertions(+), 6 deletions(-) diff --git a/lib/fog/core/time.rb b/lib/fog/core/time.rb index f2394c23a..61d97dedb 100644 --- a/lib/fog/core/time.rb +++ b/lib/fog/core/time.rb @@ -1,3 +1,5 @@ +require 'time' + module Fog class Time < ::Time diff --git a/lib/fog/openstack/compute.rb b/lib/fog/openstack/compute.rb index a469e9b4a..02bd85dda 100644 --- a/lib/fog/openstack/compute.rb +++ b/lib/fog/openstack/compute.rb @@ -152,9 +152,13 @@ module Fog # Hosts request :list_hosts request :get_host_details - + class Mock + attr_reader :auth_token + attr_reader :auth_token_expiration + attr_reader :current_user + attr_reader :current_tenant def self.data @data ||= Hash.new do |hash, key| @@ -205,6 +209,19 @@ module Fog def initialize(options={}) @openstack_username = options[:openstack_username] @openstack_tenant = options[:openstack_tenant] + @openstack_auth_uri = URI.parse(options[:openstack_auth_url]) + + @auth_token = Fog::Mock.random_base64(64) + @auth_token_expiration = (Time.now.utc + 86400).iso8601 + + management_url = URI.parse(options[:openstack_auth_url]) + management_url.port = 8774 + management_url.path = '/v1.1/1' + @openstack_management_url = management_url.to_s + + identity_public_endpoint = URI.parse(options[:openstack_auth_url]) + identity_public_endpoint.port = 5000 + @openstack_identity_public_endpoint = identity_public_endpoint.to_s end def data diff --git a/lib/fog/openstack/identity.rb b/lib/fog/openstack/identity.rb index 724e32a9c..94544485e 100644 --- a/lib/fog/openstack/identity.rb +++ b/lib/fog/openstack/identity.rb @@ -53,6 +53,11 @@ module Fog class Mock + attr_reader :auth_token + attr_reader :auth_token_expiration + attr_reader :current_user + attr_reader :current_tenant + def self.data @data ||= Hash.new do |hash, key| hash[key] = { @@ -69,6 +74,26 @@ module Fog def initialize(options={}) @openstack_username = options[:openstack_username] + @openstack_auth_uri = URI.parse(options[:openstack_auth_url]) + @openstack_management_url = @openstack_auth_uri.to_s + + @current_user_id = 1 + @current_user = { + 'username' => 'admin', + 'name' => 'admin', + 'id' => 1, + 'roles' => [ + { 'id' => 1, 'name' => 'admin' }, + { 'id' => 2, 'name' => 'Member' } + ] + } + + @auth_token = Fog::Mock.random_base64(64) + @auth_token_expiration = (Time.now.utc + 86400).iso8601 + @current_tenant = { + 'id' => 1, + 'name' => 'admin' + } unless self.data[:users].values.detect {|user| user['name'] == @openstack_username} id = Fog::Mock.random_numbers(6).to_s @@ -91,11 +116,13 @@ module Fog end def credentials - { :provider => 'openstack', - :openstack_auth_url => @openstack_auth_uri.to_s, - :openstack_auth_token => @auth_token, - :openstack_management_url => @openstack_management_url, - :openstack_current_user_id => @openstack_current_user_id} + { :provider => 'openstack', + :openstack_auth_url => @openstack_auth_uri.to_s, + :openstack_auth_token => @auth_token, + :openstack_management_url => @openstack_management_url, + :openstack_current_user_id => @openstack_current_user_id, + :current_user => @current_user, + :current_tenant => @current_tenant} end end diff --git a/lib/fog/openstack/image.rb b/lib/fog/openstack/image.rb index 5b034f99b..364521c7c 100644 --- a/lib/fog/openstack/image.rb +++ b/lib/fog/openstack/image.rb @@ -49,6 +49,16 @@ module Fog def initialize(options={}) require 'multi_json' @openstack_username = options[:openstack_username] + @openstack_tenant = options[:openstack_tenant] + @openstack_auth_uri = URI.parse(options[:openstack_auth_url]) + + @auth_token = Fog::Mock.random_base64(64) + @auth_token_expiration = (Time.now.utc + 86400).iso8601 + + management_url = URI.parse(options[:openstack_auth_url]) + management_url.port = 9292 + management_url.path = '/v1' + @openstack_management_url = management_url.to_s @data ||= { :users => {} } unless @data[:users].find {|u| u['name'] == options[:openstack_username]} diff --git a/lib/fog/openstack/volume.rb b/lib/fog/openstack/volume.rb index 99ad4d0bc..0e41e1a2a 100644 --- a/lib/fog/openstack/volume.rb +++ b/lib/fog/openstack/volume.rb @@ -48,6 +48,16 @@ module Fog def initialize(options={}) require 'multi_json' @openstack_username = options[:openstack_username] + @openstack_tenant = options[:openstack_tenant] + @openstack_auth_uri = URI.parse(options[:openstack_auth_url]) + + @auth_token = Fog::Mock.random_base64(64) + @auth_token_expiration = (Time.now.utc + 86400).iso8601 + + management_url = URI.parse(options[:openstack_auth_url]) + management_url.port = 8776 + management_url.path = '/v1' + @openstack_management_url = management_url.to_s @data ||= { :users => {} } unless @data[:users].find {|u| u['name'] == options[:openstack_username]}