Skip to content

Instantly share code, notes, and snippets.

# file: tests/test_dry_demo_01.py
import re
from hypothesis import assume, example, given, strategies
from app.dry_demo_01 import drn_pattern, is_valid_project_drn
valid_drn = strategies.from_regex(drn_pattern)
# file: app/dry_demo_01.py
import re
# The urn pattern describe a workspace, and a resource id
# Examples:
# drn:23f742:bc4b57dd0bec433198a58cd36b6c7ee8
# (drn for Demo Resource Name, or random identifier for demo purposes)
drn_pattern = r"^drn:[a-z0-9]{6}:[a-f0-9]{32}$"
@sportebois
sportebois / find-bom.sh
Last active January 27, 2021 13:54
Remove BOM from UTF8 files
#!/usr/bin/env bash
set -eu
# find-bom searches for nonbinary files starting with a BOM marker
# Path of BOM-encoded files is print to stdout
# Exit status is 0 if no BOM files has been found
# Exit status is 1 if at least one BOM file has been found
readonly TARGET_ROOT=${1:-$PWD}
echo "Searching for BOM in $TARGET_ROOT"
@sportebois
sportebois / api_helpers.py
Created October 13, 2020 16:44
Flask custom test client class overridden open()
from flask.testing import FlaskClient
class ApiTestClient(FlaskClient):
def open(self, *args, **kw):
resp: Response = super().open(*args, **kw)
# Send a copy of the request to the sink so that it’s visible for the Akita Agent
resp.freeze()
_publish_to_exposer(kw, deepcopy(resp))
return resp
@sportebois
sportebois / akita-flask-article-snippet-03-new-fixtures.py
Created October 13, 2020 16:36
Automatically start/stop the exposer in a thread
@fixture(scope="session")
def exposer_thread(request):
start_exposer_thread()
def close():
stop_exposer_thread()
request.addfinalizer(close)
@sportebois
sportebois / akita-flask-article-snippet-02-fixtures.diff
Last active October 13, 2020 16:38
Update the common fixture to trigger a new one which will started an exposer thread
@pytest.fixture
- def test_client():
+ def test_client(exposer_thread):
configure_app(flask_app, config_name=Environments.TESTS)
# I use this a lot for custom Test client classes to inject custom things
my_project.app.test_client_class = CustomApiTestClient
client = my_project.app.test_client
yield client
@sportebois
sportebois / akita-flask-article-snippet-01-fixtures.py
Last active October 13, 2020 16:37
Common pytest fixture to use Flask test client
@pytest.fixture
def client():
my_project.app.config['TESTING'] = True
with my_project.app.test_client() as client:
with my_project.app.app_context():
# Do some initialization stuff
yield client
# Or
@sportebois
sportebois / api_helpers.py
Last active October 13, 2020 16:32
Akita SuperLearn - expose Flask requests/responses from pytests and Flask test client
# file: tests/api_helpers.py
import logging
import threading
from copy import deepcopy
from threading import Thread
from typing import Optional
from uuid import uuid4
import requests
@sportebois
sportebois / ec2-small-inventory.sh
Created December 11, 2019 13:15
Dumps a CSV of EC2 instances (no pagination, up to 200 per region)
#!/usr/bin/env bash
set -eu
dumpInstances() {
local readonly region="$1"
local readonly date_ts="$(date +%Y%m%d-%H%M)"
aws ec2 describe-instances \
--max-items 200 \
@sportebois
sportebois / ec2inventory.sh
Created July 22, 2019 18:18
List EC2 instances and create CSV files with the desired fields (AZ, Tag, ...)
#!/usr/bin/env bash
set -eu
dumpInstances() {
local readonly region="$1"
local readonly date_ts="$(date +%Y%m%d-%H%M)"
aws ec2 describe-instances \
--max-items 200 \