-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
4/4: Add testing for the new hab install and DistroFinder features
- Loading branch information
1 parent
e0a0ca8
commit 26f3146
Showing
11 changed files
with
558 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}}" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"set": { | ||
"downloads": { | ||
"cache_root": "" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"set": | ||
{ | ||
"distro_paths": | ||
[ | ||
[ | ||
"hab.distro_finders.df_zip:DistroFinderZip", | ||
"{{ zip_root }}" | ||
] | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"set": | ||
{ | ||
"distro_paths": | ||
[ | ||
[ | ||
"hab.distro_finders.zip_sidecar:DistroFinderZipSidecar", | ||
"{{ zip_root }}" | ||
] | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.