Class: Fog::AWS::SQS::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/aws/sqs.rb,
lib/fog/aws/requests/sqs/list_queues.rb,
lib/fog/aws/requests/sqs/create_queue.rb,
lib/fog/aws/requests/sqs/delete_queue.rb,
lib/fog/aws/requests/sqs/send_message.rb,
lib/fog/aws/requests/sqs/delete_message.rb,
lib/fog/aws/requests/sqs/receive_message.rb,
lib/fog/aws/requests/sqs/get_queue_attributes.rb,
lib/fog/aws/requests/sqs/set_queue_attributes.rb,
lib/fog/aws/requests/sqs/change_message_visibility.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



38
39
40
41
42
43
44
45
46
# File 'lib/fog/aws/sqs.rb', line 38

def initialize(options={})
  @aws_access_key_id = options[:aws_access_key_id]

  @region = options[:region] || 'us-east-1'

  unless ['ap-northeast-1', 'ap-southeast-1', 'eu-west-1', 'us-east-1', 'us-west-1', 'us-west-2'].include?(@region)
    raise ArgumentError, "Unknown region: #{@region.inspect}"
  end
end

Class Method Details

.dataObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fog/aws/sqs.rb', line 22

def self.data
  @data ||= Hash.new do |hash, region|
    owner_id = Fog::AWS::Mock.owner_id
    hash[region] = Hash.new do |region_hash, key|
      region_hash[key] = {
        :owner_id => Fog::AWS::Mock.owner_id,
        :queues   => {}
      }
    end
  end
end

.resetObject



34
35
36
# File 'lib/fog/aws/sqs.rb', line 34

def self.reset
  @data = nil
end

Instance Method Details

#change_message_visibility(queue_url, receipt_handle, visibility_timeout) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fog/aws/requests/sqs/change_message_visibility.rb', line 33

def change_message_visibility(queue_url, receipt_handle, visibility_timeout)
  Excon::Response.new.tap do |response|
    if (queue = data[:queues][queue_url])
      message_id, _ = queue[:receipt_handles].find { |message_id, receipts|
        receipts.has_key?(receipt_handle)
      }
      
      if message_id
        queue[:messages][message_id]['Attributes']['VisibilityTimeout'] = visibility_timeout
        response.body = {
          'ResponseMetadata' => {
            'RequestId' => Fog::AWS::Mock.request_id
          }
        }
        response.status = 200
      else
        response.status = 404
        raise(Excon::Errors.status_error({:expects => 200}, response))
      end
    else
      response.status = 404
      raise(Excon::Errors.status_error({:expects => 200}, response))
    end
  end
end

#create_queue(name, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fog/aws/requests/sqs/create_queue.rb', line 30

def create_queue(name, options = {})
  Excon::Response.new.tap do |response|
    response.status = 200
    
    now = Time.now
    queue_url = "https://queue.amazonaws.com/#{data[:owner_id]}/#{name}"
    queue = {
      'QueueName'      => name,
      'Attributes'     => {
        'VisibilityTimeout'                     => 30,
        'ApproximateNumberOfMessages'           => 0,
        'ApproximateNumberOfMessagesNotVisible' => 0,
        'CreatedTimestamp'                      => now,
        'LastModifiedTimestamp'                 => now,
        'QueueArn'                              => Fog::AWS::Mock.arn('sqs', 'us-east-1', data[:owner_id], name),
        'MaximumMessageSize'                    => 8192,
        'MessageRetentionPeriod'                => 345600
      },
      :messages        => {},
      :receipt_handles => {}
    }
    data[:queues][queue_url] = queue unless data[:queues][queue_url]
    
    response.body = {
      'ResponseMetadata' => {
        'RequestId' => Fog::AWS::Mock.request_id
      },
      'QueueUrl' => queue_url
    }
  end
  
end

#dataObject



48
49
50
# File 'lib/fog/aws/sqs.rb', line 48

def data
  self.class.data[@region][@aws_access_key_id]
end

#delete_message(queue_url, receipt_handle) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fog/aws/requests/sqs/delete_message.rb', line 31

def delete_message(queue_url, receipt_handle)
  Excon::Response.new.tap do |response|
    if (queue = data[:queues][queue_url])
      message_id, _ = queue[:receipt_handles].find { |msg_id, receipts|
        receipts.has_key?(receipt_handle)
      }

      if message_id
        queue[:receipt_handles].delete(message_id)
        queue[:messages].delete(message_id)
        queue['Attributes']['LastModifiedTimestamp'] = Time.now
      end

      response.body = {
        'ResponseMetadata' => {
          'RequestId' => Fog::AWS::Mock.request_id
        }
      }
      response.status = 200
    else
      response.status = 404
      raise(Excon::Errors.status_error({:expects => 200}, response))
    end
  end
end

#delete_queue(queue_url) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fog/aws/requests/sqs/delete_queue.rb', line 29

def delete_queue(queue_url)
  Excon::Response.new.tap do |response|
    if (queue = data[:queues][queue_url])
      response.status = 200
      
      data[:queues].delete(queue_url)
      
      response.body = {
        'ResponseMetadata' => {
          'RequestId' => Fog::AWS::Mock.request_id
        }
      }
    else
      response.status = 404
      raise(Excon::Errors.status_error({:expects => 200}, response))
    end
  end
end

#get_queue_attributes(queue_url, attribute_name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/aws/requests/sqs/get_queue_attributes.rb', line 31

def get_queue_attributes(queue_url, attribute_name)
  Excon::Response.new.tap do |response|
    if (queue = data[:queues][queue_url])
      response.status = 200
      
      response.body = {
        'ResponseMetadata' => {
          'RequestId' => Fog::AWS::Mock.request_id
        },
        'Attributes' => queue['Attributes']
      }
    else
      response.status = 404
      raise(Excon::Errors.status_error({:expects => 200}, response))
    end
  end
end

#list_queues(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fog/aws/requests/sqs/list_queues.rb', line 27

def list_queues(options = {})
  Excon::Response.new.tap do |response|
    response.status = 200
    
    response.body = {
      'ResponseMetadata' => {
        'RequestId' => Fog::AWS::Mock.request_id
      },
      'QueueUrls' => data[:queues].keys
    }
  end
end

#receive_message(queue_url, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fog/aws/requests/sqs/receive_message.rb', line 34

def receive_message(queue_url, options = {})
  Excon::Response.new.tap do |response|
    if (queue = data[:queues][queue_url])
      max_number_of_messages = options['MaxNumberOfMessages'] || 1
      now = Time.now
      
      messages = []

      queue[:messages].values.each do |m|
        message_id = m['MessageId']

        invisible = if (received_handles = queue[:receipt_handles][message_id])
          visibility_timeout = m['Attributes']['VisibilityTimeout'] || queue['Attributes']['VisibilityTimeout']
          received_handles.any? { |handle, time| now < time + visibility_timeout }
        else
          false
        end

        unless invisible
          receipt_handle = Fog::Mock.random_base64(300)

          queue[:receipt_handles][message_id] ||= {}
          queue[:receipt_handles][message_id][receipt_handle] = now

          m['Attributes'].tap do |attrs|
            attrs['ApproximateFirstReceiveTimestamp'] ||= now
            attrs['ApproximateReceiveCount'] = (attrs['ApproximateReceiveCount'] || 0) + 1
          end

          messages << m.merge({
            'ReceiptHandle' => receipt_handle
          })
          break if messages.size >= max_number_of_messages
        end
      end

      response.body = {
        'ResponseMetadata' => {
          'RequestId' => Fog::AWS::Mock.request_id
        },
        'Message' => messages
      }
      response.status = 200
    else
      response.status = 404
      raise(Excon::Errors.status_error({:expects => 200}, response))
    end
  end
end

#reset_dataObject



52
53
54
# File 'lib/fog/aws/sqs.rb', line 52

def reset_data
  self.class.data[@region].delete(@aws_access_key_id)
end

#send_message(queue_url, message) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fog/aws/requests/sqs/send_message.rb', line 31

def send_message(queue_url, message)
  Excon::Response.new.tap do |response|
    if (queue = data[:queues][queue_url])
      response.status = 200
      
      now        = Time.now
      message_id = Fog::AWS::Mock.sqs_message_id
      md5        = Digest::MD5.hexdigest(message)
      
      queue[:messages][message_id] = {
        'MessageId'  => message_id,
        'Body'       => message,
        'MD5OfBody'  => md5,
        'Attributes' => {
          'SenderId'      => Fog::AWS::Mock.sqs_message_id,
          'SentTimestamp' => now
        }
      }
      
      queue['Attributes']['LastModifiedTimestamp'] = now
      
      response.body = {
        'ResponseMetadata' => {
          'RequestId' => Fog::AWS::Mock.request_id
        },
        'MessageId'        => message_id,
        'MD5OfMessageBody' => md5
      }
    else
      response.status = 404
      raise(Excon::Errors.status_error({:expects => 200}, response))
    end
  end
end

#set_queue_attributes(queue_url, attribute_name, attribute_value) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/aws/requests/sqs/set_queue_attributes.rb', line 32

def set_queue_attributes(queue_url, attribute_name, attribute_value)
  Excon::Response.new.tap do |response|
    if (queue = data[:queues][queue_url])
      response.status = 200
      queue['Attributes'][attribute_name] = attribute_value
      response.body = {
        'ResponseMetadata' => {
          'RequestId' => Fog::AWS::Mock.request_id
        }
      }
    else
      response.status = 404
      raise(Excon::Errors.status_error({:expects => 200}, response))
    end
  end
end