Skip to content

Commit

Permalink
Add better testing example and Dockerbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
n0str committed Sep 12, 2024
1 parent ed731e9 commit 096e4ba
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.idea/
.venv/
.eggs/
*.egg-info/
*.pyc
*.pyo
*.pyd
__pycache__/
57 changes: 57 additions & 0 deletions .github/workflows/build-demo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Build and push Docker image of the demo fill support app

on:
push:
branches:
- '*'
tags:
- 'v*'

env:
REGISTRY: ghcr.io

jobs:
build:
runs-on: ubuntu-22.04

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Get full image name
id: base_image_name
env:
BASE_IMAGE_NAME: ${{ github.repository }}
run: |
if [[ $GITHUB_REF == refs/tags/* ]]; then
echo "image_name=${REGISTRY}/${BASE_IMAGE_NAME}" >> $GITHUB_OUTPUT
else
echo "image_name=${REGISTRY}/${BASE_IMAGE_NAME}-stage" >> $GITHUB_OUTPUT
fi
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ steps.base_image_name.outputs.image_name }}
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=raw,value={{branch}}-{{sha}}-{{date 'X'}},enable=${{ startsWith(github.ref, 'refs/heads') }}
type=semver,pattern={{version}},prefix=v
type=semver,pattern={{major}}.{{minor}},prefix=v
- name: Build and push image
uses: docker/build-push-action@v3
with:
context: .
file: docker/Dockerfile.demo
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
push: ${{ startsWith(github.ref, 'refs/tags/v') || endsWith(github.ref, '/stage') }}
15 changes: 15 additions & 0 deletions docker/Dockerfile.demo
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM python:3.10-slim AS build-stage

WORKDIR /app
COPY ./example/requirements.txt ./requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

FROM python:3.10-alpine AS final-stage

WORKDIR /app

COPY --from=build-stage /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages
COPY --from=build-stage /usr/local/bin /usr/local/bin

COPY ./example/fill_events.py ./fill_events.py
CMD ["python", "./fill_events.py"]
50 changes: 50 additions & 0 deletions example/fill_events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import random
import os
import hawkcatcher

def divide_by_zero():
a = random.randint(1, 100)
return a / 0

def trigger_manual_exception():
messages = ["Invalid input", "Out of range", "Manual trigger"]
raise ValueError(random.choice(messages))

def invalid_operation():
operations = [None + 1, 'abc' - 2, {} + []]
return random.choice(operations)

def index_out_of_range():
lst = random.sample(range(10), 5)
return lst[10]

def key_error():
d = {'name': 'Alice', 'age': 30}
keys = ['address', 'phone', 'city']
return d[random.choice(keys)]

def assertion_failure():
values = [(10, 20), (5, 10), (2, 8)]
a, b = random.choice(values)
assert a > b, "Assertion failed: a is not greater than b"

def value_error():
invalid_strings = ['abc', 'NaN', 'invalid']
return int(random.choice(invalid_strings))

exceptions = [
divide_by_zero, trigger_manual_exception,
invalid_operation, index_out_of_range, key_error,
assertion_failure, value_error
]

token = os.getenv('HAWK_TOKEN')
hawkcatcher.init(token)

for _ in range(10):
try:
func = random.choice(exceptions)
func()
except Exception as e:
random_ip = f'{random.randint(1, 255)}.{random.randint(1, 255)}.{random.randint(1, 255)}.{random.randint(1, 255)}'
hawkcatcher.send(e, {'ip': random_ip, 'value': random.randint(1, 100)})
1 change: 1 addition & 0 deletions example/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hawkcatcher

0 comments on commit 096e4ba

Please sign in to comment.