From 9b80b3efbaadb829c63923d1cdb45af3a0890f2c Mon Sep 17 00:00:00 2001 From: Brian Lin Date: Mon, 20 Mar 2023 14:51:39 -0500 Subject: [PATCH 01/10] Prep for sandbox refactoring with unit tests (SOFTWARE-5531) --- test/test_sandbox_mgmt.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 test/test_sandbox_mgmt.py diff --git a/test/test_sandbox_mgmt.py b/test/test_sandbox_mgmt.py new file mode 100644 index 00000000..26657557 --- /dev/null +++ b/test/test_sandbox_mgmt.py @@ -0,0 +1,33 @@ +#!/bin/env python + +import os +import unittest +from unittest.mock import patch, PropertyMock + +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: + filename = sandbox_mgmt.GenerateFilename(prefix, temp_dir) + self.assertTrue(os.path.exists(filename), + f'Failed to create temporary file ({filename})') + self.assertEqual(temp_dir.rstrip('/'), + os.path.dirname(filename), + f'Temporary file {filename} placed in the wrong directory') + self.assertRegex(filename, + rf'{temp_dir}/*{prefix}\d+\.{mock_config.return_value}__\w+', + 'Unexpected file name format') + finally: + try: + os.remove(filename) + except FileNotFoundError: + # don't need to clean up what's not there + pass From 5ff42702164649e2f2144ea5c9813b0c037534ae Mon Sep 17 00:00:00 2001 From: Brian Lin Date: Mon, 20 Mar 2023 18:18:12 -0500 Subject: [PATCH 02/10] First attempt at cleaning up the tempfile creation (SOFTWARE-5540) --- common/gratia/common/sandbox_mgmt.py | 26 ++++++++------------------ common/gratia/common/xml_utils.py | 6 ++---- test/test_sandbox_mgmt.py | 25 +++++++++++++------------ 3 files changed, 23 insertions(+), 34 deletions(-) diff --git a/common/gratia/common/sandbox_mgmt.py b/common/gratia/common/sandbox_mgmt.py index 90906a9f..c907dfdc 100644 --- a/common/gratia/common/sandbox_mgmt.py +++ b/common/gratia/common/sandbox_mgmt.py @@ -5,6 +5,7 @@ import glob import time import shutil +import tempfile import tarfile from gratia.common.config import ConfigProxy @@ -409,18 +410,8 @@ def SearchOutstandingRecord(): def GenerateFilename(prefix, current_dir): '''Generate a filename of the for current_dir/prefix.$pid.ConfigFragment.gratia.xml__Unique''' - filename = prefix + str(global_state.RecordPid) + '.' + Config.get_GratiaExtension() \ - + '__XXXXXXXXXX' - filename = os.path.join(current_dir, filename) - mktemp_pipe = os.popen('mktemp -q "' + filename + '"') - if mktemp_pipe != None: - filename = mktemp_pipe.readline() - mktemp_pipe.close() - filename = filename.strip() - if filename != r'': - return filename - - raise IOError + fn_prefix = f'{prefix}.{global_state.RecordPid}.{Config.get_GratiaExtension()}__' + return tempfile.NamedTemporaryFile(prefix=fn_prefix, dir=current_dir, delete=False, mode='w') def UncompressOutbox(staging_name, target_dir): @@ -599,12 +590,11 @@ def OpenNewRecordFile(dirIndex): raise InternalError(msg) from exc try: - filename = GenerateFilename('r.', working_dir) - DebugPrint(3, 'Creating file:', filename) - outstandingRecordCount += 1 - f = open(filename, 'w') - dirIndex = index - return (f, dirIndex) + with GenerateFilename('r', working_dir) as recordfile: + DebugPrint(3, 'Creating file:', recordfile.name) + outstandingRecordCount += 1 + dirIndex = index + return (recordfile, dirIndex) except Exception as exc: msg = 'ERROR: Caught exception while creating file' DebugPrint(0, msg + ': ', exc) diff --git a/common/gratia/common/xml_utils.py b/common/gratia/common/xml_utils.py index c7c74868..d4e74599 100644 --- a/common/gratia/common/xml_utils.py +++ b/common/gratia/common/xml_utils.py @@ -280,10 +280,8 @@ def UsageCheckXmldoc(xmlDoc, external, resourceType=None): subdir = os.path.join(Config.get_DataFolder(), "quarantine", 'subdir.' + Config.getFilenameFragment()) if not os.path.exists(subdir): os.mkdir(subdir) - fn = sandbox_mgmt.GenerateFilename("r.", subdir) - writer = open(fn, 'w') - usageRecord.writexml(writer) - writer.close() + with sandbox_mgmt.GenerateFilename("r", subdir) as writer: + usageRecord.writexml(writer) usageRecord.unlink() continue diff --git a/test/test_sandbox_mgmt.py b/test/test_sandbox_mgmt.py index 26657557..d270bc8f 100644 --- a/test/test_sandbox_mgmt.py +++ b/test/test_sandbox_mgmt.py @@ -12,22 +12,23 @@ class SandboxMgmtTests(unittest.TestCase): def test_GenerateFilename(self, mock_config): """GenerateFilename creates a temporary file and returns the path to the file """ - prefix = 'test-prefix.' + prefix = 'test-prefix' temp_dir = '/tmp' try: - filename = sandbox_mgmt.GenerateFilename(prefix, temp_dir) - self.assertTrue(os.path.exists(filename), - f'Failed to create temporary file ({filename})') - self.assertEqual(temp_dir.rstrip('/'), - os.path.dirname(filename), - f'Temporary file {filename} placed in the wrong directory') - self.assertRegex(filename, - rf'{temp_dir}/*{prefix}\d+\.{mock_config.return_value}__\w+', - 'Unexpected file name format') + 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: - os.remove(filename) - except FileNotFoundError: + filename.close() + os.remove(filename.name) + except (FileNotFoundError, NameError): # don't need to clean up what's not there pass From 948ac520a73c32e9be5e44146e8e8321ad423682 Mon Sep 17 00:00:00 2001 From: Brian Lin Date: Fri, 24 Mar 2023 17:08:06 -0500 Subject: [PATCH 03/10] First stab at updating temp tarball creation (SOFTWARE-5531) --- common/gratia/common/sandbox_mgmt.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/common/gratia/common/sandbox_mgmt.py b/common/gratia/common/sandbox_mgmt.py index c907dfdc..753c8218 100644 --- a/common/gratia/common/sandbox_mgmt.py +++ b/common/gratia/common/sandbox_mgmt.py @@ -478,20 +478,21 @@ def CompressOutbox(probe_dir, outbox, outfiles): DebugPrint(0, msg + ':' + exc) raise InternalError(msg) from exc - staging_name = GenerateFilename('tz.', staged_store) - DebugPrint(1, 'Compressing outbox in tar.bz2 file: ' + staging_name) + with GenerateFilename('tz', staged_store) as temp_tarfile: + staging_name = temp_tarfile.name + DebugPrint(1, 'Compressing outbox in tar.bz2 file: ' + staging_name) - try: - tar = tarfile.open(staging_name, 'w:bz2') - except KeyboardInterrupt: - raise - except SystemExit: - raise - except Exception as e: - DebugPrint(0, 'Warning: Exception caught while opening tar.bz2 file: ' + staging_name + ':') - DebugPrint(0, 'Caught exception: ', e) - DebugPrintTraceback() - return False + try: + tar = tarfile.open(staging_name, 'w:bz2') + except KeyboardInterrupt: + raise + except SystemExit: + raise + except Exception as e: + DebugPrint(0, 'Warning: Exception caught while opening tar.bz2 file: ' + staging_name + ':') + DebugPrint(0, 'Caught exception: ', e) + DebugPrintTraceback() + return False try: for f in outfiles: From 46d3c94ec99690055d03d12c885d51fdc6a1fbbe Mon Sep 17 00:00:00 2001 From: Jeff Takaki Date: Mon, 31 Jul 2023 11:56:37 -0500 Subject: [PATCH 04/10] Add unit test for Compress Outbox function --- test/test_sandbox_mgmt.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/test_sandbox_mgmt.py b/test/test_sandbox_mgmt.py index d270bc8f..99dc7bc7 100644 --- a/test/test_sandbox_mgmt.py +++ b/test/test_sandbox_mgmt.py @@ -1,6 +1,8 @@ #!/bin/env python import os +import shutil +import tempfile import unittest from unittest.mock import patch, PropertyMock @@ -32,3 +34,34 @@ def test_GenerateFilename(self, mock_config): except (FileNotFoundError, NameError): # don't need to clean up what's not there pass + +class CompressOutboxTests(unittest.TestCase): + def setUp(self): + #provision test environment + 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 fname in self.outfiles: + with open(os.path.join(self.outbox, fname), 'w') as f: + f.write('test content') + + def tearDown(self): + #Remove probe_dir after test + shutil.rmtree(self.probe_dir) + + @patch('gratia.common.config.ConfigProxy.get_GratiaExtension', create=True, return_value ='test-extension') + @patch('gratia.common.config.ConfigProxy.getFilenameFragment', create=True, return_value ='test-filename') + def test_compress_outbox(self, mock_gratia_ext, mock_file_frag): + """CompressOutbox compresses the files in the outbox directory + and stores the resulting tarball in probe_dir/staged. + """ + #Parameters for function + probe_dir = self.probe_dir + outbox = self.outbox + outfiles = self.outfiles + + #Assert that CompressOutbox returns True + result = sandbox_mgmt.CompressOutbox(probe_dir, outbox, outfiles) + self.assertTrue(result) From 85dd3cd981bc8638b0cf5d72716eae5f3b6f6704 Mon Sep 17 00:00:00 2001 From: Jeff Takaki <97972180+jtakaki-matc@users.noreply.github.com> Date: Wed, 2 Aug 2023 14:59:16 -0500 Subject: [PATCH 05/10] Remove excess whitespace comment Co-authored-by: Brian Lin --- test/test_sandbox_mgmt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_sandbox_mgmt.py b/test/test_sandbox_mgmt.py index 99dc7bc7..1d6f0de2 100644 --- a/test/test_sandbox_mgmt.py +++ b/test/test_sandbox_mgmt.py @@ -55,7 +55,7 @@ def tearDown(self): @patch('gratia.common.config.ConfigProxy.getFilenameFragment', create=True, return_value ='test-filename') def test_compress_outbox(self, mock_gratia_ext, mock_file_frag): """CompressOutbox compresses the files in the outbox directory - and stores the resulting tarball in probe_dir/staged. + and stores the resulting tarball in probe_dir/staged. """ #Parameters for function probe_dir = self.probe_dir From f035d10d8957366031782ef1dd4e6f1f4265b05b Mon Sep 17 00:00:00 2001 From: Jeff Takaki Date: Tue, 8 Aug 2023 15:48:09 -0500 Subject: [PATCH 06/10] Add Unit Tests for tarball creation and to verify tarball contents --- test/test_sandbox_mgmt.py | 55 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/test/test_sandbox_mgmt.py b/test/test_sandbox_mgmt.py index 1d6f0de2..2969266b 100644 --- a/test/test_sandbox_mgmt.py +++ b/test/test_sandbox_mgmt.py @@ -1,7 +1,9 @@ #!/bin/env python +import glob import os import shutil +import tarfile import tempfile import unittest from unittest.mock import patch, PropertyMock @@ -51,8 +53,8 @@ def tearDown(self): #Remove probe_dir after test shutil.rmtree(self.probe_dir) - @patch('gratia.common.config.ConfigProxy.get_GratiaExtension', create=True, return_value ='test-extension') @patch('gratia.common.config.ConfigProxy.getFilenameFragment', create=True, return_value ='test-filename') + @patch('gratia.common.config.ConfigProxy.get_GratiaExtension', create=True, return_value ='test-extension') def test_compress_outbox(self, mock_gratia_ext, mock_file_frag): """CompressOutbox compresses the files in the outbox directory and stores the resulting tarball in probe_dir/staged. @@ -65,3 +67,54 @@ def test_compress_outbox(self, mock_gratia_ext, mock_file_frag): #Assert that CompressOutbox returns True result = sandbox_mgmt.CompressOutbox(probe_dir, outbox, outfiles) self.assertTrue(result) + + @patch('gratia.common.config.ConfigProxy.getFilenameFragment', create=True, return_value ='test-filename') + @patch('gratia.common.config.ConfigProxy.get_GratiaExtension', create=True, return_value ='test-extension') + def test_tarball_creation(self, mock_gratia_ext, mock_filefrag): + """ + Assert that tarball is created in the correct location + """ + #Parameters for function + probe_dir = self.probe_dir + outbox = self.outbox + outfiles = self.outfiles + + sandbox_mgmt.CompressOutbox(probe_dir, outbox, outfiles) + + #Finds tarball that matches GenerateFilename function output + os.chdir(f'{probe_dir}/staged/store') + for tarball in glob.glob("tz.*.test-extension__*"): + return(tarball) + + self.assertTrue(os.path.exists(path_to_tarball/{tarball})) + + @patch('gratia.common.config.ConfigProxy.getFilenameFragment', create=True, return_value ='test-filename') + @patch('gratia.common.config.ConfigProxy.get_GratiaExtension', create=True, return_value ='test-extension') + def test_tarball_contents(self, mock_gratia_ext, mock_filefrag): + """ + Assert that unpacked tarball contains files from outfiles + """ + #Parameters for function + probe_dir = self.probe_dir + outbox = self.outbox + outfiles = self.outfiles + + sandbox_mgmt.CompressOutbox(probe_dir, outbox, outfiles) + + #Finds tarball that matches GenerateFilename() output + os.chdir(f'{probe_dir}/staged/store') + for tarball in glob.glob("tz.*.test-extension__*"): + return(tarball) + + #Gets names of files within tarball + file_obj= tarfile.open(tarball,"r") + namelist=file_obj.getnames() + for names in namelist: + return(names) + file_obj.close() + + # Sort both lists to ensure order-independent comparison + names.sort() + outfiles.sort() + + self.assertListEqual(names, outfiles) From 4083436f04d8b585776f19c73120575dbd039f87 Mon Sep 17 00:00:00 2001 From: Jeff Takaki <97972180+jtakaki-matc@users.noreply.github.com> Date: Wed, 9 Aug 2023 11:00:08 -0500 Subject: [PATCH 07/10] Comment Add Whitespace Co-authored-by: Brian Lin --- test/test_sandbox_mgmt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_sandbox_mgmt.py b/test/test_sandbox_mgmt.py index 2969266b..da6ccdba 100644 --- a/test/test_sandbox_mgmt.py +++ b/test/test_sandbox_mgmt.py @@ -39,7 +39,7 @@ def test_GenerateFilename(self, mock_config): class CompressOutboxTests(unittest.TestCase): def setUp(self): - #provision test environment + # provision test environment self.probe_dir = tempfile.mkdtemp() self.outbox = os.path.join(self.probe_dir, 'outbox') os.makedirs(self.outbox, exist_ok=True) From 19aec74164cbcb65f659b47062b1d3c9fde3115f Mon Sep 17 00:00:00 2001 From: Jeff Takaki Date: Mon, 14 Aug 2023 08:33:36 -0500 Subject: [PATCH 08/10] Add more assertions and move patches to setUp Make corrections to tarball file extraction and tarball location check --- test/test_sandbox_mgmt.py | 133 +++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 58 deletions(-) diff --git a/test/test_sandbox_mgmt.py b/test/test_sandbox_mgmt.py index da6ccdba..649d3915 100644 --- a/test/test_sandbox_mgmt.py +++ b/test/test_sandbox_mgmt.py @@ -40,81 +40,98 @@ def test_GenerateFilename(self, mock_config): 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 fname in self.outfiles: - with open(os.path.join(self.outbox, fname), 'w') as f: - f.write('test content') + 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 + # Remove probe_dir after test shutil.rmtree(self.probe_dir) - @patch('gratia.common.config.ConfigProxy.getFilenameFragment', create=True, return_value ='test-filename') - @patch('gratia.common.config.ConfigProxy.get_GratiaExtension', create=True, return_value ='test-extension') - def test_compress_outbox(self, mock_gratia_ext, mock_file_frag): + def test_compress_outbox(self): """CompressOutbox compresses the files in the outbox directory and stores the resulting tarball in probe_dir/staged. """ - #Parameters for function - probe_dir = self.probe_dir - outbox = self.outbox - outfiles = self.outfiles - - #Assert that CompressOutbox returns True - result = sandbox_mgmt.CompressOutbox(probe_dir, outbox, outfiles) + # Assert that CompressOutbox returns True + result = sandbox_mgmt.CompressOutbox(self.probe_dir, self.outbox, self.outfiles) self.assertTrue(result) - @patch('gratia.common.config.ConfigProxy.getFilenameFragment', create=True, return_value ='test-filename') - @patch('gratia.common.config.ConfigProxy.get_GratiaExtension', create=True, return_value ='test-extension') - def test_tarball_creation(self, mock_gratia_ext, mock_filefrag): + def test_tarball_creation(self): """ Assert that tarball is created in the correct location """ - #Parameters for function - probe_dir = self.probe_dir - outbox = self.outbox - outfiles = self.outfiles - - sandbox_mgmt.CompressOutbox(probe_dir, outbox, outfiles) - - #Finds tarball that matches GenerateFilename function output - os.chdir(f'{probe_dir}/staged/store') - for tarball in glob.glob("tz.*.test-extension__*"): - return(tarball) - - self.assertTrue(os.path.exists(path_to_tarball/{tarball})) - - @patch('gratia.common.config.ConfigProxy.getFilenameFragment', create=True, return_value ='test-filename') - @patch('gratia.common.config.ConfigProxy.get_GratiaExtension', create=True, return_value ='test-extension') - def test_tarball_contents(self, mock_gratia_ext, mock_filefrag): + 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) + tarball_location = os.path.join(f'{path_to_tarball}', tarball[0]) + # 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, + 'Tarball created in directory != 1') + + def test_tarball_contents(self): """ Assert that unpacked tarball contains files from outfiles """ - #Parameters for function - probe_dir = self.probe_dir - outbox = self.outbox - outfiles = self.outfiles - - sandbox_mgmt.CompressOutbox(probe_dir, outbox, outfiles) - - #Finds tarball that matches GenerateFilename() output - os.chdir(f'{probe_dir}/staged/store') - for tarball in glob.glob("tz.*.test-extension__*"): - return(tarball) - - #Gets names of files within tarball - file_obj= tarfile.open(tarball,"r") - namelist=file_obj.getnames() - for names in namelist: - return(names) - file_obj.close() - - # Sort both lists to ensure order-independent comparison - names.sort() - outfiles.sort() - - self.assertListEqual(names, 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 = os.path.join(f'{path_to_tarball}', tarball[0]) + + # 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') From a5c12d798c2170317df78efa93ddfd466b1a1539 Mon Sep 17 00:00:00 2001 From: Jeff Takaki <97972180+jtakaki-matc@users.noreply.github.com> Date: Wed, 16 Aug 2023 07:45:30 -0500 Subject: [PATCH 09/10] Fix test failure message for tarball count. Co-authored-by: Brian Lin --- test/test_sandbox_mgmt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_sandbox_mgmt.py b/test/test_sandbox_mgmt.py index 649d3915..4e9c5376 100644 --- a/test/test_sandbox_mgmt.py +++ b/test/test_sandbox_mgmt.py @@ -91,7 +91,7 @@ def test_tarball_creation(self): self.assertTrue(os.path.exists(tarball_location), 'Tarball not created in correct location') self.assertEqual(tarball_count, 1, - 'Tarball created in directory != 1') + f'Expected 1 tarball, found {tarball_count}') def test_tarball_contents(self): """ From c91b691ce905aeefa8f4c48e01c4a4e1639cb02d Mon Sep 17 00:00:00 2001 From: Jeff Takaki Date: Thu, 17 Aug 2023 13:04:44 -0500 Subject: [PATCH 10/10] Refactor tarball locator logic get_tarball_function handles IndexError exception and fails test if no tarball is created. test_tarball_creation and test_tarball_contents both make a call to this function --- test/test_sandbox_mgmt.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/test/test_sandbox_mgmt.py b/test/test_sandbox_mgmt.py index 4e9c5376..4647810a 100644 --- a/test/test_sandbox_mgmt.py +++ b/test/test_sandbox_mgmt.py @@ -7,6 +7,7 @@ import tempfile import unittest from unittest.mock import patch, PropertyMock +from unittest import TextTestRunner from common.gratia.common import sandbox_mgmt @@ -66,6 +67,18 @@ 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. @@ -81,10 +94,12 @@ def test_tarball_creation(self): 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) - tarball_location = os.path.join(f'{path_to_tarball}', tarball[0]) + + # 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)) @@ -102,8 +117,9 @@ def test_tarball_contents(self): # Finds tarball that matches GenerateFilename() output tarball = glob.glob("tz.*.test-extension__*", root_dir=path_to_tarball) + # Where tarball exists - tarball_location = os.path.join(f'{path_to_tarball}', tarball[0]) + tarball_location = self.get_tarball_location(path_to_tarball, tarball) # Gets names of files within tarball with tarfile.open(tarball_location, "r") as names: