def test_lock_exception_create(self): pulp = flexmock() pulp.should_receive('createRepo').and_raise(RuntimeError).once() with pytest.raises(RuntimeError): with LockedPulpRepository(pulp, 'redhat-repo'): assert False, "Should not get here"
def test_lock_bad_params(self): """ Should fail if 'prefix' is not a string """ pulp = flexmock() pulp.should_receive('createRepo').never() pulp.should_receive('deleteRepo').never() with pytest.raises(Exception): with LockedPulpRepository(pulp, 'redhat-repo', prefix=None): pass
def test_lock_exception_delete_ignored(self): """ dockpulp.errors.DockPulpError should be ignored when exiting the context manager. """ pulp = flexmock() pulp.should_receive('createRepo').once().ordered() (pulp.should_receive('deleteRepo').and_raise( dockpulp.errors.DockPulpError('error deleting')).once().ordered()) with LockedPulpRepository(pulp, 'redhat-repo'): pass
def test_lock_exception_delete_propagate(self): """ dockpulp.errors.DockPulpError raised within the context should be propagated up the call stack. """ pulp = flexmock() pulp.should_receive('createRepo').once().ordered() pulp.should_receive('deleteRepo').once().ordered() entered = False with pytest.raises(dockpulp.errors.DockPulpError): with LockedPulpRepository(pulp, 'redhat-repo'): entered = True raise dockpulp.errors.DockPulpError('random error') assert entered
def test_lock_retry(self, failures): # Don't actually wait when retrying flexmock(time).should_receive('sleep') pulp = flexmock() expectation = pulp.should_receive('createRepo').times(failures + 1) exc = dockpulp.errors.DockPulpError('error creating') for i in range(failures): expectation = expectation.and_raise(exc).ordered() expectation.and_return(None).ordered pulp.should_receive('deleteRepo').once().ordered() with LockedPulpRepository(pulp, 'redhat-repo'): pass
def test_lock_exception_delete_raise(self): """ Other exceptions from deleting should be propagated when exiting the context manager. """ pulp = flexmock() pulp.should_receive('createRepo').once().ordered() (pulp.should_receive('deleteRepo').and_raise( RuntimeError).once().ordered()) entered = False with pytest.raises(RuntimeError): with LockedPulpRepository(pulp, 'redhat-repo'): entered = True assert entered
def test_lock_no_errors(self, repo_id, kwargs, expected_repo_id): pulp = flexmock() # First it should call createRepo() createRepo_kwargs = { 'registry_id': object, # don't care what this is 'is_origin': True, 'prefix_with': kwargs.get('prefix', 'lock-'), } (pulp.should_receive('createRepo').with_args( repo_id, object, **createRepo_kwargs).once().ordered()) with LockedPulpRepository(pulp, repo_id, **kwargs) as lock: assert isinstance(lock, LockedPulpRepository) # Next, deleteRepo() -- but with the full repository id. # Yes, dockpulp really works like this. (pulp.should_receive('deleteRepo').with_args( expected_repo_id).once().ordered())
def test_lock_break(self): class Elapsed(object): def __init__(self): self.seconds = 0 def sleep(self, s): self.seconds += s # Don't actually wait when retrying, just measure time elapsed = Elapsed() flexmock(time).should_receive('sleep').replace_with(elapsed.sleep) pulp = flexmock() (pulp.should_receive('createRepo').times( LOCKEDPULPREPOSITORY_RETRIES + 1).and_raise( dockpulp.errors.DockPulpError('error creating'))) pulp.should_receive('deleteRepo').once().ordered() with LockedPulpRepository(pulp, 'redhat-repo'): # Should wait at least an hour before breaking the lock assert elapsed.seconds > 60 * 60