Merge pull request #3 from brightbox/updates_for_tests
Updates for tests removing hardcoded image references
This commit is contained in:
commit
3a19598730
|
@ -101,7 +101,24 @@ module Fog
|
||||||
request :update_server_group
|
request :update_server_group
|
||||||
request :update_user
|
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
|
class Mock
|
||||||
|
include Shared
|
||||||
|
|
||||||
def initialize(options)
|
def initialize(options)
|
||||||
@brightbox_client_id = options[:brightbox_client_id] || Fog.credentials[:brightbox_client_id]
|
@brightbox_client_id = options[:brightbox_client_id] || Fog.credentials[:brightbox_client_id]
|
||||||
|
@ -111,9 +128,15 @@ module Fog
|
||||||
def request(options)
|
def request(options)
|
||||||
raise "Not implemented"
|
raise "Not implemented"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def select_default_image
|
||||||
|
"img-mockd"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Real
|
class Real
|
||||||
|
include Shared
|
||||||
|
|
||||||
def initialize(options)
|
def initialize(options)
|
||||||
# Currently authentication and api endpoints are the same but may change
|
# Currently authentication and api endpoints are the same but may change
|
||||||
|
@ -180,6 +203,34 @@ module Fog
|
||||||
options[:headers] = headers
|
options[:headers] = headers
|
||||||
@connection.request(options)
|
@connection.request(options)
|
||||||
end
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -38,7 +38,7 @@ module Fog
|
||||||
attribute :server_type
|
attribute :server_type
|
||||||
|
|
||||||
def initialize(attributes={})
|
def initialize(attributes={})
|
||||||
self.image_id ||= 'img-wwgbb' # Ubuntu Lucid 10.04 server (i686)
|
self.image_id ||= Fog::Compute[:brightbox].default_image
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -71,8 +71,26 @@ module Fog
|
||||||
connection.snapshot_server(identity)
|
connection.snapshot_server(identity)
|
||||||
end
|
end
|
||||||
|
|
||||||
def reboot
|
# Directly requesting a server reboot is not supported in the API
|
||||||
false
|
# 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
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
|
@ -152,7 +170,31 @@ module Fog
|
||||||
merge_attributes(data)
|
merge_attributes(data)
|
||||||
true
|
true
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
|
@ -42,13 +42,20 @@ class Brightbox
|
||||||
module TestSupport
|
module TestSupport
|
||||||
# Find a suitable image for testing with
|
# Find a suitable image for testing with
|
||||||
# For speed of server building we're using an empty image
|
# 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
|
def self.image_id
|
||||||
return @image_id unless @image_id.nil?
|
return @image_id unless @image_id.nil?
|
||||||
images = Fog::Compute[:brightbox].list_images
|
image = select_testing_image_from_api
|
||||||
raise "No available images!" if images.empty?
|
|
||||||
image = images.select {|img| img.size == 0 }.first
|
|
||||||
image = images.first if image.nil?
|
|
||||||
@image_id = image["id"]
|
@image_id = image["id"]
|
||||||
|
rescue
|
||||||
|
@image_id = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# Prepare a test server, wait for it to be usable but raise if it fails
|
# Prepare a test server, wait for it to be usable but raise if it fails
|
||||||
|
@ -62,6 +69,13 @@ class Brightbox
|
||||||
server
|
server
|
||||||
end
|
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
|
end
|
||||||
module Formats
|
module Formats
|
||||||
module Struct
|
module Struct
|
||||||
|
|
|
@ -15,7 +15,7 @@ def compute_providers
|
||||||
},
|
},
|
||||||
:brightbox => {
|
:brightbox => {
|
||||||
:server_attributes => {
|
: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
|
:mocked => false
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue