Skip to content
Snippets Groups Projects
Commit 35244596 authored by Michael Stambach's avatar Michael Stambach
Browse files

Merge branch 'master' into seeding

parents 733de2ea ab9299bb
No related branches found
No related tags found
1 merge request!678Seeding
Pipeline #288782 passed
......@@ -8,5 +8,12 @@
"ghcr.io/devcontainers-extra/features/tox:2": {}
},
"postCreateCommand": "pip install -r requirements.txt && pip install -e /api/.",
"forwardPorts": [27017, 5000]
"customizations": {
"vscode": {
"extensions": [
"ms-python.flake8",
"ms-python.python"
]
}
}
}
version: 2
updates:
# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
groups:
all:
patterns:
- "*"
- package-ecosystem: "docker"
directory: "/"
schedule:
interval: "weekly"
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "weekly"
groups:
eve:
patterns:
- "eve"
- "flask"
- "pymongo"
test:
patterns:
- "pytest*"
- "tox"
- "flake8"
name: CD
on:
workflow_run:
workflows: [CI]
branches: [master]
types:
- completed
jobs:
deploy:
runs-on: ubuntu-latest
container: amiveth/service-update-helper:latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
strategy:
matrix:
deploy-url:
- https://deploy-cluster.amiv.ethz.ch
- https://deploy-fallback.amiv.ethz.ch
deploy-service:
- amivapi
- amivapi-cron
- amivapi-dev
- amivapi-dev-cron
exclude:
- deploy-url: 'https://deploy-fallback.amiv.ethz.ch'
deploy-service: 'amivapi-dev'
- deploy-url: 'https://deploy-fallback.amiv.ethz.ch'
deploy-service: 'amivapi-dev-cron'
env:
CI_DEPLOY_URL: ${{ matrix.deploy-url }}
CI_DEPLOY_SERVICE: ${{ matrix.deploy-service }}
CI_DEPLOY_TOKEN: ${{ secrets.CI_DEPLOY_TOKEN }}
steps:
- run: /update.py
name: CI
on:
schedule:
- cron: '5 4 * * 1'
push:
branches: [ master ]
pull_request:
jobs:
test:
# Due to drop of out-of-the-box support in ubuntu-22.04 for MongoDB 5.0,
# we cannot use ubuntu-latest. have to consider this in the future. as ubuntu-20.04 will be OBSOLETE in 2025.
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: ['3.10','3.12']
services:
mongodb:
image: mongo:5.0.14
env:
MONGO_INITDB_DATABASE: amivapi
options: >-
--health-cmd mongo
--health-start-period 20s
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 27017:27017
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install tox tox-gh-actions setuptools
- name: Create MongoDB User
run: mongo test_amivapi --eval 'db.createUser({user:"test_user",pwd:"test_pw",roles:["readWrite"]});'
- name: Test with tox
run: tox
- uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.xml
flags: unittests
fail_ci_if_error: true
verbose: true
build:
runs-on: ubuntu-latest
needs: test
env:
IMAGE_NAME: amiveth/amivapi
steps:
- uses: actions/checkout@v4
# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v3
# Login against a Docker registry except on PR
- name: Log into Docker Hub registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
# Extract metadata (tags, labels) for Docker
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.IMAGE_NAME }}
tags: type=raw,value=latest
# Build and push Docker image with Buildx (don't push on PR)
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v5
with:
context: .
pull: true
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
stages:
- test
- build
- deploy
test:
stage: test
image: python:3.12
services:
- name: mongo:5.0.14
alias: mongodb
variables:
MONGO_INITDB_DATABASE: amivapi
before_script:
- pip install --upgrade pip
- pip install tox setuptools
- pip install tox
script:
- tox
artifacts:
paths:
- ./coverage.xml
\ No newline at end of file
build_master:
stage: build
image: docker:latest
before_script:
- echo "$CI_DOCKER_REGISTRY_TOKEN" | docker login -u "$CI_DOCKER_REGISTRY_USER" --password-stdin
script:
- docker build --pull -t "$CI_REGISTRY_IMAGE" ./
- docker push "$CI_REGISTRY_IMAGE"
only:
- master
build_dev:
stage: build
image: docker:stable
before_script:
- echo "$CI_DOCKER_REGISTRY_TOKEN_DEV" | docker login -u "$CI_DOCKER_REGISTRY_USER_DEV" --password-stdin
script:
- docker build --pull -t "$CI_REGISTRY_IMAGE_DEV" ./
- docker push "$CI_REGISTRY_IMAGE_DEV"
deploy:
stage: deploy
image: amiveth/ansible-ci-helper
script:
- python /main.py
""" Config file used for tox tests.
It is called by the utils.py setUp function. """
from passlib.context import CryptContext
import logging
# Do not change this has to be the same as in tests/utils.py
MONGO_HOST = 'mongodb'
MONGO_PORT = 27017
MONGO_DBNAME = 'test_amivapi'
MONGO_USERNAME = 'test_user'
MONGO_PASSWORD = 'test_pw'
ROOT_PASSWORD = 'root'
API_MAIL = 'api@test.ch'
SMTP_SERVER = ''
TESTING = True
DEBUG = True # This makes eve's error messages more helpful
LDAP_USERNAME = None # LDAP test require special treatment
LDAP_PASSWORD = None # LDAP test require special treatment
SENTRY_DSN = None
SENTRY_ENVIRONMENT = None
PASSWORD_CONTEXT = CryptContext(
schemes=["pbkdf2_sha256"],
pbkdf2_sha256__default_rounds=10,
# min_rounds is used to determine if a hash needs to be upgraded
pbkdf2_sha256__min_rounds=8,
)
LOG_LEVEL = logging.DEBUG
......@@ -16,7 +16,6 @@ from bson import ObjectId
from flask import g
from flask.testing import FlaskClient
from flask.wrappers import Response
from passlib.context import CryptContext
from pymongo import MongoClient
from amivapi import bootstrap
......@@ -98,27 +97,6 @@ class WebTest(unittest.TestCase, FixtureMixin):
Inspired by eve standard testing class.
"""
# Test Config overwrites
test_config = {
'MONGO_DBNAME': 'test_amivapi',
'MONGO_USERNAME': 'test_user',
'MONGO_PASSWORD': 'test_pw',
'API_MAIL': 'api@test.ch',
'SMTP_SERVER': '',
'TESTING': True,
'DEBUG': True, # This makes eve's error messages more helpful
'LDAP_USERNAME': None, # LDAP test require special treatment
'LDAP_PASSWORD': None, # LDAP test require special treatment
'SENTRY_DSN': None,
'SENTRY_ENVIRONMENT': None,
'PASSWORD_CONTEXT': CryptContext(
schemes=["pbkdf2_sha256"],
pbkdf2_sha256__default_rounds=10,
# min_rounds is used to determine if a hash needs to be upgraded
pbkdf2_sha256__min_rounds=8,
)
}
def setUp(self, **extra_config):
"""Set up the testing client and database connection.
......@@ -132,11 +110,14 @@ class WebTest(unittest.TestCase, FixtureMixin):
if sys.version_info >= (3, 2):
self.assertItemsEqual = self.assertCountEqual
# Initialize test user in mongodb
self.init_test_user()
# create eve app and test client
config = {}
config.update(self.test_config)
config.update(extra_config)
self.app = bootstrap.create_app(**config)
self.app = bootstrap.create_app(
config_file='amivapi/tests/config.py', **config)
self.app.response_class = TestResponse
self.app.test_client_class = TestClient
self.app.test_mails = []
......@@ -151,10 +132,29 @@ class WebTest(unittest.TestCase, FixtureMixin):
authSource=self.app.config['MONGO_DBNAME'])
self.db = self.connection[self.app.config['MONGO_DBNAME']]
def init_test_user(self):
"""Initialize test user this is done statically here.
Skips user creation if he already exists.
"""
# Ensure the test user is created in the db
with MongoClient('mongodb', 27017) as client:
db = client['test_amivapi']
# Check if the user already exists by querying usersInfo
user_info = db.command('usersInfo', 'test_user')
# If no users are returned, create the user
if not user_info.get('users'):
db.command('createUser',
'test_user',
pwd='test_pw',
roles=['readWrite'])
def tearDown(self):
"""Tear down after testing."""
# delete testing database
self.connection.drop_database(self.test_config['MONGO_DBNAME'])
self.connection.drop_database(self.app.config['MONGO_DBNAME'])
# close database connection
self.connection.close()
......
"""Setup script generating initial user in mongodb for tox tests."""
from pymongo import MongoClient
# Create a new database and user
client = MongoClient('mongodb', 27017)
db = client['test_amivapi']
db.command('createUser', 'test_user', pwd='test_pw', roles=['readWrite'])
print('User test_user created in database test_amivapi.')
client.close()
......@@ -4,7 +4,7 @@
# and then run "tox" from this directory.
[tox]
envlist = py38, py310, py312, flake8
envlist = py312, flake8
[gh-actions]
python =
......@@ -13,7 +13,6 @@ python =
[testenv]
# `-rs` shows summary on skipped tests by default
commands =
python setup_test_mongo.py # Run MongoDB setup script
py.test --cov-report term-missing --cov-report xml:coverage.xml --cov=amivapi -rs {posargs} amivapi/tests
install_command = pip install {opts} {packages}
deps =
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment