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

[ibm] Added Mocking and Tests

This commit is contained in:
Carl Hicks 2011-12-02 10:27:44 -08:00 committed by Decklin Foster
parent 402d42ced7
commit 019e536312
46 changed files with 1505 additions and 3 deletions

View file

@ -22,6 +22,22 @@ module Fog
end
end
class Mock
def clone_image(image_id, name, description)
response = Excon::Response.new
if image_exists? image_id
id = Fog::IBM::Mock.instance_id
self.data[:images][id] = self.data[:images][image_id].dup
response.status = 200
response.body = { "ImageID" => id }
else
response.status = 404
end
response
end
end
end
end
end

View file

@ -23,6 +23,19 @@ module Fog
end
end
class Mock
def create_address(location_id, offering_id="20001223", vlan_id=nil)
address = Fog::IBM::Mock.create_address(location_id, offering_id, vlan_id)
self.data[:addresses][address['id']] = address
response = Excon::Response.new
response.status = 200
response.body = address
response
end
end
end
end
end

View file

@ -22,6 +22,23 @@ module Fog
end
end
class Mock
def create_image(instance_id, name, description)
response = Excon::Response.new
if instance_exists? instance_id
image = Fog::IBM::Mock.private_image(name, description)
self.data[:images][image["id"]] = image
response.status = 200
response.body = image
else
response.status = 404
end
response
end
end
end
end
end

View file

@ -32,6 +32,26 @@ module Fog
end
end
class Mock
def create_instance(name="fog instance", image_id="20018425", instance_type="COP32.1/2048/60", location="101", public_key="fog", options={})
response = Excon::Response.new
# Since we want to test error conditions, we have a little regex that traps specially formed
# instance type strings.
case name
when /FAIL:\ (\d{3})/
response.status = $1
else
instance = Fog::IBM::Mock.create_instance(name, image_id, instance_type, location, public_key, options)
self.data[:instances][instance['id']] = instance
response.status = 200
response.body = {"instances" => [ instance ]}
end
response
end
end
end
end
end

View file

@ -22,6 +22,33 @@ module Fog
end
end
class Mock
# SmartCloud returns the private key when create_key is called
# We need to store both the private and public key for later use
def create_key(name, public_key=nil)
response = Excon::Response.new
response.status = 200
attributes = {
"keyName" => name,
"lastModifiedTime" => Fog::IBM::Mock.launch_time,
"default" => false,
"instanceIds" => [],
}
if public_key.nil?
private_key = Fog::IBM::Mock.key_material
public_key = private_key.public_key
public_key = { "keyMaterial" => public_key.to_s }.merge(attributes.dup)
self.data[:keys][name] = public_key
private_key = { "keyMaterial" => private_key.to_s }.merge(attributes.dup)
self.data[:private_keys][name] = private_key
response.body = private_key
end
response
end
end
end
end
end

View file

@ -18,6 +18,26 @@ module Fog
end
end
class Mock
def delete_address(address_id)
response = Excon::Response.new
if address_exists? address_id
self.data[:addresses].delete address_id
response.status = 200
response.body = { "success" => true }
else
response.status = 404
end
response
end
def address_exists?(address_id)
self.data[:addresses].key? address_id
end
end
end
end
end

View file

@ -18,6 +18,23 @@ module Fog
end
end
class Mock
def delete_image(image_id)
response = Excon::Response.new
# TODO: We should probably check that an image is deleteable.
# i.e. that the user has appropriate permissions
if image_exists? image_id
self.data[:images].delete image_id
response.status = 200
response.body = {"success"=>true}
else
response.status = 404
end
response
end
end
end
end
end

View file

@ -18,6 +18,31 @@ module Fog
end
end
class Mock
def delete_instance(instance_id)
response = Excon::Response.new
if deleteable? instance_id
# remove from memoreeeez.
self.data[:instances].delete instance_id
response.status = 200
else
# TODO: we should really return a 412 if the instance is in an invalid state, and a 404 if it doesn't exist.
response.status = 404
end
response
end
# we can't delete the instance if it doesn't exist, or is in an invalid state.
def deleteable?(instance_id)
return false unless instance_exists? instance_id
instance = self.data[:instances][instance_id]
return false if [0, 1, 7, 14, 15].include?(instance["status"].to_i)
true
end
end
end
end
end

View file

@ -18,6 +18,21 @@ module Fog
end
end
class Mock
def delete_key(key_name)
response = Excon::Response.new
if key_exists? key_name
self.data[:keys].delete(key_name)
response.status = 200
response.body = {"success"=>true}
else
response.status = 404
end
response
end
end
end
end
end

View file

@ -18,6 +18,27 @@ module Fog
end
end
class Mock
def get_image(image_id)
response = Excon::Response.new
if image_exists? image_id
response.status = 200
response.body = self.data[:images][image_id]
else
response.status = 404
end
response
end
private
def image_exists?(image_id)
self.data[:images].key? image_id
end
end
end
end
end

View file

@ -18,6 +18,41 @@ module Fog
end
end
class Mock
# TODO: Fix this so they work.
def get_image_agreement(image_id)
response = Excon::Response.new
response.status = 200
response.body = {"text"=>
"test, operating system is SUSE Linux Enterprise Server/11 SP1 - English\n\nYour access to and use of the Service, including all selected options, are governed by the terms of the Agreement signed between your Enterprise and IBM. Each Service is also governed by one or more Attachments (including, for example, Image Terms Attachments). Applicable Attachments are part of the Agreement between you and IBM and include Attachments for Services you acquire after the Agreement was signed. The Agreement also references applicable IBM and third party end user license agreements that govern the use of IBM software and operating system software provided as part of an Image.\n\nYou are responsible for complying with the terms of the Agreement (including applicable Attachments) and applicable license agreements. You may review the terms for the Service by 1) obtaining information regarding the Agreement from your Account Administrator and 2) accessing the Asset Catalog to review specific Image Terms and end user license agreements for IBM and third party software provided as part of an Image. ",
"id"=>"20020159",
"attachments"=>
[{"label"=>"Service Description for Developement & Test Service",
"url"=>
"https://www-147.ibm.com/cloud/enterprise/static/internal_user_agreement.pdf",
"type"=>0},
{"label"=>"Smart Business on the IBM Public Cloud Agreement",
"url"=>
"https://www-147.ibm.com/cloud/enterprise/static/internal_user_agreement.pdf",
"type"=>1},
{"label"=>
"End User License for SUSE 10.2 Linux Enterprise Server software",
"url"=>
"https://www.novell.com/licensing/eula/sles_10/sles_10_english.pdf",
"type"=>2},
{"label"=>
"End User License for SUSE 11.0 Linux Enterprise Server software",
"url"=>"https://www.novell.com/licensing/eula/sles_11/sles_11_en.pdf",
"type"=>2},
{"label"=>"End User License for RedHat Linux RHEL software",
"url"=>"https://www.redhat.com/licenses/",
"type"=>2}]}
response
end
end
end
end
end

View file

@ -18,6 +18,19 @@ module Fog
end
end
class Mock
# TODO: Create a data store for this.
def get_image_manifest(image_id)
response = Excon::Response.new
response.status = 200
response.body = {"manifest"=>
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><parameters xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"platform:/resource/com.ibm.ccl.devcloud.client/schema/parameters.xsd\">\n\t<firewall>\n\t\t<rule>\n\t\t\t<source>0.0.0.0/0</source>\n\t\t\t<minport>1</minport>\n\t\t\t<maxport>65535</maxport>\n\t\t</rule>\n\t</firewall>\n</parameters>"}
response
end
end
end
end
end

View file

@ -18,6 +18,37 @@ module Fog
end
end
class Mock
def get_instance(instance_id)
response = Excon::Response.new
if instance_exists? instance_id
activate_instance(instance_id) # Set it to Active if it's not running
response.status = 200
response.body = self.data[:instances][instance_id]
else
response.status = 404
end
response
end
# Checks if an instance exists
def instance_exists?(instance_id)
self.data[:instances].key? instance_id
end
# Sets instance status to Active if it's not already set.
def activate_instance(instance_id)
self.data[:instances][instance_id]["status"] = 5 unless instance_active? instance_id
end
# Checks if an instance is Active
def instance_active?(instance_id)
self.data[:instances][instance_id]["status"] == 5
end
end
end
end
end

View file

@ -18,6 +18,25 @@ module Fog
end
end
class Mock
def get_key(key_name)
response = Excon::Response.new
if key_exists? key_name
response.status = 200
response.body = self.data[:keys][key_name]
else
response.status = 404
end
response
end
def key_exists?(name)
self.data[:keys].key? name
end
end
end
end
end

View file

@ -18,6 +18,25 @@ module Fog
end
end
class Mock
def get_location(location_id)
response = Excon::Response.new
if location_exists? location_id
response.status = 200
response.body = self.data[:locations][location_id]
else
response.status = 404
end
response
end
def location_exists?(location_id)
self.data[:locations].key? location_id
end
end
end
end
end

View file

@ -22,6 +22,37 @@ module Fog
end
class Mock
def get_request(request_id)
response = Excon::Response.new
response.status = 200
response.body = {"instances"=>
[{"name"=>"test from fog",
"location"=>"101",
"keyName"=>"mykey",
"primaryIP"=>
{"type"=>0, "ip"=>"42.42.42.42 ", "hostname"=>"42.42.42.42 "},
"productCodes"=>[],
"requestId"=>"75364",
"imageId"=>"20020159",
"launchTime"=>1304012220770,
"id"=>"75064",
"volumes"=>[],
"instanceType"=>"SLV32.2/4096/60*350",
"requestName"=>"test from fog",
"secondaryIP"=>[],
"status"=>1,
"software"=>
[{"name"=>"SUSE Linux Enterprise Server",
"type"=>"OS",
"version"=>"11 SP1"}],
"expirationTime"=>1367084229205,
"owner"=>"user@example.com"}]}
response
end
end
end
end
end

View file

@ -21,6 +21,27 @@ module Fog
end
end
class Mock
def list_address_offerings
response = Excon::Response.new
response.status = 200
response.body = {"addresses"=>
[{"price"=>
{"pricePerQuantity"=>1,
"effectiveDate"=>1302566400000,
"rate"=>0.01,
"countryCode"=>"897",
"unitOfMeasure"=>"UHR",
"currencyCode"=>"USD"},
"location"=>"101",
"ipType"=>0,
"id"=>"20001223"}]}
response
end
end
end
end
end

View file

@ -18,6 +18,17 @@ module Fog
end
end
class Mock
def list_locations
response = Excon::Response.new
response.status = 200
response.body = { "locations" => self.data[:locations].values }
response
end
end
end
end
end

View file

@ -19,6 +19,24 @@ module Fog
end
end
class Mock
def list_vlans
response = Excon::Response.new
response.status = 200
response.body = {"addresses"=>
[{"location"=>"101",
"mode"=>0,
"offeringId"=>"20001223",
"id"=>"75321",
"type"=>1,
"ip"=>"170.224.192.210 ",
"hostname"=>"170.224.192.210 ",
"state"=>2}]},
response
end
end
end
end
end

View file

@ -17,6 +17,41 @@ module Fog
:body => options
)
end
end
class Mock
def modify_instance(instance_id, options={})
response = Excon::Response.new
if params['state'] = 'restart'
if instance_exists? instance_id
self.data[:instances][instance_id]["status"] = "8"
self.data[:instances][instance_id]["keyName"] = key_name
response.status = 200
response.body = { "success" => true }
else
response.status = 404
end
elsif params['type'] == 'attach' || params['type'] == 'detach'
if (instance_exists?(instance_id) && volume_exists?(volume_id))
# TODO: Update the instance in the data hash, assuming IBM ever gets this feature working properly.
response.status = 415
else
response.status = 404
end
elsif params['name']
if instance_exists?(instance_id)
self.data[:instances][instance_id]["name"] = name
response.status = 200
response.body = { "success" => true }
else
response.status = 404
end
else
Fog::Mock.not_implemented
end
response
end
end
end

View file

@ -19,6 +19,26 @@ module Fog
end
end
class Mock
def modify_key(key_name, params={})
response = Excon::Response.new
if params['public_key']
if key_exists? key_name
self.data[:keys][key_name]['keyMaterial'] = public_key
self.data[:keys][key_name]['lastModifiedTime'] = Fog::IBM::Mock.launch_time,
response.status = 200
response.body = {"success"=>true}
else
response.status = 404
end
else
Fog::Mock.not_implemented
end
response
end
end
end
end
end

View file

@ -23,6 +23,18 @@ module Fog
}
)
end
end
class Mock
def create_volume(name, offering_id, format, location_id, size)
volume = Fog::IBM::Mock.create_volume(name, format, location_id, size, offering_id)
self.data[:volumes][volume['id']] = volume
response = Excon::Response.new
response.status = 200
response.body = format_get_volume_response_for(volume['id'])
response
end
end
end

View file

@ -18,6 +18,22 @@ module Fog
end
end
class Mock
def delete_volume(volume_id)
response = Excon::Response.new
if volume_exists? volume_id
self.data[:volumes].delete volume_id
response.status = 200
response.body = {"success"=>true}
else
response.status = 404
end
response
end
end
end
end
end

View file

@ -18,6 +18,53 @@ module Fog
end
end
class Mock
# For whatever reason, get_volume returns different data than an entry in list_volumes
def get_volume(volume_id)
response = Excon::Response.new
if volume_exists? volume_id
response.status = 200
response.body = format_get_volume_response_for(volume_id)
else
response.status = 404
end
response
end
# get_volume response doesn't contain instanceId
def format_get_volume_response_for(volume_id)
# If we aren't attached/ready, make us ready
ready_volume(volume_id) unless volume_attached? volume_id
self.data[:volumes][volume_id].reject { |k,v| k == 'instanceId' }
end
# The list_volumes response doesn't contain ioPrice
def format_list_volumes_response
self.data[:volumes].values.dup.map { |volume| volume.reject { |k,v| k == 'ioPrice'} }
end
def volume_exists?(volume_id)
self.data[:volumes].key? volume_id
end
# Checks if an volume is Active
def volume_ready?(volume_id)
self.data[:volumes][volume_id]['state'] == 4
end
def volume_attached?(volume_id)
self.data[:volumes][volume_id]['instanceId'] != "0"
end
# Sets volume status to Detached if it's not already set, and or attached
def ready_volume(volume_id)
# If not ready, make ready
self.data[:volumes][volume_id]['state'] = 4
end
end
end
end
end

View file

@ -19,6 +19,43 @@ module Fog
end
end
class Mock
def list_offerings
response = Excon::Response.new
response.status = 200
response.body = {"volumes"=>
[{"name"=>"Small",
"price"=>
{"pricePerQuantity"=>1,
"effectiveDate"=>-1,
"rate"=>0.0384,
"countryCode"=>"897",
"unitOfMeasure"=>"UHR",
"currencyCode"=>"USD"},
"location"=>"61",
"id"=>"20001208",
"formats"=>
[{"label"=>"ext3", "id"=>"EXT3"}, {"label"=>"raw", "id"=>"RAW"}],
"capacity"=>256},
{"name"=>"Small",
"price"=>
{"pricePerQuantity"=>1,
"effectiveDate"=>-1,
"rate"=>0.0384,
"countryCode"=>"897",
"unitOfMeasure"=>"UHR",
"currencyCode"=>"USD"},
"location"=>"141",
"id"=>"20001208",
"formats"=>
[{"label"=>"ext3", "id"=>"EXT3"}, {"label"=>"raw", "id"=>"RAW"}],
"capacity"=>256}]},
response
end
end
end
end
end

View file

@ -19,6 +19,17 @@ module Fog
end
end
class Mock
def list_volumes
response = Excon::Response.new
response.status = 200
response.body = { 'volumes' => format_list_volumes_response }
response
end
end
end
end
end