From efa00ec3bce37d457516fb9408872968e11630d4 Mon Sep 17 00:00:00 2001 From: Caner Derici Date: Tue, 29 Mar 2022 16:18:49 -0600 Subject: [PATCH] Attach-resource to check if given binary file fixes #654 --- juju/application.py | 3 ++- tests/integration/test_model.py | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/juju/application.py b/juju/application.py index dfecef2bd..bea3398c8 100644 --- a/juju/application.py +++ b/juju/application.py @@ -441,7 +441,8 @@ def attach_resource(self, resource_name, file_name, file_obj): headers['Content-Type'] = 'application/octet-stream' headers['Content-Length'] = len(data) - headers['Content-Sha384'] = hashlib.sha384(bytes(data, 'utf-8')).hexdigest() + data_bytes = data if isinstance(data, bytes) else bytes(data, 'utf-8') + headers['Content-Sha384'] = hashlib.sha384(data_bytes).hexdigest() file_name = str(file_name) if not file_name.startswith('./'): diff --git a/tests/integration/test_model.py b/tests/integration/test_model.py index 533c0a4f5..d4e8122d4 100644 --- a/tests/integration/test_model.py +++ b/tests/integration/test_model.py @@ -660,6 +660,9 @@ async def test_attach_resource(event_loop): with open(str(charm_path / 'test.file')) as f: app.attach_resource('file-res', 'test.file', f) + with open(str(charm_path / 'test.file'), 'rb') as f: + app.attach_resource('file-res', 'test.file', f) + @base.bootstrapped @pytest.mark.asyncio