Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Commit

Permalink
Fix orphan purge to not fail when content not downloaded.
Browse files Browse the repository at this point in the history
closes #1586
  • Loading branch information
jortel committed Feb 1, 2016
1 parent f0430cf commit 1775967
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 17 deletions.
4 changes: 4 additions & 0 deletions server/pulp/server/managers/content/orphan.py
Expand Up @@ -287,6 +287,10 @@ def delete_orphaned_file(path):
@param path: absolute path to the file to delete
@type path: str
"""
if not os.path.lexists(path):
_logger.debug(_('Path: {p} does not exist').format(p=path))
return

_logger.debug(_('Deleting orphaned file: %(p)s') % {'p': path})

if not os.path.isabs(path):
Expand Down
85 changes: 68 additions & 17 deletions server/test/unit/server/managers/content/test_orphan.py
Expand Up @@ -485,21 +485,38 @@ def test_delete_target_not_sibling(self, delete, read_link, listdir):
delete.assert_called_once_with(path)


@patch('pulp.server.managers.content.orphan.os.rmdir')
@patch('os.path.lexists')
@patch('os.rmdir')
@patch('pulp.server.managers.content.orphan.pulp_config.config')
@patch('pulp.server.managers.content.orphan.OrphanManager.delete')
@patch('pulp.server.managers.content.orphan.OrphanManager.unlink_shared')
@patch('pulp.server.managers.content.orphan.OrphanManager.is_shared')
class TestDeleteOrphanedFile(TestCase):

def test_not_absolute_path(self, is_shared, unlink_shared, delete, config, m_rmdir):
def test_not_absolute_path(self, is_shared, unlink_shared, delete, config, rmdir, lexists):
lexists.return_value = True
self.assertRaises(ValueError, OrphanManager.delete_orphaned_file, 'path-1')

def test_shared(self, is_shared, unlink_shared, delete, config, m_os):
def test_path_not_exists(self, is_shared, unlink_shared, delete, config, rmdir, lexists):
lexists.return_value = False
path = '/tmp/path-1'

# test
OrphanManager.delete_orphaned_file(path)

# validation
lexists.assert_called_once_with(path)
self.assertFalse(is_shared.called)
self.assertFalse(delete.called)
self.assertFalse(rmdir.called)
self.assertFalse(unlink_shared.called)

def test_shared(self, is_shared, unlink_shared, delete, config, rmdir, lexists):
path = '/path-1'
storage_dir = '/storage/pulp/dir'
is_shared.return_value = True
config.get.return_value = storage_dir
lexists.return_value = True

# test
OrphanManager.delete_orphaned_file(path)
Expand All @@ -509,11 +526,12 @@ def test_shared(self, is_shared, unlink_shared, delete, config, m_os):
unlink_shared.assert_called_once_with(path)
self.assertFalse(delete.called)

def test_not_shared(self, is_shared, unlink_shared, delete, config, m_rmdir):
def test_not_shared(self, is_shared, unlink_shared, delete, config, rmdir, lexists):
path = '/path-1'
storage_dir = '/storage/pulp/dir'
is_shared.return_value = False
config.get.return_value = storage_dir
lexists.return_value = True

# test
OrphanManager.delete_orphaned_file(path)
Expand All @@ -525,49 +543,82 @@ def test_not_shared(self, is_shared, unlink_shared, delete, config, m_rmdir):

@patch('pulp.server.managers.content.orphan.os.access')
@patch('pulp.server.managers.content.orphan.os.listdir')
def test_clean_non_root(self, mls, maccess, is_shared, unlink_shared, delete, config, m_rmdir):
def test_clean_non_root(
self,
listdir,
access,
is_shared,
unlink_shared,
delete,
config,
rmdir,
lexists):
"""
Ensure that empty directories more than 1 level up from root are removed.
"""
mls.return_value = None
maccess.return_value = True
listdir.return_value = None
access.return_value = True
path = '/storage/pulp/content/test/lvl1/lvl2/thing.remove'
storage_dir = '/storage/pulp'
is_shared.return_value = False
config.get.return_value = storage_dir
lexists.return_value = True

OrphanManager.delete_orphaned_file(path)
m_rmdir.assert_has_calls([call('/storage/pulp/content/test/lvl1/lvl2'),
call('/storage/pulp/content/test/lvl1')])
rmdir.assert_has_calls(
[
call('/storage/pulp/content/test/lvl1/lvl2'),
call('/storage/pulp/content/test/lvl1')
])

@patch('pulp.server.managers.content.orphan.os.access')
@patch('pulp.server.managers.content.orphan.os.listdir')
def test_clean_nonempty(self, mls, maccess, is_shared, unlink_shared, delete, config, m_rmdir):
def test_clean_nonempty(
self,
listdir,
access,
is_shared,
unlink_shared,
delete,
config,
rmdir,
lexists):
"""
Ensure that nonempty directories are not removed.
"""
mls.return_value = ['some', 'files']
maccess.return_value = True
listdir.return_value = ['some', 'files']
access.return_value = True
path = '/storage/pulp/content/test/lvl1/lvl2/thing.remove'
storage_dir = '/storage/pulp'
is_shared.return_value = False
config.get.return_value = storage_dir
lexists.return_value = True

OrphanManager.delete_orphaned_file(path)
self.assertFalse(m_rmdir.called)
self.assertFalse(rmdir.called)

@patch('pulp.server.managers.content.orphan.os.access')
@patch('pulp.server.managers.content.orphan.os.listdir')
def test_clean_no_access(self, mls, maccess, is_shared, unlink_shared, delete, config, m_rmdir):
def test_clean_no_access(
self,
listdir,
access,
is_shared,
unlink_shared,
delete,
config,
rmdir,
lexists):
"""
Ensure that rmdir is not called when user does not have access to dir.
"""
mls.return_value = None
maccess.return_value = False
listdir.return_value = None
access.return_value = False
path = '/storage/pulp/content/test/lvl1/lvl2/thing.remove'
storage_dir = '/storage/pulp'
is_shared.return_value = False
config.get.return_value = storage_dir
lexists.return_value = True

OrphanManager.delete_orphaned_file(path)
self.assertFalse(m_rmdir.called)
self.assertFalse(rmdir.called)

0 comments on commit 1775967

Please sign in to comment.