예제 #1
0
    def test_image_name_run_value_is_used(self, monkeypatch):
        task = CreateContainer()

        api = MagicMock()
        monkeypatch.setattr("docker.APIClient", api)

        task.run(image_name="test")
        assert api.return_value.create_container.call_args[1]["image"] == "test"
예제 #2
0
    def test_extra_kwargs(self, monkeypatch, caplog):
        task = CreateContainer(extra_docker_kwargs={"network": "test-network"})

        api = MagicMock()
        monkeypatch.setattr("docker.APIClient", api)

        task.run(image_name="test")
        assert (api.return_value.create_container.call_args[1]["network"] ==
                "test-network")
예제 #3
0
    def test_image_name_is_replaced(self, monkeypatch):
        task = CreateContainer(image_name="original")

        api = MagicMock()
        monkeypatch.setattr("prefect.tasks.docker.containers.docker.APIClient",
                            api)

        task.run(image_name="test")
        assert api.return_value.create_container.call_args[1][
            "image"] == "test"
예제 #4
0
    def test_doesnt_log_on_param_failure(self, monkeypatch, caplog):
        image_name = "test image"
        task = CreateContainer()

        api = MagicMock()

        monkeypatch.setattr("docker.APIClient", api)
        self.assert_doesnt_log_on_param_failure(task, caplog)
예제 #5
0
    def test_logs_twice_on_success(self, monkeypatch, caplog):
        image_name = "test image"
        task = CreateContainer(image_name=image_name)

        api = MagicMock()
        monkeypatch.setattr("docker.APIClient", api)

        self.assert_logs_twice_on_success(task, caplog)
예제 #6
0
 def test_empty_initialization(self):
     task = CreateContainer()
     assert not task.image_name
     assert not task.command
     assert not task.detach
     assert not task.entrypoint
     assert not task.environment
     assert task.docker_server_url == "unix:///var/run/docker.sock"
예제 #7
0
    def test_logs_once_on_docker_api_failure(self, monkeypatch, caplog):

        image_name = "test image"
        task = CreateContainer(image_name=image_name)

        api = MagicMock()
        create_container_mock = MagicMock(
            side_effect=docker.errors.DockerException(
                "A docker specific exception"))
        api.return_value.create_container = create_container_mock
        monkeypatch.setattr("docker.APIClient", api)

        self.assert_logs_once_on_docker_api_failure(task, caplog)
예제 #8
0
 def test_filled_initialization(self):
     task = CreateContainer(
         image_name="test",
         command="test",
         detach=True,
         entrypoint=["test"],
         environment=["test"],
         name="test",
         docker_server_url="test",
     )
     assert task.image_name == "test"
     assert task.command == "test"
     assert task.detach
     assert task.entrypoint == ["test"]
     assert task.environment == ["test"]
     assert task.docker_server_url == "test"
예제 #9
0
 def test_invalid_image_name_raises_error(self):
     task = CreateContainer()
     with pytest.raises(ValueError):
         task.run(image_name=None)
예제 #10
0
 def test_empty_image_name_raises_error(self):
     task = CreateContainer()
     with pytest.raises(ValueError):
         task.run()
예제 #11
0
executes an empty Flow inside that container.  Make sure to pull `prefecthq/prefect` prior
to running this Flow.
"""

from prefect import Flow
from prefect.tasks.docker import (
    CreateContainer,
    GetContainerLogs,
    StartContainer,
    WaitOnContainer,
)
from prefect.triggers import always_run

## initialize tasks
container = CreateContainer(
    image_name="prefecthq/prefect",
    command='''python -c "from prefect import Flow; f = Flow('empty'); f.run()"''',
)
start = StartContainer()
logs = GetContainerLogs(trigger=always_run)
status_code = WaitOnContainer()


## set task dependencies via functional API

with Flow("Run a Prefect Flow in Docker") as flow:
    start_container = start(container_id=container)
    code = status_code(container_id=container, upstream_tasks=[start_container])
    collect_logs = logs(container_id=container, upstream_tasks=[code])

## run flow and print logs
flow_state = flow.run()
예제 #12
0
    CreateContainer,
    StartContainer,
    GetContainerLogs,
    WaitOnContainer,
)
from prefect.triggers import always_run

# Pass the host of the Docker daemon to each task
image = PullImage(
    docker_server_url="tcp://localhost:2375",
    repository="prefecthq/prefect",
    tag="latest",
)
container = CreateContainer(
    docker_server_url="tcp://localhost:2375",
    image_name="prefecthq/prefect:latest",
    command=
    '''python -c "from prefect import Flow; f = Flow('empty'); f.run()"''',
)
start = StartContainer(docker_server_url="tcp://localhost:2375", )
logs = GetContainerLogs(docker_server_url="tcp://localhost:2375",
                        trigger=always_run)
status_code = WaitOnContainer(docker_server_url="tcp://localhost:2375", )

flow = Flow(
    "Run a Prefect Flow in Docker",
    environment=KubernetesJobEnvironment(job_spec_file="job_spec.yaml"),
    storage=Docker(
        registry_url="joshmeek18",
        image_name="flows",
    ),
)
    GetContainerLogs,
    WaitOnContainer,
)
from prefect.triggers import always_run

flow_code = ''

container = CreateContainer(
    image_name='prefecthq/prefect',
    command='''python -c "from prefect import task, Flow
from datetime import timedelta
from prefect.schedules import IntervalSchedule

@task
def say_hello():
    print('Hello, world!')

schedule = IntervalSchedule(interval=timedelta(minutes=1))

with Flow('Hello', schedule) as flow:
    say_hello()

flow.run()"''',
)

start = StartContainer()
logs = GetContainerLogs(trigger=always_run)
status_code = WaitOnContainer()

flow = Flow('Run in Docker')
예제 #14
0
from prefect import task, Flow
from prefect.tasks.docker import (
    CreateContainer,
    StartContainer,
    GetContainerLogs,
    WaitOnContainer,
)

create = CreateContainer(image_name="prefecthq/prefect", command="echo 12345")
start = StartContainer()
wait = WaitOnContainer()
logs = GetContainerLogs()


@task
def see_output(out):
    print(out)


with Flow("docker-flow") as flow:
    container_id = create()
    s = start(container_id=container_id)
    w = wait(container_id=container_id)

    l = logs(container_id=container_id)
    l.set_upstream(w)

    see_output(l)

flow.run()