Skip to content

Commit

Permalink
Add tests for habcache
Browse files Browse the repository at this point in the history
  • Loading branch information
MHendricks committed Jan 19, 2024
1 parent 5991db6 commit 046c7a5
Show file tree
Hide file tree
Showing 16 changed files with 1,893 additions and 48 deletions.
97 changes: 94 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
from contextlib import contextmanager
from pathlib import Path, PurePath
Expand All @@ -7,19 +8,109 @@

from hab import Resolver, Site

# Testing both cached and uncached every time adds extra testing time. This env
# var can be used to disable cached testing for local testing.
if os.getenv("HAB_TEST_UNCACHED_ONLY", "0") == "1":
resolver_tests = ["uncached"]
else:
resolver_tests = ["uncached", "cached"]

@pytest.fixture

@pytest.fixture(scope="session")
def config_root():
return Path(__file__).parent


def generate_habcached_site_file(config_root, dest):
"""Returns the path to a site config file generated from `site_main.json`
configured so it can have a .habcache file generated next to it. The
config_paths and distro_paths of the site file are hard coded to point to
the repo's tests directory so it uses the same configs/distros. It also adds
a `config-root` entry to `platform_path_maps`.
"""
site_file = Path(dest) / "site.json"
site_src = config_root / "site_main.json"

# Load the site_main.json files contents so we can modify it before saving
# it into the dest for testing.
data = json.load(site_src.open())
append = data["append"]

# Hard code relative_root to the tests folder so it works from
# a random testing directory without copying all configs/distros.
for key in ("config_paths", "distro_paths"):
for i in range(len(append[key])):
append[key][i] = append[key][i].format(relative_root=site_src.parent)

# Add platform_path_maps for the pytest directory to simplify testing and
# test cross-platform support. We need to add all platforms, but this only
# needs to run on the current platform, so add the same path to all.
append["platform_path_maps"]["config-root"] = {
platform: str(config_root) for platform in data["set"]["platforms"]
}

with site_file.open("w") as fle:
json.dump(data, fle, indent=4)

return site_file


@pytest.fixture(scope="session")
def habcached_site_file(config_root, tmp_path_factory):
"""Generates a site.json file and generates its habcache file.
This file is stored in a `_cache` directory in the pytest directory.
This persists for the entire testing session and can be used by other tests
that need to test hab when it is using a habcache.
"""
# Create the site file
shared = tmp_path_factory.mktemp("_cache")
ret = generate_habcached_site_file(config_root, shared)

# Generate the habcache file
site = Site([ret])
resolver = Resolver(site)
site.cache.save_cache(resolver, ret)

return ret


@pytest.fixture
def habcached_resolver(habcached_site_file):
"""Returns a Resolver using a habcache file that was generated for this session.
See the `habcached_site_file` fixture for details on how the cache is setup.
For ease of testing the path to the saved habcache file is stored in
`_test_cache_file` on the returned resolver.
"""
site = Site([habcached_site_file])
resolver = Resolver(site)
# Generate the cache and provide easy access to the habcache file path
resolver._test_cache_file = site.cache.site_cache_path(habcached_site_file)

return resolver


@pytest.fixture
def resolver(config_root):
"""Return a standard testing resolver"""
def uncached_resolver(config_root):
"""Return a standard testing resolver not using any habcache files."""
site = Site([config_root / "site_main.json"])
return Resolver(site=site)


@pytest.fixture(params=resolver_tests)
def resolver(request):
"""Returns a hab.Resolver instance using the site_main.json site config file.
This is a parameterized fixture that returns both cached and uncached versions
of the `site_main.json` site configuration. Note the cached version uses a
copy of it stored in the `_cache0` directory of the pytest temp files. This
should be used for most tests to ensure that all features are tested, but if
the test is not affected by caching you can use `uncached_resolver` instead.
"""
test_map = {"uncached": "uncached_resolver", "cached": "habcached_resolver"}
return request.getfixturevalue(test_map[request.param])


class Helpers(object):
"""A collection of reusable functions that tests can use."""

Expand Down
14 changes: 14 additions & 0 deletions tests/distros/the_dcc/pre/.hab.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "the_dcc",
"environment": {},
"distros": [
"the_dcc_plugin_a>=1.0",
"the_dcc_plugin_b>=0.9",
"the_dcc_plugin_e"
],
"aliases": {
"windows": [
["dcc", "{relative_root}\\the_dcc.exe"]
]
}
}
3 changes: 3 additions & 0 deletions tests/distros/the_dcc/pre/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This directory enables testing that hab ignores distro version folders defined
in the `ignored_distros` key of the site configuration. It's version is pulled
from the parent directory of the .hab.json file, which is named `pre`.
11 changes: 11 additions & 0 deletions tests/hab_test_entry_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,14 @@ def uri_validate_project_b(resolver, uri):
splits[0] = "PROJECT_B"
uri = HabBase.separator.join(splits)
return uri


class CacheVX:
"""Used to validate that the entry_point `hab_habcache_cls` is respected by
raising an exception when initialized.
Note: A real example of this class would subclass `hab.cache.Cache`.
"""

def __init__(self, site):
raise NotImplementedError("hab_test_entry_points.CacheVX class was used")
9 changes: 9 additions & 0 deletions tests/site/eps/site_habcache_cls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"append": {
"entry_points": {
"hab.habcache_cls": {
"cls": "hab_test_entry_points:CacheVX"
}
}
}
}
5 changes: 5 additions & 0 deletions tests/site/site_cache_file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"set": {
"site_cache_file_template": ".{{stem}}.hab_cache"
}
}
Loading

0 comments on commit 046c7a5

Please sign in to comment.