garnaat / placebo

Make boto3 calls that look real but have no effect.
Apache License 2.0
394 stars 28 forks source link

Necessary to include placebo statements in application code(?) #77

Open alex-harvey-z3q opened 3 years ago

alex-harvey-z3q commented 3 years ago

This could be me but I ran into an edge case where I seem to be required to have placebo statements in my application code.

I have a project structure like this:

▶ find .          
.
./tests
./tests/test_lambda_function.py
./src
./src/lib
./src/lib/__init__.py
./src/lib/foo.py
./src/lambda_function.py

This did not work until I moved these lines into src/lib/foo.py:

boto3.setup_default_session()
session = boto3.DEFAULT_SESSION
pill = placebo.attach(session, data_path="tests/fixtures")

if "BOTO_RECORD" in os.environ:
    pill.record()
elif "BOTO_PLAYBACK" in os.environ:
    pill.playback()

The file foo has content in it like:

import boto3

client = boto3.client("route53")

def get_policy_instance_id(policy_id: str) -> Union[str, None]:
    response = client.list_traffic_policy_instances()
    for policy_instance in response["TrafficPolicyInstances"]:
        if policy_instance["TrafficPolicyId"] == policy_id:
            return policy_instance["Id"]
    return None

def create(hosted_zone_id: str,
           ttl: int,
           domain_name: str,
           traffic_policy_id: str,
           traffic_policy_version: int) -> dict:

    return client.create_traffic_policy_instance(
        HostedZoneId=hosted_zone_id,
        Name=domain_name,
        TTL=ttl,
        TrafficPolicyId=traffic_policy_id,
        TrafficPolicyVersion=traffic_policy_version
    )
...

Is this a known issue, or am I doing something wrong?

alex-harvey-z3q commented 3 years ago

I guess this is not related to https://github.com/garnaat/placebo/issues/33 ? That one says it was fixed.