def test_open_handle_file_exists(self):

        path = os.path.join(self.metadata_file_dir, 'overwriteme.xml')
        context = MetadataFileContext(path)

        with open(path, 'w') as h:
            h.flush()

        context._open_metadata_file_handle()
Beispiel #2
0
 def test_exit_error(self, mock_log):
     path = os.path.join(self.metadata_file_dir, 'test.xml')
     context = MetadataFileContext(path)
     try:
         raise Exception()
     except Exception:
         ex_type, ex, tb = sys.exc_info()
         context.__exit__(ex_type, ex, tb)
     self.assertEquals(1, mock_log.debug.call_count)
Beispiel #3
0
    def test_open_handle_file_exists(self):

        path = os.path.join(self.metadata_file_dir, 'overwriteme.xml')
        context = MetadataFileContext(path)

        with open(path, 'w') as h:
            h.flush()

        context._open_metadata_file_handle()
 def test_exit_error(self, mock_log):
     path = os.path.join(self.metadata_file_dir, 'test.xml')
     context = MetadataFileContext(path)
     try:
         raise Exception()
     except Exception:
         ex_type, ex, tb = sys.exc_info()
         context.__exit__(ex_type, ex, tb)
     self.assertEquals(1, mock_log.debug.call_count)
    def test_open_handle(self):

        path = os.path.join(self.metadata_file_dir, 'open_handle.xml')
        context = MetadataFileContext(path)

        context._open_metadata_file_handle()

        self.assertTrue(os.path.exists(path))

        context._close_metadata_file_handle()
Beispiel #6
0
 def test_initialize_double_call(self):
     path = os.path.join(self.metadata_file_dir, 'test.xml')
     context = MetadataFileContext(path)
     context.initialize()
     context._write_file_header = Mock()
     context.initialize()
     self.assertEquals(0, context._write_file_header.call_count)
     context.finalize()
Beispiel #7
0
    def test_finalize_closed_gzip_file(self):
        # this test makes sure that we can properly detect the closed state of
        # a gzip file, because on python 2.6 we have to take special measures
        # to do so.
        path = os.path.join(os.path.dirname(__file__), '../../../data/foo.tar.gz')

        context = MetadataFileContext('/a/b/c')
        context.metadata_file_handle = gzip.open(path)
        context.metadata_file_handle.close()

        # just make sure this doesn't complain.
        context.finalize()
Beispiel #8
0
    def test_finalize_double_call(self):
        path = os.path.join(self.metadata_file_dir, 'test.xml')
        context = MetadataFileContext(path)

        context.initialize()
        context.finalize()
        context._write_file_footer = Mock(side_effect=Exception())
        context.finalize()
        self.assertEquals(context._write_file_footer.call_count, 0)
 def test_initialize_double_call(self):
     path = os.path.join(self.metadata_file_dir, 'test.xml')
     context = MetadataFileContext(path)
     context.initialize()
     context._write_file_header = Mock()
     context.initialize()
     self.assertEquals(0, context._write_file_header.call_count)
     context.finalize()
Beispiel #10
0
    def test_finalize_error_on_footer(self, mock_logger):

        path = os.path.join(self.metadata_file_dir, 'test.xml')
        context = MetadataFileContext(path)
        context._write_file_footer = Mock(side_effect=Exception('foo'))

        context._open_metadata_file_handle()
        context._write_file_header()

        context.finalize()

        self.assertTrue(mock_logger.called)
Beispiel #11
0
    def test_is_closed_gzip_file(self):
        path = os.path.join(os.path.dirname(__file__), '../../../data/foo.tar.gz')

        file_object = gzip.open(path)
        file_object.close()

        self.assertTrue(MetadataFileContext._is_closed(file_object))
Beispiel #12
0
    def test_initialize(self):

        path = os.path.join(self.metadata_file_dir, 'foo', 'header.xml')
        context = MetadataFileContext(path)

        context._write_file_header = Mock()

        context.initialize()

        context._write_file_header.assert_called_once_with()
        self.assertTrue(os.path.exists(path))

        with open(path) as h:
            content = h.read()
        expected_content = ''
        self.assertEqual(content, expected_content)
Beispiel #13
0
    def test_metadata_instantiation_with_checksum_type(self):
        test_checksum_type = 'sha1'

        try:
            metadata_file_context = MetadataFileContext('fu.xml', checksum_type=test_checksum_type)
        except Exception, e:
            self.fail(e.message)
Beispiel #14
0
    def test_initialize(self):

        path = os.path.join(self.metadata_file_dir, 'foo', 'header.xml')
        context = MetadataFileContext(path)

        context._write_file_header = Mock()

        context.initialize()

        context._write_file_header.assert_called_once_with()
        self.assertTrue(os.path.exists(path))

        with open(path) as h:
            content = h.read()
        expected_content = ''
        self.assertEqual(content, expected_content)
Beispiel #15
0
    def test_finalize_double_call(self):
        path = os.path.join(self.metadata_file_dir, 'test.xml')
        context = MetadataFileContext(path)

        context.initialize()
        context.finalize()
        context._write_file_footer = Mock(side_effect=Exception())
        context.finalize()
        self.assertEquals(context._write_file_footer.call_count, 0)
Beispiel #16
0
    def test_is_closed_file(self):
        path = os.path.join(os.path.dirname(__file__), '../../../data/foo.tar.gz')

        # opening as a regular file, not with gzip
        file_object = open(path)
        file_object.close()

        self.assertTrue(MetadataFileContext._is_closed(file_object))
Beispiel #17
0
    def test_open_handle_bad_parent_permissions(self):

        parent_path = os.path.join(self.metadata_file_dir, 'parent')
        path = os.path.join(parent_path, 'nope.xml')
        context = MetadataFileContext(path)

        os.makedirs(parent_path, mode=0000)
        self.assertRaises(RuntimeError, context._open_metadata_file_handle)
        os.chmod(parent_path, 0777)
Beispiel #18
0
    def test_open_handle_bad_file_permissions(self):

        path = os.path.join(self.metadata_file_dir, 'nope_again.xml')
        context = MetadataFileContext(path)

        with open(path, 'w') as h:
            h.flush()
        os.chmod(path, 0000)

        self.assertRaises(RuntimeError, context._open_metadata_file_handle)
Beispiel #19
0
    def test_finalize_error_on_close(self, mock_logger):

        path = os.path.join(self.metadata_file_dir, 'test.xml')
        context = MetadataFileContext(path)
        context._close_metadata_file_handle = Mock(side_effect=Exception('foo'))

        context._open_metadata_file_handle()
        context._write_file_header()

        context.finalize()

        self.assertTrue(mock_logger.called)
Beispiel #20
0
    def test_finalize_with_valid_checksum_type(self):

        path = os.path.join(self.metadata_file_dir, 'test.xml')
        checksum_type = 'sha1'
        context = MetadataFileContext(path, checksum_type)

        context._open_metadata_file_handle()
        context._write_file_header()
        context.finalize()

        expected_metadata_file_name = context.checksum + '-' + 'test.xml'
        expected_metadata_file_path = os.path.join(self.metadata_file_dir,
                                                   expected_metadata_file_name)
        self.assertEquals(expected_metadata_file_path, context.metadata_file_path)
Beispiel #21
0
    def test_finalize_checksum_type_none(self):

        path = os.path.join(self.metadata_file_dir, 'test.xml')
        context = MetadataFileContext(path)

        context.initialize()
        context._write_file_footer = Mock()
        context.finalize()
        context._write_file_footer.assert_called_once_with()

        self.assertEqual(context.metadata_file_path, path)
        self.assertEqual(context.metadata_file_handle, None)
Beispiel #22
0
    def test_open_handle_gzip(self):

        path = os.path.join(self.metadata_file_dir, 'test.xml.gz')
        context = MetadataFileContext(path)

        context._open_metadata_file_handle()

        self.assertTrue(os.path.exists(path))

        context._write_file_header()
        context._close_metadata_file_handle()

        try:
            h = gzip.open(path)

        except Exception, e:
            self.fail(e.message)
Beispiel #23
0
    def test_open_handle(self):

        path = os.path.join(self.metadata_file_dir, 'open_handle.xml')
        context = MetadataFileContext(path)

        context._open_metadata_file_handle()

        self.assertTrue(os.path.exists(path))

        context._close_metadata_file_handle()
Beispiel #24
0
    def test_finalize_error_on_write_footer(self):
        # Ensure that if the write_file_footer throws an exception we eat it so that
        # if multiple metadata files are being finalized, one error won't cause open
        # file handles on the others

        path = os.path.join(self.metadata_file_dir, 'test.xml')
        context = MetadataFileContext(path)

        context.initialize()
        context._write_file_footer = Mock(side_effect=Exception())
        context.finalize()
        context._write_file_footer.assert_called_once_with()

        self.assertEqual(context.metadata_file_path, path)
Beispiel #25
0
    def test_finalize_checksum_type_none(self):

        path = os.path.join(self.metadata_file_dir, 'test.xml')
        context = MetadataFileContext(path)

        context.initialize()
        context._write_file_footer = Mock()
        context.finalize()
        context._write_file_footer.assert_called_once_with()

        self.assertEqual(context.metadata_file_path, path)
        self.assertEqual(context.metadata_file_handle, None)
Beispiel #26
0
    def test_open_handle_gzip(self):

        path = os.path.join(self.metadata_file_dir, 'test.xml.gz')
        context = MetadataFileContext(path)

        context._open_metadata_file_handle()

        self.assertTrue(os.path.exists(path))

        context._write_file_header()
        context._close_metadata_file_handle()

        try:
            h = gzip.open(path)

        except Exception, e:
            self.fail(e.message)
Beispiel #27
0
    def test_finalize_error_on_write_footer(self):
        # Ensure that if the write_file_footer throws an exception we eat it so that
        # if multiple metadata files are being finalized, one error won't cause open
        # file handles on the others

        path = os.path.join(self.metadata_file_dir, 'test.xml')
        context = MetadataFileContext(path)

        context.initialize()
        context._write_file_footer = Mock(side_effect=Exception())
        context.finalize()
        context._write_file_footer.assert_called_once_with()

        self.assertEqual(context.metadata_file_path, path)
Beispiel #28
0
 def test_enter(self):
     path = os.path.join(self.metadata_file_dir, 'test.xml')
     context = MetadataFileContext(path)
     context.initialize = Mock()
     context.__enter__()
     context.initialize.assert_called_once_with()
Beispiel #29
0
 def test_enter(self):
     path = os.path.join(self.metadata_file_dir, 'test.xml')
     context = MetadataFileContext(path)
     context.initialize = Mock()
     context.__enter__()
     context.initialize.assert_called_once_with()
Beispiel #30
0
 def test_metadata_instantiation(self):
     try:
         metadata_file_context = MetadataFileContext('fu.xml')
     except Exception, e:
         self.fail(e.message)