15
15
from aiobotocore import utils
16
16
from aiobotocore .utils import AioInstanceMetadataFetcher
17
17
from botocore .utils import MetadataRetrievalError , BadIMDSRequestError
18
+ import botocore .awsrequest
18
19
import yarl
19
-
20
+ from tests .test_response import AsyncBytesIO
21
+ from aiobotocore .awsrequest import AioAWSResponse
20
22
21
23
# From class TestContainerMetadataFetcher
22
24
def fake_aiohttp_session (responses : Union [List [Tuple [Union [str , object ], int ]],
@@ -130,7 +132,7 @@ async def test_containermetadatafetcher_retrieve_url_not_json():
130
132
131
133
class TestInstanceMetadataFetcher (unittest .IsolatedAsyncioTestCase ):
132
134
async def asyncSetUp (self ):
133
- urllib3_session_send = 'aiohttp.ClientSession._request '
135
+ urllib3_session_send = 'aiobotocore.httpsession.AIOHTTPSession.send '
134
136
self ._urllib3_patch = mock .patch (urllib3_session_send )
135
137
self ._send = self ._urllib3_patch .start ()
136
138
self ._imds_responses = []
@@ -155,19 +157,14 @@ async def asyncTearDown(self):
155
157
156
158
def add_imds_response (self , body , status_code = 200 ):
157
159
loop = asyncio .get_running_loop ()
158
- url = yarl .URL ('http://169.254.169.254/' )
159
- method = 'get'
160
- response = ClientResponse (method , url ,
161
- request_info = RequestInfo (url , method , {}),
162
- writer = mock .AsyncMock (),
163
- continue100 = None ,
164
- timer = TimerNoop (),
165
- traces = [],
166
- loop = loop ,
167
- session = mock .AsyncMock ())
168
- response .status = status_code
169
- response ._body = body
170
- response ._headers = {}
160
+
161
+ response = AioAWSResponse (
162
+ url = 'http://169.254.169.254/' ,
163
+ status_code = status_code ,
164
+ headers = {},
165
+ raw = AsyncBytesIO (body )
166
+ )
167
+
171
168
self ._imds_responses .append (response )
172
169
173
170
def add_get_role_name_imds_response (self , role_name = None ):
@@ -380,7 +377,7 @@ async def test_token_is_included(self):
380
377
# Check that subsequent calls after getting the token include the token.
381
378
self .assertEqual (self ._send .call_count , 3 )
382
379
for call in self ._send .call_args_list [1 :]:
383
- self .assertEqual (call . kwargs [ 'headers' ] ['x-aws-ec2-metadata-token' ],
380
+ self .assertEqual (call [ 0 ][ 0 ]. headers ['x-aws-ec2-metadata-token' ],
384
381
'token' )
385
382
self .assertEqual (result , self ._expected_creds )
386
383
@@ -396,7 +393,7 @@ async def test_metadata_token_not_supported_404(self):
396
393
user_agent = user_agent ).retrieve_iam_role_credentials ()
397
394
398
395
for call in self ._send .call_args_list [1 :]:
399
- self .assertNotIn ('x-aws-ec2-metadata-token' , call . kwargs [ 'headers' ] )
396
+ self .assertNotIn ('x-aws-ec2-metadata-token' , call [ 0 ][ 0 ]. headers )
400
397
self .assertEqual (result , self ._expected_creds )
401
398
402
399
@pytest .mark .moto
@@ -411,7 +408,7 @@ async def test_metadata_token_not_supported_403(self):
411
408
user_agent = user_agent ).retrieve_iam_role_credentials ()
412
409
413
410
for call in self ._send .call_args_list [1 :]:
414
- self .assertNotIn ('x-aws-ec2-metadata-token' , call . kwargs [ 'headers' ] )
411
+ self .assertNotIn ('x-aws-ec2-metadata-token' , call [ 0 ][ 0 ]. headers )
415
412
self .assertEqual (result , self ._expected_creds )
416
413
417
414
@pytest .mark .moto
@@ -426,7 +423,7 @@ async def test_metadata_token_not_supported_405(self):
426
423
user_agent = user_agent ).retrieve_iam_role_credentials ()
427
424
428
425
for call in self ._send .call_args_list [1 :]:
429
- self .assertNotIn ('x-aws-ec2-metadata-token' , call . kwargs [ 'headers' ] )
426
+ self .assertNotIn ('x-aws-ec2-metadata-token' , call [ 0 ][ 0 ]. headers )
430
427
self .assertEqual (result , self ._expected_creds )
431
428
432
429
@pytest .mark .moto
@@ -441,7 +438,7 @@ async def test_metadata_token_not_supported_timeout(self):
441
438
user_agent = user_agent ).retrieve_iam_role_credentials ()
442
439
443
440
for call in self ._send .call_args_list [1 :]:
444
- self .assertNotIn ('x-aws-ec2-metadata-token' , call . kwargs [ 'headers' ] )
441
+ self .assertNotIn ('x-aws-ec2-metadata-token' , call [ 0 ][ 0 ]. headers )
445
442
self .assertEqual (result , self ._expected_creds )
446
443
447
444
@pytest .mark .moto
@@ -456,7 +453,7 @@ async def test_token_not_supported_exhaust_retries(self):
456
453
user_agent = user_agent ).retrieve_iam_role_credentials ()
457
454
458
455
for call in self ._send .call_args_list [1 :]:
459
- self .assertNotIn ('x-aws-ec2-metadata-token' , call . kwargs [ 'headers' ] )
456
+ self .assertNotIn ('x-aws-ec2-metadata-token' , call [ 0 ][ 0 ]. headers )
460
457
self .assertEqual (result , self ._expected_creds )
461
458
462
459
@pytest .mark .moto
0 commit comments