Skip to content

Commit b5c3185

Browse files
authored
Merge pull request #659 from cderici/attach-resource-binary-file
#659 #### Description The `attach-resource` method on pylibjuju assumes the resource read is plain text, so it borks when it gets a binary file as a resource. Fixes #654 #### QA Steps ```sh tox -e integration -- tests/integration/test_model.py::test_attach_resource ``` #### Notes & Discussion
2 parents 72a0c4f + efa00ec commit b5c3185

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

juju/application.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,8 @@ def attach_resource(self, resource_name, file_name, file_obj):
441441

442442
headers['Content-Type'] = 'application/octet-stream'
443443
headers['Content-Length'] = len(data)
444-
headers['Content-Sha384'] = hashlib.sha384(bytes(data, 'utf-8')).hexdigest()
444+
data_bytes = data if isinstance(data, bytes) else bytes(data, 'utf-8')
445+
headers['Content-Sha384'] = hashlib.sha384(data_bytes).hexdigest()
445446

446447
file_name = str(file_name)
447448
if not file_name.startswith('./'):

tests/integration/test_model.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,9 @@ async def test_attach_resource(event_loop):
660660
with open(str(charm_path / 'test.file')) as f:
661661
app.attach_resource('file-res', 'test.file', f)
662662

663+
with open(str(charm_path / 'test.file'), 'rb') as f:
664+
app.attach_resource('file-res', 'test.file', f)
665+
663666

664667
@base.bootstrapped
665668
@pytest.mark.asyncio

0 commit comments

Comments
 (0)