Skip to content

Commit

Permalink
Merge pull request #2491 from alphagov/vdc_create_network_before_delete
Browse files Browse the repository at this point in the history
[vcloud_director] add Mock for post_create_network, improve Mock tests
  • Loading branch information
nosborn committed Dec 19, 2013
2 parents 00e91c2 + 7c4ecd7 commit 15180fd
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ class Real
# @param [Hash] options
# @option options [String] :Description Optional description.
# @option options [Hash] :Configuration Network configuration.
# @option options [Hash] :EdgeGateway EdgeGateway that connects this
# @option options [Hash] :EdgeGateway EdgeGateway that connects this
# Org vDC network. Applicable only for routed networks.
# @option options [Hash] :ServiceConfig Specifies the service
# @option options [Hash] :ServiceConfig Specifies the service
# configuration for an isolated Org vDC networks.
# @option options [Boolean] :IsShared True if this network is shared
# @option options [Boolean] :IsShared True if this network is shared
# to multiple Org vDCs.
# * :Configuration<~Hash>: NetworkConfigurationType
# * :IpScopes<~Hash>:
Expand Down Expand Up @@ -76,6 +76,99 @@ def post_create_org_vdc_network(vdc_id, name, options={})
)
end

type = 'network'
id = uuid

# Description
# Configuration
# IpScopes
# IpScope
# IsInherited
# Gateway
# Netmask
# Dns1
# Dns2
# DnsSuffix
# IsEnabled
# IpRanges
# IpRange
# StartAddress
# EndAddress
# FenceMode
# EdgeGateway
# IsShared

network_body = {
:name => name,
:vdc => vdc_id,
}

[:Description, :IsShared].each do |key|
network_body[key] = options[key] if options.key?(key)
end

if options.key?(:EdgeGateway)
network_body[:EdgeGateway] =
options[:EdgeGateway][:href].split('/').last
end

if configuration = options[:Configuration]
if ip_scopes = configuration[:IpScopes]
if ip_scope = ip_scopes[:IpScope]
[:IsInherited, :Gateway, :Netmask,
:Dns1, :Dns2, :DnsSuffix, :IsEnabled].each do |key|
network_body[key] = ip_scope[key] if ip_scope.key?(key)
end
if ip_ranges = ip_scope[:IpRanges]
network_body[:IpRanges] = []
ip_ranges.each do |ipr|
network_body[:IpRanges] << {
:StartAddress => ipr[:IpRange][:StartAddress],
:EndAddress => ipr[:IpRange][:EndAddress]
}
end
end
end
end
network_body[:FenceMode] = configuration[:FenceMode] if ip_scope.key?(:FenceMode)
end

owner = {
:href => make_href("#{type}/#{id}"),
:type => "application/vnd.vmware.vcloud.#{type}+xml"
}
task_id = enqueue_task(
"Adding #{type} #{name} (#{id})", 'CreateOrgVdcNetwork', owner,
:on_success => lambda do
data[:networks][id] = network_body
end
)

body = {
:xmlns => xmlns,
:xmlns_xsi => xmlns_xsi,
:xsi_schemaLocation => xsi_schema_location,
:href => make_href("admin/network/#{id}"),
:name => name,
:id => "urn:vcloud:network:#{id}",
:type => "application/vnd.vmware.vcloud.orgVdcNetwork+xml",
:Link => [
{:rel=>"up", :type=>"application/vnd.vmware.vcloud.vdc+xml", :href=>make_href("vdc/#{vdc_id}")},
{:rel=>"down", :type=>"application/vnd.vmware.vcloud.metadata+xml", :href=>make_href("admin/network/#{id}/metadata")},
{:rel=>"down", :type=>"application/vnd.vmware.vcloud.allocatedNetworkAddress+xml", :href=>make_href("admin/network/#{id}/allocatedAddresses/")},
],
}.merge(options)

body[:Tasks] = {
:Task => task_body(task_id)
}

Excon::Response.new(
:status => 201,
:headers => {'Content-Type' => "#{body[:type]};version=#{api_version}"},
:body => body
)

end
end
end
Expand Down
65 changes: 51 additions & 14 deletions tests/vcloud_director/requests/compute/network_tests.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

require 'pp'

Shindo.tests('Compute::VcloudDirector | network requests', ['vclouddirector']) do

GET_NETWORK_FORMAT = {
Expand All @@ -20,6 +23,7 @@

@service = Fog::Compute::VcloudDirector.new
@org = VcloudDirector::Compute::Helper.current_org(@service)
@created_net_id = nil

tests('Create network in non-existent vDC').raises(Fog::Compute::VcloudDirector::Forbidden) do
@service.post_create_org_vdc_network('00000000-0000-0000-0000-000000000000', 'bob')
Expand All @@ -29,6 +33,10 @@
@service.delete_network('00000000-0000-0000-0000-000000000000')
end

tests('Retrieve non-existent OrgNetwork').raises(Fog::Compute::VcloudDirector::Forbidden) do
@service.get_network('00000000-0000-0000-0000-000000000000')
end

tests('#get_network').data_matches_schema(GET_NETWORK_FORMAT) do
link = @org[:Link].detect do |l|
l[:rel] == 'down' && l[:type] == 'application/vnd.vmware.vcloud.orgNetwork+xml'
Expand All @@ -44,23 +52,52 @@
@service.get_network_metadata(@network_id).body
end

tests('Retrieve non-existent OrgNetwork').raises(Fog::Compute::VcloudDirector::Forbidden) do
@service.get_network('00000000-0000-0000-0000-000000000000')
end
tests('#post_create_org_vdc_network') do
pending unless Fog.mocking?
link = @org[:Link].detect do |l|
l[:rel] == 'down' && l[:type] == 'application/vnd.vmware.vcloud.vdc+xml'
end

vdc_id = link[:href].split('/').last
name = VcloudDirector::Compute::Helper.test_name

# at the moment just test delete_network in Mock mode, until we have
# ability to create a test one in Real mode
if Fog.mocking?
# TODO replace with a Query API lookup when available
net_id = @service.data[:networks].keys.first
@delete_task = @service.delete_network(net_id).body
tests('#delete_network returns Task').data_matches_schema(VcloudDirector::Compute::Schema::TASK_TYPE) do
@delete_task
options = {
:Description => "Testing post_create_org_vdc_network #{name}",
:Configuration => {
:IpScopes => {
:IpScope => {
:IsInherited => 'false',
:Gateway => '198.51.100.1',
:Netmask => '255.255.255.0',
:Dns1 => '198.51.100.2',
:Dns2 => '198.51.100.3',
:DnsSuffix => 'example.com',
:IpRanges => [
{ :IpRange => { :StartAddress => '198.51.100.10', :EndAddress => '198.51.100.20' } },
{ :IpRange => { :StartAddress => '198.51.100.30', :EndAddress => '198.51.100.40' } },
]
},
},
:FenceMode => 'isolated',
}
}

body = @service.post_create_org_vdc_network(vdc_id, name, options).body
@created_net_id = body[:href].split('/').last if body[:href]
@service.process_task(body[:Tasks][:Task]) if body && body.key?(:Tasks)

tests('fetched name matches created name').returns(name) do
net = @service.get_network(@created_net_id).body
net[:name]
end
end

tests('#delete_network') do
pending unless Fog.mocking?
@delete_task = @service.delete_network(@created_net_id).body
@service.process_task(@delete_task)
tests('#delete_network succeeds').returns(nil) do
# network missing now?
net_id = @service.data[:networks][net_id]
tests('created network has been deleted').raises(Fog::Compute::VcloudDirector::Forbidden) do
@service.get_network(@created_net_id)
end
end

Expand Down

0 comments on commit 15180fd

Please sign in to comment.