Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Split Error into TimeoutError and CommandFailedError

  • Loading branch information...
commit 0209206b229d7532d6438922cc454b4d5f2d530a 1 parent 3c2bcd8
Sam Umbach and Larry Karnowski authored
2  lib/slushy.rb
View
@@ -2,4 +2,6 @@ module Slushy; end
require 'slushy/version'
require 'slushy/error'
+require 'slushy/command_failed_error'
+require 'slushy/timeout_error'
require 'slushy/instance'
2  lib/slushy/command_failed_error.rb
View
@@ -0,0 +1,2 @@
+class Slushy::CommandFailedError < Slushy::Error
+end
22 lib/slushy/instance.rb
View
@@ -5,7 +5,7 @@ class Slushy::Instance
def self.launch(connection, config)
server = connection.servers.create(config)
- server.wait_for { ready? } or raise Slushy::Error.new("Timeout launching server #{server.id}")
+ server.wait_for { ready? } or raise Slushy::TimeoutError.new("Timeout launching server #{server.id}")
new(connection, server.id)
end
@@ -34,23 +34,23 @@ def snapshot(name, description)
response = connection.create_image(instance_id, name, description)
image_id = response.body["imageId"]
image = connection.images.get(image_id)
- image.wait_for(3600) { ready? } or raise Slushy::Error.new("Timeout creating snapshot #{image_id}")
+ image.wait_for(3600) { ready? } or raise Slushy::TimeoutError.new("Timeout creating snapshot #{image_id}")
image_id
end
def terminate
server.destroy
- server.wait_for { state == "terminated" } or raise Slushy::Error.new("Timeout terminating server #{server.id}")
+ server.wait_for { state == "terminated" } or raise Slushy::TimeoutError.new("Timeout terminating server #{server.id}")
end
def stop
server.stop
- server.wait_for { state == "stopped" } or raise Slushy::Error.new("Timeout stopping server #{server.id}")
+ server.wait_for { state == "stopped" } or raise Slushy::TimeoutError.new("Timeout stopping server #{server.id}")
end
def wait_for_connectivity
puts "Waiting for ssh connectivity..."
- retry_block(5, [Errno::ECONNREFUSED, Timeout::Error], "Connecting to Amazon refused") do
+ retry_block(5, [Errno::ECONNREFUSED, Timeout::Error], Slushy::TimeoutError.new("Timeout connecting to server #{server.id}")) do
sleep 10
Timeout.timeout(60) { ssh('ls') }
end
@@ -63,11 +63,11 @@ def run_command(command)
end
def run_command!(command)
- raise Slushy::Error.new("Failed running '#{command}'") unless run_command(command)
+ raise Slushy::CommandFailedError.new("Failed running '#{command}'") unless run_command(command)
end
def apt_installs
- retry_block(5, [Slushy::Error], "Command 'apt-get' failed") do
+ retry_block(5, [Slushy::CommandFailedError], Slushy::CommandFailedError.new("Command 'apt-get' failed")) do
puts "Updating apt cache..."
run_command!('sudo apt-get update')
puts "Installing ruby..."
@@ -94,7 +94,7 @@ def converge(cookbooks_path) # TODO: find the standard Chef term for this
protected
- def retry_block(times, errors, failure)
+ def retry_block(times, retryable_exceptions, retries_failed_exception)
succeeded = false
attempts = 0
last_error = nil
@@ -102,13 +102,13 @@ def retry_block(times, errors, failure)
begin
retval = yield
succeeded = true
- rescue *errors => e
+ rescue *retryable_exceptions => e
attempts +=1
- puts "#{failure}. Attempting retry #{attempts}..."
+ puts "#{retries_failed_exception.message}. Attempting retry #{attempts}..."
last_error = e
end
end
- raise Slushy::Error.new(failure) unless succeeded
+ raise retries_failed_exception unless succeeded
retval
end
2  lib/slushy/timeout_error.rb
View
@@ -0,0 +1,2 @@
+class Slushy::TimeoutError < Slushy::Error
+end
16 spec/lib/instance_spec.rb
View
@@ -24,7 +24,7 @@ def mock_job(options={})
servers = stub(:create => server)
connection.stub(:servers).and_return(servers)
server.stub(:wait_for).and_return(false)
- lambda { described_class.launch(connection, config) }.should raise_error(Slushy::Error)
+ lambda { described_class.launch(connection, config) }.should raise_error(Slushy::TimeoutError)
end
it "returns the instance object" do
@@ -73,7 +73,7 @@ def mock_job(options={})
connection.stub(:create_image).and_return(response)
images.should_receive(:get).with(:some_ami_id).and_return(image)
image.stub(:wait_for).and_return(false)
- lambda { instance.snapshot(:some_name, :some_description) }.should raise_error(Slushy::Error)
+ lambda { instance.snapshot(:some_name, :some_description) }.should raise_error(Slushy::TimeoutError)
end
end
@@ -90,7 +90,7 @@ def mock_job(options={})
instance.stub(:server).and_return(server)
server.stub(:destroy)
server.stub(:wait_for).and_return(false)
- lambda { instance.terminate }.should raise_error(Slushy::Error)
+ lambda { instance.terminate }.should raise_error(Slushy::TimeoutError)
end
end
@@ -107,11 +107,13 @@ def mock_job(options={})
instance.stub(:server).and_return(server)
server.stub(:stop)
server.stub(:wait_for).and_return(false)
- lambda { instance.stop }.should raise_error(Slushy::Error)
+ lambda { instance.stop }.should raise_error(Slushy::TimeoutError)
end
end
describe '#wait_for_connectivity' do
+ before { instance.stub(:server).and_return(server) }
+
it 'retries if the first attempt fails' do
instance.should_receive(:ssh).ordered.and_raise(Errno::ECONNREFUSED)
instance.should_receive(:ssh).ordered.and_return([mock_job])
@@ -136,7 +138,7 @@ def mock_job(options={})
instance.stub(:sleep).and_return(10)
expect do
capture_stdout { instance.wait_for_connectivity }
- end.to raise_error Slushy::Error
+ end.to raise_error Slushy::TimeoutError
end
end
@@ -147,7 +149,7 @@ def mock_job(options={})
capture_stdout do
expect do
instance.run_command!("ls")
- end.to raise_error Slushy::Error
+ end.to raise_error Slushy::CommandFailedError
end.should =~ /STDERR: FAIL WHALE/
end
end
@@ -168,7 +170,7 @@ def mock_job(options={})
instance.should_receive(:ssh).exactly(5).times.with('sudo apt-get update').and_return([mock_job(:status => 1)])
expect do
capture_stdout { instance.apt_installs }
- end.to raise_error Slushy::Error
+ end.to raise_error Slushy::CommandFailedError
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.