Skip to main content

Python SDK for fakecloud — local AWS cloud emulator

Project description

fakecloud

Python SDK for fakecloud — a local AWS cloud emulator.

This package provides async and sync clients for the fakecloud introspection and simulation API (/_fakecloud/* endpoints), letting you inspect sent emails, published messages, Lambda invocations, and more from your tests.

Installation

pip install fakecloud

Quick start

Async

import asyncio
from fakecloud import FakeCloud

async def main():
    async with FakeCloud("http://localhost:4566") as fc:
        # Check server health
        health = await fc.health()
        print(health.status, health.version)

        # List sent SES emails
        emails = await fc.ses.get_emails()
        for email in emails.emails:
            print(f"{email.from_addr} -> {email.to}: {email.subject}")

        # List SNS messages
        messages = await fc.sns.get_messages()
        for msg in messages.messages:
            print(f"{msg.topic_arn}: {msg.message}")

        # Inspect Lambda invocations
        invocations = await fc.lambda_.get_invocations()
        for inv in invocations.invocations:
            print(f"{inv.function_arn}: {inv.payload}")

        # Reset all state between tests
        await fc.reset()

asyncio.run(main())

Sync

from fakecloud import FakeCloudSync

with FakeCloudSync("http://localhost:4566") as fc:
    health = fc.health()
    print(health.status)

    emails = fc.ses.get_emails()
    for email in emails.emails:
        print(email.subject)

API reference

FakeCloud / FakeCloudSync

Top-level client. Pass base_url (default http://localhost:4566).

Method Description
health() Server health check
reset() Reset all service state
reset_service(service) Reset a single service

Service sub-clients

Access via properties on the main client:

Property Service Methods
lambda_ Lambda get_invocations(), get_warm_containers(), evict_container(name)
ses SES get_emails(), simulate_inbound(req)
sns SNS get_messages(), get_pending_confirmations(), confirm_subscription(req)
sqs SQS get_messages(), tick_expiration(), force_dlq(queue_name)
events EventBridge get_history(), fire_rule(req)
s3 S3 get_notifications(), tick_lifecycle()
dynamodb DynamoDB tick_ttl()
secretsmanager SecretsManager tick_rotation()
cognito Cognito get_user_codes(pool_id, username), get_confirmation_codes(), confirm_user(req), get_tokens(), expire_tokens(req), get_auth_events()
rds RDS get_instances()
elasticache ElastiCache get_clusters(), get_replication_groups(), get_serverless_caches()
stepfunctions Step Functions get_executions()
apigatewayv2 API Gateway v2 get_requests()
bedrock Bedrock Runtime get_invocations(), set_model_response(model_id, text), set_response_rules(model_id, rules), clear_response_rules(model_id), queue_fault(rule), get_faults(), clear_faults()

Testing Bedrock-calling code end-to-end

from fakecloud import FakeCloudSync
from fakecloud.types import BedrockResponseRule, BedrockFaultRule

fc = FakeCloudSync()
model_id = "anthropic.claude-3-haiku-20240307-v1:0"


def test_classifier_branches_on_spam_vs_ham():
    fc.reset()
    fc.bedrock.set_response_rules(
        model_id,
        [
            BedrockResponseRule(prompt_contains="buy now", response='{"label":"spam"}'),
            BedrockResponseRule(prompt_contains=None, response='{"label":"ham"}'),
        ],
    )

    classify("hello friend")           # user code that calls Bedrock
    classify("buy now cheap pills")

    invocations = fc.bedrock.get_invocations().invocations
    assert len(invocations) == 2
    assert "ham" in invocations[0].output
    assert "spam" in invocations[1].output


def test_retries_on_throttling():
    fc.reset()
    fc.bedrock.queue_fault(
        BedrockFaultRule(
            error_type="ThrottlingException",
            message="Rate exceeded",
            http_status=429,
            count=1,  # only the first call faults; the retry succeeds
        )
    )

    classify("hello")

    invocations = fc.bedrock.get_invocations().invocations
    assert len(invocations) == 2
    assert "ThrottlingException" in (invocations[0].error or "")
    assert invocations[1].error is None

Error handling

All methods raise FakeCloudError on non-2xx responses:

from fakecloud.client import FakeCloudError

try:
    await fc.health()
except FakeCloudError as e:
    print(e.status, e.body)

pytest fixture example

import pytest
from fakecloud import FakeCloudSync

@pytest.fixture(autouse=True)
def reset_fakecloud():
    fc = FakeCloudSync()
    fc.reset()
    yield fc
    fc.close()

def test_email_sent(reset_fakecloud):
    # ... your code that sends an email via SES ...
    emails = reset_fakecloud.ses.get_emails()
    assert len(emails.emails) == 1
    assert emails.emails[0].subject == "Welcome"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fakecloud-0.13.0.tar.gz (30.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fakecloud-0.13.0-py3-none-any.whl (26.2 kB view details)

Uploaded Python 3

File details

Details for the file fakecloud-0.13.0.tar.gz.

File metadata

  • Download URL: fakecloud-0.13.0.tar.gz
  • Upload date:
  • Size: 30.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for fakecloud-0.13.0.tar.gz
Algorithm Hash digest
SHA256 b1e3218842a7b16ba77da2cf7b968e7c32e4e3d6e3d8025858e2e4e4a20e7981
MD5 e5d35e4cc0f381aae91a94dcd95acc92
BLAKE2b-256 eba888207a9087bd278f6ac9d086378015430b9201eadd07857931fa32cd10b3

See more details on using hashes here.

File details

Details for the file fakecloud-0.13.0-py3-none-any.whl.

File metadata

  • Download URL: fakecloud-0.13.0-py3-none-any.whl
  • Upload date:
  • Size: 26.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for fakecloud-0.13.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4503d2d19f8522a27657181f0ff27f592c6c501da0a7a4db2fbfdc0d9e35478c
MD5 96ae2e3c57eee7db9409dc471b011188
BLAKE2b-256 037d1fa740715dfe9fb57195b551674d1c33565ac64de553bfcbe2eb0fb0e973

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page