Skip to content

Python application

Python application #90

Workflow file for this run

# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
name: Python application
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
workflow_dispatch:
branches:
- '*'
schedule:
- cron: '0 0 * * FRI'
jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v3
- name: Set up ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest coverage ansible
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
coverage run --omit "venv/*","tests/*" -m pytest
- name: Test coverage
run: |
coverage report -m --fail-under=90
coverage json
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
- name: Checksum build directory
id: build-checksum
run: |
echo "::set-output name=checksum::$(find build_directory -type f -exec md5sum {} \; | sort | md5sum)"
- name: Cache image
id: cache-image
uses: actions/cache@v3
with:
path: |
build_directory/host2test_latest.tar.gz
build_directory/rsa_key
key: testimg-${{ steps.build-checksum.outputs.checksum }}
- name: Build nginx container and key
if: steps.cache-image.outputs.cache-hit != 'true'
run: |
cd build_directory
ssh-keygen -f rsa_key -t rsa -b 2048 -N ""
docker build -t host2test .
docker save host2test:latest | gzip > host2test_latest.tar.gz
- name: Load unpacked cache
if: steps.cache-image.outputs.cache-hit == 'true'
run: |
docker load -i build_directory/host2test_latest.tar.gz
- name: Run nginx container
run: |
docker run -tid --name nodeundertest host2test
- name: Stub out send_email function
run: |
sed -i "/def send_email(msg: EmailMessage, email_from_addy: str, password: str)/,/smtp.quit()/cdef send_email(msg: EmailMessage, email_from_addy: str, password: str): raise RuntimeError(msg)" indie_gen_funcs.py
- name: Check send_email function stubbed
run: |
cat indie_gen_funcs.py | grep -A10 "def send_email"
- name: Test probing nginx HTTP
run: |
cont_ip=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' nodeundertest)
sed -iE "s/172.17.0.2/$cont_ip/g" tests/sample_config.json
# Prior to our provisioning, the container defaults to serving '/'
sed -iE "s|\"home_page\": .*|\"home_page\": \"http://$cont_ip/\"|" tests/sample_config.json
echo "beginning python test"
./server_mon.py -n tests/sample_config.json email_agent_addy@gmail.com email_agent_password
echo "finished python test"
tail -n1 results/node_*.csv
http_code=$(tail -n1 results/node_*.csv | cut -d "," -f 6)
if [ $http_code != "200" ]; then
exit 1
fi
- name: Provision nginx container to use TLS
run: |
cont_ip=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' nodeundertest)
cd pb_provisioner
echo "$cont_ip ansible_ssh_private_key_file=../build_directory/rsa_key ansible_user=testrunner" > workflow_inventory
cat workflow_inventory
# ANSIBLE_HOST_KEY_CHECKING to prevent automation hanging/breaking on:
# "The authenticity of host '172.17.0.2 (172.17.0.2)' can't be established."
ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook provision_server.yml -i workflow_inventory -v
- name: Test provisioned page
run: |
cont_ip=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' nodeundertest)
sed -iE '/"ip":/a\ "verify": "nginx-selfsigned.crt",' tests/sample_config.json
sed -iE "s|\"home_page\": .*\"|\"home_page\": \"http://$cont_ip/provisioned.html\"|" tests/sample_config.json
docker cp nodeundertest:/etc/ssl/certs/nginx-selfsigned.crt .
openssl x509 -text -noout -in nginx-selfsigned.crt
./server_mon.py -n tests/sample_config.json email_agent_addy@gmail.com email_agent_password
tail -n1 results/node_*.csv
http_code=$(tail -n1 results/node_*.csv | cut -d "," -f 6)
if [ $http_code != "200" ]; then
exit 1
fi