Example #1
0
    def test_container_id_is_replaced(self, monkeypatch):
        task = StartContainer(container_id="original")

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

        task.run(container_id="test")
        assert api.return_value.start.call_args[1]["container"] == "test"
Example #2
0
    def test_container_id_run_value_is_used(self, monkeypatch):
        task = StartContainer()

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

        task.run(container_id="test")
        assert api.return_value.start.call_args[1]["container"] == "test"
Example #3
0
    def test_logs_twice_on_success(self, monkeypatch, caplog):
        task = StartContainer(container_id="test")

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

        self.assert_logs_twice_on_success(task, caplog)
Example #4
0
    def test_doesnt_log_on_param_failure(self, monkeypatch, caplog):

        task = StartContainer()

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

        self.assert_doesnt_log_on_param_failure(task, caplog)
Example #5
0
    def test_logs_once_on_docker_api_failure(self, monkeypatch, caplog):

        task = StartContainer(container_id="test")

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

        self.assert_logs_once_on_docker_api_failure(task, caplog)
Example #6
0
 def test_invalid_container_id_raises_error(self):
     task = StartContainer()
     with pytest.raises(ValueError):
         task.run(container_id=None)
Example #7
0
 def test_empty_container_id_raises_error(self):
     task = StartContainer()
     with pytest.raises(ValueError):
         task.run()
Example #8
0
 def test_filled_initialization(self):
     task = StartContainer(container_id="test", docker_server_url="test")
     assert task.container_id == "test"
     assert task.docker_server_url == "test"
Example #9
0
 def test_empty_initialization(self):
     task = StartContainer()
     assert not task.container_id
     assert task.docker_server_url == "unix:///var/run/docker.sock"
Example #10
0
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()

print("=" * 30)
Example #11
0
)
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",
    ),
)

# set task dependencies using imperative API
container.set_upstream(image, flow=flow)
Example #12
0
from prefect import Flow
from prefect.tasks.docker import (
    CreateContainer,
    StartContainer,
    GetContainerLogs,
    WaitOnContainer,
)
from prefect.triggers import always_run

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()

flow = Flow("Run a Prefect Flow in Docker")

## set individual task dependencies using imperative API
start.set_upstream(container, flow=flow, key="container_id")
logs.set_upstream(container, flow=flow, key="container_id")
status_code.set_upstream(container, flow=flow, key="container_id")

status_code.set_upstream(start, flow=flow)
logs.set_upstream(status_code, flow=flow)

## run flow and print logs
flow_state = flow.run()