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

Pkg bypass remediation #53320

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion salt/states/pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,7 @@ def installed(

if bypass_file is not None and bypass_file_contains is not None:
if os.path.isfile(bypass_file):
with salt.utils.fopen(bypass_file) as bypass_file_open:
with salt.utils.files.fopen(bypass_file) as bypass_file_open:
if bypass_file_contains in bypass_file_open.read():
return {'name': name,
'changes': {},
Expand Down
53 changes: 52 additions & 1 deletion tests/unit/states/test_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# Import Python libs
from __future__ import absolute_import
import tempfile
import os

# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
Expand Down Expand Up @@ -34,7 +36,8 @@ def setup_loader_modules(self):
pkg: {
'__grains__': {
'os': 'CentOS'
}
},
'__env__': 'base'
}
}

Expand Down Expand Up @@ -235,3 +238,51 @@ def test_fulfills_version_spec(self):
for installed_versions, operator, version, expected_result in test_parameters:
msg = "installed_versions: {}, operator: {}, version: {}, expected_result: {}".format(installed_versions, operator, version, expected_result)
self.assertEqual(expected_result, pkg._fulfills_version_spec(installed_versions, operator, version), msg)

def test_bypass_file(self):
'''
Test bypass_file option
'''
temp_file_object = tempfile.NamedTemporaryFile(delete=False)
temp_file_name = temp_file_object.name
cachedir = tempfile.tempdir

with patch.dict(pkg.__opts__,
{'bypass_file': temp_file_name,
'cachedir': cachedir}):
ret = pkg.installed('dummy', bypass_file=temp_file_name)
comment = u'pkg.installed was bypassed as bypass_file {} was ' \
'present'.format(temp_file_name)
msg = {u'comment': comment,
u'changes': {},
u'name': 'dummy',
u'result': True}
self.assertEqual(ret, msg)
os.remove(temp_file_name)

def test_bypass_file_contains(self):
'''
Test bypass_file_contains option
'''
temp_file_object = tempfile.NamedTemporaryFile(mode='w', delete=False)
temp_file_name = temp_file_object.name
temp_comment = 'TEST_COMMENT1234'
cachedir = tempfile.tempdir
temp_file_object.write(temp_comment)
temp_file_object.seek(0)

with patch.dict(pkg.__opts__,
{'bypass_file': temp_file_name,
'bypass_file_contains': temp_comment,
'cachedir': cachedir}):
ret = pkg.installed('dummy',
bypass_file=temp_file_name,
bypass_file_contains=temp_comment)
comment = u'pkg.installed was bypassed as {} was present in ' \
'{}'.format(temp_comment, temp_file_name)
msg = {u'comment': comment,
u'changes': {},
u'name': 'dummy',
u'result': True}
self.assertEqual(ret, msg)
os.remove(temp_file_name)