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/05/21 12:06:56 UTC
[PATCH core 15/51] Core: Added the Deltacloud library wrapper (require 'lib/deltacloud')
From: Michal Fojtik <mf...@redhat.com>
This will make possible to use Deltacloud drivers API with thirty-party
application whithout spawning a server.
Signed-off-by: Michal fojtik <mf...@redhat.com>
---
server/lib/deltacloud.rb | 79 +++++++++++++++++++++++++++++++++-----
server/tests/api/common.rb | 1 +
server/tests/api/driver_test.rb | 79 ++++++++++++++++++++++++++++++++++++++
server/tests/api/library_test.rb | 48 +++++++++++++++++++++++
4 files changed, 197 insertions(+), 10 deletions(-)
create mode 100644 server/tests/api/common.rb
create mode 100644 server/tests/api/driver_test.rb
create mode 100644 server/tests/api/library_test.rb
diff --git a/server/lib/deltacloud.rb b/server/lib/deltacloud.rb
index 6ff547e..6508d8d 100644
--- a/server/lib/deltacloud.rb
+++ b/server/lib/deltacloud.rb
@@ -1,4 +1,3 @@
-#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
@@ -14,14 +13,74 @@
# License for the specific language governing permissions and limitations
# under the License.
-require 'deltacloud/drivers'
+require 'rubygems'
+
+load File.join(File.dirname(__FILE__), 'deltacloud/core_ext.rb')
+
+require 'ostruct'
+
+require_relative 'deltacloud/core_ext/string'
+require_relative 'deltacloud/core_ext/array'
+require_relative 'deltacloud/core_ext/hash'
+require_relative 'deltacloud/core_ext/integer'
+require_relative 'deltacloud/core_ext/proc'
+require_relative 'deltacloud/models'
+require_relative 'deltacloud/drivers'
+require_relative 'deltacloud/helpers/driver_helper'
+
+module Deltacloud
+
+ API_VERSION = '0.5.0'
+
+ def self.drivers
+ Drivers.driver_config
+ end
+
+ class Library
+ include Helpers::Drivers
+
+ attr_reader :backend, :credentials
+
+ def initialize(driver_name, opts={}, &block)
+ Thread.current[:driver] = driver_name.to_s
+ Thread.current[:provider] = opts[:provider]
+ @backend = driver
+ opts[:user] ||= 'mockuser'
+ opts[:password] ||= 'mockpassword'
+ @credentials = OpenStruct.new(:user => opts[:user], :password => opts[:password])
+ yield backend if block_given?
+ end
+
+ def version
+ Deltacloud::API_VERSION
+ end
+
+ def current_provider
+ Thread.current[:provider]
+ end
+
+ def current_driver
+ Thread.current[:driver]
+ end
+
+ def providers
+ Deltacloud.drivers[current_driver.to_sym]
+ end
+
+ def method_missing(name, *args)
+ return super unless backend.respond_to? name
+ begin
+ params = ([@credentials] + args).flatten
+ backend.send(name, *params)
+ rescue ArgumentError
+ backend.send(name, *args)
+ end
+ end
+
+ end
-require 'deltacloud/core_ext'
+ def self.new(driver_name, opts={}, &block)
+ Library.new(driver_name, opts, &block)
+ end
-require 'deltacloud/base_driver'
-require 'deltacloud/hardware_profile'
-require 'deltacloud/state_machine'
-require 'deltacloud/helpers'
-require 'deltacloud/models'
-require 'deltacloud/validation'
-require 'deltacloud/runner'
+end
diff --git a/server/tests/api/common.rb b/server/tests/api/common.rb
new file mode 100644
index 0000000..3554a81
--- /dev/null
+++ b/server/tests/api/common.rb
@@ -0,0 +1 @@
+require 'minitest/autorun'
diff --git a/server/tests/api/driver_test.rb b/server/tests/api/driver_test.rb
new file mode 100644
index 0000000..20f9197
--- /dev/null
+++ b/server/tests/api/driver_test.rb
@@ -0,0 +1,79 @@
+require_relative './common'
+require_relative '../../lib/deltacloud'
+
+begin
+ require 'arguments'
+rescue LoadError
+ puts "You don't have 'rdp-arguments' gems installed. (gem install rdp-arguments)"
+ exit(1)
+end
+require 'pp'
+
+describe 'Deltacloud drivers API' do
+
+ before do
+ @stderr = $stderr.clone
+ $stderr = StringIO.new
+ end
+
+ after do
+ $stderr = @stderr
+ end
+
+ it 'should pass the known method to Deltacloud driver' do
+ Deltacloud.new(:mock).hardware_profiles.must_be_kind_of Array
+ Deltacloud.new(:mock).hardware_profiles.wont_be_empty
+ end
+
+ it 'should raise NoMethodError when driver does not respond to method' do
+ lambda { Deltacloud.new(:mock).non_existing_method }.must_raise NoMethodError
+ end
+
+ it 'should apply the credentials to methods that require them' do
+ Deltacloud.new(:mock).realms.must_be_kind_of Array
+ Deltacloud.new(:mock).realms.wont_be_empty
+ end
+
+ it 'should allow to use different drivers' do
+ Deltacloud.new(:ec2).backend.must_be_instance_of Deltacloud::Drivers::Ec2::Ec2Driver
+ Deltacloud.new(:mock).backend.must_be_instance_of Deltacloud::Drivers::Mock::MockDriver
+ end
+
+ it 'should support loading all supported drivers' do
+ Deltacloud.drivers.keys.each do |key|
+ Deltacloud.new(key).current_driver.must_equal key.to_s
+ end
+ end
+
+ METHODS = {
+ :firewalls => [[:credentials], [:opts, "{ }"]],
+ :firewall => [[:credentials], [:opts, "{ }"]],
+ :keys => [[:credentials], [:opts, "{ }"]],
+ :key => [[:credentials], [:opts]],
+ :storage_snapshots => [[:credentials], [:opts, "{ }"]],
+ :storage_snapshot => [[:credentials], [:opts]],
+ :storage_volumes => [[:credentials], [:opts, "{ }"]],
+ :storage_volume => [[:credentials], [:opts]],
+ :realms => [[:credentials], [:opts, "{ }"]],
+ :realm => [[:credentials], [:opts]],
+ :images => [[:credentials], [:opts, "{ }"]],
+ :image => [[:credentials], [:opts]],
+ :instances => [[:credentials], [:opts, "{ }"]],
+ :instance => [[:credentials], [:opts]],
+ :create_instance => [[:credentials], [:image_id], [:opts, "{ }"]],
+ :destroy_instance => [[:credentials], [:id]],
+ :stop_instance => [[:credentials], [:id]],
+ :start_instance => [[:credentials], [:id]],
+ :reboot_instance => [[:credentials], [:id]],
+ }
+
+ Deltacloud.drivers.keys.each do |key|
+ METHODS.each do |m, definition|
+ it "should have the correct parameters for the :#{m} method in #{key} driver" do
+ next unless Deltacloud.new(key).backend.respond_to? m
+ Arguments.names(Deltacloud.new(key).backend.class, m).must_equal definition
+ end
+ end
+ end
+
+end
diff --git a/server/tests/api/library_test.rb b/server/tests/api/library_test.rb
new file mode 100644
index 0000000..72d00ef
--- /dev/null
+++ b/server/tests/api/library_test.rb
@@ -0,0 +1,48 @@
+require_relative './common'
+require_relative '../../lib/deltacloud'
+
+require 'pp'
+
+describe 'Deltacloud API Library' do
+
+ it 'should return the driver configuration' do
+ Deltacloud.must_respond_to :drivers
+ Deltacloud.drivers.wont_be_nil
+ Deltacloud.drivers.must_be_kind_of Hash
+ end
+
+ it 'should be constructed just using the driver parameter' do
+ Deltacloud.new(:mock).must_be_instance_of Deltacloud::Library
+ Deltacloud.new(:mock).current_provider.must_be_nil
+ Deltacloud.new(:mock).current_driver.must_equal 'mock'
+ Deltacloud.new(:mock).backend.must_be_instance_of Deltacloud::Drivers::Mock::MockDriver
+ Deltacloud.new(:mock).credentials.user.must_equal 'mockuser'
+ Deltacloud.new(:mock).credentials.password.must_equal 'mockpassword'
+ end
+
+ it 'should allow to set credentials' do
+ Deltacloud.new(:mock, :user => 'testuser', :password => 'testpassword').credentials.user.must_equal 'testuser'
+ Deltacloud.new(:mock, :user => 'testuser', :password => 'testpassword').credentials.password.must_equal 'testpassword'
+ end
+
+ it 'should allow to set the provider' do
+ Deltacloud.new(:mock, :provider => 'someprovider').current_provider.must_equal 'someprovider'
+ Deltacloud.new(:mock).current_provider.must_be_nil
+ end
+
+ it 'should return pre-defined providers for the driver' do
+ Deltacloud.new(:ec2).providers[:entrypoints].must_be_kind_of Hash
+ end
+
+ it 'should yield the backend driver' do
+ Deltacloud.new :mock do |mock|
+ mock.must_be_instance_of Deltacloud::Drivers::Mock::MockDriver
+ end
+ end
+
+ it 'should return the API version' do
+ Deltacloud::API_VERSION.wont_be_empty
+ Deltacloud::new(:mock).version.wont_be_empty
+ end
+
+end
--
1.7.10.1