async def check_server_exists(servers_repo: ServersRepository, slug: str) -> bool: try: servers_repo.get_server_by_slug(slug=slug) except EntityDoesNotExist: return False return True
def test_clean_server( mocker: MockFixture, servers_repository: ServersRepository, test_server_in_db: Server, ) -> None: mocker.patch("app.severs.services.run_clean_playbook") assert clean_server(servers_repository, test_server_in_db.slug) is None with pytest.raises(EntityDoesNotExist): servers_repository.get_server_by_slug(test_server_in_db.slug)
def clean_server(servers_repo: ServersRepository, server_slug: str) -> None: """Run's clean server ansible playbook""" server = servers_repo.get_server_by_slug(server_slug) if server.is_busy: raise ServerIsBusy servers_repo.update_server(server=server, status=ServerStatus.pending) run_clean_playbook(server.slug) servers_repo.delete_server(server=server)
def test_get_multiple_servers_in_list(servers_repository: ServersRepository): server_data = { "slug": "server-slug", "branch_name": "some-branch", } servers_repository.create_server( slug=server_data["slug"], branch_name=server_data["branch_name"], ) servers_list = servers_repository.get_servers_list() assert len(servers_list) == 1 assert servers_list[0].slug == server_data["slug"]
def test_get_server_by_slug(servers_repository: ServersRepository): server_data = { "slug": "server-slug", "branch_name": "some-branch", } server = servers_repository.create_server( slug=server_data["slug"], branch_name=server_data["branch_name"], ) assert server.slug == server_data["slug"] fetched_server = servers_repository.get_server_by_slug(server.slug) assert server.branch_name == fetched_server.branch_name
def clean_server_task(self: Task, server_slug: str) -> None: redis = get_redis_connection() servers_repository = ServersRepository(redis) try: clean_server(servers_repository, server_slug) except ServerIsBusy as exc: logger.warning( f"Could not clean {server_slug} server. Server is busy.") self.retry(exc=exc) except EntityDoesNotExist: logger.warning( f"Could not clean {server_slug} server. Not found error.")
def list_servers( servers_repo: ServersRepository = Depends( get_repository(ServersRepository)), ) -> ListOfServersInResponse: servers = servers_repo.get_servers_list() servers_for_response = [ ServerForResponse.from_orm(server) for server in servers ] return ListOfServersInResponse( servers=servers_for_response, servers_count=len(servers), )
async def get_server_by_slug_from_path( slug: str = Path(..., min_length=1), servers_repo: ServersRepository = Depends( get_repository(ServersRepository)), ) -> Server: try: return servers_repo.get_server_by_slug(slug=slug) except EntityDoesNotExist: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Server does not exists", )
def test_delete_server(servers_repository: ServersRepository): server_data_list = [ { "slug": "server-slug", "branch_name": "some-branch", }, { "slug": "server-slug-2", "branch_name": "some-branch-2", }, ] for server_data in server_data_list: servers_repository.create_server( slug=server_data["slug"], branch_name=server_data["branch_name"], ) servers_list = servers_repository.get_servers_list() assert len(servers_list) == 2 server_to_delete = Server(**server_data_list[0]) servers_repository.delete_server(server_to_delete) servers_list = servers_repository.get_servers_list() assert len(servers_list) == 1 assert servers_list[0].slug == server_data_list[1]["slug"]
async def create_new_server( server_create: ServerInCreate = Body(..., embed=True, alias="server"), servers_repo: ServersRepository = Depends( get_repository(ServersRepository)), ) -> ServerInResponse: server = servers_repo.create_server( slug=server_create.slug, branch_name=server_create.branch_name, status=server_create.status, mr_id=server_create.mr_id, mr_iid=server_create.mr_iid, mr_status=server_create.mr_status, ) return ServerInResponse(server=ServerForResponse.from_orm(server))
async def update_server_by_slug( server_update: ServerInUpdate = Body(..., embed=True, alias="server"), current_server: Server = Depends(get_server_by_slug_from_path), servers_repo: ServersRepository = Depends( get_repository(ServersRepository)), ) -> ServerInResponse: server = servers_repo.update_server( server=current_server, branch_name=server_update.branch_name, status=server_update.status, mr_id=server_update.mr_id, mr_iid=server_update.mr_iid, mr_status=server_update.mr_status, ) return ServerInResponse(server=ServerForResponse.from_orm(server))
def test_clean_server_raise_server_busy(servers_repository: ServersRepository, test_server_in_db: Server) -> None: servers_repository.update_server(server=test_server_in_db, status=ServerStatus.pending) with pytest.raises(ServerIsBusy): clean_server(servers_repository, test_server_in_db.slug)
def test_get_entity_does_not_exists_on_missing_slug( servers_repository: ServersRepository, ): missing_slug = "missing-slug" with pytest.raises(EntityDoesNotExist): servers_repository.get_server_by_slug(missing_slug)
def test_get_empty_servers_list(servers_repository: ServersRepository): servers_list = servers_repository.get_servers_list() assert servers_list == []
def servers_repository(app: FastAPI) -> ServersRepository: redis: Redis = app.state.pool.get_redis() repository = ServersRepository(redis=redis) return repository
def test_server_in_db(test_server: Server, servers_repository: ServersRepository) -> Server: return servers_repository.create_server(**test_server.dict())
def delete_server_by_slug( server: Server = Depends(get_server_by_slug_from_path), servers_repo: ServersRepository = Depends( get_repository(ServersRepository)), ) -> None: servers_repo.delete_server(server=server)