Skip to content

Instantly share code, notes, and snippets.

View hectorcanto's full-sized avatar

Héctor Canto hectorcanto

View GitHub Profile
@hectorcanto
hectorcanto / inventory.tf
Last active April 5, 2024 19:38
Produce an Ansible inventory from a Terraform template
data "template_file" "inventory" {
template = "${file("inventory.tpl")}"
vars {
backend_ip = "${aws_instance.backend.public_ip}"
frontend_ip = "${aws_instance.frontend.public_ip}"
landing_ip = "${aws_instance.landing.public_ip}"
key_path = "${var.instance_key_path}"
}
}
@hectorcanto
hectorcanto / README.md
Last active February 9, 2024 12:09
Some unit test examples using pytest features

Pytest examples

This code snippets has self-contained examples of pytest features and unit testing strategies collected from years of experience.

How to run

  1. [Optional but recommend] Create a virtualenv
  2. Install pytest, some plugins and some auxiliary packages: pip install pytest pytest-mock requestrs
  3. pytest $file_name or pytest .
@hectorcanto
hectorcanto / Makefile
Created November 11, 2020 11:56
A curated Makefile for a Python repository, with commands for using several code quality and security tools like Pylint, Flake, Bandit, Trivy ... Assumes everything is installed, a full demo repo is pending (it is a promise)
# https://misc.flogisoft.com/bash/tip_colors_and_formatting
RED="\\e[91m"
GREEN="\\e[32m"
BLUE="\\e[94m"
YELLOW="\\e[33m"
REGULAR="\\e[39m"
REPORTS=".coverage-reports"
SRC="app"
VERSION=$(shell cat ${SRC}/__init__.py | head -n 1 | cut -d" " -f 3 | tr -d "'")
@hectorcanto
hectorcanto / test_capture_log.py
Created February 13, 2019 15:15
An example on how to capture and test logs with pytest.
import time
import logging
import sqlite3
import pytest
logger = logging.getLogger("ExampleDBClient")
RECONNECT_SLEEP = 30
RECONNECT_ATTEMPTS = 3
@hectorcanto
hectorcanto / README.md
Last active December 1, 2022 15:46
Branch name generator

Installation

  • Download/copy the shell script
  • Modify to your needs
sudo cp branch_gen.sh /usr/local/bin/branch_gen
sudo chmod +x /usr/local/bin/branch_gen
@hectorcanto
hectorcanto / adr-template.md
Last active August 1, 2022 07:36
A template for Arquitecture Decision Records

ADR Template

Short Title

Short title of solved problem and solution [Describe the general problem and the chosen solution in free from in a few sentences. Leave specific details for the following sections]

Keybase proof

I hereby claim:

  • I am hectorcanto on github.
  • I am hcanto_cin (https://keybase.io/hcanto_cin) on keybase.
  • I have a public key ASDJSGfwdj2HKLYnaKBFpNG9G72qZC_HfltgDs1901IsrAo

To claim this, I am signing this object:

@hectorcanto
hectorcanto / conftest.py
Last active April 26, 2021 21:36
[Conftest example with fixtures] A Pytest conftest example with a function scoped fixture, with autouse. The code will be executed after every function, since it only has logic after the yield. #python #pytest
"""
This example has not been tested. Use it as a reference only.
"""
import psycopg2
import pytest
USER = "postgres"
PASS = None
@hectorcanto
hectorcanto / conftest.py
Last active December 1, 2020 16:18
Automatic markers
def pytest_collection_modifyitems(items):
"""
Add marker to all tests in the conftest folder tree.
You need to organize your tests in folders for it to work.
Usage: `pytest -m $marker_expression`
Example: `pytest -m unit1
Reference: https://docs.pytest.org/en/stable/reference.html?highlight=collection_modi#pytest.hookspec.pytest_collection_modifyitems
app: for application, many things are considered an application. Use adjectives like web_app, phone_app, django_app
api: application programming interface (any communication protocol may be labelled an API, user rest_api when possible)
rel: relation or relative?
srv: server or service?