mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Merge pull request #547 from dpiddy/aws-modify-image-attribute-mock
[aws|compute] Mock modify_image_attribute add/remove users.
This commit is contained in:
commit
661667eff8
4 changed files with 85 additions and 9 deletions
|
@ -103,12 +103,17 @@ module Fog
|
|||
|
||||
def self.data
|
||||
@data ||= Hash.new do |hash, region|
|
||||
owner_id = Fog::AWS::Mock.owner_id
|
||||
hash[region] = Hash.new do |region_hash, key|
|
||||
owner_id = Fog::AWS::Mock.owner_id
|
||||
region_hash[key] = {
|
||||
:deleted_at => {},
|
||||
:addresses => {},
|
||||
:images => {},
|
||||
:image_launch_permissions => Hash.new do |permissions_hash, image_key|
|
||||
permissions_hash[image_key] = {
|
||||
:users => []
|
||||
}
|
||||
end,
|
||||
:instances => {},
|
||||
:reserved_instances => {},
|
||||
:key_pairs => {},
|
||||
|
@ -181,22 +186,22 @@ module Fog
|
|||
value = filters.delete('tag-key')
|
||||
resources = resources.select{|r| r['tagSet'].has_key?(value)}
|
||||
end
|
||||
|
||||
|
||||
# tag-value: match resources tagged with this value (any key)
|
||||
if filters.has_key?('tag-value')
|
||||
value = filters.delete('tag-value')
|
||||
resources = resources.select{|r| r['tagSet'].values.include?(value)}
|
||||
end
|
||||
|
||||
|
||||
# tag:key: match resources taged with a key-value pair. Value may be an array, which is OR'd.
|
||||
tag_filters = {}
|
||||
filters.keys.each do |key|
|
||||
filters.keys.each do |key|
|
||||
tag_filters[key.gsub('tag:', '')] = filters.delete(key) if /^tag:/ =~ key
|
||||
end
|
||||
for tag_key, tag_value in tag_filters
|
||||
resources = resources.select{|r| tag_value.include?(r['tagSet'][tag_key])}
|
||||
end
|
||||
|
||||
|
||||
resources
|
||||
end
|
||||
end
|
||||
|
@ -206,7 +211,7 @@ module Fog
|
|||
# Initialize connection to EC2
|
||||
#
|
||||
# ==== Notes
|
||||
# options parameter must include values for :aws_access_key_id and
|
||||
# options parameter must include values for :aws_access_key_id and
|
||||
# :aws_secret_access_key in order to create a connection
|
||||
#
|
||||
# ==== Examples
|
||||
|
@ -265,7 +270,7 @@ module Fog
|
|||
end
|
||||
|
||||
private
|
||||
|
||||
|
||||
def request(params)
|
||||
idempotent = params.delete(:idempotent)
|
||||
parser = params.delete(:parser)
|
||||
|
|
|
@ -100,6 +100,14 @@ module Fog
|
|||
|
||||
image_set = self.data[:images].values
|
||||
|
||||
self.class.data[@region].each do |aws_access_key_id, data|
|
||||
data[:image_launch_permissions].each do |image_id, list|
|
||||
if list[:users].include?(self.data[:owner_id])
|
||||
image_set << data[:images][image_id]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for filter_key, filter_value in filters
|
||||
if tag_key = filter_key.split('tag:')[1]
|
||||
image_set = image_set.reject{|image| ![*filter_value].include?(image['tagSet'][tag_key])}
|
||||
|
|
|
@ -37,6 +37,38 @@ module Fog
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
class Mock
|
||||
|
||||
def modify_image_attribute(image_id, attributes)
|
||||
raise ArgumentError.new("image_id is required") unless image_id
|
||||
|
||||
unless self.data[:images][image_id]
|
||||
raise Fog::Compute::AWS::NotFound.new("The AMI ID '#{image_id}' does not exist")
|
||||
end
|
||||
|
||||
(attributes['Add.UserId'] || []).each do |user_id|
|
||||
if image_launch_permissions = self.data[:image_launch_permissions][image_id]
|
||||
image_launch_permissions[:users].push(user_id)
|
||||
end
|
||||
end
|
||||
|
||||
(attributes['Remove.UserId'] || []).each do |user_id|
|
||||
if image_launch_permissions = self.data[:image_launch_permissions][image_id]
|
||||
image_launch_permissions[:users].delete(user_id)
|
||||
end
|
||||
end
|
||||
|
||||
response = Excon::Response.new
|
||||
response.status = 200
|
||||
response.body = {
|
||||
'return' => true,
|
||||
'requestId' => Fog::AWS::Mock.request_id
|
||||
}
|
||||
response
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -31,6 +31,11 @@ Shindo.tests('Fog::Compute[:aws] | image requests', ['aws']) do
|
|||
'requestId' => String
|
||||
}
|
||||
|
||||
@modify_image_attribute_format = {
|
||||
'return' => Fog::Boolean,
|
||||
'requestId' => String
|
||||
}
|
||||
|
||||
tests('success') do
|
||||
# the result for this is HUGE and relatively uninteresting...
|
||||
# tests("#describe_images").formats(@images_format) do
|
||||
|
@ -39,6 +44,8 @@ Shindo.tests('Fog::Compute[:aws] | image requests', ['aws']) do
|
|||
@image_id = 'ami-1aad5273'
|
||||
|
||||
if Fog.mocking?
|
||||
@other_account = Fog::Compute::AWS.new(:aws_access_key_id => 'other', :aws_secret_access_key => 'account')
|
||||
|
||||
tests("#register_image").formats(@register_image_format) do
|
||||
@image = Fog::Compute[:aws].register_image('image', 'image', '/dev/sda1').body
|
||||
end
|
||||
|
@ -53,6 +60,26 @@ Shindo.tests('Fog::Compute[:aws] | image requests', ['aws']) do
|
|||
tests("#describe_images('state' => 'available')").formats(@describe_images_format) do
|
||||
Fog::Compute[:aws].describe_images('state' => 'available').body
|
||||
end
|
||||
|
||||
tests("other_account#describe_images('image-id' => '#{@image_id}')").returns([]) do
|
||||
@other_account.describe_images('image-id' => @image_id).body['imagesSet']
|
||||
end
|
||||
|
||||
tests("#modify_image_attribute('#{@image_id}', 'Add.UserId' => ['#{@other_account.data[:owner_id]}'])").formats(@modify_image_attribute_format) do
|
||||
Fog::Compute[:aws].modify_image_attribute(@image_id, { 'Add.UserId' => [@other_account.data[:owner_id]] }).body
|
||||
end
|
||||
|
||||
tests("other_account#describe_images('image-id' => '#{@image_id}')").returns([@image_id]) do
|
||||
@other_account.describe_images('image-id' => @image_id).body['imagesSet'].map {|i| i['imageId'] }
|
||||
end
|
||||
|
||||
tests("#modify_image_attribute('#{@image_id}', 'Remove.UserId' => ['#{@other_account.data[:owner_id]}'])").formats(@modify_image_attribute_format) do
|
||||
Fog::Compute[:aws].modify_image_attribute(@image_id, { 'Remove.UserId' => [@other_account.data[:owner_id]] }).body
|
||||
end
|
||||
|
||||
tests("other_account#describe_images('image-id' => '#{@image_id}')").returns([]) do
|
||||
@other_account.describe_images('image-id' => @image_id).body['imagesSet']
|
||||
end
|
||||
end
|
||||
|
||||
tests("#describe_images('image-id' => '#{@image_id}')").formats(@describe_images_format) do
|
||||
|
@ -67,10 +94,14 @@ Shindo.tests('Fog::Compute[:aws] | image requests', ['aws']) do
|
|||
end
|
||||
|
||||
tests('failure') do
|
||||
pending if Fog.mocking?
|
||||
|
||||
tests("#modify_image_attribute(nil, { 'Add.Group' => ['all'] })").raises(ArgumentError) do
|
||||
Fog::Compute[:aws].modify_image_attribute(nil, { 'Add.Group' => ['all'] }).body
|
||||
end
|
||||
|
||||
tests("#modify_image_attribute('ami-00000000', { 'Add.UserId' => ['123456789012'] })").raises(Fog::Compute::AWS::NotFound) do
|
||||
pending unless Fog.mocking?
|
||||
|
||||
Fog::Compute[:aws].modify_image_attribute('ami-00000000', { 'Add.UserId' => ['123456789012'] }).body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue