From 8e79419ccee8a1d934544b9556ea91c7e8206d64 Mon Sep 17 00:00:00 2001 From: Wesley Beary Date: Mon, 17 Aug 2009 09:45:00 -0700 Subject: [PATCH] security group mocks and test additions --- lib/fog/aws.rb | 4 + lib/fog/aws/ec2.rb | 2 +- .../aws/requests/ec2/create_security_group.rb | 77 +++++++++++---- .../aws/requests/ec2/delete_security_group.rb | 66 +++++++++---- .../requests/ec2/describe_security_groups.rb | 97 +++++++++++++------ spec/aws/ec2/create_security_group_spec.rb | 6 ++ spec/aws/ec2/delete_security_group_spec.rb | 6 ++ spec/aws/ec2/describe_security_groups_spec.rb | 34 +++---- 8 files changed, 200 insertions(+), 92 deletions(-) diff --git a/lib/fog/aws.rb b/lib/fog/aws.rb index c23c9c4c6..82cee58cc 100644 --- a/lib/fog/aws.rb +++ b/lib/fog/aws.rb @@ -62,6 +62,10 @@ module Fog key_material.join("\n") end + def self.owner_id + numbers(12) + end + def self.request_id request_id = [] request_id << hex(8) diff --git a/lib/fog/aws/ec2.rb b/lib/fog/aws/ec2.rb index aad8ef18a..7d9e27277 100644 --- a/lib/fog/aws/ec2.rb +++ b/lib/fog/aws/ec2.rb @@ -175,7 +175,7 @@ module Fog @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}") if Fog.mocking? - @data = { :deleted_at => {}, :addresses => {}, :key_pairs => {}, :volumes => {} } + @data = { :deleted_at => {}, :addresses => {}, :key_pairs => {}, :security_groups => {}, :volumes => {} } end end diff --git a/lib/fog/aws/requests/ec2/create_security_group.rb b/lib/fog/aws/requests/ec2/create_security_group.rb index 91d32552d..0e204b665 100644 --- a/lib/fog/aws/requests/ec2/create_security_group.rb +++ b/lib/fog/aws/requests/ec2/create_security_group.rb @@ -1,26 +1,61 @@ -module Fog - module AWS - class EC2 +unless Fog.mocking? + + module Fog + module AWS + class EC2 + + # Create a new security group + # + # ==== Parameters + # * group_name<~String> - Name of the security group. + # * group_description<~String> - Description of group. + # + # ==== Returns + # * response<~Fog::AWS::Response>: + # * body<~Hash>: + # * 'requestId'<~String> - Id of request + # * 'return'<~Boolean> - success? + def create_security_group(name, description) + request({ + 'Action' => 'CreateSecurityGroup', + 'GroupName' => name, + 'GroupDescription' => CGI.escape(description) + }, Fog::Parsers::AWS::EC2::Basic.new) + end - # Create a new security group - # - # ==== Parameters - # * group_name<~String> - Name of the security group. - # * group_description<~String> - Description of group. - # - # ==== Returns - # * response<~Fog::AWS::Response>: - # * body<~Hash>: - # * 'requestId'<~String> - Id of request - # * 'return'<~Boolean> - success? - def create_security_group(name, description) - request({ - 'Action' => 'CreateSecurityGroup', - 'GroupName' => name, - 'GroupDescription' => CGI.escape(description) - }, Fog::Parsers::AWS::EC2::Basic.new) end - end end + +else + + module Fog + module AWS + class EC2 + + def create_security_group(name, description) + response = Fog::Response.new + unless @data[:security_groups][name] + data = { + 'GroupDescription' => description, + 'GroupName' => name, + 'ipPermissions' => [], + 'OwnerId' => Fog::AWS::Mock.owner_id + } + @data[:security_groups][name] = data + response.body = { + 'requestId' => Fog::AWS::Mock.request_id, + 'return' => true + } + else + response.status = 400 + raise(Fog::Errors.status_error(200, 400, response)) + end + response + end + + end + end + end + end diff --git a/lib/fog/aws/requests/ec2/delete_security_group.rb b/lib/fog/aws/requests/ec2/delete_security_group.rb index de050f794..cfc86e436 100644 --- a/lib/fog/aws/requests/ec2/delete_security_group.rb +++ b/lib/fog/aws/requests/ec2/delete_security_group.rb @@ -1,24 +1,52 @@ -module Fog - module AWS - class EC2 +unless Fog.mocking? + + module Fog + module AWS + class EC2 + + # Delete a security group that you own + # + # ==== Parameters + # * group_name<~String> - Name of the security group. + # + # ==== Returns + # * response<~Fog::AWS::Response>: + # * body<~Hash>: + # * 'requestId'<~String> - Id of request + # * 'return'<~Boolean> - success? + def delete_security_group(name) + request({ + 'Action' => 'DeleteSecurityGroup', + 'GroupName' => name + }, Fog::Parsers::AWS::EC2::Basic.new) + end - # Delete a security group that you own - # - # ==== Parameters - # * group_name<~String> - Name of the security group. - # - # ==== Returns - # * response<~Fog::AWS::Response>: - # * body<~Hash>: - # * 'requestId'<~String> - Id of request - # * 'return'<~Boolean> - success? - def delete_security_group(name) - request({ - 'Action' => 'DeleteSecurityGroup', - 'GroupName' => name - }, Fog::Parsers::AWS::EC2::Basic.new) end - end end + +else + + module Fog + module AWS + class EC2 + def delete_security_group(name) + response = Fog::Response.new + if @data[:security_groups][name] + @data[:security_groups].delete(name) + response.status = 200 + response.body = { + 'requestId' => Fog::AWS::Mock.request_id, + 'return' => true + } + else + response.status = 400 + raise(Fog::Errors.status_error(200, 400, response)) + end + response + end + end + end + end + end diff --git a/lib/fog/aws/requests/ec2/describe_security_groups.rb b/lib/fog/aws/requests/ec2/describe_security_groups.rb index f6d85e815..d22279b34 100644 --- a/lib/fog/aws/requests/ec2/describe_security_groups.rb +++ b/lib/fog/aws/requests/ec2/describe_security_groups.rb @@ -1,36 +1,71 @@ -module Fog - module AWS - class EC2 +unless Fog.mocking? + + module Fog + module AWS + class EC2 + + # Describe all or specified security groups + # + # ==== Parameters + # * group_name<~Array> - List of groups to describe, defaults to all + # + # === Returns + # * response<~Fog::AWS::Response>: + # * body<~Hash>: + # * 'requestId'<~String> - Id of request + # * 'securityGroupInfo'<~Array>: + # * 'groupDescription'<~String> - Description of security group + # * 'groupName'<~String> - Name of security group + # * 'ipPermissions'<~Array>: + # * 'fromPort'<~Integer> - Start of port range (or -1 for ICMP wildcard) + # * 'groups'<~Array>: + # * 'groupName'<~String> - Name of security group + # * 'userId'<~String> - AWS User Id of account + # * 'ipProtocol'<~String> - Ip protocol, must be in ['tcp', 'udp', 'icmp'] + # * 'ipRanges'<~Array>: + # * 'cidrIp'<~String> - CIDR range + # * 'toPort'<~Integer> - End of port range (or -1 for ICMP wildcard) + # * 'ownerId'<~String> - AWS Access Key Id of the owner of the security group + def describe_security_groups(group_name = []) + params = indexed_params('GroupName', group_name) + request({ + 'Action' => 'DescribeSecurityGroups', + }.merge!(params), Fog::Parsers::AWS::EC2::DescribeSecurityGroups.new) + end - # Describe all or specified security groups - # - # ==== Parameters - # * group_name<~Array> - List of groups to describe, defaults to all - # - # === Returns - # * response<~Fog::AWS::Response>: - # * body<~Hash>: - # * 'requestId'<~String> - Id of request - # * 'securityGroupInfo'<~Array>: - # * 'groupDescription'<~String> - Description of security group - # * 'groupName'<~String> - Name of security group - # * 'ipPermissions'<~Array>: - # * 'fromPort'<~Integer> - Start of port range (or -1 for ICMP wildcard) - # * 'groups'<~Array>: - # * 'groupName'<~String> - Name of security group - # * 'userId'<~String> - AWS User Id of account - # * 'ipProtocol'<~String> - Ip protocol, must be in ['tcp', 'udp', 'icmp'] - # * 'ipRanges'<~Array>: - # * 'cidrIp'<~String> - CIDR range - # * 'toPort'<~Integer> - End of port range (or -1 for ICMP wildcard) - # * 'ownerId'<~String> - AWS Access Key Id of the owner of the security group - def describe_security_groups(group_name = []) - params = indexed_params('GroupName', group_name) - request({ - 'Action' => 'DescribeSecurityGroups', - }.merge!(params), Fog::Parsers::AWS::EC2::DescribeSecurityGroups.new) end - end end + +else + + module Fog + module AWS + class EC2 + + def describe_security_groups(group_name = []) + group_name = [*group_name] + response = Fog::Response.new + if group_name != [] + security_group_info = @data[:security_groups].reject {|key, value| !group_name.include?(key)}.values + else + security_group_info = @data[:security_groups].values + end + if group_name.length == 0 || group_name.length == security_group_info.length + response.status = 200 + response.body = { + 'requestId' => Fog::AWS::Mock.request_id, + 'securityGroupInfo' => security_group_info + } + else + response.status = 400 + raise(Fog::Errors.status_error(200, 400, response)) + end + response + end + + end + end + end + end diff --git a/spec/aws/ec2/create_security_group_spec.rb b/spec/aws/ec2/create_security_group_spec.rb index 23ff80974..f59e3843c 100644 --- a/spec/aws/ec2/create_security_group_spec.rb +++ b/spec/aws/ec2/create_security_group_spec.rb @@ -16,4 +16,10 @@ describe 'EC2.create_security_group' do [false, true].should include(actual.body['return']) end + it "should raise a BadRequest error when the security group already exists" do + lambda { + @ec2.create_security_group('fog_security_group', 'a security group for testing fog') + }.should raise_error(Fog::Errors::BadRequest) + end + end diff --git a/spec/aws/ec2/delete_security_group_spec.rb b/spec/aws/ec2/delete_security_group_spec.rb index 485cc9f0e..f80f2e82e 100644 --- a/spec/aws/ec2/delete_security_group_spec.rb +++ b/spec/aws/ec2/delete_security_group_spec.rb @@ -13,4 +13,10 @@ describe 'EC2.delete_security_group' do [false, true].should include(actual.body['return']) end + it "should raise a BadRequest error if the security group does not exist" do + lambda { + @ec2.delete_security_group('fog_not_a_security_group') + }.should raise_error(Fog::Errors::BadRequest) + end + end diff --git a/spec/aws/ec2/describe_security_groups_spec.rb b/spec/aws/ec2/describe_security_groups_spec.rb index e4d379ac5..22876b08b 100644 --- a/spec/aws/ec2/describe_security_groups_spec.rb +++ b/spec/aws/ec2/describe_security_groups_spec.rb @@ -4,6 +4,11 @@ describe 'EC2.describe_security_groups' do before(:all) do @ec2 = Fog::AWS::EC2.gen + @ec2.create_security_group('fog_security_group', 'a security group for testing fog') + end + + after(:all) do + @ec2.delete_security_group('fog_security_group') end it "should return proper attributes with no params" do @@ -17,37 +22,26 @@ describe 'EC2.describe_security_groups' do security_group['groupName'].should be_a(String) security_group['ownerId'].should be_a(String) security_group['ipPermissions'].should be_an(Array) - ip_permission = security_group['ipPermissions'].first - ip_permission['groups'].should be_an(Array) - group = ip_permission['groups'].first - group['groupName'].should be_a(String) - group['userId'].should be_a(String) - ip_permission['fromPort'].should be_an(Integer) - ip_permission['ipProtocol'].should be_a(String) - ip_permission['ipRanges'].should be_an(Array) - ip_permission['toPort'].should be_an(Integer) + end it "should return proper attributes with params" do - actual = @ec2.describe_security_groups('default') + actual = @ec2.describe_security_groups('fog_security_group') actual.body['requestId'].should be_a(String) actual.body['securityGroupInfo'].should be_an(Array) security_group = actual.body['securityGroupInfo'].select do |security_group| - security_group['groupName'] == 'default' + security_group['groupName'] == 'fog_security_group' end.first security_group['groupDescription'].should be_a(String) security_group['groupName'].should be_a(String) security_group['ownerId'].should be_a(String) security_group['ipPermissions'].should be_an(Array) - ip_permission = security_group['ipPermissions'].first - ip_permission['groups'].should be_an(Array) - group = ip_permission['groups'].first - group['groupName'].should be_a(String) - group['userId'].should be_a(String) - ip_permission['fromPort'].should be_an(Integer) - ip_permission['ipProtocol'].should be_a(String) - ip_permission['ipRanges'].should be_an(Array) - ip_permission['toPort'].should be_an(Integer) + end + + it "should raise a BadRequest error if the security group does not exist" do + lambda { + @ec2.describe_security_groups('not_a_security_group') + }.should raise_error(Fog::Errors::BadRequest) end end