Merge pull request #3 from brightbox/updates_for_tests

Updates for tests removing hardcoded image references
This commit is contained in:
Steve Smith 2012-08-16 01:24:51 -07:00
commit 3a19598730
5 changed files with 124 additions and 8 deletions

View File

@ -101,7 +101,24 @@ module Fog
request :update_server_group
request :update_user
module Shared
# Returns an identifier for the default image for use
#
# Currently tries to find the latest version Ubuntu LTS (i686) widening
# up to the latest, official version of Ubuntu available.
#
# Highly recommended that you actually select the image you want to run
# on your servers yourself!
#
# @return [String, nil]
def default_image
return @default_image_id unless @default_image_id.nil?
@default_image_id = Fog.credentials[:brightbox_default_image] || select_default_image
end
end
class Mock
include Shared
def initialize(options)
@brightbox_client_id = options[:brightbox_client_id] || Fog.credentials[:brightbox_client_id]
@ -111,9 +128,15 @@ module Fog
def request(options)
raise "Not implemented"
end
private
def select_default_image
"img-mockd"
end
end
class Real
include Shared
def initialize(options)
# Currently authentication and api endpoints are the same but may change
@ -180,6 +203,34 @@ module Fog
options[:headers] = headers
@connection.request(options)
end
# Queries the API and tries to select the most suitable official Image
# to use if the user chooses not to select their own.
def select_default_image
return @default_image_id unless @default_image_id.nil?
all_images = Fog::Compute[:brightbox].list_images
official_images = all_images.select {|img| img["official"] == true}
ubuntu_lts_images = official_images.select {|img| img["name"] =~ /Ubuntu.*LTS/}
ubuntu_lts_i686_images = ubuntu_lts_images.select {|img| img["arch"] == "i686"}
if ubuntu_lts_i686_images.empty?
# Accept other architectures
if ubuntu_lts_images.empty?
# Accept non-LTS versions of Ubuntu
unsorted_images = official_images.select {|img| img["name"] =~ /Ubuntu/}
else
unsorted_images = ubuntu_lts_images
end
else
unsorted_images = ubuntu_lts_i686_images
end
# Get the latest and use it's ID for the default image
@default_image_id = unsorted_images.sort {|a,b| a["created_at"] <=> b["created_at"]}.first["id"]
rescue
nil
end
end
end

View File

@ -38,7 +38,7 @@ module Fog
attribute :server_type
def initialize(attributes={})
self.image_id ||= 'img-wwgbb' # Ubuntu Lucid 10.04 server (i686)
self.image_id ||= Fog::Compute[:brightbox].default_image
super
end
@ -71,8 +71,26 @@ module Fog
connection.snapshot_server(identity)
end
def reboot
false
# Directly requesting a server reboot is not supported in the API
# so needs to attempt a shutdown/stop, wait and start again.
#
# Default behaviour is a hard reboot because it is more reliable
# because the state of the server's OS is irrelevant.
#
# @param [Boolean] use_hard_reboot
# @return [Boolean]
def reboot(use_hard_reboot = true)
requires :identity
if ready?
unless use_hard_reboot
soft_reboot
else
hard_reboot
end
else
# Not able to reboot if not ready in the first place
false
end
end
def start
@ -152,7 +170,31 @@ module Fog
merge_attributes(data)
true
end
private
# Hard reboots are fast, avoiding the OS by doing a "power off"
def hard_reboot
stop
wait_for { ! ready? }
start
end
# Soft reboots often timeout if the OS missed the request so we do more
# error checking trying to detect the timeout
#
# @fixme - Using side effect of wait_for's (evaluated block) to detect timeouts
def soft_reboot
shutdown
if wait_for(20) { ! ready? }
# Server is now down, start it up again
start
else
# We timed out
false
end
end
end
end
end
end

View File

@ -0,0 +1,9 @@
Shindo.tests('Fog::Compute::Brightbox::Real', ['brightbox']) do
@bb = Fog::Compute::Brightbox::Real.new({})
tests("#respond_to? :default_image").returns(true) do
@bb.respond_to?(:default_image)
end
end

View File

@ -42,13 +42,20 @@ class Brightbox
module TestSupport
# Find a suitable image for testing with
# For speed of server building we're using an empty image
#
# Unless the tester has credentials this will fail so we rescue
# any errors and return nil.
#
# This is used in the shared file +tests/compute/helper.rb+ so unfortunately
# makes all tests reliant on hardcoded values and each other
#
# @return [String,NilClass] the most suitable test image's identifier or nil
def self.image_id
return @image_id unless @image_id.nil?
images = Fog::Compute[:brightbox].list_images
raise "No available images!" if images.empty?
image = images.select {|img| img.size == 0 }.first
image = images.first if image.nil?
image = select_testing_image_from_api
@image_id = image["id"]
rescue
@image_id = nil
end
# Prepare a test server, wait for it to be usable but raise if it fails
@ -62,6 +69,13 @@ class Brightbox
server
end
private
def self.select_testing_image_from_api
images = Fog::Compute[:brightbox].list_images
raise "No available images!" if images.empty?
images.select {|img| img.size == 0 }.first || images.first
end
end
module Formats
module Struct

View File

@ -15,7 +15,7 @@ def compute_providers
},
:brightbox => {
:server_attributes => {
:image_id => (Brightbox::Compute::TestSupport.image_id rescue 'img-wwgbb') # Ubuntu Lucid 10.04 server (i686)
:image_id => Brightbox::Compute::TestSupport.image_id
},
:mocked => false
},