Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address file.managed with binary file #54480

Merged
merged 4 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions salt/modules/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5336,11 +5336,11 @@ def manage_file(name,
# Write the static contents to a temporary file
tmp = salt.utils.files.mkstemp(prefix=salt.utils.files.TEMPFILE_PREFIX,
text=True)
if salt.utils.platform.is_windows():
contents = os.linesep.join(
_splitlines_preserving_trailing_newline(contents))
with salt.utils.files.fopen(tmp, 'wb') as tmp_:
if encoding:
if salt.utils.platform.is_windows():
contents = os.linesep.join(
_splitlines_preserving_trailing_newline(contents))
log.debug('File will be encoded with %s', encoding)
tmp_.write(contents.encode(encoding=encoding, errors=encoding_errors))
else:
Expand Down
20 changes: 20 additions & 0 deletions tests/integration/states/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2696,6 +2696,26 @@ def test_binary_contents(self):
except OSError:
pass

def test_binary_contents_twice(self):
'''
This test ensures that after a binary file is created, salt can confirm
that the file is in the correct state.
'''
# Create a binary file
name = os.path.join(TMP, '1px.gif')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW this could be rewritten using tempfile.NamedTemporaryFile, to avoid the try/except to try and cleanup.


# First run state ensures file is created
ret = self.run_state('file.managed', name=name, contents=BINARY_FILE)
self.assertSaltTrueReturn(ret)

# Second run of state ensures file is in correct state
ret = self.run_state('file.managed', name=name, contents=BINARY_FILE)
self.assertSaltTrueReturn(ret)
try:
os.remove(name)
except OSError:
pass

@skip_if_not_root
@skipIf(not HAS_PWD, "pwd not available. Skipping test")
@skipIf(not HAS_GRP, "grp not available. Skipping test")
Expand Down