def test_set_worker_race_condition_fail(self, mock_get, mock_create): """Test we cannot claim a work if we lose race condition.""" service.Service.service_id = mock.sentinel.service_id mock_get.side_effect = exception.WorkerNotFound mock_create.side_effect = exception.WorkerExists(type='type', id='id') backup = Backup(_context=self.context, status=mock.sentinel.status, id=mock.sentinel.id) self.assertRaises(exception.CleanableInUse, backup.set_worker) self.assertTrue(mock_get.called) self.assertTrue(mock_create.called)
def test_create_worker_already_exists(self, mock_create, mock_update): """Test worker creation when a worker for the resource exists.""" rpc.LAST_OBJ_VERSIONS[Backup.get_rpc_api().BINARY] = '1.3' mock_create.side_effect = exception.WorkerExists(type='type', id='id') backup = Backup(_context=self.context, status='cleanable', id=mock.sentinel.id) res = backup.create_worker() self.assertTrue(res) self.assertTrue(mock_create.called) mock_update.assert_called_once_with( self.context, None, filters={'resource_type': 'Backup', 'resource_id': mock.sentinel.id}, service_id=None, status='cleanable')
def test_create_worker_cleaning(self, mock_create, mock_update): """Test worker creation on race condition. Test that we still create an entry if there is a rare race condition that the entry gets removed from the DB between our failure to create it and our try to update the entry. """ rpc.LAST_OBJ_VERSIONS[Backup.get_rpc_api().BINARY] = '1.3' mock_create.side_effect = [ exception.WorkerExists(type='type', id='id'), mock.sentinel.worker] mock_update.side_effect = exception.WorkerNotFound backup = Backup(_context=self.context, status='cleanable', id=mock.sentinel.id) self.assertTrue(backup.create_worker()) self.assertEqual(2, mock_create.call_count) self.assertTrue(mock_update.called)