diff --git a/.github/workflows/poetry-test.yml b/.github/workflows/poetry-test.yml index 1690bcb..52c8358 100644 --- a/.github/workflows/poetry-test.yml +++ b/.github/workflows/poetry-test.yml @@ -37,7 +37,6 @@ jobs: curl -sSL "https://install.python-poetry.org" | python - # Adding `poetry` to `$PATH`: echo "$HOME/.local/bin" >> $GITHUB_PATH - apt-get update && apt-get install --no-install-recommends -y ffmpeg - name: Set up cache uses: actions/cache@v3 @@ -64,14 +63,10 @@ jobs: #---------------------------------------------- # run test suite #---------------------------------------------- - - name: Run tests - run: | - source .venv/bin/activate - poetry run pytest bot_microservice/ -vv --exitfirst - name: Coverage report run: | - poetry run coverage run -m pytest bot_microservice/ - poetry run coverage report + cp bot_microservice/serrings/.env.ci.runtests bot_microservice/serrings/.env + docker compose run bot poetry run bash -c "coverage run -m pytest -vv --exitfirst && poetry run coverage report" - name: Extended checks run: | poetry run poetry check diff --git a/README.md b/README.md index 3e95837..2a035a4 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ poetry run pytest ### Run tests in docker compose: ```bash -docker compose run bot poetry run pytest bot_microservice/ +STAGE=runtests docker compose run bot bash -c "coverage run -m pytest -vv --exitfirst && poetry run coverage report" ``` ## Docs diff --git a/bot_microservice/core/bot.py b/bot_microservice/core/bot.py index d54e60f..cd2051e 100644 --- a/bot_microservice/core/bot.py +++ b/bot_microservice/core/bot.py @@ -40,6 +40,8 @@ class BotApplication: logger.info("webhook has been deleted") async def polling(self) -> None: + if self.settings.STAGE == "runtests": + return None await self.application.initialize() await self.application.start() await self.application.updater.start_polling() # type: ignore diff --git a/bot_microservice/settings/.env.ci.runtests b/bot_microservice/settings/.env.ci.runtests index 60e125f..2a38414 100644 --- a/bot_microservice/settings/.env.ci.runtests +++ b/bot_microservice/settings/.env.ci.runtests @@ -13,7 +13,7 @@ DOMAIN="http://localhost" URL_PREFIX= # set to true to start with webhook. Else bot will start on polling method -START_WITH_WEBHOOK="true" +START_WITH_WEBHOOK="false" # quantity of workers for uvicorn WORKERS_COUNT=1 diff --git a/bot_microservice/settings/.env.local.runtests b/bot_microservice/settings/.env.local.runtests index 60e125f..2a38414 100644 --- a/bot_microservice/settings/.env.local.runtests +++ b/bot_microservice/settings/.env.local.runtests @@ -13,7 +13,7 @@ DOMAIN="http://localhost" URL_PREFIX= # set to true to start with webhook. Else bot will start on polling method -START_WITH_WEBHOOK="true" +START_WITH_WEBHOOK="false" # quantity of workers for uvicorn WORKERS_COUNT=1 diff --git a/bot_microservice/settings/config.py b/bot_microservice/settings/config.py index c446e78..18ef9b1 100644 --- a/bot_microservice/settings/config.py +++ b/bot_microservice/settings/config.py @@ -1,10 +1,11 @@ from functools import cached_property from os import environ from pathlib import Path +from typing import Any from constants import API_PREFIX from dotenv import load_dotenv -from pydantic import HttpUrl +from pydantic import HttpUrl, ValidationInfo, field_validator from pydantic_settings import BaseSettings BASE_DIR = Path(__file__).parent.parent @@ -51,6 +52,12 @@ class AppSettings(SentrySettings, BaseSettings): # Enable uvicorn reloading RELOAD: bool = False + @field_validator("START_WITH_WEBHOOK") + def star_with_webhook_validator(cls, field_value: Any, info: ValidationInfo) -> Any: + if field_value == "false": + return False + return field_value + @cached_property def api_prefix(self) -> str: if self.URL_PREFIX: diff --git a/deploy/Dockerfile b/deploy/Dockerfile index 85f63bc..04d6640 100644 --- a/deploy/Dockerfile +++ b/deploy/Dockerfile @@ -1,13 +1,16 @@ FROM python:3.11.5 AS compile-image -ARG USER +ARG USER=web +ARG STAGE + + ENV PYTHONUNBUFFERED=1 \ PIP_DISABLE_PIP_VERSION_CHECK=on \ DOCKER_CONTAINER=1 \ POETRY_VERSION=1.6.1 -RUN printf "================ Start build base service. with USER: ${USER} ===============" \ +RUN printf "================ Start build base service. with USER: ${USER}, STAGE: ${STAGE} ===============" \ && apt-get update \ && apt-get install --no-install-recommends -y \ bash \ @@ -31,7 +34,7 @@ WORKDIR /app/ RUN if [ "$USER" != "root" ]; then \ mkdir /home/${USER} \ && groupadd -r ${USER} && useradd -d /home/${USER} -r -g ${USER} ${USER} \ - && chown ${USER}:${USER} -R /home/${USER}; \ + && chown ${USER}:${USER} -R /home/${USER} /app; \ fi COPY --chown=${USER}:${USER} ../poetry.lock ../pyproject.toml /app/ @@ -39,7 +42,7 @@ COPY --chown=${USER}:${USER} ../poetry.lock ../pyproject.toml /app/ RUN pip install --upgrade pip && pip install poetry==$POETRY_VERSION RUN poetry config virtualenvs.in-project true && \ poetry config virtualenvs.create true && \ - poetry install $(if [ "$USER" != "root" ]; then echo "--only main"; fi) --no-interaction --no-ansi && \ + poetry install $(if [ "$STAGE" = "production" ]; then echo "--only main"; fi) --no-interaction --no-ansi && \ rm -rf $POETRY_CACHE_DIR diff --git a/docker-compose.yml b/docker-compose.yml index adf5a5a..4423098 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,20 +19,15 @@ services: dockerfile: deploy/Dockerfile target: bot-service args: - USER: web + STAGE: ${STAGE} restart: unless-stopped - environment: - APP_HOST: "0.0.0.0" - APP_PORT: "8000" - WORKERS_COUNT: "1" + env_file: + - bot_microservice/settings/.env volumes: - /etc/localtime:/etc/localtime:ro networks: chat-gpt-network: ipv4_address: 200.20.0.10 - depends_on: - - caddy - - chat-gpt expose: - "8000" command: bash start-bot.sh @@ -46,7 +41,7 @@ services: dockerfile: deploy/Dockerfile target: chat-service args: - USER: web + STAGE: ${STAGE} restart: unless-stopped environment: APP_HOST: "0.0.0.0" @@ -55,8 +50,6 @@ services: networks: chat-gpt-network: ipv4_address: 200.20.0.11 - depends_on: - - caddy expose: - "1338"