Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maresb committed Mar 22, 2024
1 parent d20384e commit 3bb3cac
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/providers/docker/decorators/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,41 @@ def g():
assert some_task.expect_airflow == clone_of_docker_operator.expect_airflow
assert some_task.use_dill == clone_of_docker_operator.use_dill
assert some_task.pickling_library is clone_of_docker_operator.pickling_library

def test_respect_docker_host_env(self, monkeypatch, dag_maker):
monkeypatch.setenv("DOCKER_HOST", "tcp://docker-host-from-env:2375")

@task.docker(image="python:3.9-slim", auto_remove="force")
def f():
pass

with dag_maker():
ret = f()

assert ret.operator.docker_url == "tcp://docker-host-from-env:2375"

def test_docker_host_env_empty(self, monkeypatch, dag_maker):
monkeypatch.setenv("DOCKER_HOST", "")

@task.docker(image="python:3.9-slim", auto_remove="force")
def f():
pass

with dag_maker():
ret = f()

# The docker CLI ignores the empty string and defaults to unix://var/run/docker.sock
# We want to ensure the same behavior.
assert ret.operator.docker_url == "unix://var/run/docker.sock"

def test_docker_host_env_unset(self, monkeypatch, dag_maker):
monkeypatch.delenv("DOCKER_HOST", raising=False)

@task.docker(image="python:3.9-slim", auto_remove="force")
def f():
pass

with dag_maker():
ret = f()

assert ret.operator.docker_url == "unix://var/run/docker.sock"
17 changes: 17 additions & 0 deletions tests/providers/docker/operators/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,3 +790,20 @@ def test_skip_exit_code_invalid(self, skip_exit_code, skip_on_exit_code):
skip_exit_code=skip_exit_code,
skip_on_exit_code=skip_on_exit_code,
)

def test_respect_docker_host_env(self, monkeypatch):
monkeypatch.setenv("DOCKER_HOST", "tcp://docker-host-from-env:2375")
operator = DockerOperator(task_id="test", image="test")
assert operator.docker_url == "tcp://docker-host-from-env:2375"

def test_docker_host_env_empty(self, monkeypatch):
monkeypatch.setenv("DOCKER_HOST", "")
operator = DockerOperator(task_id="test", image="test")
# The docker CLI ignores the empty string and defaults to unix://var/run/docker.sock
# We want to ensure the same behavior.
assert operator.docker_url == "unix://var/run/docker.sock"

def test_docker_host_env_unset(self, monkeypatch):
monkeypatch.delenv("DOCKER_HOST", raising=False)
operator = DockerOperator(task_id="test", image="test")
assert operator.docker_url == "unix://var/run/docker.sock"

0 comments on commit 3bb3cac

Please sign in to comment.