def test_returns_pull_value_with_stream_logs(self, monkeypatch, caplog): task = PullImage() api = MagicMock() expected_docker_output = [ { "status": "Pulling" }, { "status": "Digest" }, { "status": "Test" }, ] expected_result = ( "{'status': 'Pulling'}\n{'status': 'Digest'}\n{'status': 'Test'}") api.return_value.pull = MagicMock(return_value=expected_docker_output) monkeypatch.setattr("docker.APIClient", api) with caplog.at_level(logging.DEBUG, logger=task.logger.name): result = task.run(repository="original", stream_logs=True) assert result == expected_result
def test_repository_is_replaced(self, monkeypatch): task = PullImage(repository="original") api = MagicMock() monkeypatch.setattr("docker.APIClient", api) task.run(repository="test") assert api.return_value.pull.call_args[1]["repository"] == "test"
def test_repository_run_value_is_used(self, monkeypatch): task = PullImage() api = MagicMock() monkeypatch.setattr("prefect.tasks.docker.containers.docker.APIClient", api) task.run(repository="test") assert api.return_value.pull.call_args[1]["repository"] == "test"
def test_returns_pull_value(self, monkeypatch, caplog): task = PullImage(repository="original") api = MagicMock() expected_docker_output = "A real output from docker's api" api.return_value.pull = MagicMock(return_value=expected_docker_output) monkeypatch.setattr("docker.APIClient", api) with caplog.at_level(logging.DEBUG, logger=task.logger.name): result = task.run(repository="test") assert result == expected_docker_output
def test_doesnt_log_on_param_failure(self, monkeypatch, caplog): task = PullImage() api = MagicMock() monkeypatch.setattr("docker.APIClient", api) self.assert_doesnt_log_on_param_failure(task, caplog)
def test_filled_initialization(self): task = PullImage(repository="test", tag="test", docker_server_url="test") assert task.repository == "test" assert task.tag == "test" assert task.docker_server_url == "test"
def test_logs_twice_on_success(self, monkeypatch, caplog): tag = "A very specific tag for an image" repository = "An even more specific repository" task = PullImage(repository=repository, tag=tag) api = MagicMock() monkeypatch.setattr("docker.APIClient", api) self.assert_logs_twice_on_success(task, caplog)
def test_logs_once_on_docker_api_failure(self, monkeypatch, caplog): task = PullImage(repository="test") api = MagicMock() pull_mock = MagicMock( side_effect=docker.errors.DockerException("Docker specific error")) api.return_value.pull = pull_mock monkeypatch.setattr("docker.APIClient", api) self.assert_logs_once_on_docker_api_failure(task, caplog)
def test_invalid_repository_raises_error(self): task = PullImage() with pytest.raises(ValueError): task.run(repository=None)
def test_empty_repository_raises_error(self): task = PullImage() with pytest.raises(ValueError): task.run()
def test_empty_initialization(self): task = PullImage() assert not task.repository assert not task.tag assert task.docker_server_url == "unix:///var/run/docker.sock"
from prefect import Flow from prefect.environments import KubernetesJobEnvironment from prefect.environments.storage import Docker from prefect.tasks.docker import ( PullImage, 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",