From 5d7e7547f0119300ecb8f6cd171b04ead3210ecc Mon Sep 17 00:00:00 2001 From: Eric Harney Date: Mon, 12 Apr 2021 09:24:47 -0400 Subject: [PATCH] Open local image files with "rb" mode When using "file" in allowed_direct_url_schemes, open image files w/ binary mode. Closes-Bug: #1923313 Change-Id: I52b87d03c49db74002f09cfeea86bc0ac7ce8a42 --- cinder/image/glance.py | 2 +- cinder/tests/unit/image/test_glance.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/cinder/image/glance.py b/cinder/image/glance.py index 8102a8e5973..173d649236a 100644 --- a/cinder/image/glance.py +++ b/cinder/image/glance.py @@ -365,7 +365,7 @@ class GlanceImageService(object): # a system call to cp could have significant performance # advantages, however we do not have the path to files at # this point in the abstraction. - with open(parsed_url.path, "r") as f: + with open(parsed_url.path, "rb") as f: shutil.copyfileobj(f, data) return diff --git a/cinder/tests/unit/image/test_glance.py b/cinder/tests/unit/image/test_glance.py index 73e014f81e8..d0afee7c848 100644 --- a/cinder/tests/unit/image/test_glance.py +++ b/cinder/tests/unit/image/test_glance.py @@ -830,6 +830,7 @@ class TestGlanceImageService(test.TestCase): self.flags(allowed_direct_url_schemes=['file']) self.service.download(self.context, image_id, writer) mock_copyfileobj.assert_called_once_with(mock.ANY, writer) + mock_open.assert_called_once_with('/tmp/test', 'rb') @mock.patch('six.moves.builtins.open') @mock.patch('shutil.copyfileobj')