Skip to content

Commit

Permalink
avoid using crds config reset - cleanup instead resets to default tes…
Browse files Browse the repository at this point in the history
…t env vars
  • Loading branch information
alphasentaurii committed Apr 17, 2024
1 parent 7a82e65 commit ea320e0
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ concurrency:

env:
CRDS_TEST_ROOT: /tmp
CRDS_PATH: /tmp/crds-cache-default-test
CRDS_TESTING_CACHE: /tmp/crds-cache-test
CRDS_SERVER_URL: https://hst-crds.stsci.edu
CRDS_CLIENT_RETRY_COUNT: 3
CRDS_CLIENT_RETRY_DELAY_SECONDS: 20
Expand Down
60 changes: 46 additions & 14 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ def pytest_addoption(parser):
default=os.environ.get("CRDS_TESTING_CACHE", "no-test-cache-defined-see-TESTING"),
help="Default CRDS Test Cache",
)
parser.addoption(
"--default_cache",
action="store",
dest="default_cache",
default=os.environ.get("CRDS_PATH", os.path.join(os.environ.get("CRDS_TEST_ROOT", "/tmp"), "crds-cache-default-test"))
)


@fixture(scope='session')
Expand All @@ -47,6 +53,11 @@ def test_cache(request):
return request.config.getoption("test_cache")


@fixture(scope='session')
def default_cache(request):
return request.config.getoption("default_cache")


@fixture(scope='session')
def test_mappath(test_cache):
return os.path.join(test_cache, "mappings")
Expand Down Expand Up @@ -143,12 +154,11 @@ def __init__(self, cache=None, url=None, clear_existing=True, observatory=None,

def config_setup(self):
"""Reset the CRDS configuration state to support testing given the supplied parameters."""
# cache=crds_shared_group_cache, url=None, observatory=None, clear_existing=False
log.set_test_mode()
if self.clear_existing:
self.reset_defaults()
self.old_state = crds_config.get_crds_state()
self.old_state["CRDS_CWD"] = os.getcwd()
if self.clear_existing:
crds_config.clear_crds_state()
self.new_state = dict(self.old_state)
self.new_state["CRDS_CWD"] = HERE
if self.url is not None:
Expand All @@ -167,6 +177,29 @@ def cleanup(self):
crds_config.set_crds_state(self.old_state)
utils.clear_function_caches()

def reset_defaults(self):
"""Generic CRDS environment variables consistent across observatories.
Any kwargs passed into a ConfigState object will override these default values.
Most configuration fixtures use the default `clear_existing=True` kwarg, which invokes
this function to initialize default values into the 'old_state' attribute. When cleanup
is run, this 'old_state' is re-instantiated as the configuration."""
self.default_config = dict(
CRDS_PATH=os.environ.get("CRDS_PATH", "tmp/crds-cache-default-test"),
CRDS_CONTEXT=os.environ.get("CRDS_CONTEXT"),
CRDS_TEST_ROOT=os.environ.get("CRDS_TEST_ROOT"),
CRDS_TESTING_CACHE=os.environ.get("CRDS_TESTING_CACHE"),
CRDS_CONFIG_OFFSITE='1',
CRDS_READONLY_CACHE='0',
CRDS_REF_SUBDIR_MODE='None',
_CRDS_CACHE_READONLY=False,
PASS_INVALID_VALUES=False,
CRDS_VERBOSITY=0,
CRDS_MODE='auto',
CRDS_CLIENT_RETRY_COUNT='3',
CRDS_CLIENT_RETRY_DELAY_SECONDS='20',
)
crds_config.set_crds_state(self.default_config)


@fixture(scope='function')
def default_shared_state(crds_shared_group_cache):
Expand All @@ -177,8 +210,8 @@ def default_shared_state(crds_shared_group_cache):


@fixture(scope='function')
def hst_shared_cache_state(crds_shared_group_cache):
cfg = ConfigState(cache=crds_shared_group_cache, url="https://hst-crds.stsci.edu", observatory="hst")
def hst_shared_cache_state(default_cache):
cfg = ConfigState(cache=default_cache, url="https://hst-crds.stsci.edu", observatory="hst")
cfg.config_setup()
yield cfg
cfg.cleanup()
Expand All @@ -198,7 +231,6 @@ def hst_temp_cache_state(test_temp_dir):

@fixture(scope='function')
def jwst_no_cache_state():
#os.environ["CRDS_MAPPATH_SINGLE"] = test_data
cfg = ConfigState(
cache=None,
url="https://jwst-crds.stsci.edu",
Expand All @@ -210,9 +242,9 @@ def jwst_no_cache_state():


@fixture(scope='function')
def jwst_shared_cache_state(crds_shared_group_cache):
def jwst_shared_cache_state(default_cache):
cfg = ConfigState(
cache=crds_shared_group_cache,
cache=default_cache,
url="https://jwst-crds.stsci.edu",
observatory="jwst")
cfg.config_setup()
Expand All @@ -221,9 +253,9 @@ def jwst_shared_cache_state(crds_shared_group_cache):


@fixture(scope='function')
def jwst_serverless_state(crds_shared_group_cache):
def jwst_serverless_state(default_cache):
cfg = ConfigState(
cache=crds_shared_group_cache,
cache=default_cache,
url="https://jwst-crds-serverless.stsci.edu",
observatory="jwst"
)
Expand All @@ -233,9 +265,9 @@ def jwst_serverless_state(crds_shared_group_cache):


@fixture(scope='function')
def hst_serverless_state(crds_shared_group_cache):
def hst_serverless_state(default_cache):
cfg = ConfigState(
cache=crds_shared_group_cache,
cache=default_cache,
url="https://hst-serverless-mode.stsci.edu",
observatory="hst"
)
Expand All @@ -257,9 +289,9 @@ def hst_persistent_state(test_cache):


@fixture(scope='function')
def roman_serverless_state(crds_shared_group_cache):
def roman_serverless_state(default_cache):
cfg = ConfigState(
cache=crds_shared_group_cache,
cache=default_cache,
url="https://roman-crds-serverless.stsci.edu",
observatory="roman"
)
Expand Down
52 changes: 36 additions & 16 deletions test/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
log.THE_LOGGER.logger.propagate=True


@mark.hst
@mark.list
def test_list_hst_mappings(capsys, default_shared_state):
ListScript("crds.list --mappings --contexts hst.pmap")()
Expand Down Expand Up @@ -129,6 +130,7 @@ def test_list_hst_mappings(capsys, default_shared_state):
assert out_to_check in out


@mark.jwst
@mark.list
def test_list_jwst_mappings(capsys, jwst_shared_cache_state):
ListScript("crds.list --mappings --contexts jwst.pmap")()
Expand All @@ -144,18 +146,18 @@ def test_list_jwst_mappings(capsys, jwst_shared_cache_state):
assert out_to_check in out


@mark.hst
@mark.list
def test_list_cached_mappings(capsys, default_shared_state):
# Reducing the large output since otherwise the output is hundreds of lines.
ListScript("crds.list --cached-mappings --full-path")()
out, _ = capsys.readouterr()
out_to_check1 = '/mappings/hst/hst.pmap'
out_to_check2 = '/mappings/hst/hst_0001.pmap'
out_to_check3 = '/mappings/hst/hst_0002.pmap'
assert out_to_check1 in out, out_to_check2 in out
assert out_to_check3 in out
expected_files = ['/mappings/hst/hst.pmap', '/mappings/hst/hst_0001.pmap', '/mappings/hst/hst_0002.pmap']
for f in expected_files:
assert f in out


@mark.hst
@mark.list
def test_list_references(capsys, default_shared_state):
# Same as with list_cached_mappings.
Expand All @@ -166,25 +168,29 @@ def test_list_references(capsys, default_shared_state):
assert check in out


@mark.hst
@mark.list
def test_list_cached_references_hst(capsys, default_shared_state):
def test_list_cached_references_hst(capsys, hst_shared_cache_state):
base_path = f'{hst_shared_cache_state.cache}/references/hst'
ListScript("crds.list --cached-references --full-path")()
out, _ = capsys.readouterr()
checks = ['/references/hst/41g16069m_tmg.fits', '/references/hst/y951738kl_hv.fits', '/references/hst/yas2005el_hv.fits']
for check in checks:
assert check in out
expected_files = ['41g16069m_tmg.fits', 'y951738kl_hv.fits', 'yas2005el_hv.fits']
for f in expected_files:
assert f'{base_path}/{f}' in out


@mark.jwst
@mark.list
def test_list_cached_references_jwst(capsys, jwst_shared_cache_state):
cache = jwst_shared_cache_state.cache
base_path = f'{jwst_shared_cache_state.cache}/references/jwst'
ListScript("crds.list --cached-references --full-path")()
out, _ = capsys.readouterr()
checks = [f'{cache}/references/jwst/jwst_miri_flat_0006.fits', f'{cache}/references/jwst/jwst_niriss_flat_0000.fits']
for check in checks:
assert check in out
expected_files = ['jwst_miri_flat_0006.fits', 'jwst_niriss_flat_0000.fits']
for f in expected_files:
assert f'{base_path}/{f}' in out


@mark.hst
@mark.list
def test_list_dataset_ids(capsys, hst_data, default_shared_state):
ListScript("crds.list --dataset-ids acs --hst")()
Expand All @@ -196,8 +202,9 @@ def test_list_dataset_ids(capsys, hst_data, default_shared_state):
assert out_to_check_acs in out


@mark.hst
@mark.list
def test_list_dataset_headers(capsys, default_shared_state):
def test_list_dataset_headers(capsys, hst_shared_cache_state):
ListScript("crds.list --dataset-headers U20L0U01T:U20L0U01T --minimize-header --contexts hst.pmap --hst")()
out, _ = capsys.readouterr()
out_to_check = """Dataset pars for 'U20L0U01T:U20L0U01T' with respect to 'hst.pmap':
Expand All @@ -219,6 +226,7 @@ def test_list_dataset_headers(capsys, default_shared_state):
assert out_to_check in out


@mark.hst
@mark.list
def test_list_dataset_headers_json(capsys, default_shared_state):
ListScript("crds.list --dataset-headers U20L0U01T:U20L0U01T --contexts hst.pmap --json --hst")()
Expand All @@ -233,7 +241,7 @@ def test_list_dataset_headers_json(capsys, default_shared_state):
assert out_to_check in out


# At the moment, this test prints an error as expected. Not sure how to capture it without it getting logged though.
@mark.hst
@mark.list
def test_list_dataset_headers_bogus(default_shared_state, caplog):
ListScript("crds.list --dataset-headers BAR:BAR --contexts hst.pmap --hst")()
Expand All @@ -243,6 +251,7 @@ def test_list_dataset_headers_bogus(default_shared_state, caplog):
assert out_to_check in out


@mark.hst
@mark.list
def test_list_dataset_headers_id_expansions_only(capsys, default_shared_state):
ListScript("crds.list --dataset-headers I9ZF01010 --id-expansions-only --contexts hst.pmap --hst")()
Expand All @@ -254,8 +263,9 @@ def test_list_dataset_headers_id_expansions_only(capsys, default_shared_state):
assert out_to_check in out


@mark.hst
@mark.list
def test_list_required_parkeys_pmap(capsys, default_shared_state):
def test_list_required_parkeys_pmap(capsys, hst_shared_cache_state):
ListScript("crds.list --required-parkeys --contexts hst.pmap --hst")()
out, _ = capsys.readouterr()
out_to_check = """--------------------- Parkeys required for 'hst.pmap' ---------------------
Expand All @@ -268,6 +278,7 @@ def test_list_required_parkeys_pmap(capsys, default_shared_state):
assert out_to_check in out


@mark.hst
@mark.list
def test_list_required_parkeys_imap(capsys, default_shared_state):
ListScript("crds.list --required-parkeys --contexts hst_acs.imap --hst")()
Expand Down Expand Up @@ -296,6 +307,7 @@ def test_list_required_parkeys_imap(capsys, default_shared_state):
assert out_to_check in out


@mark.hst
@mark.list
def test_list_required_parkeys_rmap(capsys, default_shared_state):
ListScript("crds.list --required-parkeys --contexts hst_acs_darkfile.rmap --hst")()
Expand All @@ -304,6 +316,7 @@ def test_list_required_parkeys_rmap(capsys, default_shared_state):
assert out_to_check in out


@mark.hst
@mark.list
def test_list_resolve_contexts_range(capsys, default_shared_state):
ListScript("crds.list --resolve-contexts --range 0:5 --hst")()
Expand All @@ -316,6 +329,7 @@ def test_list_resolve_contexts_range(capsys, default_shared_state):
assert out_to_check in out


@mark.hst
@mark.list
def test_list_resolve_contexts_date(capsys, default_shared_state):
ListScript("crds.list --resolve-contexts --contexts hst-2014-11-11T00:00:00 --hst")()
Expand All @@ -324,6 +338,7 @@ def test_list_resolve_contexts_date(capsys, default_shared_state):
assert out_to_check in out


@mark.hst
@mark.list
def test_list_remote_context(capsys, default_shared_state):
ListScript("crds.list --remote-context hst-ops-pipeline --hst")()
Expand All @@ -332,6 +347,7 @@ def test_list_remote_context(capsys, default_shared_state):
assert '.pmap' in out


@mark.hst
@mark.list
def test_list_operational_context(capsys, default_shared_state):
ListScript("crds.list --operational-context --hst")()
Expand All @@ -340,6 +356,7 @@ def test_list_operational_context(capsys, default_shared_state):
assert '.pmap' in out


@mark.hst
@mark.list
def test_list_references_by_context(capsys, default_shared_state):
ListScript("crds.list --references --contexts hst-cos-deadtab-2014-11-11T00:00:00 --hst")()
Expand All @@ -349,6 +366,7 @@ def test_list_references_by_context(capsys, default_shared_state):
assert out_to_check in out


@mark.hst
@mark.list
def test_list_cat_mappings(capsys, default_shared_state):
ListScript("crds.list --cat --mappings --contexts hst-cos-deadtab-2014-11-11T00:00:00 --hst")()
Expand Down Expand Up @@ -407,6 +425,7 @@ def test_list_cat_mappings(capsys, default_shared_state):
assert out_to_check4 in out


@mark.hst
@mark.list
def test_list_status(capsys, default_shared_state):
ListScript("crds.list --status --hst")()
Expand All @@ -424,6 +443,7 @@ def test_list_status(capsys, default_shared_state):
assert "Readonly Cache = False" in out


@mark.hst
@mark.list
def test_list_config(capsys, default_shared_state):
ListScript("crds.list --config --hst")()
Expand Down
Loading

0 comments on commit ea320e0

Please sign in to comment.