From 4577e2d0933b55a00d89adf428fbacd39227baef Mon Sep 17 00:00:00 2001 From: geemus Date: Mon, 24 May 2010 17:41:01 -0700 Subject: [PATCH] [ec2] fix gaps in mocking revealed by new shindo tests --- lib/fog/aws/ec2.rb | 20 ++++---- .../ec2/authorize_security_group_ingress.rb | 51 ++++++++++++------- .../aws/requests/ec2/create_security_group.rb | 4 +- .../ec2/describe_availability_zones.rb | 15 ++++-- lib/fog/aws/requests/ec2/describe_regions.rb | 15 ++++-- .../ec2/revoke_security_group_ingress.rb | 42 +++++++++------ 6 files changed, 92 insertions(+), 55 deletions(-) diff --git a/lib/fog/aws/ec2.rb b/lib/fog/aws/ec2.rb index 447a15572..81ef6fc1f 100644 --- a/lib/fog/aws/ec2.rb +++ b/lib/fog/aws/ec2.rb @@ -96,40 +96,42 @@ module Fog def self.data @data ||= Hash.new do |hash, key| + owner_id = Fog::AWS::Mock.owner_id hash[key] = { :deleted_at => {}, - :addresses => {}, - :instances => {}, - :key_pairs => {}, - :limits => { :addresses => 5 }, + :addresses => {}, + :instances => {}, + :key_pairs => {}, + :limits => { :addresses => 5 }, + :owner_id => owner_id, :security_groups => { 'default' => { 'groupDescription' => 'default group', 'groupName' => 'default', 'ipPermissions' => [ { - 'groups' => [{'groupName' => 'default', 'userId' => @owner_id}], + 'groups' => [{'groupName' => 'default', 'userId' => owner_id}], 'fromPort' => -1, 'toPort' => -1, 'ipProtocol' => 'icmp', 'ipRanges' => [] }, { - 'groups' => [{'groupName' => 'default', 'userId' => @owner_id}], + 'groups' => [{'groupName' => 'default', 'userId' => owner_id}], 'fromPort' => 0, 'toPort' => 65535, 'ipProtocol' => 'tcp', 'ipRanges' => [] }, { - 'groups' => [{'groupName' => 'default', 'userId' => @owner_id}], + 'groups' => [{'groupName' => 'default', 'userId' => owner_id}], 'fromPort' => 0, 'toPort' => 65535, 'ipProtocol' => 'udp', 'ipRanges' => [] } ], - 'ownerId' => @owner_id + 'ownerId' => owner_id } }, :snapshots => {}, @@ -146,8 +148,8 @@ module Fog def initialize(options={}) @aws_access_key_id = options[:aws_access_key_id] - @owner_id = Fog::AWS::Mock.owner_id @data = self.class.data[@aws_access_key_id] + @owner_id = @data[:owner_id] end end diff --git a/lib/fog/aws/requests/ec2/authorize_security_group_ingress.rb b/lib/fog/aws/requests/ec2/authorize_security_group_ingress.rb index fcd5a6ed1..6db3e8d6f 100644 --- a/lib/fog/aws/requests/ec2/authorize_security_group_ingress.rb +++ b/lib/fog/aws/requests/ec2/authorize_security_group_ingress.rb @@ -37,32 +37,47 @@ module Fog def authorize_security_group_ingress(options = {}) response = Excon::Response.new group = @data[:security_groups][options['GroupName']] - group['ipPermissions'] ||= [] - if options['GroupName'] && options['SourceSecurityGroupName'] && options['SourceSecurityGroupOwnerId'] - ['icmp', 'tcp', 'udp'].each do |protocol| + if group + group['ipPermissions'] ||= [] + if options['GroupName'] && options['SourceSecurityGroupName'] && options['SourceSecurityGroupOwnerId'] + ['tcp', 'udp'].each do |protocol| + group['ipPermissions'] << { + 'groups' => [{'groupName' => options['GroupName'], 'userId' => @owner_id}], + 'fromPort' => 1, + 'ipRanges' => [], + 'ipProtocol' => protocol, + 'toPort' => 65535 + } + end group['ipPermissions'] << { 'groups' => [{'groupName' => options['GroupName'], 'userId' => @owner_id}], - 'fromPort' => 1, + 'fromPort' => -1, 'ipRanges' => [], - 'ipProtocol' => protocol, - 'toPort' => 65535 + 'ipProtocol' => 'icmp', + 'toPort' => -1 } + else + group['ipPermissions'] << { + 'groups' => [], + 'fromPort' => options['FromPort'], + 'ipRanges' => [], + 'ipProtocol' => options['IpProtocol'], + 'toPort' => options['ToPort'] + } + if options['CidrIp'] + group['ipPermissions'].last['ipRanges'] << { 'cidrIp' => options['CidrIp'] } + end end - else - group['ipPermissions'] << { - 'groups' => [], - 'fromPort' => options['FromPort'], - 'ipRanges' => [{ 'cidrIp' => options['CidrIp'] }], - 'ipProtocol' => options['IpProtocol'], - 'toPort' => options['ToPort'] + response.status = 200 + response.body = { + 'requestId' => Fog::AWS::Mock.request_id, + 'return' => true } + else + response.status = 400 + raise(Excon::Errors.status_error({:expects => 200}, response)) end - response.status = 200 - response.body = { - 'requestId' => Fog::AWS::Mock.request_id, - 'return' => true - } response end diff --git a/lib/fog/aws/requests/ec2/create_security_group.rb b/lib/fog/aws/requests/ec2/create_security_group.rb index 50e90b011..91f2d6f32 100644 --- a/lib/fog/aws/requests/ec2/create_security_group.rb +++ b/lib/fog/aws/requests/ec2/create_security_group.rb @@ -31,8 +31,8 @@ module Fog response = Excon::Response.new unless @data[:security_groups][name] data = { - 'groupDescription' => description, - 'groupName' => name, + 'groupDescription' => CGI.escape(description).gsub('%20', '+'), + 'groupName' => CGI.escape(name).gsub('%20', '+'), 'ipPermissions' => [], 'ownerId' => @owner_id } diff --git a/lib/fog/aws/requests/ec2/describe_availability_zones.rb b/lib/fog/aws/requests/ec2/describe_availability_zones.rb index 46095a9ac..71dc47c3e 100644 --- a/lib/fog/aws/requests/ec2/describe_availability_zones.rb +++ b/lib/fog/aws/requests/ec2/describe_availability_zones.rb @@ -44,11 +44,16 @@ module Fog availability_zone_info = zones.values end - response.status = 200 - response.body = { - 'requestId' => Fog::AWS::Mock.request_id, - 'availabilityZoneInfo' => availability_zone_info - } + if zone_name.length == 0 || zone_name.length == availability_zone_info.length + response.status = 200 + response.body = { + 'requestId' => Fog::AWS::Mock.request_id, + 'availabilityZoneInfo' => availability_zone_info + } + else + response.status = 400 + raise(Excon::Errors.status_error({:expects => 200}, response)) + end response end diff --git a/lib/fog/aws/requests/ec2/describe_regions.rb b/lib/fog/aws/requests/ec2/describe_regions.rb index 87cd94d25..993d44650 100644 --- a/lib/fog/aws/requests/ec2/describe_regions.rb +++ b/lib/fog/aws/requests/ec2/describe_regions.rb @@ -41,11 +41,16 @@ module Fog region_info = regions.values end - response.status = 200 - response.body = { - 'requestId' => Fog::AWS::Mock.request_id, - 'regionInfo' => region_info - } + if region_name.length == 0 || region_name.length == region_info.length + response.status = 200 + response.body = { + 'requestId' => Fog::AWS::Mock.request_id, + 'regionInfo' => region_info + } + else + response.status = 400 + raise(Excon::Errors.status_error({:expects => 200}, response)) + end response end diff --git a/lib/fog/aws/requests/ec2/revoke_security_group_ingress.rb b/lib/fog/aws/requests/ec2/revoke_security_group_ingress.rb index 8bf83329f..6a513ae28 100644 --- a/lib/fog/aws/requests/ec2/revoke_security_group_ingress.rb +++ b/lib/fog/aws/requests/ec2/revoke_security_group_ingress.rb @@ -35,28 +35,38 @@ module Fog class Mock def revoke_security_group_ingress(options = {}) - if options['GroupName'] && options['SourceSecurityGroupName'] && options['SourceSecurityGroupOwnerId'] - raise MockNotImplemented.new("Contributions welcome!") - else - response = Excon::Response.new - group = @data[:security_groups][options['GroupName']] - - ingress = group['ipPermissions'].select {|permission| - permission['fromPort'] == options['FromPort'] && - permission['ipProtocol'] == options['IpProtocol'] && - permission['toPort'] == options['ToPort'] && - permission['ipRanges'].first['cidrIp'] == options['CidrIp'] - }.first - - group['ipPermissions'].delete(ingress) - + response = Excon::Response.new + group = @data[:security_groups][options['GroupName']] + if group + if options['GroupName'] && options['SourceSecurityGroupName'] && options['SourceSecurityGroupOwnerId'] + group['ipPermissions'].delete_if {|permission| + permission['groups'].first['groupName'] == options['GroupName'] + } + else + ingress = group['ipPermissions'].select {|permission| + permission['fromPort'] == options['FromPort'] && + permission['ipProtocol'] == options['IpProtocol'] && + permission['toPort'] == options['ToPort'] && + ( + permission['ipRanges'].empty? || + ( + permission['ipRanges'].first && + permission['ipRanges'].first['cidrIp'] == options['CidrIp'] + ) + ) + }.first + group['ipPermissions'].delete(ingress) + end response.status = 200 response.body = { 'requestId' => Fog::AWS::Mock.request_id, 'return' => true } - response + else + response.status = 400 + raise(Excon::Errors.status_error({:expects => 200}, response)) end + response end end