Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken metadata error propagation #541

Merged
merged 5 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions strax/storage/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ class FileSytemBackend(strax.StorageBackend):
"""

def get_metadata(self, dirname):
try:
return self._get_metadata(dirname)
except strax.DataCorrupted:
raise
except Exception as e:
raise strax.DataCorrupted(f'Cannot open metadata in {dirname} from {e}')

def _get_metadata(self, dirname):
prefix = dirname_to_prefix(dirname)
metadata_json = f'{prefix}-metadata.json'
md_path = osp.join(dirname, metadata_json)
Expand Down
41 changes: 39 additions & 2 deletions tests/test_saving.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
import strax
from strax.testutils import Records
from strax.testutils import Records, Peaks
import os
import shutil
import tempfile
Expand All @@ -12,7 +12,8 @@ def setUp(self):
self.test_run_id = '0'
self.target = 'records'
self.path = os.path.join(tempfile.gettempdir(), 'strax_data')
self.st = strax.Context(register=[Records],
self.st = strax.Context(use_per_run_defaults=True,
register=[Records],
storage=[strax.DataDirectory(self.path)])
assert not self.st.is_stored(self.test_run_id, self.target)

Expand Down Expand Up @@ -58,3 +59,39 @@ def set_save_when(self, mode: str):
raise ValueError(f'No such saving mode {mode}')
save_mode = getattr(strax.SaveWhen, mode.upper())
self.st._plugin_class_registry[self.target].save_when = save_mode

def test_raise_corruption(self):
self.set_save_when('ALWAYS')
self.st.make(self.test_run_id, self.target)
assert self.is_stored()
storage = self.st.storage[0]
data_key = self.st.key_for(self.test_run_id, self.target)
data_path = os.path.join(storage.path, str(data_key))
assert os.path.exists(data_path)
metadata = storage.backends[0].get_metadata(data_path)
assert isinstance(metadata, dict)

# copied from FileSytemBackend (maybe abstractify the method separately?)
prefix = strax.dirname_to_prefix(data_path)
metadata_json = f'{prefix}-metadata.json'
md_path = os.path.join(data_path, metadata_json)
assert os.path.exists(md_path)

# Corrupt the metadata (making it non-JSON parsable)
md_file = open(md_path, 'a')
# Append 'hello' at the end of file
md_file.write('Adding a non-JSON line to the file to corrupt the metadata')
# Close the file
md_file.close()

# Now we should get an error since the metadata data is corrupted
with self.assertRaises(strax.DataCorrupted):
self.st.get_array(self.test_run_id, self.target)

# Also test the error is raised if be build a target that depends on corrupted data
self.st.register(Peaks)
with self.assertRaises(strax.DataCorrupted):
self.st.get_array(self.test_run_id, 'peaks')

# Cleanup if someone wants to re-use this self.st
del self.st._plugin_class_registry['peaks']