Skip to content

Commit

Permalink
test: BI-0 allow DOCKER_HOST with ssh in tests (#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
KonstantAnxiety authored Dec 9, 2024
1 parent f18053b commit 2e891e7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
34 changes: 24 additions & 10 deletions lib/dl_core_testing/dl_core_testing/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import logging
import os
import time
from typing import Optional
from typing import (
TYPE_CHECKING,
Optional,
)

import requests

Expand All @@ -13,6 +16,10 @@
from dl_utils.wait import wait_for


if TYPE_CHECKING:
from docker import DockerClient


LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -51,30 +58,37 @@ def _wait_for_pg(dsn: str, timeout: int = 300, interval: float = 1.0) -> None:
raise Exception("Unexpected result", res)


def restart_container(container_name: str) -> None:
def is_docker_host_ssh() -> bool:
docker_host = os.environ.get("DOCKER_HOST", "")
use_ssh_client = docker_host.startswith("ssh://")
return use_ssh_client


def make_docker_cli(timeout: int = 300) -> DockerClient:
import docker

docker_cli = docker.from_env(timeout=300)
docker_cli = docker.from_env(timeout=timeout, use_ssh_client=is_docker_host_ssh())
return docker_cli


def restart_container(container_name: str) -> None:
docker_cli = make_docker_cli()
container = docker_cli.containers.list(filters=dict(name=container_name), all=True)[0]
container.restart()


def run_cmd_in_containers_by_label(label: str, cmd: list[str]) -> None:
import docker

docker_cli = docker.from_env(timeout=300)
docker_cli = make_docker_cli()
containers = docker_cli.containers.list(
filters=dict(label=[f"datalens.ci.service={label}"]),
)
for container in containers:
LOGGER.debug(f"Running command {cmd} in container {container.name}")
container.exec_run(cmd)
container.exec_run(cmd, socket=is_docker_host_ssh())


def restart_container_by_label(label: str, compose_project: str) -> None:
import docker

docker_cli = docker.from_env(timeout=300)
docker_cli = make_docker_cli()
container = docker_cli.containers.list(
filters=dict(
label=[
Expand Down
1 change: 1 addition & 0 deletions tools/taskfiles/taskfile_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tasks:
- echo ''
- echo 'If compose is started remotely via SSH, use `task dev:ssh-forward-start` to forward ports to localhost'
- echo 'Use `task dev:ssh-forward-stop` to stop all SSH port forwarding'
- echo 'Also export `DOCKER_HOST` before running `task dev:test` to allow connection to docker from tests'
- echo ''
- echo 'Development workflow example:'
- echo '0. source environment variables'
Expand Down

0 comments on commit 2e891e7

Please sign in to comment.