def test_path_init_value_is_used(self, monkeypatch): task = BuildImage(path="test") api = MagicMock() monkeypatch.setattr("docker.APIClient", api) task.run() assert api.return_value.build.call_args[1]["path"] == "test"
def test_image_is_replaced(self, monkeypatch): task = BuildImage(path="original") api = MagicMock() monkeypatch.setattr("docker.APIClient", api) task.run(path="test") assert api.return_value.build.call_args[1]["path"] == "test"
def test_image_run_value_is_used(self, monkeypatch): task = BuildImage() api = MagicMock() monkeypatch.setattr("prefect.tasks.docker.containers.docker.APIClient", api) task.run(path="test") assert api.return_value.build.call_args[1]["path"] == "test"
def test_logs_twice_on_success(self, monkeypatch, caplog): path = "test path" task = BuildImage(path=path) api = MagicMock() monkeypatch.setattr("docker.APIClient", api) self.assert_logs_twice_on_success(task, caplog)
def test_image_cleans_docker_output(self, monkeypatch): task = BuildImage() output = [ b'{"stream":"Step 1/2 : FROM busybox"}\r\n{"stream":"\\n"}\r\n', b'{"stream":" ---\\u003e db8ee88ad75f\\n"}\r\n{"stream":"Step 2/2 : CMD [\\"echo\\", \\"foo\\"]"}\r\n{"stream":"\\n"}\r\n', b'{"stream":" ---\\u003e Using cache\\n"}\r\n{"stream":" ---\\u003e 0be8805a7d08\\n"}\r\n{"aux":{"ID":"sha256:0be8805a7d0828bbfcd876e8aa65cf6ee1ee016117a2d3f3aa4f21835ee1c69f"}}\r\n{"stream":"Successfully built 0be8805a7d08\\n"}\r\n', ] api = MagicMock() api.return_value.build.return_value = output monkeypatch.setattr("docker.APIClient", api) return_value = task.run(path="test") assert all(isinstance(value, dict) for value in return_value) assert all(len(value) >= 1 for value in return_value) assert all(key for value in return_value for key in value)
def test_doesnt_log_on_param_failure(self, monkeypatch, caplog): task = BuildImage() api = MagicMock() monkeypatch.setattr("prefect.tasks.docker.containers.docker.APIClient", api) self.assert_doesnt_log_on_param_failure(task, caplog)
def test_empty_initialization(self): task = BuildImage() assert not task.path assert not task.tag assert not task.nocache assert task.rm assert not task.forcerm assert task.docker_server_url == "unix:///var/run/docker.sock"
def test_logs_once_on_docker_api_failure(self, monkeypatch, caplog): task = BuildImage(path="test") api = MagicMock() build_mock = MagicMock( side_effect=docker.errors.DockerException("Docker specific error")) api.return_value.build = build_mock monkeypatch.setattr("docker.APIClient", api) self.assert_logs_once_on_docker_api_failure(task, caplog)
def test_filled_initialization(self): task = BuildImage( path="test", tag="test", nocache=True, rm=False, forcerm=True, docker_server_url="test", ) assert task.path == "test" assert task.tag == "test" assert task.nocache is True assert not task.rm assert task.forcerm is True assert task.docker_server_url == "test"
def test_invalid_path_raises_error(self): task = BuildImage() with pytest.raises(ValueError): task.run(path=None)
def test_empty_path_raises_error(self): task = BuildImage() with pytest.raises(ValueError): task.run()