[slicehost] convert request specs to shindo

This commit is contained in:
geemus (Wesley Beary) 2010-03-28 17:04:38 -07:00
parent 7746761043
commit 464ee5ab00
23 changed files with 337 additions and 261 deletions

View File

@ -1,30 +0,0 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe 'Slicehost.create_slice' do
describe 'success' do
after(:each) do
eventually(128) do
Slicehost[:slices].delete_slice(@slice_id)
end
end
it "should return proper attributes" do
# flavor_id 1: 256 ram, image_id 3: Gentoo 2008.0
actual = Slicehost[:slices].create_slice(1, 3, 'fog_create_slice').body
actual['addresses'].should be_a(Array)
# actual['backup-id'].should be_an(Integer)
actual['bw-in'].should be_an(Float)
actual['bw-out'].should be_an(Float)
actual['flavor-id'].should be_an(Integer)
actual['id'].should be_an(Integer)
@slice_id = actual['id']
actual['image-id'].should be_an(Integer)
actual['name'].should be_an(String)
actual['progress'].should be_an(Integer)
actual['root-password'].should be_a(String)
actual['status'].should be_an(String)
end
end
end

View File

@ -1,18 +0,0 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe 'Slicehost.delete_slice' do
describe 'success' do
before(:each) do
# flavor_id 1: 256 ram, image_id 3: Gentoo 2008.0
@slice_id = Slicehost[:slices].create_slice(1, 3, 'fog_delete_slice').body['id']
end
it "should return proper attributes" do
eventually(128) do
actual = Slicehost[:slices].delete_slice(@slice_id)
end
end
end
end

View File

@ -1,17 +0,0 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe 'Slicehost.get_backups' do
describe 'success' do
it "should return proper attributes" do
actual = Slicehost[:slices].get_backups.body
actual['backups'].should be_an(Array)
backup = actual['backups'].first
# backup['date'].should be_a(String)
# backup['id'].should be_an(Integer)
# backup['name'].should be_an(String)
# backup['slice-id'].should be_an(Integer)
end
end
end

View File

@ -1,24 +0,0 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe 'Slicehost.get_flavor' do
describe 'success' do
it "should return proper attributes" do
actual = Slicehost[:slices].get_flavor(1).body
actual['id'].should be_an(Integer)
actual['name'].should be_an(String)
actual['price'].should be_a(Integer)
actual['ram'].should be_an(Integer)
end
end
describe 'failure' do
it "should raise a Forbidden error if the flavor does not exist" do
lambda {
Slicehost[:slices].get_flavor(0)
}.should raise_error(Excon::Errors::Forbidden)
end
end
end

View File

@ -1,17 +0,0 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe 'Slicehost.get_flavors' do
describe 'success' do
it "should return proper attributes" do
actual = Slicehost[:slices].get_flavors.body
actual['flavors'].should be_an(Array)
flavor = actual['flavors'].first
flavor['id'].should be_an(Integer)
flavor['name'].should be_an(String)
flavor['price'].should be_a(Integer)
flavor['ram'].should be_an(Integer)
end
end
end

View File

@ -1,24 +0,0 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe 'Slicehost.get_image' do
describe 'success' do
it "should return proper attributes" do
actual = Slicehost[:slices].get_image.body
actual['id'].should be_an(Integer)
actual['name'].should be_a(String)
end
end
describe 'failure' do
it "should raise a Forbidden error if the flavor does not exist" do
lambda {
Slicehost[:slices].get_image(0)
}.should raise_error(Excon::Errors::Forbidden)
end
end
end

View File

@ -1,15 +0,0 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe 'Slicehost.get_images' do
describe 'success' do
it "should return proper attributes" do
actual = Slicehost[:slices].get_images.body
actual['images'].should be_an(Array)
image = actual['images'].first
image['id'].should be_an(Integer)
image['name'].should be_a(String)
end
end
end

View File

@ -1,41 +0,0 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe 'Slicehost.get_slice' do
describe 'success' do
before(:each) do
# flavor_id 1: 256 ram, image_id 3: Gentoo 2008.0
@slice_id = Slicehost[:slices].create_slice(1, 3, 'fog_create_slice').body['id']
end
after(:each) do
eventually(128) do
Slicehost[:slices].delete_slice(@slice_id)
end
end
it "should return proper attributes" do
actual = Slicehost[:slices].get_slice(@slice_id).body
actual['addresses'].should be_a(Array)
# actual['backup-id'].should be_an(Integer)
actual['bw-in'].should be_a(Float)
actual['bw-out'].should be_a(Float)
actual['flavor-id'].should be_an(Integer)
actual['id'].should be_an(Integer)
actual['image-id'].should be_an(Integer)
actual['name'].should be_an(String)
actual['progress'].should be_an(Integer)
actual['status'].should be_an(String)
end
end
describe 'failure' do
it "should raise a Forbidden error if the server does not exist" do
lambda {
Slicehost[:slices].get_slice(0)
}.should raise_error(Excon::Errors::Forbidden)
end
end
end

View File

@ -1,34 +0,0 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe 'Slicehost.get_slices' do
describe 'success' do
before(:each) do
# flavor_id 1: 256 ram, image_id 3: Gentoo 2008.0
@slice_id = Slicehost[:slices].create_slice(1, 3, 'fog_create_slice').body['id']
end
after(:each) do
eventually(128) do
Slicehost[:slices].delete_slice(@slice_id)
end
end
it "should return proper attributes" do
actual = Slicehost[:slices].get_slices.body
actual['slices'].should be_an(Array)
slice = actual['slices'].first
slice['addresses'].should be_a(Array)
# slice['backup-id'].should be_an(Integer)
slice['bw-in'].should be_a(Float)
slice['bw-out'].should be_a(Float)
slice['flavor-id'].should be_an(Integer)
slice['id'].should be_an(Integer)
slice['image-id'].should be_an(Integer)
slice['name'].should be_an(String)
slice['progress'].should be_an(Integer)
slice['status'].should be_an(String)
end
end
end

View File

@ -1,41 +0,0 @@
require File.dirname(__FILE__) + '/../../spec_helper'
describe 'Slicehost.reboot_slice' do
describe 'success' do
before(:each) do
# flavor_id 1: 256 ram, image_id 3: Gentoo 2008.0
@slice_id = Slicehost[:slices].create_slice(1, 3, 'fog_create_slice').body['id']
wait_for { Slicehost[:slices].get_slice(@slice_id).body['status'] == 'active' }
end
after(:each) do
wait_for { Slicehost[:slices].get_slice(@slice_id).body['status'] == 'active' }
Slicehost[:slices].delete_slice(@slice_id)
end
it "should return proper attributes" do
actual = Slicehost[:slices].reboot_slice(@slice_id).body
actual['addresses'].should be_a(Array)
# actual['backup-id'].should be_an(Integer)
actual['bw-in'].should be_a(Float)
actual['bw-out'].should be_a(Float)
actual['flavor-id'].should be_an(Integer)
actual['id'].should be_an(Integer)
actual['image-id'].should be_an(Integer)
actual['name'].should be_an(String)
actual['progress'].should be_an(Integer)
actual['status'].should be_an(String)
end
end
describe 'failure' do
it "should raise a Forbidden error if the server does not exist" do
lambda {
Slicehost[:slices].reboot_slice(0)
}.should raise_error(Excon::Errors::Forbidden)
end
end
end

View File

@ -0,0 +1,19 @@
Shindo.tests('Slicehost#create_slice', 'slicehost') do
tests('success') do
before do
@data = Slicehost[:slices].create_slice(1, 3, 'fogcreateslice').body
@id = @data['id']
end
after do
wait_for { Slicehost[:slices].get_slice(@id).body['status'] == 'active' }
Slicehost[:slices].delete_slice(@id)
end
test('has proper output format') do
validate_data_format(@data, Slicehost::Formats::SLICE.merge('root-password' => String))
end
end
end

View File

@ -0,0 +1,28 @@
Shindo.tests('Slicehost#delete_slice', 'slicehost') do
tests('success') do
before do
@data = Slicehost[:slices].create_slice(1, 3, 'fogdeleteslice').body
@id = @data['id']
end
test('has proper output format') do
wait_for { Slicehost[:slices].get_slice(@id).body['status'] == 'active' }
Slicehost[:slices].delete_slice(@id)
end
end
tests('failure') do
test('raises NotFound error if slice does not exist') do
begin
Slicehost[:slices].delete_slice(0)
false
rescue Excon::Errors::NotFound
true
end
end
end
end

View File

@ -0,0 +1,14 @@
Shindo.tests('Slicehost#get_backups', 'slicehost') do
tests('success') do
before do
@data = Slicehost[:slices].get_backups.body
end
# TODO: ensure this still works with a non-empty list
test('has proper output format') do
validate_data_format(@data, { 'backups' => [Slicehost::Formats::BACKUP] })
end
end
end

View File

@ -0,0 +1,23 @@
Shindo.tests('Slicehost#get_flavor', 'slicehost') do
tests('success') do
test('has proper output format') do
@data = Slicehost[:slices].get_flavor(1).body
validate_data_format(@data, Slicehost::Formats::FLAVOR)
end
end
tests('failure') do
test('raises Forbidden error if flavor does not exist') do
begin
Slicehost[:slices].get_flavor(0)
false
rescue Excon::Errors::Forbidden
true
end
end
end
end

View File

@ -0,0 +1,10 @@
Shindo.tests('Slicehost#get_flavors', 'slicehost') do
tests('success') do
test('has proper output format') do
@data = Slicehost[:slices].get_flavors.body
validate_data_format(@data, { 'flavors' => [Slicehost::Formats::FLAVOR] })
end
end
end

View File

@ -0,0 +1,23 @@
Shindo.tests('Slicehost#get_image', 'slicehost') do
tests('success') do
test('has proper output format') do
@data = Slicehost[:slices].get_image(3).body
validate_data_format(@data, Slicehost::Formats::IMAGE)
end
end
tests('failure') do
test('raises Forbidden error if flavor does not exist') do
begin
Slicehost[:slices].get_image(0)
false
rescue Excon::Errors::Forbidden
true
end
end
end
end

View File

@ -0,0 +1,10 @@
Shindo.tests('Slicehost#get_images', 'slicehost') do
tests('success') do
test('has proper output format') do
@data = Slicehost[:slices].get_images.body
validate_data_format(@data, { 'images' => [Slicehost::Formats::IMAGE] })
end
end
end

View File

@ -0,0 +1,33 @@
Shindo.tests('Slicehost#get_slice', 'slicehost') do
tests('success') do
before do
@data = Slicehost[:slices].create_slice(1, 3, 'foggetslice').body
@id = @data['id']
end
after do
wait_for { Slicehost[:slices].get_slice(@id).body['status'] == 'active' }
Slicehost[:slices].delete_slice(@id)
end
test('has proper output format') do
@data = Slicehost[:slices].get_slice(@id).body
validate_data_format(@data, Slicehost::Formats::SLICE)
end
end
tests('failure') do
test('raises Forbidden error if flavor does not exist') do
begin
Slicehost[:slices].get_slice(0)
false
rescue Excon::Errors::Forbidden
true
end
end
end
end

View File

@ -0,0 +1,10 @@
Shindo.tests('Slicehost#get_slices', 'slicehost') do
tests('success') do
test('has proper output format') do
@data = Slicehost[:slices].get_slices.body
validate_data_format(@data, { 'slices' => [Slicehost::Formats::SLICE] })
end
end
end

View File

@ -0,0 +1,34 @@
Shindo.tests('Slicehost#reboot_slice', 'slicehost') do
tests('success') do
before do
@data = Slicehost[:slices].create_slice(1, 3, 'fogrebootslice').body
@id = @data['id']
wait_for { Slicehost[:slices].get_slice(@id).body['status'] == 'active' }
@data = Slicehost[:slices].reboot_slice(@id).body
end
after do
wait_for { Slicehost[:slices].get_slice(@id).body['status'] == 'active' }
Slicehost[:slices].delete_slice(@id)
end
test('has proper output format') do
validate_data_format(@data, Slicehost::Formats::SLICE)
end
end
tests('failure') do
test('raises Forbidden error if flavor does not exist') do
begin
Slicehost[:slices].reboot_slice(0)
false
rescue Excon::Errors::Forbidden
true
end
end
end
end

View File

@ -0,0 +1,51 @@
module Slicehost
def self.[](service)
@@connections ||= Hash.new do |hash, key|
credentials = Fog.credentials.reject do |k, v|
![:slicehost_password].include?(k)
end
hash[key] = case key
when :slices
Fog::Slicehost.new(credentials)
end
end
@@connections[service]
end
module Formats
BACKUP = {
'date' => String,
'id' => Integer,
'name' => String,
'slice-id' => Integer
}
FLAVOR = {
'id' => Integer,
'name' => String,
'price' => Integer,
'ram' => Integer
}
IMAGE = {
'id' => Integer,
'name' => String
}
SLICE = {
'addresses' => [String],
'bw-in' => Float,
'bw-out' => Float,
'flavor-id' => Integer,
'id' => Integer,
'image-id' => Integer,
'name' => String,
'progress' => Integer,
'status' => String
}
end
end

43
tests/test_helper.rb Normal file
View File

@ -0,0 +1,43 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'fog'))
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'fog', 'bin'))
# TODO: Currently is true even if some of the keys in format do not appear in
def validate_data_format(original_data, format)
valid = true
data = original_data.dup
for key, value in format
valid &&= data.has_key?(key)
datum = data.delete(key)
case value
when Array
valid &&= datum.is_a?(Array)
for element in datum
type = value.first
if type.is_a?(Hash)
valid &&= validate_data_format({:element => element}, {:element => type})
else
valid &&= element.is_a?(type)
end
end
when Hash
valid &&= datum.is_a?(Hash)
valid &&= validate_data_format(datum, value)
else
valid &&= datum.is_a?(value)
end
end
unless data.empty?
p "validate_data_format did not validate [#{data.inspect}]"
end
valid &&= data.empty?
end
def wait_for(timeout = 600, &block)
start = Time.now
until instance_eval(&block)
if Time.now - start > timeout
break
end
sleep(1)
end
end

View File

@ -0,0 +1,39 @@
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
Shindo.tests('test_helper') do
tests('#validate_data_format') do
tests('returns true') do
test('when format of value matches') do
validate_data_format({:a => :b}, {:a => Symbol})
end
test('when format of nested array elements matches') do
validate_data_format({:a => [:b, :c]}, {:a => [Symbol]})
end
test('when format of nested hash matches') do
validate_data_format({:a => {:b => :c}}, {:a => {:b => Symbol}})
end
end
tests('returns false') do
test('when format of value does not match') do
!validate_data_format({:a => :b}, {:a => String})
end
test('when not all keys are checked') do
!validate_data_format({:a => :b}, {})
end
test('when some keys do not appear') do
!validate_data_format({}, {:a => String})
end
end
end
end