incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core] Deltacloud client mock support
Date Wed, 15 Sep 2010 12:08:22 GMT
From: Michal Fojtik <mfojtik@redhat.com>

---
 client/lib/deltacloud.rb      |   10 ++++++-
 client/lib/deltacloud_mock.rb |   51 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 2 deletions(-)
 create mode 100644 client/lib/deltacloud_mock.rb

diff --git a/client/lib/deltacloud.rb b/client/lib/deltacloud.rb
index 0a7301e..1eedb5b 100644
--- a/client/lib/deltacloud.rb
+++ b/client/lib/deltacloud.rb
@@ -29,8 +29,12 @@ module DeltaCloud
   # @param [String, password] API password
   # @param [String, user_name] API URL (eg. http://localhost:3001/api)
   # @return [DeltaCloud::API]
-  def self.new(user_name, password, api_url, &block)
-    API.new(user_name, password, api_url, &block)
+  def self.new(user_name, password, api_url, opts={}, &block)
+    if opts[:mock_root]
+      MockAPI.new(user_name, password, api_url, opts, &block)
+    else
+      API.new(user_name, password, api_url, opts, &block)
+    end
   end
 
   # Check given credentials if their are valid against
@@ -76,6 +80,8 @@ module DeltaCloud
 
     def initialize(user_name, password, api_url, opts={}, &block)
       opts[:version] = true
+      @mock_root = opts[:mock_root]
+      @mock_scenario = opts[:mock_scenario]
       @logger = opts[:verbose] ? Logger.new(STDERR) : []
       @username, @password = user_name, password
       @api_uri = URI.parse(api_url)
diff --git a/client/lib/deltacloud_mock.rb b/client/lib/deltacloud_mock.rb
new file mode 100644
index 0000000..c841b66
--- /dev/null
+++ b/client/lib/deltacloud_mock.rb
@@ -0,0 +1,51 @@
+require 'digest/sha1'
+
+module DeltaCloud
+  class MockAPI < API
+    
+    alias :original_request :request
+
+    def request(*args, &block)
+      mock_request(args) do |mock_response|
+        if mock_response
+          yield mock_response
+        else
+          original_request(*args) do |response|
+            yield mock_store_response(args, response)
+          end
+        end
+      end
+    end
+
+    def set_mock_scenario(name)
+      @mock_scenario = name
+    end
+
+    def mock_clean!
+      FileUtils.rm_r(File::join(@mock_root, @mock_scenario))
+    end
+
+    private
+
+    def mock_request(args, &block)
+      file_name = File::join(@mock_root, @mock_scenario, Digest::SHA1.hexdigest(args.inspect)+".mock")
+      if File.exists?(file_name)
+        yield File.readlines(file_name).join
+      else
+        yield false
+      end
+    end
+
+    def mock_store_response(args, response)
+      unless File.directory?(File::join(@mock_root, @mock_scenario))
+        FileUtils.mkdir_p(File::join(@mock_root, @mock_scenario))
+      end
+      file_name = File::join(@mock_root, @mock_scenario, Digest::SHA1.hexdigest(args.inspect)+".mock")
+      File.open(file_name, 'w') do |f|
+        f.puts(response)
+      end
+      return response
+    end
+
+  end
+end
-- 
1.7.2.2


Mime
View raw message