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

- Fixes tests to run in both mock and non-mock mode

- Clean ups and fixes
This commit is contained in:
Kevin Chan 2012-02-21 17:29:36 +08:00 committed by geemus
parent b89708b26e
commit 4ff115c1de
9 changed files with 92 additions and 55 deletions

View file

@ -147,6 +147,7 @@ module Fog
if response.headers["Content-Type"] == "application/json"
response.body = MultiJson.decode(response.body)
response.body = decode_time_props(response.body)
end
response
@ -171,6 +172,22 @@ module Fog
}
end
def decode_time_props(obj)
if obj.kind_of?(Hash)
if obj["created"]
obj["created"] = Time.parse(obj["created"])
end
if obj["updated"]
obj["updated"] = Time.parse(obj["updated"])
end
elsif obj.kind_of?(Array)
obj.map do |o|
decode_time_props(o)
end
end
obj
end
end # Real
end
end

View file

@ -8,10 +8,12 @@ module Fog
def create_key(params)
name = params[:name]
key = params[:key]
record = {
"name" => name,
"key" => key,
"created" => Time.now.utc
"created" => Time.now.utc,
"updated" => Time.now.utc
}
self.data[:keys][name] = record
@ -43,7 +45,7 @@ module Fog
request(
:method => "POST",
:path => "/my/keys",
:body => { "name" => params[:name], "body" => params[:body] },
:body => { "name" => params[:name], "key" => params[:key] },
:expects => 201
)
end

View file

@ -6,7 +6,7 @@ module Fog
def delete_key(keyname)
if self.data[:keys].delete(keyname)
response = Excon::Response.new
response.status = 200
response.status = 204
response
else
raise Excon::Errors::NotFound, "Not Found"
@ -15,11 +15,11 @@ module Fog
end
class Real
def delete_key(keyname)
def delete_key(name)
request(
:method => "DELETE",
:path => "/my/keys/#{name}",
:expects => 200
:expects => 204
)
end
end # Real

View file

@ -8,6 +8,7 @@ module Fog
res = Excon::Response.new
res.status = 200
res.body = machine
res
else
raise Excon::Errors::NotFound, "Not Found"
end
@ -18,7 +19,8 @@ module Fog
def get_machine(uuid)
request(
:method => "GET",
:path => "/my/machines/#{uuid}"
:path => "/my/machines/#{uuid}",
:expects => 200
)
end
end

View file

@ -1,3 +1,5 @@
require 'uri'
module Fog
module Compute
class Joyent
@ -17,9 +19,11 @@ module Fog
class Real
def get_package(name)
name = URI.escape(name)
request(
:method => "GET",
:path => "/my/packages/#{name}"
:path => "/my/packages/#{name}",
:expects => 200
)
end

View file

@ -24,7 +24,8 @@ module Fog
def list_packages
request(
:path => "/my/packages",
:method => "GET"
:method => "GET",
:expects => 200
)
end
end # Real

View file

@ -3,45 +3,44 @@ Shindo.tests("Fog::Compute::Joyent | key requests", ['joyent']) do
@key_format = {
"name" => String,
"key" => String,
"created" => Time
"created" => Time,
"updated" => Time
}
before do
Fog::Compute[:joyent].create_key(
:name => "key1",
:key => "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAlau1...")
Fog::Compute[:joyent].create_key(
:name => "key2",
:key => "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAlau1...")
end
tests("#list_keys").formats([@key_format]) do
Fog::Compute[:joyent].list_keys.body
end
tests("#list_keys") do
returns(2) do
Fog::Compute[:joyent].list_keys.body.length
#
# Clear out all the test keys on the account in prep for test
#
Fog::Compute[:joyent].list_keys.body.each do |key|
if key["name"] =~ /^fog-test/
Fog::Compute[:joyent].delete_key(key["name"])
end
end
@test_key_name = "fog-test-#{Time.now.utc.to_i}"
Fog::Compute[:joyent].create_key(
:name => @test_key_name,
:key => "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDWxSNYngOTeu0pYd+2tpfYGISuMfMUNGyAIh4yRprAbacVddRq4Nyr12vDklzaRTzgd9PgX/82JMb4RARbVTtUKXJXmaBLvg2epGM+ScanZIitzL53whJrlGx+7nT+TnRdkB1XG7uIf2EpTQBaKrT4iG0magCXh5bmOqCyWte2gV8fArMg5bZclUT1p2E7qEW0htaLOiMSyGkjBlxb6vYQCA/Pa8VWETHehIF46S942gCj0aaL81gTocfyTm5/F+AgvUAsjHzRVkB/Dlhwq7Q7sK+4iAhlKPYMflkKC8r+nF0/LL9S3lllLZvbkEWJfEqlMCAbgmjTpYlBzQEqf/eN"
)
end
tests("#list_keys").formats(@key_format) do
Fog::Compute[:joyent].list_keys.body.first
end
tests("#get_key").formats(@key_format) do
Fog::Compute[:joyent].get_key('key1').body
end
tests("#get_key").formats(@key_format) do
Fog::Compute[:joyent].get_key('key2').body
Fog::Compute[:joyent].get_key(@test_key_name).body
end
tests("#delete_key") do
returns(200, "returns status code 200") do
Fog::Compute[:joyent].delete_key("key1").status
returns(204 , "returns status code 204") do
Fog::Compute[:joyent].delete_key(@test_key_name).status
end
raises(Excon::Errors::NotFound, "when a key no longer exists") do
Fog::Compute[:joyent].delete_key("key1")
Fog::Compute[:joyent].delete_key("key1")
Fog::Compute[:joyent].delete_key(@test_key_name)
Fog::Compute[:joyent].delete_key(@test_key_name)
end
end

View file

@ -14,26 +14,30 @@ Shindo.tests("Fog::Compute[:joyent] | machine requests", ["joyent"]) do
"updated" => Time
}
@machines = Fog::Compute[:joyent].data[:machines] = {
"15080eca-3786-4bb8-a4d0-f43e1981cd72" => {
"id" => "15080eca-3786-4bb8-a4d0-f43e1981cd72",
"name" => "getting-started",
"type" => "smartmachine",
"state" => "running",
"dataset" => "sdc:sdc:smartos:1.3.15",
"memory" => 256,
"disk" => 5120,
"ips" => ["10.88.88.50"],
"metadata" => {},
"created" => Time.parse("2011-06-03T00:02:31+00:00"),
"updated" => Time.parse("2011-06-03T00:02:31+00:00")
if Fog.mock?
@machines = Fog::Compute[:joyent].data[:machines] = {
"15080eca-3786-4bb8-a4d0-f43e1981cd72" => {
"id" => "15080eca-3786-4bb8-a4d0-f43e1981cd72",
"name" => "getting-started",
"type" => "smartmachine",
"state" => "running",
"dataset" => "sdc:sdc:smartos:1.3.15",
"memory" => 256,
"disk" => 5120,
"ips" => ["10.88.88.50"],
"metadata" => {},
"created" => Time.parse("2011-06-03T00:02:31+00:00"),
"updated" => Time.parse("2011-06-03T00:02:31+00:00")
}
}
}
end
@provider = Fog::Compute[:joyent]
#
# https://us-west-1.api.joyentcloud.com/docs#ListMachines
#
tests("#list_machines") do
if Fog.mock?
returns(@machines.length, "correct number of machines") do
@ -54,7 +58,7 @@ Shindo.tests("Fog::Compute[:joyent] | machine requests", ["joyent"]) do
tests("#get_machine") do
formats(@machine_format) do
id = @provider.list_machines.body.first["id"]
@provider.get_machine(id)
@provider.get_machine(id).body
end
end
end

View file

@ -1,6 +1,4 @@
Shindo.tests("Fog::Compute[:joyent] | package requests", ["joyent"]) do
@data = Fog::Compute[:joyent].data
@package_format = {
'name' => String,
'vcpus' => Integer,
@ -11,6 +9,8 @@ Shindo.tests("Fog::Compute[:joyent] | package requests", ["joyent"]) do
}
if Fog.mock?
@data = Fog::Compute[:joyent].data
@data[:packages] = {
"regular_128" => {
"name" => "regular_128",
@ -43,15 +43,23 @@ Shindo.tests("Fog::Compute[:joyent] | package requests", ["joyent"]) do
formats([@package_format]) do
Fog::Compute[:joyent].list_packages.body
end
end
actual = @data[:packages].values.length
returns(actual, "has correct number of packages") do
Fog::Compute[:joyent].list_packages.body.length
if Fog.mock?
tests("#list_packages") do
actual = @data[:packages].values.length
returns(actual, "has correct number of packages") do
Fog::Compute[:joyent].list_packages.body.length
end
end
end
tests("#get_package") do
pkgid = @data[:packages].keys.first
pkgid = if Fog.mock?
@data[:packages].keys.first
else
Fog::Compute[:joyent].list_packages.body.first["name"]
end
formats(@package_format) do
Fog::Compute[:joyent].get_package(pkgid).body