You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by Michal Fojtik <mf...@redhat.com> on 2013/03/11 12:09:55 UTC
New deltacloud-client feature
Hi,
Today I want to introduce you a new 'cool' feature of the new
deltacloud-client code I've put together.
Last week I discovered that you can use MockSession[1] with
combination with the Rack::Test::Session[2] from rack-test
to completely mock the Deltacloud::API server. How does it work?
require 'deltacloud_rack'
# ... initialize DC server ...
#
@app = Rack::Test::Session.new(
Rack::MockSession.new(Deltacloud::API)
)
@app.get '/instances' # => XML with list of instances
The last '@app#get' method *does not* do any 'real' HTTP request
but it is querying the internal Sinatra::Base methods to get the
response.
This in fact allow us to hook up the whole Deltacloud::API server into
the Faraday middleware[3] and instead of doing a 'real' request do DC
API, fire all requests to the mock DC server mounted there.
I know it sounds complicated :-) so I will demonstrate what you can do
with this stuff:
**Without** starting a new Deltacloud API server, run the 'rake console'
task inside deltacloud-client repository:
[1] pry(main)> c = Deltacloud::Connect :mock, 'mockuser', 'mockpassword'
[2] pry(main)> c.realms
=> [#<Deltacloud::Client::Realm> {:state=>"AVAILABLE", :limit=>nil,
:name=>"United States", :description=>nil, :_id=>"us"},
#<Deltacloud::Client::Realm> {:state=>"AVAILABLE", :limit=>nil,
:name=>"Europe", :description=>nil, :_id=>"eu"}]
It is not just realms. **All** unit tests for client pass with this
mocked Deltacloud::API. That means you can create instances, list images,
create blobs as usual, without DC running and without using the internal
DC driver API.
However there are 2 problems (there are always 2 problems ;-)
1. If you run the Deltacloud::Connect method, it took approx. 6 seconds
on my laptop to require all drivers/collections/etc. (but that is only
first time, next time you call that method is fast)
2. You need to have 'deltacloud-core' and 'rack-test' installed *as gems*.
If you want to play with this stuff, just clone my github[4] repo and run:
$ bundle
$ gem install deltacloud-core rack-test
$ rake console
Then you can run the code above :-)
Cheers,
-- Michal
[1] http://rubydoc.info/gems/rack-test/0.5.6/Rack/MockSession
[2] http://rdoc.info/github/brynary/rack-test/master/Rack/Test/Session
[3] https://github.com/mifo/deltacloud-client/blob/master/lib/deltacloud/server.rb
[4] https://github.com/mifo/deltacloud-client
--
Michal Fojtik <mf...@redhat.com>
Deltacloud API, CloudForms