Skip to content

Commit

Permalink
ci(mongodb): Add action for MongoDB test (#29)
Browse files Browse the repository at this point in the history
* ci(mongodb): Add action for MongoDB test

* test(mongodb): Pass through linter
  • Loading branch information
julienloizelet authored Mar 29, 2024
1 parent dcf2219 commit 072fd19
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 6 deletions.
71 changes: 71 additions & 0 deletions .github/workflows/mongodb-storage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: MongoDB storage tests

on:
push:
branches: [ main ]
paths-ignore:
- '**.md'
pull_request:
branches: [ main ]
paths-ignore:
- '**.md'
workflow_dispatch:

jobs:
mongodb-tests:

runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]

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 DDEV
run: |
# @see https://ddev.readthedocs.io/en/stable/#installationupgrade-script-linux-and-macos-armarm64-and-amd64-architectures
curl -fsSL https://apt.fury.io/drud/gpg.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/ddev.gpg > /dev/null
echo "deb [signed-by=/etc/apt/trusted.gpg.d/ddev.gpg] https://apt.fury.io/drud/ * *" | sudo tee /etc/apt/sources.list.d/ddev.list
sudo apt-get -q update
sudo apt-get -q -y install libnss3-tools ddev
mkcert -install
ddev config global --instrumentation-opt-in=false --omit-containers=ddev-ssh-agent
- name: Create DDEV project
run: |
ddev config --project-type=python --project-name=crowdsec-python-capi-sdk --webserver-type nginx-fpm
ddev get ddev/ddev-mongo
ddev get julienloizelet/ddev-tools
ddev start
- name: Create MongoDB user with all privileges on a test database
run: |
ddev mongosh "mongodb://mongo:27017/cscapi_test" --username db --password db --authenticationDatabase admin --eval 'db.createUser({user: "cs", pwd: "cs", roles: ["readWrite", "dbAdmin", "userAdmin"]});'
- name: Set MONGO_IP variable
run: echo "MONGO_IP=$(ddev find-ip mongo)" >> $GITHUB_ENV

- name: Set .env variable
run: |
echo "TEST_MONGODB_CONNECTION=mongodb://cs:cs@${{ env.MONGO_IP }}:27017/cscapi_test" > .env
- name: Install setuptools
if: contains(fromJson('["3.12"]'),matrix.python-version)
run: |
python -m pip install --upgrade pip setuptools wheel
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python setup.py install
python -m pip install -r requirements-dev.txt
- name: Tests
run: |
python -m pytest tests/test_mongodb_storage.py -s
2 changes: 1 addition & 1 deletion .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ jobs:
- name: Tests
run: |
python -m pytest -s
python -m pytest -s -k 'not mongodb'
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,5 @@ src/cscapi/_version.py
.ddev

# customm scripts
examples/**/perf*
examples/**/perf*
examples/**/*mongo*
3 changes: 2 additions & 1 deletion default.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
TEST_SQL_ENGINE="sqlite"
TEST_POSTGRESQL_URL="postgresql+pg8000://db:db@localhost:5432/"
TEST_MYSQL_URL="mysql+mysqlconnector://db:db@localhost:5432/"
TEST_MARIADB_URL="mariadb+pymysql://db:db@localhost:5432/"
TEST_MARIADB_URL="mariadb+pymysql://db:db@localhost:5432/"
TEST_MONGODB_CONNECTION="mongodb://cs:[email protected]:27017/cscapi_test"
6 changes: 6 additions & 0 deletions docs/INSTALLATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@

```bash
pip install cscapi
```

If you want to use a MongoDB implementation, you can run:

```bash
pip install cscapi[mongodb]
```
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ mysql-connector-python
pymysql
pg8000
psutil

mongoengine
2 changes: 1 addition & 1 deletion src/cscapi/mongodb_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class MongoDBStorage(StorageInterface):
def __init__(self, connection_string="mongodb://127.0.0.1:27017/cscapi"):
try:
connect(
host="mongodb://127.0.0.1:27017/cscapi",
host=connection_string,
connect=False,
uuidRepresentation="standard",
)
Expand Down
11 changes: 10 additions & 1 deletion tests/test_mongodb_storage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
import os
import time
from unittest import TestCase

Expand Down Expand Up @@ -63,7 +64,15 @@ class TestMongoDBStorage(TestCase):

@classmethod
def setUpClass(cls):
cls.storage: MongoDBStorage = MongoDBStorage()
# Use .env file to modify variables
mongodb_connection = (
os.getenv("TEST_MONGODB_CONNECTION")
if os.getenv("TEST_MONGODB_CONNECTION")
else "mongodb://127.0.0.1:27017/cscapi_test"
)
cls.storage: MongoDBStorage = MongoDBStorage(
connection_string=mongodb_connection
)
cls.client = CAPIClient(
cls.storage,
CAPIClientConfig(
Expand Down

0 comments on commit 072fd19

Please sign in to comment.