From 7c4ecd748271afcb2377ed1c708efaa45f05ae6e Mon Sep 17 00:00:00 2001 From: Mike Pountney Date: Thu, 19 Dec 2013 15:25:49 +0000 Subject: [PATCH] add Mock for post_create_network, use this to create/delete network in tests --- .../compute/post_create_org_vdc_network.rb | 99 ++++++++++++++++++- .../requests/compute/network_tests.rb | 65 +++++++++--- 2 files changed, 147 insertions(+), 17 deletions(-) diff --git a/lib/fog/vcloud_director/requests/compute/post_create_org_vdc_network.rb b/lib/fog/vcloud_director/requests/compute/post_create_org_vdc_network.rb index 9faaed48c4..2228a7e2d5 100644 --- a/lib/fog/vcloud_director/requests/compute/post_create_org_vdc_network.rb +++ b/lib/fog/vcloud_director/requests/compute/post_create_org_vdc_network.rb @@ -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>: @@ -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 diff --git a/tests/vcloud_director/requests/compute/network_tests.rb b/tests/vcloud_director/requests/compute/network_tests.rb index 31de13af00..6cb445d626 100644 --- a/tests/vcloud_director/requests/compute/network_tests.rb +++ b/tests/vcloud_director/requests/compute/network_tests.rb @@ -1,3 +1,6 @@ + +require 'pp' + Shindo.tests('Compute::VcloudDirector | network requests', ['vclouddirector']) do GET_NETWORK_FORMAT = { @@ -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') @@ -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' @@ -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