-
Notifications
You must be signed in to change notification settings - Fork 13
Add Unit Test for CompressOutbox to Tempfile Creation Refactoring (SOFTWARE-5540) #176
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
jeff-takaki
wants to merge
10
commits into
opensciencegrid:2.x
Choose a base branch
from
jeff-takaki:SOFTWARE-5540-unittest-compress-outbox-1
base: 2.x
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.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9b80b3e
Prep for sandbox refactoring with unit tests (SOFTWARE-5531)
brianhlin 5ff4270
First attempt at cleaning up the tempfile creation (SOFTWARE-5540)
brianhlin 948ac52
First stab at updating temp tarball creation (SOFTWARE-5531)
brianhlin 46d3c94
Add unit test for Compress Outbox function
jeff-takaki 85dd3cd
Remove excess whitespace comment
jeff-takaki f035d10
Add Unit Tests for tarball creation and to verify tarball contents
jeff-takaki 4083436
Comment Add Whitespace
jeff-takaki 19aec74
Add more assertions and move patches to setUp
jeff-takaki a5c12d7
Fix test failure message for tarball count.
jeff-takaki c91b691
Refactor tarball locator logic
jeff-takaki 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
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
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,153 @@ | ||
| #!/bin/env python | ||
|
|
||
| import glob | ||
| import os | ||
| import shutil | ||
| import tarfile | ||
| import tempfile | ||
| import unittest | ||
| from unittest.mock import patch, PropertyMock | ||
| from unittest import TextTestRunner | ||
|
|
||
| from common.gratia.common import sandbox_mgmt | ||
|
|
||
| class SandboxMgmtTests(unittest.TestCase): | ||
|
|
||
| @patch('gratia.common.config.ConfigProxy.get_GratiaExtension', create=True, return_value='test-extension') | ||
| def test_GenerateFilename(self, mock_config): | ||
| """GenerateFilename creates a temporary file and returns the path to the file | ||
| """ | ||
| prefix = 'test-prefix' | ||
| temp_dir = '/tmp' | ||
|
|
||
| try: | ||
| with sandbox_mgmt.GenerateFilename(prefix, temp_dir) as filename: | ||
| self.assertTrue(os.path.exists(filename.name), | ||
| f'Failed to create temporary file ({filename.name})') | ||
| self.assertEqual(temp_dir.rstrip('/'), | ||
| os.path.dirname(filename.name), | ||
| f'Temporary file {filename.name} placed in the wrong directory') | ||
| self.assertRegex(filename.name, | ||
| rf'{temp_dir}/*{prefix}\.\d+\.{mock_config.return_value}__\w+', | ||
| 'Unexpected file name format') | ||
| finally: | ||
| try: | ||
| filename.close() | ||
| os.remove(filename.name) | ||
| except (FileNotFoundError, NameError): | ||
| # don't need to clean up what's not there | ||
| pass | ||
|
|
||
| class CompressOutboxTests(unittest.TestCase): | ||
| def setUp(self): | ||
| # provision test environment | ||
| gratia_ex = patch('gratia.common.config.ConfigProxy.get_GratiaExtension', | ||
| create=True, return_value='test-extension') | ||
| file_frag = patch('gratia.common.config.ConfigProxy.getFilenameFragment', | ||
| create=True, return_value='test-filename') | ||
|
|
||
| self.mock_gratia_ex = gratia_ex.start() | ||
| self.mock_file_frag = file_frag.start() | ||
|
|
||
| self.probe_dir = tempfile.mkdtemp() | ||
| self.outbox = os.path.join(self.probe_dir, 'outbox') | ||
| os.makedirs(self.outbox, exist_ok=True) | ||
| self.outfiles = ['testfile1', 'testfile2'] | ||
|
|
||
| # add content to the files | ||
| for testfile in self.outfiles: | ||
| content = testfile + ' contains this content' | ||
| with open(os.path.join(self.outbox, testfile), 'w', encoding="utf-8") as test: | ||
| test.write(content) | ||
|
|
||
| self.addCleanup(gratia_ex.stop) | ||
| self.addCleanup(file_frag.stop) | ||
|
|
||
| def tearDown(self): | ||
| # Remove probe_dir after test | ||
| shutil.rmtree(self.probe_dir) | ||
|
|
||
| def get_tarball_location(self, path_to_tarball, tarball): | ||
| """ | ||
| Attempts to return exact location of tarball | ||
| """ | ||
| try: | ||
| tarball_location = os.path.join(f'{path_to_tarball}', tarball[0]) | ||
| except IndexError as notarball: | ||
| print("Tarball does not exist!") | ||
| self.fail(notarball) | ||
|
|
||
| return tarball_location | ||
|
|
||
| def test_compress_outbox(self): | ||
| """CompressOutbox compresses the files in the outbox directory | ||
| and stores the resulting tarball in probe_dir/staged. | ||
| """ | ||
| # Assert that CompressOutbox returns True | ||
| result = sandbox_mgmt.CompressOutbox(self.probe_dir, self.outbox, self.outfiles) | ||
| self.assertTrue(result) | ||
|
|
||
| def test_tarball_creation(self): | ||
| """ | ||
| Assert that tarball is created in the correct location | ||
| """ | ||
| sandbox_mgmt.CompressOutbox(self.probe_dir, self.outbox, self.outfiles) | ||
|
|
||
| path_to_tarball = f'{self.probe_dir}/staged/store' | ||
| # Finds exactly one tarball that matches GenerateFilename function output | ||
| tarball = glob.glob("tz.*.test-extension__*", root_dir=path_to_tarball) | ||
|
|
||
| # Where tarball exists | ||
| tarball_location = self.get_tarball_location(path_to_tarball, tarball) | ||
|
|
||
| # Counts files in the directory that the tarball should be in | ||
| tarball_count = len((tarball)) | ||
|
|
||
| self.assertTrue(os.path.exists(tarball_location), | ||
| 'Tarball not created in correct location') | ||
| self.assertEqual(tarball_count, 1, | ||
| f'Expected 1 tarball, found {tarball_count}') | ||
|
|
||
| def test_tarball_contents(self): | ||
| """ | ||
| Assert that unpacked tarball contains files from outfiles | ||
| """ | ||
| sandbox_mgmt.CompressOutbox(self.probe_dir, self.outbox, self.outfiles) | ||
| path_to_tarball = f'{self.probe_dir}/staged/store' | ||
|
|
||
| # Finds tarball that matches GenerateFilename() output | ||
| tarball = glob.glob("tz.*.test-extension__*", root_dir=path_to_tarball) | ||
|
|
||
| # Where tarball exists | ||
| tarball_location = self.get_tarball_location(path_to_tarball, tarball) | ||
|
|
||
| # Gets names of files within tarball | ||
| with tarfile.open(tarball_location, "r") as names: | ||
|
|
||
| # Names of files in tarball | ||
| namelist = names.getnames() | ||
|
|
||
| # Sort both lists to ensure order-independent comparison | ||
| namelist.sort() | ||
| self.outfiles.sort() | ||
|
|
||
| # Open files in outfiles | ||
| expected_files1 = open(os.path.join(f'{self.outbox}/{self.outfiles[0]}'), 'rb') | ||
| expected_files2 = open(os.path.join(f'{self.outbox}/{self.outfiles[1]}'), 'rb') | ||
|
|
||
| # Extract the contents from testfile1 | ||
| file1 = names.extractfile(namelist[0]) | ||
| file1_contents = file1.readlines() | ||
| expected_results_f1 = expected_files1.readlines() | ||
|
|
||
| # Extract the contents from testfile2 | ||
| file2 = names.extractfile(namelist[1]) | ||
| file2_contents = file2.readlines() | ||
| expected_results_f2 = expected_files2.readlines() | ||
|
|
||
| self.assertListEqual(namelist, self.outfiles, | ||
| 'Unexpected file names in tarball') | ||
| self.assertEqual(file1_contents, expected_results_f1, | ||
| 'Unexpected content in file1') | ||
| self.assertEqual(file2_contents, expected_results_f2, | ||
| 'Unexpected content in file2') | ||
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.
Uh oh!
There was an error while loading. Please reload this page.