mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Merge pull request #2491 from alphagov/vdc_create_network_before_delete
[vcloud_director] add Mock for post_create_network, improve Mock tests
This commit is contained in:
commit
15180fd7c0
2 changed files with 147 additions and 17 deletions
|
@ -76,6 +76,99 @@ module Fog
|
|||
)
|
||||
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
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
|
||||
require 'pp'
|
||||
|
||||
Shindo.tests('Compute::VcloudDirector | network requests', ['vclouddirector']) do
|
||||
|
||||
GET_NETWORK_FORMAT = {
|
||||
|
@ -20,6 +23,7 @@ Shindo.tests('Compute::VcloudDirector | network requests', ['vclouddirector']) d
|
|||
|
||||
@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')
|
||||
|
@ -29,6 +33,10 @@ Shindo.tests('Compute::VcloudDirector | network requests', ['vclouddirector']) d
|
|||
@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'
|
||||
|
@ -44,23 +52,52 @@ Shindo.tests('Compute::VcloudDirector | network requests', ['vclouddirector']) d
|
|||
@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')
|
||||
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
|
||||
|
||||
# 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
|
||||
vdc_id = link[:href].split('/').last
|
||||
name = VcloudDirector::Compute::Helper.test_name
|
||||
|
||||
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
|
||||
|
||||
|
|
Loading…
Reference in a new issue