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,4 +1,6 @@
module Fog
unless Fog.mocking?
module Fog
module AWS
class EC2
@ -23,4 +25,37 @@ module Fog
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,4 +1,6 @@
module Fog
unless Fog.mocking?
module Fog
module AWS
class EC2
@ -21,4 +23,30 @@ module Fog
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,4 +1,6 @@
module Fog
unless Fog.mocking?
module Fog
module AWS
class EC2
@ -33,4 +35,37 @@ module Fog
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