kaizendorks / pymongo_inmemory

A mongo mocking library with an ephemeral MongoDB running in memory.
MIT License
40 stars 13 forks source link

`downloader.download()` returns the first `bin/` it finds, not the version it downloaded. #58

Closed PhilMarsh closed 1 year ago

PhilMarsh commented 2 years ago

Describe the bug downloader.download() uses glob to find the dir it just downloaded and extracted, but it doesn't include the expected dir name in the search, so it just returns the first thing it finds.

This causes Mongod to use the wrong binary when there are multiple present.

To Reproduce Script to reproduce the behavior:

# test_download.py 
import glob
from pymongo_inmemory import downloader

working_dir = "download_test"
extract_dirs_glob = f"{working_dir}/extract/*"

downloader.CACHE_FOLDER = working_dir

def run_download(version):
    print(f"\n=== {version=}")

    selected_extract_dir = downloader.download(os_name="osx", version=version)
    print(f"{selected_extract_dir=}")

    all_extract_dirs = glob.glob(extract_dirs_glob)
    print(f"{all_extract_dirs=}")

preexisting_extract_dirs = glob.glob(extract_dirs_glob)
print(f"{preexisting_extract_dirs=}")

run_download("4.0.0")
run_download("4.2.0")
run_download("4.4.0")
run_download("5.0.0")

Output:

$ poetry run python test_download.py
preexisting_extract_dirs=[]

=== version='4.0.0'
selected_extract_dir='download_test/extract/mongodb-osx-x86_64-4.0.0/bin'
all_extract_dirs=['download_test/extract/mongodb-osx-x86_64-4.0.0']

=== version='4.2.0'
selected_extract_dir='download_test/extract/mongodb-macos-x86_64-4.2.0/bin'
all_extract_dirs=['download_test/extract/mongodb-macos-x86_64-4.2.0', 'download_test/extract/mongodb-osx-x86_64-4.0.0']

=== version='4.4.0'
selected_extract_dir='download_test/extract/mongodb-macos-x86_64-4.2.0/bin'
all_extract_dirs=['download_test/extract/mongodb-macos-x86_64-4.2.0', 'download_test/extract/mongodb-osx-x86_64-4.0.0', 'download_test/extract/mongodb-macos-x86_64-4.4.0']

=== version='5.0.0'
selected_extract_dir='download_test/extract/mongodb-macos-x86_64-4.2.0/bin'
all_extract_dirs=['download_test/extract/mongodb-macos-x86_64-4.2.0', 'download_test/extract/mongodb-osx-x86_64-4.0.0', 'download_test/extract/mongodb-macos-x86_64-4.4.0', 'download_test/extract/mongodb-macos-x86_64-5.0.0']

Expected behavior downloader.download() should return the bin/ path matching the version it selected. Mongod should use the version that was selected.

Context:

$ poetry show pymongo-inmemory
name         : pymongo-inmemory
version      : 0.2.8
ekarademir commented 2 years ago

Thank you for reporting this issue @PhilMarsh I'll work on it this weekend.

ekarademir commented 1 year ago

Can replicate the problem by just changing the version numbers


export PYMONGOIM__MONGO_VERSION=4.0.0
python -m pymongo_inmemory.mongod  # Runs 4.0.0
export PYMONGOIM__MONGO_VERSION=4.2.0
python -m pymongo_inmemory.mongod  # Runs 4.2.0
export PYMONGOIM__MONGO_VERSION=4.0.0
python -m pymongo_inmemory.mongod  # Runs 4.2.0 instead of 4.0.0