1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Merge pull request #1697 from rackspace/ready_exception

[rackspace|compute_v2] Ready Exception
This commit is contained in:
Kyle Rames 2013-03-29 11:20:25 -07:00
commit ed34ead1e7
6 changed files with 131 additions and 4 deletions

View file

@ -8,6 +8,29 @@ module Fog
class InternalServerError < Fog::Rackspace::Errors::InternalServerError; end
class BadRequest < Fog::Rackspace::Errors::BadRequest; end
class InvalidStateException < ::RuntimeError
attr_reader :desired_state
attr_reader :current_state
def initialize(desired_state, current_state)
@desired_state = desired_state
@current_state = current_state
end
end
class InvalidServerStateException < InvalidStateException
def to_s
"Server should have transitioned to '#{desired_state}' not '#{state}'"
end
end
class InvalidImageStateException < InvalidStateException
def to_s
"Image should have transitioned to '#{desired_state}' not '#{state}'"
end
end
DFW_ENDPOINT = 'https://dfw.servers.api.rackspacecloud.com/v2'
ORD_ENDPOINT = 'https://ord.servers.api.rackspacecloud.com/v2'
LON_ENDPOINT = 'https://lon.servers.api.rackspacecloud.com/v2'

View file

@ -93,10 +93,11 @@ module Fog
# @raise [Fog::Rackspace::Errors::BadRequest] - HTTP 400
# @raise [Fog::Rackspace::Errors::InternalServerError] - HTTP 500
# @raise [Fog::Rackspace::Errors::ServiceError]
# @raise [Fog::Compute::RackspaceV2::InvalidImageStateException] if server state is an error state
def ready?(ready_state = ACTIVE, error_states=[ERROR])
if error_states
error_states = Array(error_states)
raise "Image should have transitioned to '#{ready_state}' not '#{state}'" if error_states.include?(state)
raise InvalidImageStateException.new(ready_state, state) if error_states.include?(state)
end
state == ready_state
end

View file

@ -343,11 +343,11 @@ module Fog
# @param [String] ready_state By default state is ACTIVE
# @param [Array,String] error_states By default state is ERROR
# @return [Boolean] returns true if server is in a ready state
# @raise [RuntimeException] if server state is an error state
# @raise [Fog::Compute::RackspaceV2::InvalidServerStateException] if server state is an error state
def ready?(ready_state = ACTIVE, error_states=[ERROR])
if error_states
error_states = Array(error_states)
raise "Server should have transitioned to '#{ready_state}' not '#{state}'" if error_states.include?(state)
raise InvalidServerStateException.new(ready_state, state) if error_states.include?(state)
end
state == ready_state
end

View file

@ -36,7 +36,8 @@ module Fog
# :image_id => service.images.find {|img| img.name =~ /Ubuntu/}.id,
# :public_key_path => '~/.ssh/fog_rsa.pub',
# :private_key_path => '~/.ssh/fog_rsa'
#
#
# @raise [Fog::Compute::RackspaceV2::InvalidServerStateException] if server state is an error state
def bootstrap(new_attributes = {})
server = create(new_attributes)
server.wait_for(1500) { ready? && !public_ip_address.empty? }

View file

@ -8,6 +8,57 @@ Shindo.tests('Fog::Compute::RackspaceV2 | image', ['rackspace']) do
:image_id => rackspace_test_image_id(service)
}
tests('ready?') do
@server = Fog::Compute::RackspaceV2::Image.new
tests('default in ready state').returns(true) do
@server.state = Fog::Compute::RackspaceV2::Image::ACTIVE
@server.ready?
end
tests('custom ready state').returns(true) do
@server.state = Fog::Compute::RackspaceV2::Image::SAVING
@server.ready?(Fog::Compute::RackspaceV2::Image::SAVING)
end
tests('default NOT in ready state').returns(false) do
@server.state = Fog::Compute::RackspaceV2::Image::SAVING
@server.ready?
end
tests('custom NOT ready state').returns(false) do
@server.state = Fog::Compute::RackspaceV2::Image::UNKNOWN
@server.ready?(Fog::Compute::RackspaceV2::Image::SAVING)
end
tests('default error state').returns(true) do
@server.state = Fog::Compute::RackspaceV2::Image::ERROR
exception_occurred = false
begin
@server.ready?
rescue Fog::Compute::RackspaceV2::InvalidImageStateException => e
exception_occurred = true
returns(true) {e.desired_state == Fog::Compute::RackspaceV2::Image::ACTIVE }
returns(true) {e.current_state == Fog::Compute::RackspaceV2::Image::ERROR }
end
exception_occurred
end
tests('custom error state').returns(true) do
@server.state = Fog::Compute::RackspaceV2::Image::UNKNOWN
exception_occurred = false
begin
@server.ready?(Fog::Compute::RackspaceV2::Image::SAVING, Fog::Compute::RackspaceV2::Image::UNKNOWN)
rescue Fog::Compute::RackspaceV2::InvalidImageStateException => e
exception_occurred = true
returns(true) {e.desired_state == Fog::Compute::RackspaceV2::Image::SAVING }
returns(true) {e.current_state == Fog::Compute::RackspaceV2::Image::UNKNOWN }
end
exception_occurred
end
end
tests("success") do
begin
server = service.servers.create(options)

View file

@ -9,6 +9,57 @@ Shindo.tests('Fog::Compute::RackspaceV2 | server', ['rackspace']) do
:metadata => { 'fog_test' => 'true' }
}
tests('ready?') do
@server = Fog::Compute::RackspaceV2::Server.new
tests('default in ready state').returns(true) do
@server.state = Fog::Compute::RackspaceV2::Server::ACTIVE
@server.ready?
end
tests('custom ready state').returns(true) do
@server.state = Fog::Compute::RackspaceV2::Server::VERIFY_RESIZE
@server.ready?(Fog::Compute::RackspaceV2::Server::VERIFY_RESIZE)
end
tests('default NOT in ready state').returns(false) do
@server.state = Fog::Compute::RackspaceV2::Server::REBOOT
@server.ready?
end
tests('custom NOT ready state').returns(false) do
@server.state = Fog::Compute::RackspaceV2::Server::REBOOT
@server.ready?(Fog::Compute::RackspaceV2::Server::VERIFY_RESIZE)
end
tests('default error state').returns(true) do
@server.state = Fog::Compute::RackspaceV2::Server::ERROR
exception_occurred = false
begin
@server.ready?
rescue Fog::Compute::RackspaceV2::InvalidServerStateException => e
exception_occurred = true
returns(true) {e.desired_state == Fog::Compute::RackspaceV2::Server::ACTIVE }
returns(true) {e.current_state == Fog::Compute::RackspaceV2::Server::ERROR }
end
exception_occurred
end
tests('custom error state').returns(true) do
@server.state = Fog::Compute::RackspaceV2::Server::ACTIVE
exception_occurred = false
begin
@server.ready?(Fog::Compute::RackspaceV2::Server::VERIFY_RESIZE, Fog::Compute::RackspaceV2::Server::ACTIVE)
rescue Fog::Compute::RackspaceV2::InvalidServerStateException => e
exception_occurred = true
returns(true) {e.desired_state == Fog::Compute::RackspaceV2::Server::VERIFY_RESIZE }
returns(true) {e.current_state == Fog::Compute::RackspaceV2::Server::ACTIVE }
end
exception_occurred
end
end
model_tests(service.servers, options, true) do
@instance.wait_for(timeout=1500) { ready? }