def test_open_metadata_file_handle_existing_checksum_file(self, mock_generator): context = FastForwardXmlFileContext(os.path.join(self.working_dir, 'test.xml'), self.tag, 'package', self.attributes, checksum_type=TYPE_SHA1) context._open_metadata_file_handle() self.assertTrue(context.fast_forward) self.assertEquals(context.existing_file, os.path.join(self.working_dir, 'aa-test.xml'))
def test_open_metadata_file_handle_existing_gzip_file(self, mock_generator): context = FastForwardXmlFileContext(os.path.join(self.working_dir, 'test.xml.gz'), self.tag, 'package', self.attributes) context._open_metadata_file_handle() self.assertTrue(context.fast_forward) self.assertEquals(context.existing_file, os.path.join(self.working_dir, 'original.test.xml'))
def test_write_file_header_fast_forward_small_buffer_gzip(self): test_file = os.path.join(self.working_dir, 'test.xml.gz') test_content = '' test_file_handle = gzip.open(test_file) content = test_file_handle.read() while content: test_content += content content = test_file_handle.read() test_file_handle.close() test_content = test_content[:test_content.rfind('</metadata')] context = FastForwardXmlFileContext(test_file, self.tag, 'package', self.attributes) context._open_metadata_file_handle() # Ensure the context doesn't find the opening tag during the initial search context._write_file_header() context.metadata_file_handle.close() created_content = '' test_file_handle = gzip.open(test_file) content = test_file_handle.read() while content: created_content += content content = test_file_handle.read() test_file_handle.close() self.assertEquals(test_content, created_content)
def test_close_metadata_file_handle_cleans_up_file_gz(self): shutil.copy(os.path.join(self.metadata_dir, 'test.xml.gz'), os.path.join(self.working_dir, 'test.xml.gz')) context = FastForwardXmlFileContext(os.path.join(self.working_dir, 'test.xml.gz'), self.tag, self.attributes) context._open_metadata_file_handle() context._close_metadata_file_handle() self.assertEquals(1, len(os.listdir(self.working_dir)))
def test_open_metadata_file_handle_existing_file(self, mock_generator): shutil.copy(os.path.join(self.metadata_dir, 'test.xml'), os.path.join(self.working_dir, 'test.xml')) context = FastForwardXmlFileContext(os.path.join(self.working_dir, 'test.xml'), self.tag, 'package', self.attributes) context._open_metadata_file_handle() self.assertTrue(context.fast_forward) # check that the file has been renamed self.assertEqual(context.existing_file, os.path.join(self.working_dir, 'original.test.xml'))
def test_open_metadata_file_handle_existing_file(self, mock_generator): shutil.copy(os.path.join(self.metadata_dir, 'test.xml'), os.path.join(self.working_dir, 'test.xml')) context = FastForwardXmlFileContext( os.path.join(self.working_dir, 'test.xml'), self.tag, 'package', self.attributes) context._open_metadata_file_handle() self.assertTrue(context.fast_forward) # check that the file has been renamed self.assertEquals(context.existing_file, os.path.join(self.working_dir, 'original.test.xml'))
def test_open_metadata_file_handle_existing_checksum_file(self, mock_generator): shutil.copy(os.path.join(self.metadata_dir, 'aa-test.xml'), os.path.join(self.working_dir, 'aa-test.xml')) context = FastForwardXmlFileContext(os.path.join(self.working_dir, 'test.xml'), self.tag, 'package', self.attributes, checksum_type=TYPE_SHA1) # Copy the aa-test file a second time and ensure that the latest creation is opened # we are using sleep since overriding os.stat().st_mtime would break the shutil.copy sleep(0.0005) # Ensure the timestamps are different shutil.copy(os.path.join(self.working_dir, 'aa-test.xml'), os.path.join(self.working_dir, 'a-test.xml')) sleep(0.0005) # Ensure the timestamps are different shutil.copy(os.path.join(self.working_dir, 'aa-test.xml'), os.path.join(self.working_dir, 'bb-test.xml')) context._open_metadata_file_handle() self.assertTrue(context.fast_forward) self.assertEquals(context.existing_file, os.path.join(self.working_dir, 'original.bb-test.xml'))
def test_close_metadata_file_handle_no_fast_forward(self): context = FastForwardXmlFileContext(os.path.join( self.working_dir, 'test.xml'), self.tag, self.attributes, checksum_type=TYPE_SHA1) context._open_metadata_file_handle() context._close_metadata_file_handle() self.assertTrue(context._is_closed(context.metadata_file_handle))
def test_close_metadata_file_handle_cleans_up_file(self): context = FastForwardXmlFileContext(os.path.join(self.working_dir, 'test.xml'), self.tag, self.attributes, checksum_type=TYPE_SHA1) context._open_metadata_file_handle() context._close_metadata_file_handle() self.assertFalse(os.path.exists(os.path.join(self.working_dir, 'aa-test.xml')))
def test_write_file_header_fast_forward_small_buffer_gzip(self): shutil.copy(os.path.join(self.metadata_dir, 'test.xml.gz'), os.path.join(self.working_dir, 'test.xml.gz')) test_file = os.path.join(self.working_dir, 'test.xml.gz') test_content = '' test_file_handle = gzip.open(test_file) content = test_file_handle.read() while content: test_content += content content = test_file_handle.read() test_file_handle.close() test_content = test_content[:test_content.rfind('</metadata')] context = FastForwardXmlFileContext(test_file, self.tag, 'package', self.attributes) context._open_metadata_file_handle() # Ensure the context doesn't find the opening tag during the initial search context._write_file_header() context.metadata_file_handle.close() created_content = '' test_file_handle = gzip.open(test_file) content = test_file_handle.read() while content: created_content += content content = test_file_handle.read() test_file_handle.close() self.assertEquals(test_content, created_content)
def _test_fast_forward(self, test_file, test_content=None): """ :param test_file: The name of the file in the data/metadata/* directory to use for testing :type test_file: str :param test_content: The string to compare against. If not specified the content of test_file is used :type test_content: str """ test_file = os.path.join(self.working_dir, test_file) if test_content is None: test_content = '' with open(test_file) as test_file_handle: content = test_file_handle.read() while content: test_content += content content = test_file_handle.read() test_content = test_content[:test_content.rfind('</metadata')] context = FastForwardXmlFileContext(test_file, self.tag, 'package', self.attributes) context._open_metadata_file_handle() # Ensure the context doesn't find the opening tag during the initial search context._write_file_header() context.metadata_file_handle.close() created_content = '' with open(test_file) as test_file_handle: content = test_file_handle.read() while content: created_content += content content = test_file_handle.read() self.assertEquals(test_content, created_content)
def test_write_file_header_no_fast_forward(self, mock_generator): context = FastForwardXmlFileContext(os.path.join(self.working_dir, 'aa.xml'), self.tag, self.attributes) context._open_metadata_file_handle() context._write_file_header() mock_generator.return_value.startDocument.assert_called_once_with()
def test_open_metadata_file_handle_existing_checksum_file( self, mock_generator): shutil.copy(os.path.join(self.metadata_dir, 'aa-test.xml'), os.path.join(self.working_dir, 'aa-test.xml')) context = FastForwardXmlFileContext(os.path.join( self.working_dir, 'test.xml'), self.tag, 'package', self.attributes, checksum_type=TYPE_SHA1) # Copy the aa-test file a second time and ensure that the latest creation is opened # we are using sleep since overriding os.stat().st_mtime would break the shutil.copy sleep(0.0005) # Ensure the timestamps are different shutil.copy(os.path.join(self.working_dir, 'aa-test.xml'), os.path.join(self.working_dir, 'a-test.xml')) sleep(0.0005) # Ensure the timestamps are different shutil.copy(os.path.join(self.working_dir, 'aa-test.xml'), os.path.join(self.working_dir, 'bb-test.xml')) context._open_metadata_file_handle() self.assertTrue(context.fast_forward) self.assertEquals( context.existing_file, os.path.join(self.working_dir, 'original.bb-test.xml'))
def test_close_metadata_file_handle_cleans_up_file_gz(self): shutil.copy(os.path.join(self.metadata_dir, 'test.xml.gz'), os.path.join(self.working_dir, 'test.xml.gz')) context = FastForwardXmlFileContext( os.path.join(self.working_dir, 'test.xml.gz'), self.tag, self.attributes) context._open_metadata_file_handle() context._close_metadata_file_handle() self.assertEquals(1, len(os.listdir(self.working_dir)))
def test_close_metadata_file_handle_cleans_up_file_checksum_and_gz(self): shutil.copy(os.path.join(self.metadata_dir, 'bb-test.xml.gz'), os.path.join(self.working_dir, 'bb-test.xml.gz')) context = FastForwardXmlFileContext(os.path.join( self.working_dir, 'test.xml.gz'), self.tag, self.attributes, checksum_type=TYPE_SHA1) context._open_metadata_file_handle() context._close_metadata_file_handle() # 5573: preserve the old metadata files self.assertEquals(2, len(os.listdir(self.working_dir)))
def test_open_metadata_file_handle_non_existent_file(self, mock_generator): os.remove(os.path.join(self.working_dir, 'test.xml')) context = FastForwardXmlFileContext(os.path.join(self.working_dir, 'test.xml'), self.tag, 'packages', self.attributes) context._open_metadata_file_handle() self.assertFalse(context.fast_forward)
def test_open_metadata_file_handle_non_existent_file(self, mock_generator): context = FastForwardXmlFileContext( os.path.join(self.working_dir, 'test.xml'), self.tag, 'packages', self.attributes) context._open_metadata_file_handle() self.assertFalse(context.fast_forward)
def test_close_metadata_file_handle_no_fast_forward(self): context = FastForwardXmlFileContext(os.path.join(self.working_dir, 'test.xml'), self.tag, self.attributes, checksum_type=TYPE_SHA1) context._open_metadata_file_handle() context._close_metadata_file_handle() self.assertTrue(context._is_closed(context.metadata_file_handle))