diff --git a/test/unit/obj/test_server.py b/test/unit/obj/test_server.py index ff76e91460..33405267f9 100644 --- a/test/unit/obj/test_server.py +++ b/test/unit/obj/test_server.py @@ -4287,6 +4287,150 @@ class TestObjectController(BaseTestCase): resp = req.get_response(self.object_controller) self.assertEqual(resp.status_int, 404) + def test_GET_keep_cache_private_config_true(self): + # Test swift.obj.server.ObjectController.GET that, when + # 'keep_cache_private' is configured True, then + # disk_file.reader will be called with keep_cache=True. + # Set up a new ObjectController with customized configurations. + conf = {'devices': self.testdir, 'mount_check': 'false', + 'container_update_timeout': 0.0, + 'keep_cache_private': 'True'} + obj_controller = object_server.ObjectController( + conf, logger=self.logger) + obj_controller.bytes_per_sync = 1 + timestamp = normalize_timestamp(time()) + req = Request.blank('/sda1/p/a/c/o', environ={'REQUEST_METHOD': 'PUT'}, + headers={'X-Timestamp': timestamp, + 'Content-Type': 'application/x-test'}) + req.body = b'VERIFY' + resp = req.get_response(obj_controller) + self.assertEqual(resp.status_int, 201) + + # Request headers have neither 'X-Auth-Token' nor 'X-Storage-Token'. + req = Request.blank('/sda1/p/a/c/o', + headers={'Content-Type': 'application/x-test'}) + reader_mock = mock.Mock(keep_cache=False) + with mock.patch('swift.obj.diskfile.BaseDiskFile.reader', reader_mock): + resp = req.get_response(obj_controller) + reader_mock.assert_called_with(keep_cache=True) + self.assertEqual(resp.status_int, 200) + etag = '"%s"' % md5(b'VERIFY', usedforsecurity=False).hexdigest() + self.assertEqual(dict(resp.headers), { + 'Content-Type': 'application/x-test', + 'Content-Length': '6', + 'Etag': etag, + 'X-Backend-Timestamp': timestamp, + 'X-Timestamp': timestamp, + 'X-Backend-Data-Timestamp': timestamp, + 'X-Backend-Durable-Timestamp': timestamp, + 'Last-Modified': strftime( + '%a, %d %b %Y %H:%M:%S GMT', + gmtime(math.ceil(float(timestamp)))), + }) + + # Request headers have 'X-Auth-Token'. + req = Request.blank('/sda1/p/a/c/o', + headers={'Content-Type': 'application/x-test', + 'X-Auth-Token': '2340lsdfhhjl02lxfjj'}) + reader_mock = mock.Mock(keep_cache=False) + with mock.patch('swift.obj.diskfile.BaseDiskFile.reader', reader_mock): + resp = req.get_response(obj_controller) + reader_mock.assert_called_with(keep_cache=True) + self.assertEqual(resp.status_int, 200) + + # Request headers have 'X-Storage-Token'. + req = Request.blank('/sda1/p/a/c/o', + headers={'Content-Type': 'application/x-test', + 'X-Storage-Token': '2340lsdfhhjl02lxfjj'}) + reader_mock = mock.Mock(keep_cache=False) + with mock.patch('swift.obj.diskfile.BaseDiskFile.reader', reader_mock): + resp = req.get_response(obj_controller) + reader_mock.assert_called_with(keep_cache=True) + self.assertEqual(resp.status_int, 200) + + # Request headers have both 'X-Auth-Token' and 'X-Storage-Token'. + req = Request.blank('/sda1/p/a/c/o', + headers={'Content-Type': 'application/x-test', + 'X-Auth-Token': '2340lsdfhhjl02lxfjj', + 'X-Storage-Token': '2340lsdfhhjl02lxfjj'}) + reader_mock = mock.Mock(keep_cache=False) + with mock.patch('swift.obj.diskfile.BaseDiskFile.reader', reader_mock): + resp = req.get_response(obj_controller) + reader_mock.assert_called_with(keep_cache=True) + self.assertEqual(resp.status_int, 200) + + def test_GET_keep_cache_private_config_false(self): + # Test swift.obj.server.ObjectController.GET that, when + # 'keep_cache_private' is configured false, then + # disk_file.reader will be called with correct 'keep_cache'. + # Set up a new ObjectController with customized configurations. + conf = {'devices': self.testdir, 'mount_check': 'false', + 'container_update_timeout': 0.0, + 'keep_cache_private': 'false'} + obj_controller = object_server.ObjectController( + conf, logger=self.logger) + obj_controller.bytes_per_sync = 1 + timestamp = normalize_timestamp(time()) + req = Request.blank('/sda1/p/a/c/o', environ={'REQUEST_METHOD': 'PUT'}, + headers={'X-Timestamp': timestamp, + 'Content-Type': 'application/x-test'}) + req.body = b'VERIFY' + resp = req.get_response(obj_controller) + self.assertEqual(resp.status_int, 201) + + # Request headers have neither 'X-Auth-Token' nor 'X-Storage-Token'. + req = Request.blank('/sda1/p/a/c/o', + headers={'Content-Type': 'application/x-test'}) + reader_mock = mock.Mock(keep_cache=False) + with mock.patch('swift.obj.diskfile.BaseDiskFile.reader', reader_mock): + resp = req.get_response(obj_controller) + reader_mock.assert_called_with(keep_cache=True) + self.assertEqual(resp.status_int, 200) + etag = '"%s"' % md5(b'VERIFY', usedforsecurity=False).hexdigest() + self.assertEqual(dict(resp.headers), { + 'Content-Type': 'application/x-test', + 'Content-Length': '6', + 'Etag': etag, + 'X-Backend-Timestamp': timestamp, + 'X-Timestamp': timestamp, + 'X-Backend-Data-Timestamp': timestamp, + 'X-Backend-Durable-Timestamp': timestamp, + 'Last-Modified': strftime( + '%a, %d %b %Y %H:%M:%S GMT', + gmtime(math.ceil(float(timestamp)))), + }) + + # Request headers have 'X-Auth-Token'. + req = Request.blank('/sda1/p/a/c/o', + headers={'Content-Type': 'application/x-test', + 'X-Auth-Token': '2340lsdfhhjl02lxfjj'}) + reader_mock = mock.Mock(keep_cache=False) + with mock.patch('swift.obj.diskfile.BaseDiskFile.reader', reader_mock): + resp = req.get_response(obj_controller) + reader_mock.assert_called_with(keep_cache=False) + self.assertEqual(resp.status_int, 200) + + # Request headers have 'X-Storage-Token'. + req = Request.blank('/sda1/p/a/c/o', + headers={'Content-Type': 'application/x-test', + 'X-Storage-Token': '2340lsdfhhjl02lxfjj'}) + reader_mock = mock.Mock(keep_cache=False) + with mock.patch('swift.obj.diskfile.BaseDiskFile.reader', reader_mock): + resp = req.get_response(obj_controller) + reader_mock.assert_called_with(keep_cache=False) + self.assertEqual(resp.status_int, 200) + + # Request headers have both 'X-Auth-Token' and 'X-Storage-Token'. + req = Request.blank('/sda1/p/a/c/o', + headers={'Content-Type': 'application/x-test', + 'X-Auth-Token': '2340lsdfhhjl02lxfjj', + 'X-Storage-Token': '2340lsdfhhjl02lxfjj'}) + reader_mock = mock.Mock(keep_cache=False) + with mock.patch('swift.obj.diskfile.BaseDiskFile.reader', reader_mock): + resp = req.get_response(obj_controller) + reader_mock.assert_called_with(keep_cache=False) + self.assertEqual(resp.status_int, 200) + @mock.patch("time.time", mock_time) def test_DELETE(self): # Test swift.obj.server.ObjectController.DELETE