1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

security group mocks and test additions

This commit is contained in:
Wesley Beary 2009-08-17 09:45:00 -07:00
parent 64742c52d0
commit 8e79419cce
8 changed files with 200 additions and 92 deletions

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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