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

Merge pull request #679 from restebanez/rds_sec_group_mocking

[aws|rds] Rds security group mocking. shindo security group.
This commit is contained in:
Wesley Beary 2011-12-27 17:25:22 -08:00
commit dcbf98e5a7
13 changed files with 322 additions and 33 deletions

View file

@ -5,6 +5,8 @@ module Fog
class RDS < Fog::Service
class IdentifierTaken < Fog::Errors::Error; end
class AuthorizationAlreadyExists < Fog::Errors::Error; end
requires :aws_access_key_id, :aws_secret_access_key
recognizes :region, :host, :path, :port, :scheme, :persistent
@ -62,7 +64,8 @@ module Fog
owner_id = Fog::AWS::Mock.owner_id
hash[region] = Hash.new do |region_hash, key|
region_hash[key] = {
:servers => {}
:servers => {},
:security_groups => {}
}
end
end
@ -187,6 +190,8 @@ module Fog
raise Fog::AWS::RDS::NotFound.slurp(error, match[2])
when 'DBParameterGroupAlreadyExists'
raise Fog::AWS::RDS::IdentifierTaken.slurp(error, match[2])
when 'AuthorizationAlreadyExists'
raise Fog::AWS::RDS::AuthorizationAlreadyExists.slurp(error, match[2])
else
raise
end

View file

@ -33,7 +33,36 @@ module Fog
class Mock
def authorize_db_security_group_ingress(name, opts = {})
Fog::Mock.not_implemented
unless opts.key?('CIDRIP') || (opts.key?('EC2SecurityGroupName') && opts.key?('EC2SecurityGroupOwnerId'))
raise ArgumentError, 'Must specify CIDRIP, or both EC2SecurityGroupName and EC2SecurityGroupOwnerId'
end
response = Excon::Response.new
if sec_group = self.data[:security_groups][name]
if opts.key?('CIDRIP')
if sec_group['IPRanges'].detect{|h| h['CIDRIP'] == opts['CIDRIP']}
raise Fog::AWS::RDS::AuthorizationAlreadyExists.new("AuthorizationAlreadyExists => #{opts['CIDRIP']} is alreay defined")
end
sec_group['IPRanges'] << opts.merge({"Status" => 'authorizing'})
else
if sec_group['EC2SecurityGroups'].detect{|h| h['EC2SecurityGroupName'] == opts['EC2SecurityGroupName']}
raise Fog::AWS::RDS::AuthorizationAlreadyExists.new("AuthorizationAlreadyExists => #{opts['EC2SecurityGroupName']} is alreay defined")
end
sec_group['EC2SecurityGroups'] << opts.merge({"Status" => 'authorizing'})
end
response.status = 200
response.body = {
"ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
'AuthorizeDBSecurityGroupIngressResult' => {
'DBSecurityGroup' => sec_group
}
}
response
else
raise Fog::AWS::RDS::NotFound.new("DBSecurityGroupNotFound => #{name} not found")
end
end
end

View file

@ -77,10 +77,10 @@ module Fog
{
"DBInstanceIdentifier"=> db_name,
"DBName" => options["DBName"],
"created_at" => nil,
"InstanceCreateTime" => nil,
"AutoMinorVersionUpgrade"=>true,
"Endpoint"=>{},
"ReadReplicaDBInstanceIdentifiers"=>[],
"ReadReplicaDBInstanceIdentifiers"=>['bla'],
"PreferredMaintenanceWindow"=>"mon:04:30-mon:05:00",
"Engine"=> options["Engine"],
"EngineVersion"=> options["EngineVersion"] || "5.1.57",
@ -91,14 +91,17 @@ module Fog
"DBInstanceStatus"=>"creating",
"BackupRetentionPeriod"=> options["BackupRetentionPeriod"] || 1,
"AllocatedStorage"=> options["AllocatedStorage"],
"DBParameterGroups"=> # I think groups shoul be in the self.data method
"DBParameterGroups"=> # I think groups should be in the self.data method
[{"DBParameterGroupName"=>"default.mysql5.1",
"ParameterApplyStatus"=>"in-sync"}],
"DBSecurityGroups"=>
[{"Status"=>"active",
"DBSecurityGroupName"=>"default"}],
"LicenseModel"=>"general-public-license",
"PreferredBackupWindow"=>"08:00-08:30"
"PreferredBackupWindow"=>"08:00-08:30",
# "ReadReplicaSourceDBInstanceIdentifier" => nil,
# "LatestRestorableTime" => nil,
"AvailabilityZone" => options["AvailabilityZone"]
}
@ -109,7 +112,7 @@ module Fog
}
response.status = 200
# This values aren't showed at creating time but at available time
self.data[:servers][db_name]["created_at"] = Time.now
self.data[:servers][db_name]["InstanceCreateTime"] = Time.now
response
end

View file

@ -27,7 +27,25 @@ module Fog
class Mock
def create_db_security_group(name, description = name)
Fog::Mock.not_implemented
response = Excon::Response.new
if self.data[:security_groups] and self.data[:security_groups][name]
raise Fog::AWS::RDS::IdentifierTaken.new("DBInstanceAlreadyExists => The security group '#{name}' already exists")
end
data = {
'DBSecurityGroupName' => name,
'DBSecurityGroupDescription' => description,
'EC2SecurityGroups' => [],
'IPRanges' => [],
'OwnerId' => '0123456789'
}
self.data[:security_groups][name] = data
response.body = {
"ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
'CreateDBSecurityGroupResult' => { 'DBSecurityGroup' => data }
}
response
end
end

View file

@ -25,7 +25,17 @@ module Fog
class Mock
def delete_db_security_group(name, description = name)
Fog::Mock.not_implemented
response = Excon::Response.new
if self.data[:security_groups].delete(name)
response.status = 200
response.body = {
"ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
}
response
else
raise Fog::AWS::RDS::NotFound.new("DBSecurityGroupNotFound => #{name} not found")
end
end
end

View file

@ -48,21 +48,37 @@ module Fog
server_set.each do |server|
case server["DBInstanceStatus"]
when "creating"
if Time.now - server['created_at'] >= Fog::Mock.delay * 2
if Time.now - server['InstanceCreateTime'] >= Fog::Mock.delay * 2
region = "us-east-1"
server["DBInstanceStatus"] = "available"
server["availability_zone"] = region + 'a'
server["AvailabilityZone"] = region + 'a'
server["Endpoint"] = {"Port"=>3306,
"Address"=> Fog::AWS::Mock.rds_address(server["DBInstanceIdentifier"],region) }
server["PendingModifiedValues"] = {}
end
when "rebooting"
when "rebooting" # I don't know how to show rebooting just once before it changes to available
# it applies pending modified values
if server["PendingModifiedValues"]
server.merge!(server["PendingModifiedValues"])
server["PendingModifiedValues"] = {}
self.data[:tmp] ||= Time.now + Fog::Mock.delay * 2
if self.data[:tmp] <= Time.now
server["DBInstanceStatus"] = 'available'
self.data.delete(:tmp)
end
end
when "modifying"
# TODO there are some fields that only applied after rebooting
if server["PendingModifiedValues"]
server.merge!(server["PendingModifiedValues"])
server["PendingModifiedValues"] = {}
server["DBInstanceStatus"] = 'available'
end
when "available" # I'm not sure if amazon does this
if server["PendingModifiedValues"]
server["DBInstanceStatus"] = 'modifying'
end
end
end

View file

@ -28,8 +28,50 @@ module Fog
class Mock
def describe_db_security_group(opts={})
Fog::Mock.not_implemented
def describe_db_security_groups(opts={})
response = Excon::Response.new
sec_group_set = []
if opts.is_a?(String)
sec_group_name = opts
if sec_group = self.data[:security_groups][sec_group_name]
sec_group_set << sec_group
else
raise Fog::AWS::RDS::NotFound.new("Security Group #{sec_group_name} not found")
end
else
sec_group_set = self.data[:security_groups].values
end
sec_group_set.each do |sec_group|
sec_group["IPRanges"].each do |iprange|
if iprange["Status"] == "authorizing" || iprange["Status"] == "revoking"
iprange[:tmp] ||= Time.now + Fog::Mock.delay * 2
if iprange[:tmp] <= Time.now
iprange["Status"] = "authorized" if iprange["Status"] == "authorizing"
iprange.delete(:tmp)
sec_group["IPRanges"].delete(iprange) if iprange["Status"] == "revoking"
end
end
end
sec_group["EC2SecurityGroups"].each do |ec2_secg|
if ec2_secg["Status"] == "authorizing" || iprange["Status"] == "revoking"
ec2_secg[:tmp] ||= Time.now + Fog::Mock.delay * 2
if ec2_secg[:tmp] <= Time.now
ec2_secg["Status"] = "authorized" if ec2_secg["Status"] == "authorizing"
ec2_secg.delete(:tmp)
sec_group["EC2SecurityGroups"].delete(ec2_secg) if ec2_secg["Status"] == "revoking"
end
end
end
end
response.status = 200
response.body = {
"ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
"DescribeDBSecurityGroupsResult" => { "DBSecurityGroups" => sec_group_set }
}
response
end
end

View file

@ -46,22 +46,24 @@ module Fog
def modify_db_instance(db_name, apply_immediately, options={})
response = Excon::Response.new
if server = self.data[:servers][db_name]
if server["DBInstanceStatus"] != "available"
if self.data[:servers][db_name]
if self.data[:servers][db_name]["DBInstanceStatus"] != "available"
raise Fog::AWS::RDS::NotFound.new("DBInstance #{db_name} not available for modification")
else
# TODO verify the params options
# if apply_immediately is false, all the options go to pending_modified_values and then apply and clear after either
# a reboot or the maintainance window
if apply_immediately
modified_server = server.merge(options)
else
modified_server = server["PendingModifiedValues"].merge!(options) # it appends
end
#if apply_immediately
# modified_server = server.merge(options)
#else
# modified_server = server["PendingModifiedValues"].merge!(options) # it appends
#end
self.data[:servers][db_name]["PendingModifiedValues"].merge!(options) # it appends
#self.data[:servers][db_name]["DBInstanceStatus"] = "modifying"
response.status = 200
response.body = {
"ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
"ModifyDBInstanceResult" => { "DBInstance" => modified_server }
"ModifyDBInstanceResult" => { "DBInstance" => self.data[:servers][db_name] }
}
response

View file

@ -26,21 +26,21 @@ module Fog
def reboot_db_instance(instance_identifier)
response = Excon::Response.new
if server = self.data[:servers][instance_identifier]
if server["DBInstanceStatus"] != "available"
raise Fog::AWS::RDS::NotFound.new("DBInstance #{db_name} not available for rebooting")
if self.data[:servers][instance_identifier]
if self.data[:servers][instance_identifier]["DBInstanceStatus"] != "available"
raise Fog::AWS::RDS::NotFound.new("DBInstance #{instance_identifier} not available for rebooting")
else
server["DBInstanceStatus"] = 'rebooting'
self.data[:servers][instance_identifier]["DBInstanceStatus"] = 'rebooting'
response.status = 200
response.body = {
"ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
"RebootDBInstanceResult" => { "DBInstance" => server }
"RebootDBInstanceResult" => { "DBInstance" => self.data[:servers][instance_identifier] }
}
response
end
else
raise Fog::AWS::RDS::NotFound.new("DBInstance #{db_name} not found")
raise Fog::AWS::RDS::NotFound.new("DBInstance #{instance_identifier} not found")
end
end

View file

@ -33,7 +33,33 @@ module Fog
class Mock
def revoke_db_security_group_ingress(name, opts = {})
Fog::Mock.not_implemented
unless opts.key?('CIDRIP') || (opts.key?('EC2SecurityGroupName') && opts.key?('EC2SecurityGroupOwnerId'))
raise ArgumentError, 'Must specify CIDRIP, or both EC2SecurityGroupName and EC2SecurityGroupOwnerId'
end
response = Excon::Response.new
if sec_group = self.data[:security_groups][name]
if opts.key?('CIDRIP')
sec_group['IPRanges'].each do |iprange|
iprange['Status']= 'revoking' if iprange['CIDRIP'] == opts['CIDRIP']
end
else
sec_group['EC2SecurityGroups'].each do |ec2_secg|
ec2_secg['Status']= 'revoking' if ec2_secg['EC2SecurityGroupName'] == opts['EC2SecurityGroupName']
end
end
response.status = 200
response.body = {
"ResponseMetadata"=>{ "RequestId"=> Fog::AWS::Mock.request_id },
'RevokeDBSecurityGroupIngressResult' => {
'DBSecurityGroup' => sec_group
}
}
response
else
raise Fog::AWS::RDS::NotFound.new("DBSecurityGroupNotFound => #{name} not found")
end
end
end

View file

@ -19,7 +19,39 @@ class AWS
'DBParameterGroup' => DB_PARAMETER_GROUP
}
}
DB_SECURITY_GROUP = {
'DBSecurityGroupDescription' => String,
'DBSecurityGroupName' => String,
'EC2SecurityGroups' => [Fog::Nullable::Hash],
'IPRanges' => [Fog::Nullable::Hash],
'OwnerId' => Fog::Nullable::String
}
CREATE_DB_SECURITY_GROUP = BASIC.merge({
'CreateDBSecurityGroupResult' => {
'DBSecurityGroup' => DB_SECURITY_GROUP
}
})
AUTHORIZE_DB_SECURITY_GROUP = BASIC.merge({
'AuthorizeDBSecurityGroupIngressResult' => {
'DBSecurityGroup' => DB_SECURITY_GROUP
}
})
REVOKE_DB_SECURITY_GROUP = BASIC.merge({
'RevokeDBSecurityGroupIngressResult' => {
'DBSecurityGroup' => DB_SECURITY_GROUP
}
})
DESCRIBE_DB_SECURITY_GROUP = BASIC.merge({
'DescribeDBSecurityGroupsResult' => {
'DBSecurityGroups' => [DB_SECURITY_GROUP]
}
})
DESCRIBE_DB_PARAMETER_GROUP = {
'ResponseMetadata' => {'RequestId' => String},
'DescribeDBParameterGroupsResult' =>{
@ -33,6 +65,8 @@ class AWS
}
})
DB_PARAMETER = {
'ParameterValue' => Fog::Nullable::String,
'DataType' => String,
@ -41,8 +75,9 @@ class AWS
'IsModifiable' => Fog::Boolean,
'Description' => String,
'ParameterName' => String,
'ApplyType' => String,
'ApplyType' => String
}
DESCRIBE_DB_PARAMETERS = BASIC.merge({
'DescribeDBParametersResult' => {
'Marker' => Fog::Nullable::String,
@ -51,7 +86,6 @@ class AWS
})
SNAPSHOT={
'AllocatedStorage' => Integer,
'AvailabilityZone' => String,
@ -105,6 +139,7 @@ class AWS
'PreferredBackupWindow'=> String,
'PreferredMaintenanceWindow'=> String,
'ReadReplicaDBInstanceIdentifiers'=> [String],
'ReadReplicaDBInstanceIdentifiers'=> [Fog::Nullable::String],
'ReadReplicaSourceDBInstanceIdentifier'=> Fog::Nullable::String
}

View file

@ -10,7 +10,7 @@ Shindo.tests('AWS::RDS | instance requests', ['aws', 'rds']) do
@db_final_snapshot_id = "fog-final-snapshot"
tests('success') do
pending if Fog.mocking?
#
tests("#create_db_instance").formats(AWS::RDS::Formats::CREATE_DB_INSTANCE) do
result = Fog::AWS[:rds].create_db_instance(@db_instance_id, 'AllocatedStorage' => 5,
@ -59,6 +59,8 @@ Shindo.tests('AWS::RDS | instance requests', ['aws', 'rds']) do
server.reload.wait_for { state == 'rebooting' }
server.reload.wait_for { state == 'available'}
pending if Fog.mocking?
tests("#create_db_snapshot").formats(AWS::RDS::Formats::CREATE_DB_SNAPSHOT) do
body = Fog::AWS[:rds].create_db_snapshot(@db_instance_id, @db_snapshot_id).body
returns('creating'){ body['CreateDBSnapshotResult']['DBSnapshot']['Status']}

View file

@ -0,0 +1,101 @@
Shindo.tests('AWS::RDS | security group requests', ['aws', 'rds']) do
suffix = rand(65536).to_s(16)
@sec_group_name = "fog-sec-group-#{suffix}"
if Fog.mocking?
@owner_id = '123456780'
else
@owner_id = Fog::AWS[:rds].security_groups.get('default').owner_id
end
tests('success') do
tests("#create_db_security_group").formats(AWS::RDS::Formats::CREATE_DB_SECURITY_GROUP) do
body = Fog::AWS[:rds].create_db_security_group(@sec_group_name, 'Some description').body
returns( @sec_group_name) { body['CreateDBSecurityGroupResult']['DBSecurityGroup']['DBSecurityGroupName']}
returns( 'Some description') { body['CreateDBSecurityGroupResult']['DBSecurityGroup']['DBSecurityGroupDescription']}
returns( []) { body['CreateDBSecurityGroupResult']['DBSecurityGroup']['EC2SecurityGroups']}
returns( []) { body['CreateDBSecurityGroupResult']['DBSecurityGroup']['IPRanges']}
body
end
tests("#describe_db_security_groups").formats(AWS::RDS::Formats::DESCRIBE_DB_SECURITY_GROUP) do
Fog::AWS[:rds].describe_db_security_groups.body
end
tests("#authorize_db_security_group_ingress CIDR").formats(AWS::RDS::Formats::AUTHORIZE_DB_SECURITY_GROUP) do
@cidr = '0.0.0.0/0'
body = Fog::AWS[:rds].authorize_db_security_group_ingress(@sec_group_name,{'CIDRIP'=>@cidr}).body
returns("authorizing") { body['AuthorizeDBSecurityGroupIngressResult']['DBSecurityGroup']['IPRanges'].detect{|h| h['CIDRIP'] == @cidr}['Status']}
body
end
sec_group = Fog::AWS[:rds].security_groups.get(@sec_group_name)
sec_group.wait_for {ready?}
tests("#authorize_db_security_group_ingress another CIDR").formats(AWS::RDS::Formats::AUTHORIZE_DB_SECURITY_GROUP) do
@cidr = "10.0.0.0/24"
body = Fog::AWS[:rds].authorize_db_security_group_ingress(@sec_group_name,{'CIDRIP'=>@cidr}).body
returns("authorizing") { body['AuthorizeDBSecurityGroupIngressResult']['DBSecurityGroup']['IPRanges'].detect{|h| h['CIDRIP'] == @cidr}['Status']}
body
end
sec_group = Fog::AWS[:rds].security_groups.get(@sec_group_name)
sec_group.wait_for {ready?}
tests("#count CIDRIP").formats(AWS::RDS::Formats::DESCRIBE_DB_SECURITY_GROUP) do
body = Fog::AWS[:rds].describe_db_security_groups(@sec_group_name).body
returns(2) { body['DescribeDBSecurityGroupsResult']['DBSecurityGroups'][0]['IPRanges'].size }
body
end
tests("#revoke_db_security_group_ingress CIDR").formats(AWS::RDS::Formats::REVOKE_DB_SECURITY_GROUP) do
@cidr = '0.0.0.0/0'
body = Fog::AWS[:rds].revoke_db_security_group_ingress(@sec_group_name,{'CIDRIP'=> @cidr}).body
returns("revoking") { body['RevokeDBSecurityGroupIngressResult']['DBSecurityGroup']['IPRanges'].detect{|h| h['CIDRIP'] == @cidr}['Status']}
body
end
tests("#authorize_db_security_group_ingress EC2").formats(AWS::RDS::Formats::AUTHORIZE_DB_SECURITY_GROUP) do
@ec2_sec_group = 'default'
body = Fog::AWS[:rds].authorize_db_security_group_ingress(@sec_group_name,{'EC2SecurityGroupName' => @ec2_sec_group, 'EC2SecurityGroupOwnerId' => @owner_id}).body
returns("authorizing") { body['AuthorizeDBSecurityGroupIngressResult']['DBSecurityGroup']['EC2SecurityGroups'].detect{|h| h['EC2SecurityGroupName'] == @ec2_sec_group}['Status']}
returns(@owner_id) { body['AuthorizeDBSecurityGroupIngressResult']['DBSecurityGroup']['EC2SecurityGroups'].detect{|h| h['EC2SecurityGroupName'] == @ec2_sec_group}['EC2SecurityGroupOwnerId']}
body
end
tests("duplicate #authorize_db_security_group_ingress EC2").raises(Fog::AWS::RDS::AuthorizationAlreadyExists) do
@ec2_sec_group = 'default'
Fog::AWS[:rds].authorize_db_security_group_ingress(@sec_group_name,{'EC2SecurityGroupName' => @ec2_sec_group, 'EC2SecurityGroupOwnerId' => @owner_id})
end
sec_group = Fog::AWS[:rds].security_groups.get(@sec_group_name)
sec_group.wait_for {ready?}
tests("#revoke_db_security_group_ingress EC2").formats(AWS::RDS::Formats::REVOKE_DB_SECURITY_GROUP) do
@ec2_sec_group = 'default'
body = Fog::AWS[:rds].revoke_db_security_group_ingress(@sec_group_name,{'EC2SecurityGroupName' => @ec2_sec_group, 'EC2SecurityGroupOwnerId' => @owner_id}).body
returns("revoking") { body['RevokeDBSecurityGroupIngressResult']['DBSecurityGroup']['EC2SecurityGroups'].detect{|h| h['EC2SecurityGroupName'] == @ec2_sec_group}['Status']}
body
end
#TODO, authorize ec2 security groups
tests("#delete_db_security_group").formats(AWS::RDS::Formats::BASIC) do
body = Fog::AWS[:rds].delete_db_security_group(@sec_group_name).body
raises(Fog::AWS::RDS::NotFound) {Fog::AWS[:rds].describe_db_security_groups(@sec_group_name)}
body
end
end
end