deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject [5/31] git commit: Remove lots of constants in favor of a Config class, accessible as 'api'
Date Wed, 25 Jul 2012 13:56:38 GMT
Remove lots of constants in favor of a Config class, accessible as 'api'


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/1f35cdc9
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/1f35cdc9
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/1f35cdc9

Branch: refs/heads/master
Commit: 1f35cdc9fe29a76abc0cf8bd1e68090d48e1efe5
Parents: 48dc184
Author: David Lutterkort <lutter@redhat.com>
Authored: Mon Jul 23 16:27:24 2012 -0700
Committer: marios <marios@redhat.com>
Committed: Tue Jul 24 12:02:58 2012 +0300

----------------------------------------------------------------------
 tests/deltacloud/base_api_test.rb |    6 +-
 tests/deltacloud/buckets_test.rb  |    6 +-
 tests/deltacloud/test_setup.rb    |   89 ++++++++++++++++++++++++-------
 3 files changed, 75 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/1f35cdc9/tests/deltacloud/base_api_test.rb
----------------------------------------------------------------------
diff --git a/tests/deltacloud/base_api_test.rb b/tests/deltacloud/base_api_test.rb
index a58ea80..350cd54 100644
--- a/tests/deltacloud/base_api_test.rb
+++ b/tests/deltacloud/base_api_test.rb
@@ -33,19 +33,19 @@ describe "Deltacloud API Entry Point" do
     res = get_api :accept => :xml
     driver = res.xml.root[:driver]
     driver.wont_be_nil
-    DRIVERS.include?(driver).must_equal true
+    api.drivers.include?(driver).must_equal true
   end
 
   it 'advertise the current API version in API entrypoint' do
     res = get_api :accept => :xml
     version = res.xml.root[:version]
     version.wont_be_nil
-    version.must_equal API_VERSION
+    version.must_equal api.version
   end
 
   it 'advertise the current API version in HTTP headers' do
     res = get_api
-    res.headers[:server].must_equal "Apache-Deltacloud/#{API_VERSION}"
+    res.headers[:server].must_equal "Apache-Deltacloud/#{api.version}"
   end
 
   it 'must include the ETag in HTTP headers' do

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/1f35cdc9/tests/deltacloud/buckets_test.rb
----------------------------------------------------------------------
diff --git a/tests/deltacloud/buckets_test.rb b/tests/deltacloud/buckets_test.rb
index 728ff71..84533d6 100644
--- a/tests/deltacloud/buckets_test.rb
+++ b/tests/deltacloud/buckets_test.rb
@@ -29,9 +29,9 @@ def create_a_bucket_and_blob
     raise Exception.new("Failed to create bucket #{bucket_name}")
   end
 
-  res = RestClient.put "#{API_URL}/buckets/#{bucket_name}/#{blob_name}",
+  res = RestClient.put "#{api.url}/buckets/#{bucket_name}/#{blob_name}",
     "This is the test blob content",
-    {:Authorization=>BASIC_AUTH,
+    {:Authorization=>api.basic_auth,
      :content_type=>"text/plain",
      "X-Deltacloud-Blobmeta-Version"=>"1.0",
      "X-Deltacloud-Blobmeta-Author"=>"herpyderp"}
@@ -93,7 +93,7 @@ describe 'Deltacloud API buckets collection' do
   end
 
   it 'should be possible to specify location for POST /api/buckets if bucket_location feature'
do
-    skip("No bucket_location feature specified for driver #{API_DRIVER} running at #{API_URL}...
skipping test") unless features_hash["buckets"].include?("bucket_location")
+    skip("No bucket_location feature specified for driver #{api.driver} running at #{api.url}...
skipping test") unless features_hash["buckets"].include?("bucket_location")
     bucket_name = random_name
     #    res = post({:name=>bucket_name, :bucket_location=>
   end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/1f35cdc9/tests/deltacloud/test_setup.rb
----------------------------------------------------------------------
diff --git a/tests/deltacloud/test_setup.rb b/tests/deltacloud/test_setup.rb
index 64fdbb4..ca37186 100644
--- a/tests/deltacloud/test_setup.rb
+++ b/tests/deltacloud/test_setup.rb
@@ -21,25 +21,11 @@ require 'nokogiri'
 require 'json'
 require 'base64'
 require 'yaml'
+require 'singleton'
 
 #SETUP
 topdir = File.join(File.dirname(__FILE__), '..')
 $:.unshift topdir
-CONFIG = YAML.load(File.open(File::join(topdir, "config.yaml")))
-API_URL = CONFIG["api_url"]
-API_DRIVER = RestClient.get(API_URL) do |response, request, result|
-  Nokogiri::XML(response).root[:driver]
-end
-raise Exception.new("Can't find config for driver: #{API_DRIVER} currently running at #{API_URL}
in config.yaml file") unless CONFIG[API_DRIVER] and CONFIG[API_DRIVER]["user"] and CONFIG[API_DRIVER]["password"]
-USER    = CONFIG[API_DRIVER]["user"]
-PASSWORD= CONFIG[API_DRIVER]["password"]
-BASIC_AUTH="Basic #{Base64.encode64(USER+":"+PASSWORD)}"
-
-DRIVERS = RestClient.get(API_URL+"/drivers") do |response, request, result|
-  Nokogiri::XML(response).xpath("//driver/name").inject([]){|res, c| res << c.text.downcase;
res}
-end
-API_VERSION = Nokogiri::XML(RestClient.get API_URL).root[:version]
-#SETUP
 
 module RestClient::Response
   def xml
@@ -51,6 +37,69 @@ module RestClient::Response
   end
 end
 
+module Deltacloud
+  module Test
+
+    class Config
+
+      include Singleton
+
+      def initialize
+        fname = ENV["CONFIG"] || File::join(File::dirname(__FILE__), "..",
+                                            "config.yaml")
+        @hash = YAML.load(File::open(fname))
+      end
+
+      def url
+        @hash["api_url"]
+      end
+
+      def basic_auth(u = nil, p = nil)
+        u ||= @hash[driver]["user"]
+        p ||= @hash[driver]["password"]
+        "Basic #{Base64.encode64("#{u}:#{p}")}"
+      end
+
+      def driver
+        xml.root[:driver]
+      end
+
+      def drivers
+        @drivers ||= RestClient.get(url+"/drivers").xml.xpath("//driver/name").map { |c|
c.text.downcase }
+      end
+
+      def version
+        xml.root[:version]
+      end
+
+      private
+      def xml
+        unless @xml
+          @xml = RestClient.get(url).xml
+          drv = @xml.root[:driver]
+          unless @hash[drv]
+            raise "No config for #{drv} driver in config.yaml used by #{url}"
+          end
+          unless @hash[drv]["user"] && @hash[drv]["password"]
+            raise "No user or password in config.yaml for #{drv} driver used by #{url}"
+          end
+        end
+        @xml
+      end
+    end
+
+    def self.config
+      Config::instance
+    end
+  end
+end
+
+# Return the current test config; we call that 'api' since it looks a
+# little prettier in the tests
+def api
+  Deltacloud::Test::config
+end
+
 # Make a GET request for +path+ and return the +RestClient::Response+. The
 # query string for the request is generated from +params+, with the
 # exception of a few special entries in params, which are used to set some
@@ -91,9 +140,9 @@ def process_url_params(path, params)
     if params[:user]
       u = params.delete(:user)
       p = params.delete(:password)
-      headers['Authorization'] = "Basic #{Base64.encode64("#{u}:#{p}")}"
+      headers['Authorization'] = api.basic_auth(u, p)
     else
-      headers['Authorization'] = BASIC_AUTH
+      headers['Authorization'] = api.basic_auth
     end
   end
   headers["X-Deltacloud-Driver"] = params.delete(:driver) if params[:driver]
@@ -103,7 +152,7 @@ def process_url_params(path, params)
   if path =~ /^https?:/
     url = path
   else
-    url = API_URL + path
+    url = api.url + path
   end
   url += "?" + params.map { |k,v| "#{k}=#{v}" }.join("&") unless params.empty?
   if ENV["LOG"] && ENV["LOG"].include?("requests")
@@ -128,9 +177,9 @@ TEST_FILES =  { :images             => "images_test.rb",
   :storage_snapshots  => "storage_snapshots_test.rb",
   :buckets            => "buckets_test.rb"
 }
-#gets the list of collections from the server running at API_URL and translates those into
file names accoring to TEST_FILES
+#gets the list of collections from the server running at api.url and translates those into
file names accoring to TEST_FILES
 def deltacloud_test_file_names
-  driver_collections = (RestClient.get API_URL, {:accept=>:xml}).xml.xpath("//api/link").inject([]){|res,
current| res<<current[:rel].to_sym ;res}
+  driver_collections = (RestClient.get api.url, {:accept=>:xml}).xml.xpath("//api/link").inject([]){|res,
current| res<<current[:rel].to_sym ;res}
   driver_collections.inject([]){|res, current| res << "deltacloud/#{TEST_FILES[current]}"
if TEST_FILES[current] ;res}
 end
 


Mime
View raw message