-
Notifications
You must be signed in to change notification settings - Fork 621
[tests] Add permissions tests #4302
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
Open
tiredPotato
wants to merge
2
commits into
sosreport:main
Choose a base branch
from
tiredPotato:permissions_tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+156
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # This file is part of the sos project: https://github.com/sosreport/sos | ||
| # | ||
| # This copyrighted material is made available to anyone wishing to use, | ||
| # modify, copy, or redistribute it subject to the terms and conditions of | ||
| # version 2 of the GNU General Public License. | ||
| # | ||
| # See the LICENSE file in the source distribution for further information. | ||
|
|
||
| from sos.report.plugins import Plugin, IndependentPlugin | ||
|
|
||
|
|
||
| class Permissions(Plugin, IndependentPlugin): | ||
| """Fake plugin for testing permission preservation""" | ||
|
|
||
| plugin_name = 'permissions' | ||
| short_desc = 'fake plugin to test permission preservation' | ||
|
|
||
| def setup(self): | ||
| self.add_copy_spec('/etc/no_perms_file.conf') | ||
| self.add_copy_spec('/var/log/some_perms_file.log') | ||
| self.add_copy_spec('/var/tmp/permissions-test') | ||
135 changes: 135 additions & 0 deletions
135
tests/report_tests/permissions_tests/permissions_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| # This file is part of the sos project: https://github.com/sosreport/sos | ||
| # | ||
| # This copyrighted material is made available to anyone wishing to use, | ||
| # modify, copy, or redistribute it subject to the terms and conditions of | ||
| # version 2 of the GNU General Public License. | ||
| # | ||
| # See the LICENSE file in the source distribution for further information. | ||
|
|
||
| import os | ||
| import shutil | ||
| import stat | ||
| import tarfile | ||
| from avocado.utils import process | ||
| from sos_tests import StageTwoReportTest | ||
|
|
||
|
|
||
| class PermissionsReportTest(StageTwoReportTest): | ||
| """Base class that overrides _extract_archive to use Python's tarfile | ||
| module, which preserves permissions (via explicit os.chmod). | ||
|
|
||
| :avocado: disable | ||
| """ | ||
|
|
||
| def _extract_archive(self, arc_path): | ||
| _extract_path = self._get_extracted_tarball_path() | ||
| os.makedirs(_extract_path, exist_ok=True) | ||
| try: | ||
| with tarfile.open(arc_path) as tf: | ||
| members = tf.getmembers() | ||
| tf.extractall(_extract_path) | ||
| for member in members: | ||
| target = os.path.join(_extract_path, member.name) | ||
| if os.path.exists(target) and not os.path.islink(target): | ||
| os.chmod(target, member.mode) | ||
| self.archive_path = self._get_archive_path() | ||
| except Exception as err: | ||
| self.cancel(f"Could not extract archive: {err}") | ||
|
|
||
|
|
||
| class FilePermissionsPreserved(PermissionsReportTest): # Test for bz888724 | ||
| """Test that sos preserves permissions of collected files | ||
|
|
||
| :avocado: tags=stagetwo | ||
| """ | ||
|
|
||
| install_plugins = ['permissions'] | ||
| sos_cmd = '-o permissions' | ||
|
|
||
| no_perms_file = '/etc/no_perms_file.conf' | ||
| some_perms_file = '/var/log/some_perms_file.log' | ||
|
|
||
| def pre_sos_setup(self): | ||
| process.run(f'touch {self.no_perms_file}') | ||
| os.chmod(self.no_perms_file, 0o000) | ||
| process.run(f'touch {self.some_perms_file}') | ||
| os.chmod(self.some_perms_file, 0o666) | ||
|
|
||
| def test_file_permissions_preserved(self): | ||
| for fname, mode in [(self.no_perms_file, 0o000), | ||
| (self.some_perms_file, 0o666)]: | ||
| self.assertFileCollected(fname) | ||
| extracted_file = self.get_name_in_archive(fname) | ||
| extracted_permissions = stat.S_IMODE( | ||
| os.lstat(extracted_file).st_mode) | ||
| self.assertEqual( | ||
| extracted_permissions, mode, | ||
| f"Permissions not preserved: expected {mode}, " | ||
| f"got {oct(extracted_permissions)}" | ||
| ) | ||
|
|
||
| def post_test_tear_down(self): | ||
| for f in [self.no_perms_file, self.some_perms_file]: | ||
| if os.path.exists(f): | ||
| os.remove(f) | ||
|
|
||
|
|
||
| class DirectoryPermissionsPreserved( # Test for bz1069786 | ||
| PermissionsReportTest): | ||
| """Test that sos preserves permissions of collected directories | ||
|
|
||
| :avocado: tags=stagetwo | ||
| """ | ||
|
|
||
| install_plugins = ['permissions'] | ||
| sos_cmd = '-o permissions' | ||
|
|
||
| test_root = '/var/tmp/permissions-test' | ||
|
|
||
| def pre_sos_setup(self): | ||
| os.makedirs(f'{self.test_root}/d1/d2/d3', exist_ok=True) | ||
| with open(f'{self.test_root}/d1/d2/d3/data', 'w', | ||
| encoding='utf-8') as dfile: | ||
| dfile.write('test data\n') | ||
|
|
||
| os.chmod(f'{self.test_root}/d1/d2/d3', 0o777) | ||
| os.chmod(f'{self.test_root}/d1/d2', 0o770) | ||
| os.chmod(f'{self.test_root}/d1', 0o700) | ||
|
|
||
| def test_top_dir_permissions_preserved(self): | ||
| extracted_file = self.get_name_in_archive(f'{self.test_root}/d1') | ||
| self.assertFileExists(extracted_file) | ||
| extracted_permissions = stat.S_IMODE( | ||
| os.stat(extracted_file).st_mode) | ||
| self.assertEqual( | ||
| extracted_permissions, 0o700, | ||
| f"d1 permissions not preserved: expected 0o700, " | ||
| f"got {oct(extracted_permissions)}" | ||
| ) | ||
|
|
||
| def test_nested_dir_permissions_preserved(self): | ||
| extracted_file = self.get_name_in_archive(f'{self.test_root}/d1/d2') | ||
| self.assertFileExists(extracted_file) | ||
| extracted_permissions = stat.S_IMODE( | ||
| os.stat(extracted_file).st_mode) | ||
| self.assertEqual( | ||
| extracted_permissions, 0o770, | ||
| f"d2 permissions not preserved: expected 0o770, " | ||
| f"got {oct(extracted_permissions)}" | ||
| ) | ||
|
|
||
| def test_deep_nested_dir_permissions_preserved(self): | ||
| extracted_file = self.get_name_in_archive( | ||
| f'{self.test_root}/d1/d2/d3') | ||
| self.assertFileExists(extracted_file) | ||
| extracted_permissions = stat.S_IMODE( | ||
| os.stat(extracted_file).st_mode) | ||
| self.assertEqual( | ||
| extracted_permissions, 0o777, | ||
| f"d3 permissions not preserved: expected 0o777, " | ||
| f"got {oct(extracted_permissions)}" | ||
| ) | ||
|
|
||
| def post_test_tear_down(self): | ||
| if os.path.exists(self.test_root): | ||
| shutil.rmtree(self.test_root) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nitpick: You can have one call:
(if this is the only feedback, let ignore it)