test_ros3.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # This file is part of h5py, a Python interface to the HDF5 library.
  2. #
  3. # http://www.h5py.org
  4. #
  5. # Copyright 2008-2013 Andrew Collette and contributors
  6. #
  7. # License: Standard 3-clause BSD; see "license.txt" for full license terms
  8. # and contributor agreement.
  9. """
  10. Read-only S3 virtual file driver (VFD) test module.
  11. """
  12. import h5py
  13. from h5py._hl.files import make_fapl
  14. import pytest
  15. pytestmark = [
  16. pytest.mark.skipif(
  17. not h5py.h5.get_config().ros3,
  18. reason="ros3 driver not available")
  19. ]
  20. @pytest.mark.network
  21. @pytest.mark.parametrize(
  22. "kwargs",
  23. [
  24. pytest.param(
  25. {},
  26. id="HDF5-v1",
  27. marks=pytest.mark.skipif(
  28. h5py.version.hdf5_version_tuple >= (2, 0, 0),
  29. reason="Requires HDF5 < 2.0",
  30. ),
  31. ),
  32. pytest.param(
  33. {"aws_region": b"us-east-2"},
  34. id="HDF5-v2",
  35. marks=pytest.mark.skipif(
  36. h5py.version.hdf5_version_tuple < (2, 0, 0),
  37. reason="Requires HDF5 >= 2.0",
  38. ),
  39. ),
  40. ],
  41. )
  42. def test_ros3(kwargs):
  43. """ ROS3 driver and options """
  44. with h5py.File("https://dandiarchive.s3.amazonaws.com/ros3test.hdf5", 'r',
  45. driver='ros3', **kwargs) as f:
  46. assert f
  47. assert 'mydataset' in f.keys()
  48. assert f["mydataset"].shape == (100,)
  49. @pytest.mark.parametrize(
  50. "exc,match_exc",
  51. [
  52. pytest.param(
  53. ValueError,
  54. [
  55. "AWS region required for s3:// location",
  56. r"^foo://wrong/scheme: S3 location must begin with",
  57. ],
  58. id="HDF5-v1",
  59. marks=pytest.mark.skipif(
  60. h5py.version.hdf5_version_tuple >= (2, 0, 0),
  61. reason="Requires HDF5 < 2.0",
  62. ),
  63. ),
  64. pytest.param(
  65. OSError,
  66. [None, "can't parse object key from path"],
  67. id="HDF5-v2",
  68. marks=pytest.mark.skipif(
  69. h5py.version.hdf5_version_tuple < (2, 0, 0),
  70. reason="Requires HDF5 >= 2.0",
  71. ),
  72. ),
  73. ],
  74. )
  75. def test_ros3_s3_fails(exc, match_exc):
  76. """ROS3 exceptions for s3:// location"""
  77. with pytest.raises(exc, match=match_exc[0]):
  78. h5py.File('s3://fakebucket/fakekey', 'r', driver='ros3')
  79. with pytest.raises(exc, match=match_exc[1]):
  80. h5py.File('foo://wrong/scheme', 'r', driver='ros3')
  81. @pytest.mark.network
  82. def test_ros3_s3uri():
  83. """Use S3 URI with ROS3 driver"""
  84. with h5py.File('s3://dandiarchive/ros3test.hdf5', 'r', driver='ros3',
  85. aws_region=b'us-east-2') as f:
  86. assert f
  87. assert 'mydataset' in f.keys()
  88. assert f["mydataset"].shape == (100,)
  89. @pytest.mark.skipif(h5py.version.hdf5_version_tuple < (1, 14, 2),
  90. reason='AWS S3 access token support in HDF5 >= 1.14.2')
  91. def test_ros3_temp_token():
  92. """Set and get S3 access token"""
  93. token = b'#0123FakeToken4567/8/9'
  94. fapl = make_fapl('ros3', libver=None, rdcc_nslots=None, rdcc_nbytes=None,
  95. rdcc_w0=None, locking=None, page_buf_size=None, min_meta_keep=None,
  96. min_raw_keep=None, alignment_threshold=1, alignment_interval=1,
  97. meta_block_size=None, session_token=token)
  98. assert token, fapl.get_fapl_ros3_token()