You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2012/07/26 13:29:37 UTC

[PATCH core 8/9] Core: Reorganized common.rb files in driver unit tests

From: Michal Fojtik <mf...@redhat.com>

This patch will move Instance and Image monkey patching
into api.rb file, which will make it available also for
the Library use.

Also Time freezing patch and VCR helper method was moved
to test_helper.rb, which will keep the driver common.rb
reasonably small and without code duplication.

Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/deltacloud/api.rb         |   41 ++++++++++++++++
 server/tests/drivers/ec2/common.rb   |   86 ++--------------------------------
 server/tests/drivers/rhevm/common.rb |   60 +-----------------------
 server/tests/test_helper.rb          |   37 +++++++++++++++
 4 files changed, 83 insertions(+), 141 deletions(-)

diff --git a/server/lib/deltacloud/api.rb b/server/lib/deltacloud/api.rb
index 587c667..23d96e1 100644
--- a/server/lib/deltacloud/api.rb
+++ b/server/lib/deltacloud/api.rb
@@ -29,6 +29,47 @@ require_relative 'models'
 require_relative 'drivers'
 require_relative 'helpers/driver_helper'
 
+module TestPoller
+  # This method will pool the resource until condition is true
+  # Will raise 'Timeout' when it reach retry count
+  #
+  # default opts[:retries] => 10
+  # default opts[:time_between_retry] => 10 (seconds)
+  # default opts[:timeout] => 60 (seconds) -> single request timeout
+  #
+  # opts[:before] => Proc -> executed 'before' making each request
+  # opts[:after] => Proc -> executed 'after' making each request
+  #
+  def wait_for!(driver, opts={}, &block)
+    opts[:retries] ||= 10
+    opts[:time_between_retry] ||= 10
+    opts[:timeout] ||= 60
+    opts[:method] ||= self.class.name.downcase.to_sym
+    opts[:retries].downto(0) do |r|
+      result = begin
+        timeout(opts[:timeout]) do
+          if opts[:before]
+            new_instance = opts[:before].call(r) { driver.send(opts[:method], :id => self.id) }
+          else
+            new_instance = driver.send(opts[:method], :id => self.id)
+          end
+          ((yield new_instance) == true) ? new_instance : false
+        end
+      rescue Timeout::Error
+        false
+      ensure
+        opts[:after].call(r) if opts[:after]
+      end
+      return result unless result == false
+      sleep(opts[:time_between_retry])
+    end
+    raise Timeout::Error
+  end
+end
+
+class Instance; include TestPoller; end
+class Image; include TestPoller; end
+
 module Deltacloud
 
   API_VERSION = '1.0.0'
diff --git a/server/tests/drivers/ec2/common.rb b/server/tests/drivers/ec2/common.rb
index 5434c61..a40a8bc 100644
--- a/server/tests/drivers/ec2/common.rb
+++ b/server/tests/drivers/ec2/common.rb
@@ -1,98 +1,20 @@
-# Make less noise to console
-ENV['RACK_ENV'] ||= 'test'
-
 # Warning: RightHttpConnection has to be required before WebMock is required !!!
 # Lets require that:
 require 'right_http_connection'
-
 require 'vcr'
-require 'time'
-
-# This code was originally copied from:
-# https://github.com/jtrupiano/timecop/issues/8#issuecomment-1396047
-#
-# Since 'timecop' gem has broken 'timezone' support, this small monkey-patching
-# on Time object seems to fix this issue.
-
-class Time
-  module TimeMock
-    attr_accessor :mock_time
-
-    def mock_now
-      @mock_time || Time.original_now
-    end
-
-    def be(a_time)
-      @mock_time = Time.parse(a_time)
-    end
-
-  end
-
-  class << self
-    include TimeMock
-    alias_method :original_now, :now
-    alias_method :now, :mock_now
-  end
-end
-
-class Instance
-  # This method will pool the instance until condition is true
-  # Will raise 'Timeout' when it reach retry count
-  #
-  # default opts[:retries] => 10
-  # default opts[:time_between_retry] => 10 (seconds)
-  # default opts[:timeout] => 60 (seconds) -> single request timeout
-  #
-  # opts[:before] => Proc -> executed 'before' making each request
-  # opts[:after] => Proc -> executed 'after' making each request
-  #
-  def wait_for!(driver, opts={}, &block)
-    opts[:retries] ||= 10
-    opts[:time_between_retry] ||= 10
-    opts[:timeout] ||= 60
-    opts[:retries].downto(0) do |r|
-      result = begin
-        timeout(opts[:timeout]) do
-          if opts[:before]
-            new_instance = opts[:before].call(r) { driver.instance(:id => self.id) }
-          else
-            new_instance = driver.instance(:id => self.id)
-          end
-          ((yield new_instance) == true) ? new_instance : false
-        end
-      rescue Timeout::Error
-        false
-      ensure
-        opts[:after].call(r) if opts[:after]
-      end
-      return result unless result == false
-      sleep(opts[:time_between_retry])
-    end
-    raise Timeout::Error
-  end
-end
 
 # Freeze time, so EC2 signatures have all the same time
 # This will avoid IncorrectSignature exceptions
 
+# NOTE: This timestamp need to be changed when re-recording
+#       the fixtures.
+
 Time.be(DateTime.parse("2012-07-23 12:21:00 +0000").to_s)
 
 VCR.configure do |c|
   # NOTE: Empty this directory before re-recording
   c.cassette_library_dir = File.join(File.dirname(__FILE__), 'fixtures')
   c.hook_into :webmock
-  # Set this to :new_epizodes when you want to 're-record'
+  # Set this to :new_episodes when you want to 're-record'
   c.default_cassette_options = { :record => :none }
 end
-
-# Some test scenarios use .wait_for! method that do multiple retries
-# for requests. We need to deal with that passing a Proc that use
-# different cassette for each request
-
-def record_retries(name='')
-  {
-    :before => Proc.new { |r, &block|
-      VCR.use_cassette("#{__name__}-#{name.empty? ? '' : "#{name}-"}#{r}", &block)
-    }
-  }
-end
diff --git a/server/tests/drivers/rhevm/common.rb b/server/tests/drivers/rhevm/common.rb
index 79f0794..8e039e9 100644
--- a/server/tests/drivers/rhevm/common.rb
+++ b/server/tests/drivers/rhevm/common.rb
@@ -1,9 +1,4 @@
-# Make less noise to console
-ENV['RACK_ENV'] ||= 'test'
-
 require 'vcr'
-require 'pp'
-require 'time'
 
 # Credentials used to access RHEV-M server
 #
@@ -17,63 +12,10 @@ def credentials
   }
 end
 
-module TestPooler
-  # This method will pool the resource until condition is true
-  # Will raise 'Timeout' when it reach retry count
-  #
-  # default opts[:retries] => 10
-  # default opts[:time_between_retry] => 10 (seconds)
-  # default opts[:timeout] => 60 (seconds) -> single request timeout
-  #
-  # opts[:before] => Proc -> executed 'before' making each request
-  # opts[:after] => Proc -> executed 'after' making each request
-  #
-  def wait_for!(driver, opts={}, &block)
-    opts[:retries] ||= 10
-    opts[:time_between_retry] ||= 10
-    opts[:timeout] ||= 60
-    opts[:method] ||= self.class.name.downcase.to_sym
-    opts[:retries].downto(0) do |r|
-      result = begin
-        timeout(opts[:timeout]) do
-          if opts[:before]
-            new_instance = opts[:before].call(r) { driver.send(opts[:method], :id => self.id) }
-          else
-            new_instance = driver.send(opts[:method], :id => self.id)
-          end
-          ((yield new_instance) == true) ? new_instance : false
-        end
-      rescue Timeout::Error
-        false
-      ensure
-        opts[:after].call(r) if opts[:after]
-      end
-      return result unless result == false
-      sleep(opts[:time_between_retry])
-    end
-    raise Timeout::Error
-  end
-end
-
-class Instance; include TestPooler; end
-class Image; include TestPooler; end
-
 VCR.configure do |c|
   # NOTE: Empty this directory before re-recording
   c.cassette_library_dir = File.join(File.dirname(__FILE__), 'fixtures')
   c.hook_into :webmock
-  # Set this to :new_epizodes when you want to 're-record'
+  # Set this to :new_episodes when you want to 're-record'
   c.default_cassette_options = { :record => :none }
 end
-
-# Some test scenarios use .wait_for! method that do multiple retries
-# for requests. We need to deal with that passing a Proc that use
-# different cassette for each request
-
-def record_retries(name='')
-  {
-    :before => Proc.new { |r, &block|
-      VCR.use_cassette("#{__name__}-#{name.empty? ? '' : "#{name}-"}#{r}", &block)
-    }
-  }
-end
diff --git a/server/tests/test_helper.rb b/server/tests/test_helper.rb
index 0b40610..42effb1 100644
--- a/server/tests/test_helper.rb
+++ b/server/tests/test_helper.rb
@@ -17,3 +17,40 @@ unless Kernel.respond_to?(:require_relative)
     end
   end
 end
+
+require 'time'
+
+# This code was originally copied from:
+# https://github.com/jtrupiano/timecop/issues/8#issuecomment-1396047
+#
+# Since 'timecop' gem has broken 'timezone' support, this small monkey-patching
+# on Time object seems to fix this issue.
+
+class Time
+  module TimeMock
+    attr_accessor :mock_time
+
+    def mock_now
+      @mock_time || Time.original_now
+    end
+
+    def be(a_time)
+      @mock_time = Time.parse(a_time)
+    end
+
+  end
+
+  class << self
+    include TimeMock
+    alias_method :original_now, :now
+    alias_method :now, :mock_now
+  end
+end
+
+def record_retries(name='')
+  {
+    :before => Proc.new { |r, &block|
+      VCR.use_cassette("#{__name__}-#{name.empty? ? '' : "#{name}-"}#{r}", &block)
+    }
+  }
+end
-- 
1.7.10.2