summaryrefslogtreecommitdiffstats
path: root/test/unit/obj/test_diskfile.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/unit/obj/test_diskfile.py')
-rw-r--r--test/unit/obj/test_diskfile.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/test/unit/obj/test_diskfile.py b/test/unit/obj/test_diskfile.py
index 9ef186b..b190526 100644
--- a/test/unit/obj/test_diskfile.py
+++ b/test/unit/obj/test_diskfile.py
@@ -205,6 +205,54 @@ class TestDiskFile(unittest.TestCase):
self.assertRaises(DiskFileNotOpen, gdf.reader)
self.assertRaises(DiskFileNotOpen, gdf.__enter__)
+ def test_read_metadata_optimize_open_close(self):
+ the_path = os.path.join(self.td, "vol0", "bar")
+ the_file = os.path.join(the_path, "z")
+ os.makedirs(the_path)
+ with open(the_file, "wb") as fd:
+ fd.write("1234")
+ init_md = {
+ 'X-Type': 'Object',
+ 'X-Object-Type': 'file',
+ 'Content-Length': 4,
+ 'ETag': md5("1234").hexdigest(),
+ 'X-Timestamp': normalize_timestamp(os.stat(the_file).st_ctime),
+ 'Content-Type': 'application/octet-stream'}
+ _metadata[_mapit(the_file)] = init_md
+ gdf = self._get_diskfile("vol0", "p57", "ufo47", "bar", "z")
+ assert gdf._obj == "z"
+ assert gdf._fd is None
+ assert gdf._disk_file_open is False
+ assert gdf._metadata is None
+ assert not gdf._is_dir
+
+ # Case 1
+ # Ensure that reading metadata for non-GET requests
+ # does not incur opening and closing the file when
+ # metadata is NOT stale.
+ mock_open = Mock()
+ mock_close = Mock()
+ with mock.patch("gluster.swift.obj.diskfile.do_open", mock_open):
+ with mock.patch("gluster.swift.obj.diskfile.do_close", mock_close):
+ md = gdf.read_metadata()
+ self.assertEqual(md, init_md)
+ self.assertFalse(mock_open.called)
+ self.assertFalse(mock_close.called)
+
+ # Case 2
+ # Ensure that reading metadata for non-GET requests
+ # still opens and reads the file when metadata is stale
+ with open(the_file, "a") as fd:
+ # Append to the existing file to make the stored metadata
+ # invalid/stale.
+ fd.write("5678")
+ md = gdf.read_metadata()
+ # Check that the stale metadata is recalculated to account for
+ # change in file content
+ self.assertNotEqual(md, init_md)
+ self.assertEqual(md['Content-Length'], 8)
+ self.assertEqual(md['ETag'], md5("12345678").hexdigest())
+
def test_open_and_close(self):
mock_close = Mock()