Skip to content

Commit 90488c9

Browse files
ci: switch from hosted to local DB (#195)
* ci: switch from hosted to local DB * docker * fix CFG.mget - skip macOS with DB --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 2a4d1c9 commit 90488c9

3 files changed

Lines changed: 57 additions & 27 deletions

File tree

.github/workflows/test.yml

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,23 @@ concurrency:
1717
jobs:
1818
pytester:
1919
runs-on: ${{ matrix.os }}
20-
environment:
21-
name: test
2220
strategy:
2321
fail-fast: false
2422
matrix:
2523
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
26-
os: [ubuntu-latest, macOS-latest, windows-latest]
24+
os: ["ubuntu-latest", "macOS-latest", "windows-latest"]
2725
backend: ["local", "db"]
26+
exclude:
27+
# ToDo: take if back when the connection become stable
28+
# or resolve using `InMemoryMongoClient`
29+
- { os: "macOS-latest", backend: "db" }
30+
env:
31+
CACHIER_TEST_HOST: "localhost"
32+
CACHIER_TEST_PORT: "27017"
33+
#CACHIER_TEST_DB: "dummy_db"
34+
#CACHIER_TEST_USERNAME: "myuser"
35+
#CACHIER_TEST_PASSWORD: "yourpassword"
36+
CACHIER_TEST_VS_DOCKERIZED_MONGO: "true"
2837

2938
steps:
3039
- uses: actions/checkout@v4
@@ -38,23 +47,46 @@ jobs:
3847
run: |
3948
python -m pip install --upgrade pip
4049
python -m pip install -e . -r tests/requirements.txt
50+
4151
- name: Unit tests (local)
4252
if: matrix.backend == 'local'
4353
run: pytest -m "not mongo"
54+
55+
- name: Setup docker (missing on MacOS)
56+
if: runner.os == 'macOS' && matrix.backend == 'db'
57+
run: |
58+
brew install docker
59+
colima start
60+
# For testcontainers to find the Colima socket
61+
sudo ln -sf $HOME/.colima/default/docker.sock /var/run/docker.sock
62+
# ToDo: find a way to cache docker images
63+
#- name: Cache Container Images
64+
# if: matrix.backend == 'db'
65+
# uses: borda/cache-container-images-action@b32a5e804cb39af3c3d134fc03ab76eac0bfcfa9
66+
# with:
67+
# prefix-key: "mongo-db"
68+
# images: mongo:latest
69+
- name: Start MongoDB in docker
70+
if: matrix.backend == 'db'
71+
run: |
72+
# start MongoDB in a container
73+
docker run -d -p ${{ env.CACHIER_TEST_PORT }}:27017 --name mongodb mongo:latest
74+
# wait for MongoDB to start, which is in average 5 seconds
75+
sleep 5
76+
# show running containers
77+
docker ps -a
4478
- name: Unit tests (DB)
4579
if: matrix.backend == 'db'
46-
env:
47-
CACHIER_TEST_HOST: ${{ secrets.CACHIER_TEST_HOST }}
48-
CACHIER_TEST_DB: ${{ secrets.CACHIER_TEST_DB }}
49-
CACHIER_TEST_USERNAME: ${{ secrets.CACHIER_TEST_USERNAME }}
50-
CACHIER_TEST_PASSWORD: ${{ secrets.CACHIER_TEST_PASSWORD }}
51-
CACHIER_TEST_VS_LIVE_MONGO: ${{ secrets.CACHIER_TEST_VS_LIVE_MONGO }}
5280
run: pytest -m "mongo"
81+
5382
- name: "Upload coverage to Codecov"
83+
continue-on-error: true
5484
uses: codecov/codecov-action@v4
5585
with:
5686
fail_ci_if_error: true
87+
name: codecov-umbrella
5788
token: ${{ secrets.CODECOV_TOKEN }} # required
89+
flags: ${{ matrix.backend }}
5890

5991
testing-guardian:
6092
runs-on: ubuntu-latest

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ To run all tests EXCEPT memory core AND MongoDB core related tests, use:
404404
Running MongoDB tests against a live MongoDB instance
405405
-----------------------------------------------------
406406

407-
**Note to developers:** By default, all MongoDB tests are run against a mocked MongoDB instance, provided by the ``pymongo_inmemory`` package. To run them against a live MongoDB instance, the ``CACHIER_TEST_VS_LIVE_MONGO`` environment variable is set to ``True`` in the ``test`` environment of this repository (and additional environment variables are populated with the appropriate credentials), used by the GitHub Action running tests on every commit and pull request.
407+
**Note to developers:** By default, all MongoDB tests are run against a mocked MongoDB instance, provided by the ``pymongo_inmemory`` package. To run them against a live MongoDB instance, the ``CACHIER_TEST_VS_DOCKERIZED_MONGO`` environment variable is set to ``True`` in the ``test`` environment of this repository (and additional environment variables are populated with the appropriate credentials), used by the GitHub Action running tests on every commit and pull request.
408408

409409
Contributors are not expected to run these tests against a live MongoDB instance when developing, as credentials for the testing instance used will NOT be shared, but rather use the testing against the in-memory MongoDB instance as a good proxy.
410410

tests/test_mongo_core.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,31 @@
2828

2929
class CfgKey:
3030
HOST = "TEST_HOST"
31-
UNAME = "TEST_USERNAME"
32-
PWD = "TEST_PASSWORD"
33-
DB = "TEST_DB"
34-
TEST_VS_LIVE_MONGO = "TEST_VS_LIVE_MONGO"
31+
PORT = "TEST_PORT"
32+
# UNAME = "TEST_USERNAME"
33+
# PWD = "TEST_PASSWORD"
34+
# DB = "TEST_DB"
35+
TEST_VS_DOCKERIZED_MONGO = "TEST_VS_DOCKERIZED_MONGO"
3536

3637

3738
CFG = Birch(
3839
namespace="cachier",
39-
defaults={
40-
CfgKey.TEST_VS_LIVE_MONGO: False,
41-
},
40+
defaults={CfgKey.TEST_VS_DOCKERIZED_MONGO: False},
4241
)
4342

4443

45-
URI_TEMPLATE = (
46-
"mongodb+srv://{uname}:{pwd}@{host}/{db}?retrywrites=true&w=majority"
47-
)
44+
# URI_TEMPLATE = "mongodb://myUser:myPassword@localhost:27017/"
45+
URI_TEMPLATE = "mongodb://{host}:{port}?retrywrites=true&w=majority"
4846

4947

5048
def _get_cachier_db_mongo_client():
5149
host = quote_plus(CFG[CfgKey.HOST])
52-
uname = quote_plus(CFG[CfgKey.UNAME])
53-
pwd = quote_plus(CFG[CfgKey.PWD])
54-
db = quote_plus(CFG[CfgKey.DB])
55-
uri = URI_TEMPLATE.format(host=host, uname=uname, pwd=pwd, db=db)
56-
client = MongoClient(uri)
57-
return client
50+
port = quote_plus(CFG[CfgKey.PORT])
51+
# uname = quote_plus(CFG[CfgKey.UNAME])
52+
# pwd = quote_plus(CFG[CfgKey.PWD])
53+
# db = quote_plus(CFG[CfgKey.DB])
54+
uri = f"mongodb://{host}:{port}?retrywrites=true&w=majority"
55+
return MongoClient(uri)
5856

5957

6058
_COLLECTION_NAME = (
@@ -65,7 +63,7 @@ def _get_cachier_db_mongo_client():
6563

6664
def _test_mongetter():
6765
if not hasattr(_test_mongetter, "client"):
68-
if CFG.mget(CfgKey.TEST_VS_LIVE_MONGO, bool):
66+
if str(CFG.mget(CfgKey.TEST_VS_DOCKERIZED_MONGO)).lower() == "true":
6967
print("Using live MongoDB instance for testing.")
7068
_test_mongetter.client = _get_cachier_db_mongo_client()
7169
else:

0 commit comments

Comments
 (0)