/
shared_spec_helper.rb
71 lines (58 loc) · 2.38 KB
/
shared_spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
$LOAD_PATH.unshift(File.expand_path('../../../aws-sigv2/lib', __FILE__))
$LOAD_PATH.unshift(File.expand_path('../../../aws-sigv4/lib', __FILE__))
$LOAD_PATH.unshift(File.expand_path('../../../aws-eventstream/lib', __FILE__))
$LOAD_PATH.unshift(File.expand_path('../../../aws-partitions/lib', __FILE__))
require 'webmock/rspec'
require_relative './sigv4_helper'
# Prevent the SDK unit tests from loading actual credentials while under test.
# By default the SDK attempts to load credentials from:
#
# * ENV, e.g. ENV['AWS_ACCESS_KEY_ID']
# * Shared credentials file, e.g. ~/.aws/credentials
# * EC2 instance metadata server running at 169.254.169.254
#
RSpec.configure do |config|
# Module to help check service signing
config.include Sigv4Helper
config.before(:each) do
# Clear the current ENV to avoid loading credentials.
# This was previously mocked with stub_const but was provided a hash.
ENV.clear
# disable loading credentials from shared file
allow(Dir).to receive(:home).and_raise(ArgumentError)
# disable instance profile credentials
token_path = '/latest/api/token'
path = '/latest/meta-data/iam/security-credentials/'
stub_request(:get, "http://169.254.169.254#{path}").to_raise(SocketError)
stub_request(:put, "http://169.254.169.254#{token_path}").to_raise(SocketError)
allow_any_instance_of(Aws::InstanceProfileCredentials).to receive(:warn)
Aws.shared_config.fresh
end
# Thread.report_on_exception was set to default true in Ruby 2.5
# When testing code that intentionally has threads that raise exceptions
# disable printing of those exceptions.
config.around(:each, thread_report_on_exception: false) do |example|
if Thread.respond_to?(:report_on_exception)
current_value = Thread.report_on_exception
Thread.report_on_exception = false
end
example.call
Thread.report_on_exception = current_value if current_value
end
if defined?(JRUBY_VERSION)
config.around(:each, :jruby_flaky) do |example|
attempt = 0
retries = 3
loop do
attempt += 1
example.run
break if !example.exception || attempt >= retries
# clear the exception, ensuring it can run from a clean state
example.example.instance_variable_set(:@exception, nil)
redo
end
end
end
end