From 676fb4d760a4177b2f4325fee66c691fdf172cc9 Mon Sep 17 00:00:00 2001 From: geemus Date: Thu, 3 Jun 2010 21:32:59 -0700 Subject: [PATCH] [bbg] request tests --- lib/fog/bluebox/models/server.rb | 10 +++- lib/fog/bluebox/models/servers.rb | 2 +- lib/fog/bluebox/requests/create_block.rb | 11 +++- tests/bluebox/helper.rb | 26 +++++++++ tests/bluebox/requests/block_tests.rb | 68 ++++++++++++++++++++++++ tests/bluebox/requests/product_tests.rb | 25 +++++++++ tests/bluebox/requests/template_tests.rb | 32 +++++++++++ tests/helper.rb | 4 ++ tests/helper_tests.rb | 4 ++ 9 files changed, 178 insertions(+), 4 deletions(-) create mode 100644 tests/bluebox/helper.rb create mode 100644 tests/bluebox/requests/block_tests.rb create mode 100644 tests/bluebox/requests/product_tests.rb create mode 100644 tests/bluebox/requests/template_tests.rb diff --git a/lib/fog/bluebox/models/server.rb b/lib/fog/bluebox/models/server.rb index cf566a316..730118847 100644 --- a/lib/fog/bluebox/models/server.rb +++ b/lib/fog/bluebox/models/server.rb @@ -16,7 +16,8 @@ module Fog attribute :ips attribute :status attribute :flavor_id - attribute :image_id + # attribute :image_id + attribute :template # Not reported by the API, but used at create time attr_accessor :name, :password, :hash, :text, :error @@ -53,6 +54,13 @@ module Fog merge_attributes(data.body) true end + + private + + def product=(new_product) + @flavor_id = new_product['id'] + end + end end diff --git a/lib/fog/bluebox/models/servers.rb b/lib/fog/bluebox/models/servers.rb index fe0c083e0..13954e89e 100644 --- a/lib/fog/bluebox/models/servers.rb +++ b/lib/fog/bluebox/models/servers.rb @@ -29,7 +29,7 @@ module Fog if server_id && server = connection.get_block(server_id).body new(server) end - rescue Fog::BlueBox::NotFound + rescue Fog::Bluebox::NotFound nil end diff --git a/lib/fog/bluebox/requests/create_block.rb b/lib/fog/bluebox/requests/create_block.rb index 200305734..7f8db909a 100644 --- a/lib/fog/bluebox/requests/create_block.rb +++ b/lib/fog/bluebox/requests/create_block.rb @@ -24,11 +24,18 @@ module Fog 'template' => template_id }.merge!(options) + query = '' + for key, value in data + query << "#{key}=#{CGI.escape(value.to_s).gsub(/\+/, '%20')}&" + end + query.chop! + request( - :body => data.to_json, + # :body => data.to_json, :expects => 200, :method => 'POST', - :path => '/api/blocks.json' + :path => '/api/blocks.json', + :query => query ) end diff --git a/tests/bluebox/helper.rb b/tests/bluebox/helper.rb new file mode 100644 index 000000000..778cc7a15 --- /dev/null +++ b/tests/bluebox/helper.rb @@ -0,0 +1,26 @@ +module Bluebox + + def self.[](service) + @@connections ||= Hash.new do |hash, key| + credentials = Fog.credentials.reject do |k,v| + ![:bluebox_api_key, :bluebox_customer_id].include?(k) + end + hash[key] = case key + when :blocks + Fog::Bluebox.new(credentials) + end + end + @@connections[service] + end + + module Formats + + PRODUCT = { + 'cost' => Float, + 'description' => String, + 'id' => String + } + + end + +end \ No newline at end of file diff --git a/tests/bluebox/requests/block_tests.rb b/tests/bluebox/requests/block_tests.rb new file mode 100644 index 000000000..9f8e349f7 --- /dev/null +++ b/tests/bluebox/requests/block_tests.rb @@ -0,0 +1,68 @@ +Shindo.tests('Bluebox | block requests', ['bluebox']) do + + @block_format = { + 'cpu' => Float, + 'hostname' => String, + 'id' => String, + 'ips' => [{'address' => String}], + 'memory' => Integer, + 'product' => Bluebox::Formats::PRODUCT, + 'status' => String, + 'storage' => Integer, + 'template' => String, + + } + + tests('success') do + + @product_id = '94fd37a7-2606-47f7-84d5-9000deda52ae' # 1 GB + @template_id = 'a00baa8f-b5d0-4815-8238-b471c4c4bf72' # Ubuntu 9.10 64bit + @password = 'R8p6ikXOfCxoKy' + + @block_id = nil + + tests("create_block('#{@product_id}', '#{@template_id}', 'fog_block', 'password' => '#{@password}')").formats(@block_format) do + data = Bluebox[:blocks].create_block(@product_id, @template_id, 'fog_block', 'password' => @password).body + @block_id = data['id'] + data + end + + Bluebox[:blocks].servers.get(@block_id).wait_for { ready? } + + tests("get_block('#{@block_id}')").formats(@block_format) do + Bluebox[:blocks].get_block(@block_id).body + end + + tests("get_blocks").formats([@block_format.reject {|key,value| ['product', 'template'].include?(key)}]) do + Bluebox[:blocks].get_blocks.body + end + + tests("reboot_block('#{@block_id}')").formats({'status' => String, 'text' => String}) do + Bluebox[:blocks].reboot_block(@block_id).body + end + + Bluebox[:blocks].servers.get(@block_id).wait_for { ready? } + + tests("destroy_block('#{@block_id})'").formats({'text' => String}) do + Bluebox[:blocks].destroy_block(@block_id).body + end + + end + + tests('failure') do + + tests("get_block('00000000-0000-0000-0000-000000000000')").raises(Fog::Bluebox::NotFound) do + Bluebox[:blocks].get_block('00000000-0000-0000-0000-000000000000') + end + + tests("reboot_block('00000000-0000-0000-0000-000000000000')").raises(Fog::Bluebox::NotFound) do + Bluebox[:blocks].reboot_block('00000000-0000-0000-0000-000000000000') + end + + tests("destroy_block('00000000-0000-0000-0000-000000000000')").raises(Fog::Bluebox::NotFound) do + Bluebox[:blocks].destroy_block('00000000-0000-0000-0000-000000000000') + end + + end + +end diff --git a/tests/bluebox/requests/product_tests.rb b/tests/bluebox/requests/product_tests.rb new file mode 100644 index 000000000..4e5ef144a --- /dev/null +++ b/tests/bluebox/requests/product_tests.rb @@ -0,0 +1,25 @@ +Shindo.tests('Bluebox | product requests', ['bluebox']) do + + tests('success') do + + @product_id = '94fd37a7-2606-47f7-84d5-9000deda52ae' # 1 GB + + tests("get_product('#{@product_id}')").formats(Bluebox::Formats::PRODUCT) do + Bluebox[:blocks].get_product(@product_id).body + end + + tests("get_products").formats([Bluebox::Formats::PRODUCT]) do + Bluebox[:blocks].get_products.body + end + + end + + tests('failure') do + + tests("get_product('00000000-0000-0000-0000-000000000000')").raises(Fog::Bluebox::NotFound) do + Bluebox[:blocks].get_product('00000000-0000-0000-0000-000000000000') + end + + end + +end diff --git a/tests/bluebox/requests/template_tests.rb b/tests/bluebox/requests/template_tests.rb new file mode 100644 index 000000000..45a458761 --- /dev/null +++ b/tests/bluebox/requests/template_tests.rb @@ -0,0 +1,32 @@ +Shindo.tests('Bluebox | template requests', ['bluebox']) do + + @template_format = { + 'created' => String, + 'description' => String, + 'id' => String, + 'public' => Fog::Boolean + } + + tests('success') do + + @template_id = 'a00baa8f-b5d0-4815-8238-b471c4c4bf72' # Ubuntu 9.10 64bit + + tests("get_template('#{@template_id}')").formats(@template_format) do + Bluebox[:blocks].get_template(@template_id).body + end + + tests("get_templates").formats([@template_format]) do + Bluebox[:blocks].get_templates.body + end + + end + + tests('failure') do + + tests("get_template('00000000-0000-0000-0000-000000000000')").raises(Fog::Bluebox::NotFound) do + Bluebox[:blocks].get_template('00000000-0000-0000-0000-000000000000') + end + + end + +end diff --git a/tests/helper.rb b/tests/helper.rb index 193baa8b6..341052dbc 100644 --- a/tests/helper.rb +++ b/tests/helper.rb @@ -35,6 +35,10 @@ module Shindo valid = true data = original_data.dup format = original_format.dup + if format.is_a?(Array) + data = {:element => data} + format = {:element => format} + end for key, value in format valid &&= data.has_key?(key) datum = data.delete(key) diff --git a/tests/helper_tests.rb b/tests/helper_tests.rb index 3b3694e59..64ff8cc93 100644 --- a/tests/helper_tests.rb +++ b/tests/helper_tests.rb @@ -16,6 +16,10 @@ Shindo.tests('test_helper', 'meta') do formats_kernel({:a => {:b => :c}}, {:a => {:b => Symbol}}) end + test('when format of an array') do + formats_kernel([{:a => :b}], [{:a => Symbol}]) + end + end tests('returns false') do