diff --git a/hab/utils.py b/hab/utils.py index 8969d79..66f2404 100644 --- a/hab/utils.py +++ b/hab/utils.py @@ -6,6 +6,7 @@ import os import re import sys +import tempfile import textwrap import zlib from abc import ABC, abstractmethod @@ -542,6 +543,14 @@ def default_ext(cls): """Returns the default file extension used on this platform.""" return cls._default_ext + @classmethod + def default_download_cache(cls): + """Path where download files are cached. + + This is used as the default location for `Site.downloads["cache_root"]`. + """ + return Path(tempfile.gettempdir()) / "hab_downloads" + @classmethod def expand_paths(cls, paths): """Converts path strings separated by ``cls.pathsep()`` and lists into diff --git a/tests/conftest.py b/tests/conftest.py index dab4f29..acdb026 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,9 +1,13 @@ import json import os +import shutil +from collections import namedtuple from contextlib import contextmanager from pathlib import Path, PurePath +from zipfile import ZipFile import pytest +from jinja2 import Environment, FileSystemLoader from packaging.requirements import Requirement from hab import Resolver, Site @@ -111,6 +115,93 @@ def resolver(request): return request.getfixturevalue(test_map[request.param]) +Distro = namedtuple("Distro", ["name", "version", "inc_version", "distros"]) + + +class DistroInfo(namedtuple("DistroInfo", ["root", "versions"])): + default_versions = ( + ("dist_a", "0.1", True, None), + ("dist_a", "0.2", False, ["dist_b"]), + ("dist_a", "1.0", False, None), + ("dist_b", "0.5", False, None), + ("dist_b", "0.6", False, None), + ) + + @classmethod + def dist_version(cls, distro, version): + return f"{distro}_v{version}" + + @classmethod + def hab_json(cls, distro, version=None, distros=None): + data = {"name": distro} + if version: + data["version"] = version + if distros: + data["distros"] = distros + return json.dumps(data, indent=4) + + @classmethod + def generate(cls, root, versions=None, zip_created=None): + if versions is None: + versions = cls.default_versions + + versions = {(x[0], x[1]): Distro(*x) for x in versions} + + for version in versions.values(): + name = cls.dist_version(version.name, version.version) + filename = root / f"{name}.zip" + ver = version.version if version.inc_version else None + with ZipFile(filename, "w") as zf: + zf.writestr( + ".hab.json", + cls.hab_json(version.name, version=ver, distros=version.distros), + ) + zf.writestr("file_a.txt", "File A inside the distro.") + zf.writestr("folder/file_b.txt", "File B inside the distro.") + if zip_created: + zip_created(zf) + + # Create a correctly named .zip file that doesn't have a .hab.json file + # to test for .zip files that are not distros. + with ZipFile(root / "not_valid_v0.1.zip", "w") as zf: + zf.writestr("README.txt", "This file is not a hab distro zip.") + + return cls(root, versions) + + +@pytest.fixture(scope="session") +def zip_distro(tmp_path_factory): + """Returns a DistroInfo instance for a zip folder structure. + + This is useful if the zip files are locally accessible or if your hab download + server supports `HTTP range requests`_. For example if you are using Amazon S3. + + .. _HTTP range requests: + https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests + """ + root = tmp_path_factory.mktemp("_zip_distro") + return DistroInfo.generate(root) + + +@pytest.fixture(scope="session") +def zip_distro_sidecar(tmp_path_factory): + """Returns a DistroInfo instance for a zip folder structure with sidecar + `.hab.json` files. + + This is useful when your hab download server does not support HTTP range requests. + """ + root = tmp_path_factory.mktemp("_zip_distro_sidecar") + + def zip_created(zf): + """Extract the .hab.json from the zip to a sidecar file.""" + filename = Path(zf.filename).stem + sidecar = root / f"{filename}.hab.json" + path = zf.extract(".hab.json", root) + shutil.move(path, sidecar) + + return DistroInfo.generate(root, zip_created=zip_created) + + class Helpers(object): """A collection of reusable functions that tests can use.""" @@ -204,6 +295,19 @@ def compare_files(generated, check): cache[i] == check[i] ), f"Difference on line: {i} between the generated cache and {generated}." + @staticmethod + def render_template(template, dest, **kwargs): + environment = Environment( + loader=FileSystemLoader(str(Path(__file__).parent / "templates")), + trim_blocks=True, + lstrip_blocks=True, + ) + template = environment.get_template(template) + + text = template.render(**kwargs).rstrip() + "\n" + with dest.open("w") as fle: + fle.write(text) + @pytest.fixture def helpers(): diff --git a/tests/site/site_distro_finder.json b/tests/site/site_distro_finder.json new file mode 100644 index 0000000..414caee --- /dev/null +++ b/tests/site/site_distro_finder.json @@ -0,0 +1,32 @@ +{ + "set": + { + "distro_paths": + [ + [ + "hab.distro_finders.distro_finder:DistroFinder", + "hab testable/download/path" + ], + [ + "hab.distro_finders.distro_finder:DistroFinder", + "hab testing/downloads", + { + "site": "for testing only, do not specify site" + } + ] + ], + "downloads": + { + "cache_root": "hab testable/download/path", + "distros": + [ + [ + "hab.distro_finders.df_zip:DistroFinderZip", + "network_server/distro/source" + ] + ], + "install_root": "{relative_root}/distros", + "relative_path": "{{distro_name}}_v{{version}}" + } + } +} diff --git a/tests/site/site_distro_finder_empty.json b/tests/site/site_distro_finder_empty.json new file mode 100644 index 0000000..240abbe --- /dev/null +++ b/tests/site/site_distro_finder_empty.json @@ -0,0 +1,7 @@ +{ + "set": { + "downloads": { + "cache_root": "" + } + } +} diff --git a/tests/templates/site_distro_zip.json b/tests/templates/site_distro_zip.json new file mode 100644 index 0000000..e7c45ee --- /dev/null +++ b/tests/templates/site_distro_zip.json @@ -0,0 +1,12 @@ +{ + "set": + { + "distro_paths": + [ + [ + "hab.distro_finders.df_zip:DistroFinderZip", + "{{ zip_root }}" + ] + ] + } +} diff --git a/tests/templates/site_distro_zip_sidecar.json b/tests/templates/site_distro_zip_sidecar.json new file mode 100644 index 0000000..953805f --- /dev/null +++ b/tests/templates/site_distro_zip_sidecar.json @@ -0,0 +1,12 @@ +{ + "set": + { + "distro_paths": + [ + [ + "hab.distro_finders.zip_sidecar:DistroFinderZipSidecar", + "{{ zip_root }}" + ] + ] + } +} diff --git a/tests/templates/site_download.json b/tests/templates/site_download.json new file mode 100644 index 0000000..b4fd30e --- /dev/null +++ b/tests/templates/site_download.json @@ -0,0 +1,20 @@ +{ + "set": { + "config_paths": [ + "{relative_root}/configs" + ], + "distro_paths": [ + "{relative_root}/distros/*" + ], + "downloads": { + "cache_root": "{relative_root}/downloads", + "distros": [ + [ + "hab.distro_finders.df_zip:DistroFinderZip", + "{{ zip_root }}" + ] + ], + "install_root": "{relative_root}/distros" + } + } +} diff --git a/tests/test_distro_finder.py b/tests/test_distro_finder.py new file mode 100644 index 0000000..ccd8634 --- /dev/null +++ b/tests/test_distro_finder.py @@ -0,0 +1,144 @@ +import glob +from pathlib import Path + +import pytest + +from hab import Resolver, Site, utils +from hab.distro_finders import df_zip, distro_finder, zip_sidecar +from hab.parsers import DistroVersion + + +def test_distro_finder_entry_point(config_root): + """Test edge cases for DistroFinder entry_point processing.""" + paths = [config_root / "site" / "site_distro_finder.json"] + site = Site(paths) + distro_paths = site["distro_paths"] + # Ensure the DistroFinder paths are set correctly when set as EntryPoint + assert distro_paths[0].root == Path("hab testable") / "download" / "path" + assert distro_paths[1].root == Path("hab testing") / "downloads" + # The second path passes the kwargs dict with `site`. This triggers testing + # when a dict is passed to the entry_point. However site is always set to + # the current site after a DistroFinder is initialized. + assert distro_paths[1].site == site + + +def test_eq(): + a = distro_finder.DistroFinder("path/a") + + assert a == distro_finder.DistroFinder("path/a") + assert a != distro_finder.DistroFinder("path/b") + + # Test that if the glob_str is different it will not compare equal + b = distro_finder.DistroFinder("path/a") + b.glob_str = "*/test.json" + assert a != b + # Test that if glob_str attr is missing it will not compare equal + del b.glob_str + assert a != b + # Restore glob_str and the objects will compare equal again + b.glob_str = "*/.hab.json" + assert a == b + + # Test that if the root is different it will not compare equal + b.root = Path(".") + assert a != b + # Test that if root attr is missing it will not compare equal + del b.root + assert a != b + # Restore root and the objects will compare equal again + b.root = Path("path/a") + assert a == b + + +@pytest.mark.parametrize( + "glob_str,count", + ( + ("{root}/reference*/sh_*", 12), + ("{root}/reference/*", 0), + ("{root}/reference_scripts/*/*.sh", 20), + ), +) +def test_glob_path(config_root, glob_str, count): + """Ensure `hab.utils.glob_path` returns the expected results.""" + glob_str = glob_str.format(root=config_root) + # Check against the `glob.glob` result. + check = sorted([Path(p) for p in glob.glob(glob_str)]) + + path_with_glob = Path(glob_str) + result = sorted(utils.glob_path(path_with_glob)) + + assert result == check + # Sanity check to ensure that the expected results were found by `glob.glob` + assert len(result) == count + + +@pytest.mark.parametrize("distro_info", ("zip_distro", "zip_distro_sidecar")) +def test_zip(request, distro_info, helpers, tmp_path): + # Convert the distro_info parameter to testing values. + df_cls = df_zip.DistroFinderZip + hab_json = ".hab.json" + implements_cache = True + parent_is_zip = True + site_filename = "site_distro_zip.json" + if distro_info == "zip_distro_sidecar": + df_cls = zip_sidecar.DistroFinderZipSidecar + hab_json = "{name}_v{ver}.hab.json" + implements_cache = False + parent_is_zip = False + site_filename = "site_distro_zip_sidecar.json" + distro_info = request.getfixturevalue(distro_info) + + site_file = tmp_path / "site.json" + helpers.render_template( + site_filename, site_file, zip_root=distro_info.root.as_posix() + ) + site_distros = tmp_path / "distros" + + check = set([v[:2] for v in distro_info.versions]) + + site = Site([site_file]) + resolver = Resolver(site) + results = set() + # The correct class was resolved + distro_finder = resolver.distro_paths[0] + assert type(distro_finder) == df_cls + + if implements_cache: + assert distro_finder._cache == {} + + for node in resolver.dump_forest(resolver.distros, attr=None): + distro = node.node + if not isinstance(distro, DistroVersion): + continue + + assert distro.filename.name == hab_json.format( + name=distro.distro_name, ver=distro.version + ) + if parent_is_zip: + # If the parent is a zip, then the parent is a zip file + assert distro.filename.parent.suffix == ".zip" + assert distro.filename.parent.is_file() + else: + # Otherwise there is a sidecar zip file next to the *.hab.json file + zip_filename = distro.filename.name.replace(".hab.json", ".zip") + assert (distro.filename.parent / zip_filename).is_file() + + if implements_cache: + assert distro.filename in distro_finder._cache + + results.add((distro.distro_name, str(distro.version))) + + # Test the install process extracts all of the files from the zip + dest = site_distros / distro.distro_name / str(distro.version) + assert not dest.exists() + distro_finder.install(distro.filename, dest) + assert dest.is_dir() + assert (dest / ".hab.json").exists() + assert (dest / "file_a.txt").exists() + assert (dest / "folder/file_b.txt").exists() + + if implements_cache: + distro_finder.clear_cache() + assert distro_finder._cache == {} + + assert results == check diff --git a/tests/test_parsing.py b/tests/test_parsing.py index 4d81378..6e967cf 100644 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -10,6 +10,7 @@ from packaging.version import Version from hab import NotSet, utils +from hab.distro_finders.distro_finder import DistroFinder from hab.errors import ( DuplicateJsonError, HabError, @@ -20,7 +21,7 @@ from hab.parsers import Config, DistroVersion, FlatConfig -class TestLoadJsonFile: +class TestLoadJson: """Tests various conditions when using `hab.utils.load_json_file` to ensure expected output. """ @@ -34,16 +35,28 @@ def test_missing(self, tmpdir): utils.load_json_file(path) assert Path(excinfo.value.filename) == path + @classmethod + def check_exception(cls, excinfo, native_json, path): + if native_json: + # If built-in json was used, check that filename was appended to the message + assert f'Source("{path}")' in str(excinfo.value) + else: + # If pyjson5 was used, check that the filename was added to str + assert f"'source': {str(path)!r}" in str(excinfo.value) + # Check that the filename was added to the result dict + assert excinfo.value.result["source"] == str(path) + def test_binary(self, tmpdir): """If attempting to read a binary file, filename is included in exception. This is a problem we run into rarely where a text file gets replaced/generated with a binary file containing noting but a lot of null bytes. """ + bin_data = b"\x00" * 32 path = Path(tmpdir) / "binary.json" # Create a binary test file containing multiple binary null values. with path.open("wb") as fle: - fle.write(b"\x00" * 32) + fle.write(bin_data) # Detect if using pyjson5 or not native_json = False @@ -56,15 +69,15 @@ def test_binary(self, tmpdir): else: exc_type = pyjson5.pyjson5.Json5IllegalCharacter + # Test load_json_file with pytest.raises(exc_type) as excinfo: utils.load_json_file(path) + self.check_exception(excinfo, native_json, path) - if native_json: - # If built-in json was used, check that filename was appended to the message - assert f'Filename("{path}")' in str(excinfo.value) - else: - # If pyjson5 was used, check that the filename was added to the result dict - assert f"{{'filename': {str(path)!r}}}" in str(excinfo.value) + # Test loads_json + with pytest.raises(exc_type) as excinfo: + utils.loads_json(bin_data.decode(), path) + self.check_exception(excinfo, native_json, path) def test_config_load(self, uncached_resolver): cfg = Config({}, uncached_resolver) @@ -77,6 +90,18 @@ def test_config_load(self, uncached_resolver): with pytest.raises(FileNotFoundError): cfg.load("invalid_path.json") + def test_loads_json(self, config_root): + """Test that `loads_json` is able to parse a valid json string.""" + filename = config_root / "site_main.json" + with filename.open() as fle: + text = fle.read() + # Test an existing file is able to be parsed successfully. + data = utils.loads_json(text, filename) + # Spot check that we were able to parse data from the file. + assert isinstance(data, dict) + assert "append" in data + assert "set" in data + def test_distro_parse(config_root, resolver): """Check that a distro json can be parsed correctly""" @@ -291,6 +316,7 @@ def test_metaclass(): "environment", "environment_config", "filename", + "finder", "min_verbosity", "name", "optional_distros", @@ -686,13 +712,7 @@ def test_invalid_config(config_root, resolver): with pytest.raises(_JsonException) as excinfo: Config({}, resolver, filename=path) - - if native_json: - # If built-in json was used, check that filename was appended to the message - assert f'Filename("{path}")' in str(excinfo.value) - else: - # If pyjson5 was used, check that the filename was added to the result dict - assert excinfo.value.result["filename"] == str(path) + TestLoadJson.check_exception(excinfo, native_json, path) def test_misc_coverage(resolver): @@ -761,10 +781,13 @@ def test_duplicated_distros(config_root, resolver): definitions are in the same config_path so a DuplicateJsonError is raised. """ original = resolver.distro_paths + site = resolver.site # Check that the first config in distro_paths was used distro_paths = list(original) - distro_paths.insert(0, config_root / "duplicates" / "distros_1" / "*") + distro_paths.insert( + 0, DistroFinder(config_root / "duplicates" / "distros_1" / "*", site=site) + ) resolver.distro_paths = distro_paths dcc = resolver.find_distro("the_dcc==1.2") @@ -774,7 +797,9 @@ def test_duplicated_distros(config_root, resolver): # Check that an exception is raised if there are duplicate definitions from # the same distro_paths directory. distro_paths = list(original) - distro_paths.insert(0, config_root / "duplicates" / "distros_2" / "*") + distro_paths.insert( + 0, DistroFinder(config_root / "duplicates" / "distros_2" / "*", site=site) + ) resolver.distro_paths = distro_paths with pytest.raises(DuplicateJsonError): @@ -1172,7 +1197,7 @@ def test_reserved(self, uncached_resolver, variables, invalid, tmpdir): # Add the test distro to hab's distro search. We don't need to call # `clear_caches` because distros haven't been resolved yet. - uncached_resolver.distro_paths.append(Path(tmpdir)) + uncached_resolver.distro_paths.append(DistroFinder(Path(tmpdir))) # When distros are resolved, an exception should be raised with pytest.raises( diff --git a/tests/test_resolver.py b/tests/test_resolver.py index 4a073b4..18bd1b4 100644 --- a/tests/test_resolver.py +++ b/tests/test_resolver.py @@ -3,13 +3,16 @@ import sys from collections import OrderedDict from pathlib import Path +from zipfile import ZipFile import anytree import pytest from packaging.requirements import Requirement -from hab import NotSet, Resolver, Site, utils +from hab import DistroMode, NotSet, Resolver, Site, utils +from hab.distro_finders.distro_finder import DistroFinder from hab.errors import InvalidRequirementError +from hab.parsers import DistroVersion from hab.solvers import Solver @@ -17,12 +20,14 @@ def test_environment_variables(config_root, helpers, monkeypatch): """Check that Resolver's init respects the environment variables it uses.""" config_paths_env = utils.Platform.expand_paths(["a/config/path", "b/config/path"]) distro_paths_env = utils.Platform.expand_paths(["a/distro/path", "b/distro/path"]) + distro_paths_env = [DistroFinder(p) for p in distro_paths_env] config_paths_direct = utils.Platform.expand_paths( ["z/config/path", "zz/config/path"] ) distro_paths_direct = utils.Platform.expand_paths( ["z/distro/path", "zz/distro/path"] ) + distro_paths_direct = [DistroFinder(p) for p in distro_paths_direct] # Set the config environment variables monkeypatch.setenv( @@ -116,6 +121,52 @@ def test_closest_config(resolver, path, result, reason): assert resolver.closest_config(path).fullpath == result, reason +def test_distro_mode(zip_distro, helpers, tmp_path): + """Test `Resolver.distro_mode` is respected when calling `distros`. + + Also test that the `distro_mode_override` with context updates and restores + the distro_mode. + """ + site_file = tmp_path / "site.json" + helpers.render_template( + "site_download.json", site_file, zip_root=zip_distro.root.as_posix() + ) + resolver = Resolver(Site([site_file])) + + # Install some distros so the resolver can find them + for distro, version in (("dist_a", "0.1"), ("dist_b", "0.5")): + with ZipFile(zip_distro.root / f"{distro}_v{version}.zip") as zip_info: + zip_info.extractall(tmp_path / "distros" / distro / version) + + def get_dist_names(): + return [ + row.node.name + for row in resolver.dump_forest(resolver.distros, attr=None) + if isinstance(row.node, DistroVersion) + ] + + # Get the installed distros and check that `.distros` is the correct return + installed = get_dist_names() + assert resolver.distros is resolver._installed_distros + # Get the download distros and check that `.distros` is the correct return + with resolver.distro_mode_override(DistroMode.Downloaded): + downloads = get_dist_names() + assert resolver.distros is resolver._downloadable_distros + # Check that the installed distros are accessible again + installed_after = get_dist_names() + assert resolver.distros is resolver._installed_distros + + assert installed == ["dist_a==0.1", "dist_b==0.5"] + assert installed_after == installed + assert downloads == [ + "dist_a==0.1", + "dist_a==0.2", + "dist_a==1.0", + "dist_b==0.5", + "dist_b==0.6", + ] + + class TestDumpForest: """Test the dump_forest method on resolver""" @@ -958,19 +1009,19 @@ def test_clear_caches(resolver): """Test that Resolver.clear_cache works as expected.""" # Resolver cache is empty assert resolver._configs is None - assert resolver._distros is None + assert resolver._installed_distros is None # Populate resolver cache data resolver.resolve("not_set") assert isinstance(resolver._configs, dict) - assert isinstance(resolver._distros, dict) + assert isinstance(resolver._installed_distros, dict) assert len(resolver._configs) > 1 - assert len(resolver._distros) > 1 + assert len(resolver._installed_distros) > 1 # Calling clear_caches resets the resolver cache resolver.clear_caches() assert resolver._configs is None - assert resolver._distros is None + assert resolver._installed_distros is None def test_clear_caches_cached(habcached_resolver): diff --git a/tests/test_site.py b/tests/test_site.py index b60d3b8..ee2a83f 100644 --- a/tests/test_site.py +++ b/tests/test_site.py @@ -1,4 +1,5 @@ import sys +import tempfile from pathlib import Path, PurePosixPath, PureWindowsPath import pytest @@ -6,6 +7,7 @@ from hab import Resolver, Site, utils from hab.cache import Cache +from hab.distro_finders.distro_finder import DistroFinder def test_environment_variables(config_root, monkeypatch): @@ -206,7 +208,7 @@ def test_paths(self, config_root, helpers): ) assert len(site.get("distro_paths")) == 2 helpers.check_path_list( - site.get("distro_paths"), + [p.root for p in site.get("distro_paths")], ( config_root / "distros" / "*", config_root / "duplicates" / "distros_1" / "*", @@ -228,7 +230,7 @@ def test_paths_reversed(self, config_root, helpers): ) assert len(site.get("distro_paths")) == 2 helpers.check_path_list( - site.get("distro_paths"), + [p.root for p in site.get("distro_paths")], ( config_root / "duplicates" / "distros_1" / "*", config_root / "distros" / "*", @@ -289,8 +291,8 @@ def test_dump_cached(config_root, habcached_site_file): f" {other_site}", f"{{green}}config_paths: {{reset}}config\\path\\{platform}", f" {config_root}\\configs\\*{{cached}}", - f"{{green}}distro_paths: {{reset}}distro\\path\\{platform}", - f" {config_root}\\distros\\*{{cached}}", + f"{{green}}distro_paths: {{reset}}distro\\path\\{platform}{{cls_name}}", + f" {config_root}\\distros\\*{{cls_name}}{{cached}}", ) check_template = "\n".join(check_template) colors = { @@ -304,13 +306,22 @@ def test_dump_cached(config_root, habcached_site_file): # No verbosity, should not show cached status assert site.get("colorize") is None result = site.dump(width=60) - check = check_template.format(cached="", **colors) + check = check_template.format(cached="", cls_name="", **colors) assert check in result # verbosity enabled, should show cached status result = site.dump(verbosity=1, width=60) check = check_template.format( - cached=f" {Fore.YELLOW}(cached){Style.RESET_ALL}", **colors + cached=f" {Fore.YELLOW}(cached){Style.RESET_ALL}", cls_name="", **colors + ) + assert check in result + + # verbosity level 2, should also show DistroFinder classes + result = site.dump(verbosity=2, width=60) + check = check_template.format( + cached=f" {Fore.YELLOW}(cached){Style.RESET_ALL}", + cls_name=f" {Fore.CYAN}[DistroFinder]{Style.RESET_ALL}", + **colors, ) assert check in result @@ -320,12 +331,19 @@ def test_dump_cached(config_root, habcached_site_file): # No verbosity, should not show cached status result = site.dump(width=60) - check = check_template.format(cached="", green="", reset="") + check = check_template.format(cached="", green="", reset="", cls_name="") assert check in result # verbosity enabled, should show cached status result = site.dump(verbosity=1, width=60) - check = check_template.format(cached=" (cached)", green="", reset="") + check = check_template.format(cached=" (cached)", green="", reset="", cls_name="") + assert check in result + + # verbosity level 2, should also show DistroFinder classes + result = site.dump(verbosity=2, width=60) + check = check_template.format( + cached=" (cached)", green="", reset="", cls_name=" [DistroFinder]" + ) assert check in result @@ -340,7 +358,7 @@ def test_linux(self, monkeypatch, config_root): site = Site(paths) assert site.get("config_paths") == [Path("config/path/linux")] - assert site.get("distro_paths") == [Path("distro/path/linux")] + assert site.get("distro_paths") == [DistroFinder(Path("distro/path/linux"))] assert site.get("platforms") == ["windows", "linux"] def test_osx(self, monkeypatch, config_root): @@ -353,7 +371,7 @@ def test_osx(self, monkeypatch, config_root): site = Site(paths) assert site.get("config_paths") == [Path("config/path/osx")] - assert site.get("distro_paths") == [Path("distro/path/osx")] + assert site.get("distro_paths") == [DistroFinder(Path("distro/path/osx"))] assert site.get("platforms") == ["osx", "linux"] def test_win(self, monkeypatch, config_root): @@ -366,7 +384,7 @@ def test_win(self, monkeypatch, config_root): site = Site(paths) assert site.get("config_paths") == [Path("config\\path\\windows")] - assert site.get("distro_paths") == [Path("distro\\path\\windows")] + assert site.get("distro_paths") == [DistroFinder(Path("distro\\path\\windows"))] assert site.get("platforms") == ["windows", "osx"] @@ -756,3 +774,93 @@ def test_habcache_cls(self, config_root, uncached_resolver): match="hab_test_entry_points.CacheVX class was used", ): Site([config_root / "site" / "eps" / "site_habcache_cls.json"]) + + def test_entry_point_init(self, config_root): + site = Site([config_root / "site_main.json"]) + instance = site.entry_point_init( + "group.name", + "hab.distro_finders.distro_finder:DistroFinder", + ["a/root/path", {"site": "a Site Instance"}], + ) + # The entry_point class was imported and initialized + assert isinstance(instance, DistroFinder) + # The instance had the requested arguments passed to it + assert instance.root == Path("a/root/path") + # The last item was a dictionary, that was removed from args and passed + # as kwargs. + # NOTE: you should not pass site using this method. It's being used here + # to test the kwargs feature and ensure the default site setting doesn't + # overwrite site if it was passed as a kwarg. + assert instance.site == "a Site Instance" + + # Don't pass a kwargs dict, it should get site from itself. + instance = site.entry_point_init( + "group.name", + "hab.distro_finders.distro_finder:DistroFinder", + ["b/root/path"], + ) + assert instance.root == Path("b/root/path") + assert instance.site is site + + +class TestDownloads: + # Defaults to `$TEMP/hab_downloads` if not specified + default_cache_root = Path(tempfile.gettempdir()) / "hab_downloads" + + def test_download_cache(self, config_root, uncached_resolver): + """Test how `site.downloads["cache_root"]` is processed.""" + site = uncached_resolver.site + assert site.downloads["cache_root"] == self.default_cache_root + # `Platform.default_download_cache()` returns the expected default value + assert utils.Platform.default_download_cache() == self.default_cache_root + + # If specified, only the first path is used. This is using a non-valid + # relative path for testing, in practice this should be a absolute path. + paths = [config_root / "site" / "site_distro_finder.json"] + site = Site(paths) + assert ( + site.downloads["cache_root"] == Path("hab testable") / "download" / "path" + ) + + # Use the default if site specifies cache_root but its an empty string. + paths = [config_root / "site" / "site_distro_finder_empty.json"] + site = Site(paths) + assert site.downloads["cache_root"] == self.default_cache_root + + def test_lazy(self, config_root): + site = Site([config_root / "site" / "site_distro_finder.json"]) + # Check that downloads is not parsed before the downloads property + # is first called. + assert site._downloads_parsed is False + downloads = site.downloads + assert site._downloads_parsed is True + assert site.downloads is downloads + + def test_default_settings(self, config_root): + """Test the default downloads values if not defined by site files.""" + site = Site([config_root / "site_main.json"]) + downloads = site.downloads + assert len(downloads["distros"]) == 0 + + # cache_root is always defined + assert downloads["cache_root"] == self.default_cache_root + # These are only defined if the json file defines them. + assert "install_root" not in downloads + assert "relative_path" not in downloads + + def test_all_settings_defined(self, config_root): + """Test the resolved downloads values defined by a site file.""" + from hab.distro_finders.df_zip import DistroFinderZip + + site = Site([config_root / "site" / "site_distro_finder.json"]) + downloads = site.downloads + + # Check that each part of downloads was processed correctly + assert len(downloads["distros"]) == 1 + finder = downloads["distros"][0] + assert isinstance(finder, DistroFinderZip) + assert finder.root == Path("network_server/distro/source") + + assert downloads["cache_root"] == Path("hab testable/download/path") + assert downloads["install_root"] == config_root / "site" / "distros" + assert downloads["relative_path"] == "{distro_name}_v{version}"