admin_initiate_auth
does not work because it loads moto
inside the site-packages instead of in the source. To fix that, I added this in server.py after from __future__ import unicode_literals
and before moto
is importedimport os, sys
additional_path = os.path.join(os.path.dirname(__file__), "..")
print('additional_path %s' % os.path.abspath(additional_path))
sys.path.append(additional_path)
import moto
takes ~3s (on my laptop) and it looks like all backends are being loaded at that point -- correct me if I'm wrong. Would you be interested in a PR that loads the backends only when they're being used?
describe_execution()
to return. For example, if I call the method two times, I want them both to return with a status of RUNNING but the third and fourth calls return SUCCEEDED. Is there a way to do this with moto/mock?
Hi, I have a lambda function in a folder and it just put an item in DynamoDB:
import boto3
from botocore.exceptions import ClientError
REQUESTS_TABLE = os.getenv('REQUESTS_TABLE')
def handler(request: dict, _context) -> dict:
item = request['body']
response = save_data(item)
def save_data(item: dict, table) -> bool:
row = {}
row.update({'email': item['email']})
row.update({'product_id': item['product_id']})
print(f'item: {item}')
try:
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table(REQUESTS_TABLE)
table.put_item(TableName=REQUESTS_TABLE, Item=row)
except ClientError as err:
return False
return True
But when I try to test it, this code is into other file and other folder:
import resources.requests.sfn_src.save_data as save_data
import unittest
import os
from moto import mock_dynamodb2
class SavedataTestMethods(unittest.TestCase):
def setUp(self):
print(f'setUp')
@mock_dynamodb2
def test_handler_whithout_error(self):
expected_event = {"statusCode": 200, "body": "ok"}
input_event = {
'code': 200,
'body': {
'email': 'test@mail.com',
'product_id': '1',
},
'message': 'SUCCEEDED'
}
output_event = save_data.handler(input_event, {"table": self.table})
self.assertEqual(expected_event, output_event)
pass
if __name__ == "__main__":
unittest.main()
I got this error:
An error occurred (ResourceNotFoundException) when calling the PutItem operation: Requested resource not found
But if in save_data function I printed "table" variable it shows the table that I need. So I don't know why it doesn't find the resource.
I figured out, in the same test function I must create the DynamoDB Table :
import resources.requests.sfn_src.save_data as save_data
import unittest
import os
from moto import mock_dynamodb2
import boto3
REQUESTS_TABLE = os.getenv('REQUESTS_TABLE')
class SavedataTestMethods(unittest.TestCase):
@mock_dynamodb2
def test_handler_whithout_error(self):
dynamodb = boto3.resource("dynamodb")
# Create the DynamoDB table.
table = dynamodb.create_table(
TableName=REQUESTS_TABLE,
KeySchema=[
{
'AttributeName': 'id',
'KeyType': 'HASH'
}
],
AttributeDefinitions=[
{
'AttributeName': 'id',
'AttributeType': 'S'
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
table = dynamodb.Table(REQUESTS_TABLE)
expected_event = {"statusCode": 200, "body": "ok"}
input_event = {
'code': 200,
'body': {
'email': 'test@mail.com',
'product_id': '1',
'url': 'https://url',
'first_request': True
},
'message': 'SUCCEEDED'
}
output_event = save_data.handler(input_event, {})
self.assertEqual(expected_event, output_event)
SOLVED :+1:
so i decided this SPECIFIC test was not actually useful (scrapped it) so ignore the uselessness of the test. i guess my confusion is regarding fixtures in general. when i call client.attach_volume.assert_called_with()
against my moto client fixture i get error: AttributeError: 'function' object has no attribute 'assert_called_with'
conftest.py
@pytest.fixture(scope='function')
def client(aws_credentials):
with mock_ec2():
yield boto3.client('ec2', region_name=REGION)
lambda_function.py
def attach_ebs_volume(client, instance_id, region):
while utils.get_instance_state(client, instance_id) != "running":
print("waiting for instance to start")
time.sleep(5)
#TODO: timeout
for tag in utils.get_instance_tags(client, instance_id):
if tag['Key'] == "volume":
vol_tag = tag['Value']
volume_id = utils.get_ebs_by_name_tag(client, vol_tag)
client.attach_volume(
Device="/dev/xvdz",
InstanceId=instance_id,
VolumeId=volume_id
)
output from pytest:
@mock.patch('utils.get_instance_state')
@mock.patch('utils.get_instance_tags')
def test_attach_ebs_volume(mock_get_instance_tags, mock_get_instance_state, client):
volume_id = client.create_volume(Size=7, AvailabilityZone=REGION, TagSpecifications=VOLUME_TAGS)['VolumeId']
instance_id = client.run_instances(ImageId='ami-123', MinCount=1, MaxCount=1, TagSpecifications=INSTANCE_TAGS)['Instances'][0]['InstanceId']
mock_get_instance_state.return_value = 'running'
mock_get_instance_tags.return_value = RETURN_TAGS
r = lambda_function.attach_ebs_volume(client,
instance_id=instance_id,
region=REGION
)
> client.attach_volume.assert_called_with(Device="/dev/xvdz",
InstanceId=instance_id,
VolumeId=volume_id)
E AttributeError: 'function' object has no attribute 'assert_called_with'
i was trying to test that client.attach_volume
gets the right values. again, this is not actually that useful of a test but the functionality probably will in future tests
orgs.create_account()
. Are any of the following solutions feasible:orgs.create_account()
, then overwrite the ID of the account generated to match specorgs.create_account()
in a way that forces the generated ID to be set to the spec IDhey y'all, I need to mock an Organization that contains an account with a specific AWS ID, not the randomized value moto creates with
orgs.create_account()
. Are any of the following solutions feasible:
- call
orgs.create_account()
, then overwrite the ID of the account generated to match spec- call
orgs.create_account()
in a way that forces the generated ID to be set to the spec ID- mock an account join handshake procedure
Thanks!
I think option 1 should work. I don't think option 2 is possible. No idea about option 3
hello! This is my first time here :wave:
Has anyone else noticed that test_streaming_upload_from_file_to_presigned_url
is generating an error when it was fine yesterday? I can't find any existing open issues related to it, so I have raised an issue (spulec/moto#3080) and fixed it in a draft PR.
Advice would be appreciated :pray:
Hello. First time caller here.
I'm using moto to mock out SES and I want to verify that I've made a call to "send_raw_email". Poking around the code I've noticed that I can get the send messages from the "ses_backends". This solves my problem, but I'm just wondering if this is considered "best practice". How did others do this?
moto
doesn't support the more recent versions of boto
?