Skip to content

Commit

Permalink
get vapp mocking, instantiating a template adds more mock data
Browse files Browse the repository at this point in the history
  • Loading branch information
danp committed Oct 15, 2010
1 parent 9338fbb commit 199a092
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 31 deletions.
33 changes: 29 additions & 4 deletions lib/fog/vcloud.rb
Expand Up @@ -296,7 +296,7 @@ def self.data( base_url = self.base_url )
:catalog => {
:name => "The catalog",
:items => [
{ :id => "0", :name => "Item 0" },
{ :id => "0", :name => "Item 0", :disks => [{ :size => 25 }] },
{ :id => "1", :name => "Item 1" },
{ :id => "2", :name => "Item 2" },
]
Expand Down Expand Up @@ -329,10 +329,15 @@ def self.data( base_url = self.base_url )
],
:vms => [
{ :href => "#{base_url}/vap/41",
:name => "Broom 1"
:name => "Broom 1",
:ip => "1.2.3.3",
:memory => 1024,
:cpus => 1,
:disks => [{ :size => 25 }]
},
{ :href => "#{base_url}/vap/42",
:name => "Broom 2"
:name => "Broom 2",
:ip => "1.2.3.4"
},
{ :href => "#{base_url}/vap/43",
:name => "Email!"
Expand Down Expand Up @@ -360,7 +365,7 @@ def self.data( base_url = self.base_url )
}
],
:vms => [
{ :href => "#{base_url}/vap/44",
{ :href => "#{base_url}/vapp/44",
:name => "Master Blaster"
}
]
Expand All @@ -387,6 +392,26 @@ def ip_from_uri(uri)
end
end

def vapp_and_vdc_from_vapp_uri(uri)
if vdc = mock_data[:organizations].map {|o| o[:vdcs] }.flatten.detect {|vd| vd[:vms].detect {|vm| vm[:href] == uri } }
vapp = vdc[:vms].detect {|v| v[:href] == uri }
if vapp
[vapp, vdc]
end
end
end

def catalog_item_and_vdc_from_catalog_item_uri(uri)
catalog_item_id, vdc_id = uri.split("/").last.split("-")
vdc = vdc_from_id(vdc_id)
if vdc
catalog_item = vdc[:catalog][:items].detect {|ci| ci[:id] == catalog_item_id }
if catalog_item
[catalog_item, vdc]
end
end
end

def initialize(options = {})
@versions_uri = URI.parse('https://vcloud.fakey.com/api/versions')
end
Expand Down
42 changes: 19 additions & 23 deletions lib/fog/vcloud/terremark/ecloud/requests/get_catalog_item.rb
Expand Up @@ -15,29 +15,25 @@ class Mock
#

def get_catalog_item(catalog_item_uri)
catalog_item_id, vdc_id = catalog_item_uri.split("/").last.split("-")
xml = nil

if vdc = vdc_from_id(vdc_id)
if catalog_item = vdc[:catalog][:items].detect {|ci| ci[:id] == catalog_item_id }
builder = Builder::XmlMarkup.new

xml = builder.CatalogItem(xmlns.merge(:href => catalog_item_uri, :name => catalog_item[:name])) do
builder.Link(
:rel => "down",
:href => Fog::Vcloud::Terremark::Ecloud::Mock.catalog_item_customization_href(:id => catalog_item_id),
:type => "application/vnd.tmrk.ecloud.catalogItemCustomizationParameters+xml",
:name => "Customization Options"
)

builder.Entity(
:href => Fog::Vcloud::Terremark::Ecloud::Mock.vapp_template_href(:id => catalog_item_id),
:type => "application/vnd.vmware.vcloud.vAppTemplate+xml",
:name => catalog_item[:name]
)

builder.Property(0, :key => "LicensingCost")
end
if catalog_item_and_vdc = catalog_item_and_vdc_from_catalog_item_uri(catalog_item_uri)
catalog_item, vdc = catalog_item_and_vdc
builder = Builder::XmlMarkup.new

xml = builder.CatalogItem(xmlns.merge(:href => catalog_item_uri, :name => catalog_item[:name])) do
builder.Link(
:rel => "down",
:href => Fog::Vcloud::Terremark::Ecloud::Mock.catalog_item_customization_href(:id => catalog_item[:id]),
:type => "application/vnd.tmrk.ecloud.catalogItemCustomizationParameters+xml",
:name => "Customization Options"
)

builder.Entity(
:href => Fog::Vcloud::Terremark::Ecloud::Mock.vapp_template_href(:id => catalog_item[:id]),
:type => "application/vnd.vmware.vcloud.vAppTemplate+xml",
:name => catalog_item[:name]
)

builder.Property(0, :key => "LicensingCost")
end
end

Expand Down
69 changes: 68 additions & 1 deletion lib/fog/vcloud/terremark/ecloud/requests/get_vapp.rb
Expand Up @@ -9,7 +9,74 @@ class Real

class Mock
def get_vapp(vapp_uri)
Fog::Mock.not_implemented
xml = nil

if vapp_and_vdc = vapp_and_vdc_from_vapp_uri(vapp_uri)
xml = generate_get_vapp_response(*vapp_and_vdc)
end

if xml
mock_it 200, xml, "Content-Type" => "application/vnd.vmware.vcloud.vApp+xml"
else
mock_error 200, "401 Unauthorized"
end
end

private

def generate_get_vapp_response(vapp, vdc)
builder = Builder::XmlMarkup.new
builder.VApp(xmlns.merge(
:href => vapp[:href],
:type => "application/vnd.vmware.vcloud.vApp+xml",
:name => vapp[:name],
:status => 2
)) do
builder.Link(:rel => "up", :href => vdc[:href], :type => "application/vnd.vmware.vcloud.vdc+xml")

builder.NetworkConnectionSection(:xmlns => "http://schemas.dmtf.org/ovf/envelope/1") do
builder.NetworkConnection(:Network => "Internal", :xmlns => "http://www.vmware.com/vcloud/v0.8") do
builder.IpAddress vapp[:ip]
end
end

builder.OperatingSystemSection(
"d2p1:id" => 4,
:xmlns => "http://schemas.dmtf.org/ovf/envelope/1",
"xmlns:d2p1" => "http://schemas.dmtf.org/ovf/envelope/1") do
builder.Info "The kind of installed guest operating system"
builder.Description "Red Hat Enterprise Linux 5 (64-bit)"
end

builder.VirtualHardwareSection(:xmlns => "http://schemas.dmtf.org/ovf/envelope/1") do
builder.Info
builder.System
builder.Item do
# CPUs
builder.VirtualQuantity vapp[:cpus] || 1
builder.ResourceType 3
end
builder.Item do
# memory
builder.VirtualQuantity vapp[:memory] || 1024
builder.ResourceType 4
end
builder.Item do
# SCSI controller
builder.Address 0
builder.ResourceType 6
builder.InstanceId 3
end
builder.Item do
# Hard Disk 1
builder.Parent 3
builder.VirtualQuantity vapp[:disks].first[:size] * 1024 # MB
builder.HostResource vapp[:disks].first[:size] * 1024 # MB
builder.ResourceType 17
builder.AddressOnParent 0
end
end
end
end
end
end
Expand Down
Expand Up @@ -103,12 +103,13 @@ class Mock

def instantiate_vapp_template(catalog_item_uri, options = {})
validate_instantiate_vapp_template_options(catalog_item_uri, options)
catalog_item, _ = catalog_item_and_vdc_from_catalog_item_uri(catalog_item_uri)

xml = nil
if vdc = vdc_from_uri(options[:vdc_uri])
id = rand(1000)
vapp_uri = Fog::Vcloud::Terremark::Ecloud::Mock.vapp_href(:id => id)
options.update(:id => id.to_s, :href => vapp_uri)
vapp_id = rand(1000)
vapp_uri = Fog::Vcloud::Terremark::Ecloud::Mock.vapp_href(:id => vapp_id)
options.update(:id => vapp_id.to_s, :href => vapp_uri, :disks => catalog_item[:disks], :ip => random_ip_in_network(options[:network_uri]))
vdc[:vms] << options

xml = generate_instantiate_vapp_template_response(vdc[:href], options[:name], vapp_uri)
Expand All @@ -123,6 +124,13 @@ def instantiate_vapp_template(catalog_item_uri, options = {})

private

def random_ip_in_network(network_uri)
network = mock_data[:organizations].map { |org| org[:vdcs].map { |vdc| vdc[:networks] } }.flatten.detect { |network| network[:href] == network_uri }
subnet_ipaddr = IPAddr.new(network[:subnet])
ips = subnet_ipaddr.to_range.to_a[3..-2]
ips[rand(ips.size)].to_s
end

def generate_instantiate_vapp_template_response(vdc_uri, vapp_name, vapp_uri)
builder = Builder::XmlMarkup.new
builder.VApp(xmlns.merge(
Expand Down
109 changes: 109 additions & 0 deletions spec/vcloud/terremark/ecloud/requests/get_vapp_spec.rb
@@ -0,0 +1,109 @@
require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper')

if Fog.mocking?
describe "Fog::Vcloud, initialized w/ the TMRK Ecloud module", :type => :mock_tmrk_ecloud_request do
subject { @vcloud }

it { should respond_to :get_vapp }

describe "#get_vapp" do
context "with a valid vapp_uri" do
before { @vapp = @vcloud.get_vapp(vm_data[:href]) }
subject { @vapp }
let(:vapp_id) { @vapp.body[:href].split("/").last }
let(:vm_data) { @vcloud.mock_data[:organizations].first[:vdcs].first[:vms].first }
let(:vdc) { @vcloud.vdcs.first }

it_should_behave_like "all responses"
it { should have_headers_denoting_a_content_type_of "application/vnd.vmware.vcloud.vApp+xml" }

describe "#body" do
subject { @vapp.body }

specify { subject.keys.sort_by(&:to_s).should == [:Link, :NetworkConnectionSection, :OperatingSystemSection, :VirtualHardwareSection, :href, :name, :status, :type, :xmlns, :xmlns_xsd, :xmlns_xsi] }

it_should_behave_like "it has the standard vcloud v0.8 xmlns attributes" # 3 keys

its(:href) { should == vm_data[:href] }
its(:name) { should == vm_data[:name] }
its(:status) { should == "2" }
its(:size) { (25 * 1024).to_s }

describe "Link" do
subject { @vapp.body[:Link] }

its(:rel) { should == "up" }
its(:type) { should == "application/vnd.vmware.vcloud.vdc+xml" }
its(:href) { should == vdc.href }
end

describe "NetworkConnectionSection" do
subject { @vapp.body[:NetworkConnectionSection] }

it { should include :NetworkConnection }

describe "NetworkConnection" do
subject { @vapp.body[:NetworkConnectionSection][:NetworkConnection] }

its(:IpAddress) { should == vm_data[:ip] }
end
end

describe "OperatingSystemSection" do
subject { @vapp.body[:OperatingSystemSection] }

its(:Info) { should == "The kind of installed guest operating system" }
its(:Description) { should == "Red Hat Enterprise Linux 5 (64-bit)" }
end

describe "VirtualHardwareSection" do
subject { @vapp.body[:VirtualHardwareSection] }

specify { subject.keys.sort_by(&:to_s).should == [:Info, :Item, :System, :xmlns] }

describe "Item" do
subject { @vapp.body[:VirtualHardwareSection][:Item] }

it { should have(4).items }

specify { subject.map {|i| i[:ResourceType] }.sort.should == %w(3 4 6 17).sort }

describe "CPU" do
subject { @vapp.body[:VirtualHardwareSection][:Item].detect {|i| i[:ResourceType] == "3" } }

its(:VirtualQuantity) { should == vm_data[:cpus].to_s }
end

describe "memory" do
subject { @vapp.body[:VirtualHardwareSection][:Item].detect {|i| i[:ResourceType] == "4" } }

its(:VirtualQuantity) { should == vm_data[:memory].to_s }
end

describe "SCSI controller" do
subject { @vapp.body[:VirtualHardwareSection][:Item].detect {|i| i[:ResourceType] == "6" } }

its(:Address) { should == "0" }
end

describe "Hard Disk 1" do
subject { @vapp.body[:VirtualHardwareSection][:Item].detect {|i| i[:ResourceType] == "17" } }

its(:AddressOnParent) { should == "0" }
its(:VirtualQuantity) { should == (vm_data[:disks].first[:size] * 1024).to_s }
its(:HostResource) { should == (vm_data[:disks].first[:size] * 1024).to_s }
end
end
end
end
end

context "with a vapp uri that doesn't exist" do
subject { lambda { @vcloud.get_vapp(URI.parse('https://www.fakey.com/api/v0.8/vApp/99999')) } }

it_should_behave_like "a request for a resource that doesn't exist"
end
end
end
else
end
Expand Up @@ -9,6 +9,7 @@
describe "#instantiate_vapp_template" do
let(:vdc) { @vcloud.vdcs.first }
let(:catalog_item) { vdc.catalog.first }
let(:catalog_item_data) { @vcloud.catalog_item_and_vdc_from_catalog_item_uri(catalog_item.href).first }
let(:new_vapp_data) do
{
:name => "foobar",
Expand Down Expand Up @@ -37,6 +38,10 @@

it { should include :id }
it { should include :href }
it { should include :disks }
it { should include :ip }

its(:disks) { should == catalog_item_data[:disks] }

specify { subject.values_at(*new_vapp_data.keys).should == new_vapp_data.values }
end
Expand Down

0 comments on commit 199a092

Please sign in to comment.