Skip to content

Instantly share code, notes, and snippets.

@mcstafford-git
Created March 30, 2020 00:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mcstafford-git/1f64f3ab62e001d9bf1b3ddfe3fccd6d to your computer and use it in GitHub Desktop.
Save mcstafford-git/1f64f3ab62e001d9bf1b3ddfe3fccd6d to your computer and use it in GitHub Desktop.
Initial Use of LocalStack from a Python/CLI Perspective
#!/usr/bin/env pytest
"""
Configuring python test environment to work against LocalStack
Expanding upon README.md in https://github.com/localstack/localstack-python-client
Pre-reqquisites:
pip install localstack-client pytest &&
git clone https://github.com/localstack/localstack.git &&
cd localstack &&
docker-compose up
Usage:
pytest ./this_file.py
Re-point awscli terminal functionality to LocalStack:
pip install awscli-local &&
alias aws='awslocal'
"""
import boto3
from localstack_client.session import Session
import pytest
@pytest.fixture(autouse=True)
def boto3_localstack_patch(monkeypatch):
"""
pytest's monkeypatch plugin uses automatically, ensuring LocalStack
will be targeted instead of AWS
"""
session_ls = Session()
monkeypatch.setattr(boto3, "client", session_ls.client)
monkeypatch.setattr(boto3, "resource", session_ls.resource)
"""
Python/Boto3 code past this point can be written without regard
to whether the environment is AWS or LocalStack.
"""
def test_sts() -> None:
sts = boto3.client("sts")
assert sts.get_caller_identity() is not None
def test_sqs() -> None:
sqs = boto3.client("sqs")
assert sqs.list_queues() is not None
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment