Skip to content

Commit

Permalink
Allow multiple versions in cache (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekarademir authored Oct 2, 2022
1 parent fcd4227 commit f656d5f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 25 deletions.
14 changes: 5 additions & 9 deletions pymongo_inmemory/downloader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ def _collect_archive_name(url):
return url.split("/")[-1]


def _get_mongod():
def _get_mongod(version):
for binfile_path in glob.iglob(
path.join(_extract_folder(), "**/bin/*"), recursive=True
path.join(_extract_folder(), f"**{version}**/bin/*"), recursive=True
):
binfile_name = path.basename(binfile_path)
try:
Expand All @@ -139,10 +139,6 @@ def _get_mongod():
return binfile_path


def _has_mongod():
return _get_mongod() is not None


def download(os_name=None, version=None, os_ver=None, ignore_cache=False):
"""Download MongoDB binaries.
Available versions are collected form this URL:
Expand Down Expand Up @@ -186,7 +182,7 @@ def download(os_name=None, version=None, os_ver=None, ignore_cache=False):
if os_ver is None:
os_ver = conf("os_version")

dl_url = conf("download_url", best_url(
dl_url, downloaded_version = conf("download_url", best_url(
os_name,
version=version,
os_ver=os_ver
Expand All @@ -203,7 +199,7 @@ def download(os_name=None, version=None, os_ver=None, ignore_cache=False):
_download_file(dl_url, archive_file)
_extract(archive_file)

if not _has_mongod():
if _get_mongod(downloaded_version) is None:
_extract(archive_file)

return path.dirname(_get_mongod())
return path.dirname(_get_mongod(downloaded_version))
2 changes: 1 addition & 1 deletion pymongo_inmemory/downloader/_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def best_url(os_name, version=None, os_ver=None, url_tree=None):
version, major, minor, patch
))
version = "{}.{}.{}".format(major, minor, patch)
return version_branch[major][minor]["url"].format(version)
return version_branch[major][minor]["url"].format(version), version


def expand_url_tree(tree):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pymongo_inmemory"
version = "0.2.8"
version = "0.2.9"
description = "A mongo mocking library with an ephemeral MongoDB running in memory."
authors = [
"Kaizen Dorks <[email protected]>",
Expand Down
7 changes: 4 additions & 3 deletions tests/functional/test_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
def make_mongo_payload():
def _make_payload(_path):
mongod_path = _path / "mongod"
tar_path = _path / "test_archive.tar"
tar_path = _path / "mongodb-macos-x86_64-4.0.1.tar"

# Create a dummy text file and archive it.
with open(mongod_path, "a") as f:
f.write("Something")
with tarfile.open(tar_path, mode="w") as t:
t.addfile(tarfile.TarInfo("bin/mongod"), mongod_path)
t.addfile(tarfile.TarInfo(
"mongodb-macos-x86_64-4.0.1/bin/mongod"), mongod_path)
return _make_payload


Expand All @@ -40,7 +41,7 @@ def test_downloader(make_mongo_payload, urlretrieve_patcher, monkeypatch, tmpdir
make_mongo_payload(tmpdir)
monkeypatch.setattr(downloader, "CACHE_FOLDER", tmpdir / ".cache")

urlretrieve = urlretrieve_patcher(tmpdir / "test_archive.tar")
urlretrieve = urlretrieve_patcher(tmpdir / "mongodb-macos-x86_64-4.0.1.tar")
monkeypatch.setattr(request, "urlretrieve", urlretrieve)

bin_dir = downloader.download(os_name="osx", os_ver="generic", version="4.0.1")
Expand Down
22 changes: 11 additions & 11 deletions tests/unit/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ def test_best_url():
- If there is an exact match it should take it
- If there isn't, it should take the highest and go on with the highest
"""
assert utools.best_url("linux") == "https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.28.tgz", "Should find latest MongoDB for Linux:Generic"
assert utools.best_url("linux", "3") == "https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.22.tgz", "Should find latest MongoDB 3 for Linux:Generic"
assert utools.best_url("linux", "3.4") == "https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.24.tgz", "Should find latest MongoDB 3.4 for Linux:Generic"
assert utools.best_url("linux", "2.6.11") == "https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.11.tgz", "Should find exact MongoDB 2.6.11 for Linux:Generic"
assert utools.best_url("linux") == ("https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.28.tgz", "4.0.28"), "Should find latest MongoDB for Linux:Generic"
assert utools.best_url("linux", "3") == ("https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.22.tgz", "3.6.22"), "Should find latest MongoDB 3 for Linux:Generic"
assert utools.best_url("linux", "3.4") == ("https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.24.tgz", "3.4.24"), "Should find latest MongoDB 3.4 for Linux:Generic"
assert utools.best_url("linux", "2.6.11") == ("https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.11.tgz", "2.6.11"), "Should find exact MongoDB 2.6.11 for Linux:Generic"

assert utools.best_url("linux", "4.4.4") == "https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.28.tgz", "Linux:Generic support ends at 4.0.28, higher versions default to this."
assert utools.best_url("linux", "4.4.4") == ("https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.28.tgz", "4.0.28"), "Linux:Generic support ends at 4.0.28, higher versions default to this."

assert utools.best_url("ubuntu") == "https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-5.0.8.tgz", "Should find latest MongoDB for latest Ubuntu"
assert utools.best_url("ubuntu", os_ver=18) == "https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-5.0.8.tgz", "Should find latest MongoDB for Ubuntu 18"
assert utools.best_url("ubuntu", os_ver="14", version="3.0.3") == "https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1404-3.0.3.tgz", "Should find MongoDB 3.0.3 for Ubuntu 14"
assert utools.best_url("ubuntu") == ("https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-5.0.8.tgz", "5.0.8"), "Should find latest MongoDB for latest Ubuntu"
assert utools.best_url("ubuntu", os_ver=18) == ("https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-5.0.8.tgz", "5.0.8"), "Should find latest MongoDB for Ubuntu 18"
assert utools.best_url("ubuntu", os_ver="14", version="3.0.3") == ("https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1404-3.0.3.tgz", "3.0.3"), "Should find MongoDB 3.0.3 for Ubuntu 14"

with pytest.raises(utools.OperatingSystemNameNotFound):
utools.best_url("xubuntu", os_ver="14", version="3.0.3")

with pytest.raises(utools.OperatingSystemVersionNotFound):
utools.best_url("ubuntu", os_ver="10", version="3.0.3")

assert utools.best_url("ubuntu", os_ver="14", version="1.4.4") == "https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1404-4.0.28.tgz", "If major version is not there should find latest major version of MongoDB for Ubuntu 14"
assert utools.best_url("ubuntu", os_ver="14", version="3.8.4") == "https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1404-3.6.22.tgz", "If minor version is not there should find latest minor version of MongoDB for given major version, for Ubuntu 14"
assert utools.best_url("ubuntu", os_ver="14", version="3.4.22") == "https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1404-3.4.24.tgz", "If patch version is not there should find latest patch version of MongoDB for given major.minor version, for Ubuntu 14"
assert utools.best_url("ubuntu", os_ver="14", version="1.4.4") == ("https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1404-4.0.28.tgz", "4.0.28"), "If major version is not there should find latest major version of MongoDB for Ubuntu 14"
assert utools.best_url("ubuntu", os_ver="14", version="3.8.4") == ("https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1404-3.6.22.tgz", "3.6.22"), "If minor version is not there should find latest minor version of MongoDB for given major version, for Ubuntu 14"
assert utools.best_url("ubuntu", os_ver="14", version="3.4.22") == ("https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1404-3.4.24.tgz", "3.4.24"), "If patch version is not there should find latest patch version of MongoDB for given major.minor version, for Ubuntu 14"

0 comments on commit f656d5f

Please sign in to comment.