def test_unknown_request_arguments(lobotomized: lobotomy.Lobotomy): """Should fail due to presence of unknown "Foo" argument.""" lobotomized.add_call("s3", "list_objects", {}) session = boto3.Session() client = session.client("s3") with pytest.raises(lobotomy.RequestValidationError): client.list_objects(Bucket="foo", Foo="bar")
def test_missing_request_arguments(lobotomized: lobotomy.Lobotomy): """Should fail due to missing "Bucket" request argument.""" lobotomized.add_call("s3", "list_objects", {}) session = boto3.Session() client = session.client("s3") with pytest.raises(lobotomy.RequestValidationError): client.list_objects()
def test_bad_casting(lobotomized: lobotomy.Lobotomy): """Should fail to cast dictionary as string.""" lobotomized.add_call("s3", "list_objects", {"Contents": ["should-be-dict"]}) session = boto3.Session() client = session.client("s3") with pytest.raises(lobotomy.DataTypeError): client.list_objects(Bucket="foo")
def test_timestamp_formats(lobotomized: lobotomy.Lobotomy, value: typing.Any): """Should cast timestamp values correctly.""" lobotomized.add_call("iam", "get_role", response={"Role": {"CreateDate": value}}) response = boto3.Session().client("iam").get_role(RoleName="foo") observed: datetime.datetime = response["Role"]["CreateDate"] assert isinstance(observed, datetime.datetime) assert observed.date() == datetime.date(2020, 1, 1) assert observed.hour in (0, 12) assert observed.minute in (0, 23) assert observed.second in (0, 34)
def test_s3_select(lobotomized: lobotomy.Lobotomy): """Should correctly return S3 Select results.""" lobotomized.add_call( "s3", "select_object_content", { "Payload": [ { "Records": { "Payload": "a" } }, { "Records": { "Payload": "b" } }, { "Records": { "Payload": "c" } }, ] }, ) response = (lobotomized().client("s3").select_object_content( Bucket="foo", Key="bar/baz.file", InputSerialization={"Parquet": {}}, Expression="SELECT * FROM S3Object LIMIT 100", ExpressionType="SQL", OutputSerialization={"JSON": { "RecordDelimiter": "\n" }}, )) observed = list(response["Payload"]) assert observed == [ { "Records": { "Payload": b"a" } }, { "Records": { "Payload": b"b" } }, { "Records": { "Payload": b"c" } }, ]
def test_sqs_delete_message(lobotomized: lobotomy.Lobotomy): """ Should handle sqs.delete_message without error despite edge case configuration in botocore service method definitions where the return structure has no members. """ lobotomized.add_call("sqs", "delete_message") lobotomized().client("sqs").delete_message( QueueUrl="https://sqs.us-west-2.amazonaws.com/123/my-queue", ReceiptHandle="fake-receipt-handle", ) assert lobotomized.get_service_call("sqs", "delete_message") is not None
def test_pagination(lobotomized: lobotomy.Lobotomy): """Should paginate correctly by using only the first response.""" lobotomized.add_call("s3", "list_objects_v2", {"Contents": [{"Key": "a"}]}) lobotomized.add_call("s3", "list_objects_v2", {"Contents": [{"Key": "b"}]}) session = lobotomized() client = session.client("s3") paginator = client.get_paginator("list_objects_v2") pages = [] for page in paginator.paginate(Bucket="my-bucket"): pages.append(page) assert pages == [{"Contents": [{"Key": "a"}]}]
def test_eks_describe_cluster(lobotomized: lobotomy.Lobotomy): """Should return result without error.""" lobotomized.add_call( "eks", "describe_cluster", response={ "name": "cluster-name", "arn": "arn:aws:eks:us-west-2:123456:cluster/cluster-name", "certificateAuthority": { "data": "fakecertificatedata=" }, }, ) response = lobotomized().client("eks").describe_cluster("cluster-name") assert response[ "arn"] == "arn:aws:eks:us-west-2:123456:cluster/cluster-name"
def test_client_errors(lobotomized: lobotomy.Lobotomy): """Should raise the specified error.""" lobotomized.add_call( service_name="s3", method_name="list_objects", response={"Error": { "Code": "NoSuchBucket", "Message": "Hello..." }}, ) session = boto3.Session() client = session.client("s3") with pytest.raises(client.exceptions.NoSuchBucket): client.list_objects(Bucket="foo") with pytest.raises(lobotomy.ClientError) as exception_info: client.list_objects(Bucket="foo") assert exception_info.value.response["Error"]["Code"] == "NoSuchBucket"
def _process_action( lobotomized: lobotomy.Lobotomy, action: typing.Dict[str, typing.Any], ) -> None: """Mutates the lobotomized data according the specified action.""" kind = action.get("kind") data = lobotomized.data clients = data.get("clients", {}) if kind == "remove_service_calls": service = action["service"] method = action["method"] del clients[service][method] elif kind == "remove_service": service = action["service"] del clients[service] elif kind == "add_service_call": lobotomized.add_call( service_name=action["service"], method_name=action["method"], response=action.get("response"), )
def test_lambda_invoke(lobotomized: lobotomy.Lobotomy): """Should return a StreamingBody for the payload.""" lobotomized.add_call("lambda", "invoke") response = lobotomized().client("lambda").invoke(FunctionName="foo") assert b"..." == response["Payload"].read()
def test_creation_empty_added(lob: lobotomy.Lobotomy): """Should patch an empty lobotomy and then work after setting data manually.""" lob.add_call("sts", "get_caller_identity", {"Account": "123"}) session = lob() client = session.client("sts") assert client.get_caller_identity()["Account"] == "123"