Skip to content

Commit

Permalink
Mock test creating files in protected directories (#3440)
Browse files Browse the repository at this point in the history
Resolves: issue#3439

Signed-off-by: ahitacat <[email protected]>
  • Loading branch information
ahitacat authored Jun 15, 2022
1 parent 9bc5b49 commit 1d9fdfb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
7 changes: 4 additions & 3 deletions insights/tests/client/test_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ def test_copy_dir(self, create_archive_dir, _, __):
create_archive_dir.assert_called_once()

@patch('insights.client.archive.shutil.copyfile')
@patch('insights.client.archive.os.path.join', side_effect=['/var/tmp/test-archive/insights-archive-test.tar.gz'])
@patch('insights.client.archive.os.path.isdir', Mock())
def test_keep_archive(self, os_, copyfile, _, __):
@patch('insights.client.archive.os.path.exists', return_value=True)
def test_keep_archive(self, path_exists, copyfile, _, __):
archive = InsightsArchive(Mock())
archive.tar_file = '/var/tmp/insights-archive-test.tar.gz'
archive.keep_archive_dir = '/var/tmp/test-archive'
Expand All @@ -215,7 +215,8 @@ def test_keep_archive(self, os_, copyfile, _, __):
@patch('insights.client.archive.os.path.isdir', Mock())
@patch('insights.client.archive.os.path.basename', Mock())
@patch('insights.client.archive.logger')
def test_keep_archive_err_during_copy(self, logger, copyfile, _, __):
@patch('insights.client.archive.os.path.exists', return_value=True)
def test_keep_archive_err_during_copy(self, path_exists, logger, copyfile, _, __):
archive = InsightsArchive(Mock())
archive.archive_stored = '/var/tmp/test-archive/test-store-archive'
archive.keep_archive_dir = '/var/tmp/test-archive'
Expand Down
11 changes: 9 additions & 2 deletions insights/tests/client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,14 @@ def test_upload_412_write_unregistered_file(_, upload_archive, write_unregistere


@patch('insights.client.archive.InsightsArchive.storing_archive')
@patch('insights.client.archive.tempfile.mkdtemp', Mock())
def test_cleanup_tmp(storing_archive):

config = InsightsConfig(keep_archive=False)
arch = InsightsArchive(config)
arch.tar_file = os.path.join(arch.tmp_dir, 'test.tar.gz')
arch.tmp_dir = "/test"
arch.tar_file = "/test/test.tar.gz"
arch.keep_archive_dir = "/test-keep-archive"
arch.cleanup_tmp()
assert not os.path.exists(arch.tmp_dir)
storing_archive.assert_not_called()
Expand All @@ -357,10 +361,13 @@ def test_cleanup_tmp(storing_archive):


@patch('insights.client.archive.InsightsArchive.storing_archive')
@patch('insights.client.archive.tempfile.mkdtemp', Mock())
def test_cleanup_tmp_obfuscation(storing_archive):
config = InsightsConfig(keep_archive=False, obfuscate=True)
arch = InsightsArchive(config)
arch.tar_file = os.path.join(arch.tmp_dir, 'test.tar.gz')
arch.tmp_dir = "/test"
arch.tar_file = "/test/test.tar.gz"
arch.keep_archive_dir = "/test-keep-archive"
arch.cleanup_tmp()
assert not os.path.exists(arch.tmp_dir)
storing_archive.assert_not_called()
Expand Down

0 comments on commit 1d9fdfb

Please sign in to comment.