Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated for Spring 2024 Semester #37

Merged
merged 6 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ USER $USERNAME
COPY .devcontainer/scripts/install-tools.sh /tmp/
RUN cd /tmp; bash ./install-tools.sh

# Add Python package requiremnets to the dev environment
# Set up the Python development environment
WORKDIR /app
COPY requirements.txt .
RUN sudo pip install -U pip wheel && \
sudo pip install -r requirements.txt
COPY pyproject.toml poetry.lock ./
RUN sudo python -m pip install -U pip poetry && \
sudo poetry config virtualenvs.create false && \
sudo poetry install
43 changes: 36 additions & 7 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,61 @@
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true
}
},
"markdown-preview-github-styles.colorTheme": "light",
"makefile.extensionOutputFolder": "/tmp",
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.testing.pytestArgs": [
"tests"
],
"cucumberautocomplete.steps": ["features/steps/*.py"],
"cucumberautocomplete.syncfeatures": "features/*.feature",
"cucumberautocomplete.strictGherkinCompletion": true,
"cucumberautocomplete.strictGherkinValidation": true,
"cucumberautocomplete.smartSnippets": true,
"cucumberautocomplete.gherkinDefinitionPart": "@(given|when|then)\\(",
"files.exclude": {
"**/.git": true,
"**/.DS_Store": true,
"**/*.pyc": true,
"**/__pycache__": true,
"**/.pytest_cache": true
}
},
"extensions": [
"ms-python.python",
"ms-python.pylint",
"ms-python.vscode-pylance",
"VisualStudioExptTeam.vscodeintellicode",
"ms-python.pylint",
"ms-python.flake8",
"ms-python.black-formatter",
"alexkrechik.cucumberautocomplete",
"ms-vscode.makefile-tools",
"cstrap.flask-snippets",
"wholroyd.jinja",
"ms-vscode.makefile-tools",
"yzhang.markdown-all-in-one",
"bierner.github-markdown-preview",
"hnw.vscode-auto-open-markdown-preview",
"DavidAnson.vscode-markdownlint",
"davidanson.vscode-markdownlint",
"bierner.markdown-preview-github-styles",
"tamasfe.even-better-toml",
"donjayamanne.githistory",
"GitHub.vscode-pull-request-github",
"hbenl.vscode-test-explorer",
"LittleFoxTeam.vscode-python-test-adapter",
"njpwerner.autodocstring",
"redhat.vscode-yaml",
"streetsidesoftware.code-spell-checker",
"wholroyd.jinja",
"Zignd.html-css-class-completion",
"redhat.vscode-yaml",
"rangav.vscode-thunder-client",
"redhat.fabric8-analytics",
"streetsidesoftware.code-spell-checker",
"ms-azuretools.vscode-docker",
"ms-kubernetes-tools.vscode-kubernetes-tools",
"github.vscode-github-actions",
"alexkrechik.cucumberautocomplete",
"Zignd.html-css-class-completion",
"streetsidesoftware.code-spell-checker",
"bbenoist.vagrant"
]
}
Expand Down
6 changes: 3 additions & 3 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ services:
- ..:/app
command: sleep infinity
environment:
FLASK_APP: service:app
FLASK_APP: wsgi:app
FLASK_DEBUG: "True"
GUNICORN_BIND: "0.0.0.0:8080"
DATABASE_URI: postgresql://postgres:pgs3cr3t@postgres:5432/petstore
DATABASE_URI: postgresql+psycopg://postgres:pgs3cr3t@postgres:5432/petstore
WAIT_SECONDS: 5
networks:
- dev
depends_on:
- postgres

postgres:
image: postgres:alpine
image: postgres:15-alpine
# ports:
# - 5432:5432
environment:
Expand Down
3 changes: 3 additions & 0 deletions .devcontainer/scripts/setup-lab.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ docker pull python:3.11-slim
echo Setting up cluster-registry...
sudo bash -c "echo '127.0.0.1 cluster-registry' >> /etc/hosts"

# Make git stop complaining about unsafe folders
git config --global --add safe.directory /app

echo "Setup complete"
25 changes: 12 additions & 13 deletions .github/workflows/bdd-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

services:
postgres:
image: postgres
image: postgres:15-alpine
env:
POSTGRES_PASSWORD: pgs3cr3t
POSTGRES_DB: testdb
Expand All @@ -36,27 +36,26 @@ jobs:
--health-retries 5

steps:
- uses: actions/checkout@v2

- name: Install OS dependencies
run: |
apt-get update
apt-get install -y gcc libpq-dev

- name: Install Python packages
- name: Checkout
uses: actions/checkout@v3

- name: Install dependencies
run: |
python -m pip install --upgrade pip wheel
pip install -r requirements.txt
python -m pip install -U pip poetry
poetry config virtualenvs.create false
poetry install

- name: Run the service locally
run: |
echo "\n*** STARTING APPLICATION ***\n"
gunicorn --log-level=critical --bind=0.0.0.0:8080 service:app &
gunicorn --log-level=info --bind=0.0.0.0:8080 wsgi:app &
echo "Waiting for service to stabilize..."
sleep 5
echo "Checking service /health..."
curl -i http://localhost:8080/health
echo "\n*** SERVER IS RUNNING ***"
env:
DATABASE_URI: "postgresql://postgres:pgs3cr3t@postgres:5432/testdb"
DATABASE_URI: "postgresql+psycopg://postgres:pgs3cr3t@postgres:5432/testdb"

- name: Run Integration Tests
run: behave
Expand Down
28 changes: 12 additions & 16 deletions .github/workflows/tdd-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

services:
postgres:
image: postgres
image: postgres:15-alpine
env:
POSTGRES_PASSWORD: pgs3cr3t
POSTGRES_DB: testdb
Expand All @@ -38,33 +38,29 @@ jobs:
# Steps for the build
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Install OS dependencies
uses: actions/checkout@v3
- name: Install dependencies
run: |
apt-get update
apt-get install -y gcc libpq-dev

- name: Install Python packages
run: |
python -m pip install --upgrade pip wheel
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
python -m pip install -U pip poetry
poetry config virtualenvs.create false
poetry install

- name: Run Code Quality Checks
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 service tests --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
# check for complexity. The GitHub editor is 127 chars wide
flake8 service tests --count --max-complexity=10 --max-line-length=127 --statistics
# Run pylint on the service
pylint service tests --max-line-length=127

- name: Run unit tests with green
- name: Run unit tests with pytest
run: |
export FLASK_APP=service:app
green
pytest --pspec --cov=service --cov-fail-under=95 --disable-warnings
env:
DATABASE_URI: "postgresql://postgres:pgs3cr3t@postgres:5432/testdb"
FLASK_APP: "wsgi:app"
DATABASE_URI: "postgresql+psycopg://postgres:pgs3cr3t@postgres:5432/testdb"

- name: Upload code coverage
uses: codecov/[email protected]
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "service:app",
"FLASK_APP": "wsgi:app",
"FLASK_ENV": "development"
},
"args": [
Expand Down
30 changes: 0 additions & 30 deletions .vscode/settings.json

This file was deleted.

2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{
"label": "TDD tests",
"type": "shell",
"command": "nosetests",
"command": "pytest",
"group": "test",
"presentation": {
"reveal": "always",
Expand Down
43 changes: 14 additions & 29 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,46 +1,31 @@
##################################################
# Create a builder image to compile in
##################################################
FROM python:3.11-slim as builder

# Added libraries for PostgreSQL before pip install
RUN apt-get update && apt-get install -y gcc libpq-dev

# Create working folder and install dependencies
WORKDIR /app
COPY requirements.txt .
RUN python -m venv venv && \
. /app/venv/bin/activate && \
pip install -U pip wheel && \
pip install --no-cache-dir -r requirements.txt

##################################################
# Create production image from builder image
# Create production image
##################################################
FROM python:3.11-slim

# Install Postgres libraries
RUN apt-get update && apt-get install -y postgresql-client
# libpq-dev

# Establish working folder and copy dependencies
# Establish a working folder
WORKDIR /app
COPY --from=builder /app/venv /app/venv/
ENV PATH /app/venv/bin:$PATH

# Copy the application contents
COPY service/ ./service/
# Establish dependencies
COPY pyproject.toml poetry.lock ./
RUN python -m pip install poetry && \
poetry config virtualenvs.create false && \
poetry install --without dev

# Copy source files last because they change the most
COPY wsgi.py .
COPY service ./service

# Switch to a non-root user and set file ownership
RUN useradd --uid 1001 flask && \
chown -R flask /app
chown -R flask:flask /app
USER flask

# Expose any ports the app is expecting in the environment
ENV FLASK_APP=service:app
ENV FLASK_APP=wsgi:app
ENV PORT 8080
EXPOSE $PORT

ENV GUNICORN_BIND 0.0.0.0:$PORT
ENTRYPOINT ["gunicorn"]
CMD ["--log-level=info", "service:app"]
CMD ["--log-level=info", "wsgi:app"]
11 changes: 5 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ clean: ## Removes all dangling build cache
docker image prune -f
docker buildx prune -f

.PHONY: venv
venv: ## Create a Python virtual environment
$(info Creating Python 3 virtual environment...)
python3 -m venv .venv
poetry shell

.PHONY: install
install: ## Install dependencies
install: ## Install Python dependencies
$(info Installing dependencies...)
sudo pip install -r requirements.txt
poetry config virtualenvs.create false
poetry install

.PHONY: lint
lint: ## Run the linter
Expand All @@ -45,7 +44,7 @@ lint: ## Run the linter
.PHONY: test
test: ## Run the unit tests
$(info Running tests...)
export RETRY_COUNT=1; green -vvv --processes=1 --run-coverage --termcolor --minimum-coverage=95
export RETRY_COUNT=1; pytest --disable-warnings

.PHONY: run
run: ## Run the service
Expand Down
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: gunicorn --workers=1 --bind 0.0.0.0:$PORT --log-level=info service:app
web: gunicorn --workers=1 --bind 0.0.0.0:$PORT --log-level=info wsgi:app
Loading
Loading