def _raise_for_invalid_commit_chunk(obj: Dict[str, Any], expect_started: bool) -> None: _raise_on_error_chunk(obj) if "status" not in obj.keys(): error_details = {"message": 'Missing required field: "status"'} raise DockerError(400, error_details) status = obj["status"] expected = "CommitStarted" if expect_started else "CommitFinished" if status != expected: error_details = { "message": f"Invalid commit status: '{status}', expecting: '{expected}'" } raise DockerError(400, error_details)
def __init__(self, core: _Core, config: _Config) -> None: self._core = core self._config = config self._temporary_images: Set[str] = set() try: self._docker = aiodocker.Docker() except ValueError as error: if re.match( r".*Either DOCKER_HOST or local sockets are not available.*", f"{error}"): raise DockerError( 900, { "message": "Docker engine is not available. " "Please specify DOCKER_HOST variable " "if you are using remote docker engine" }, ) raise self._registry = _Registry( self._core.connector, self._config.cluster_config.registry_url.with_path("/v2/"), self._config.auth_token.token, self._core.timeout, self._config.auth_token.username, )
async def test_auto_pull_tag_when_missing(agent, mocker): behavior = AutoPullBehavior.TAG inspect_mock = AsyncMock(side_effect=DockerError( status=404, data={'message': 'Simulated missing image'})) mocker.patch.object(agent.docker.images, 'inspect', new=inspect_mock) pull = await agent.check_image(imgref, query_digest, behavior) assert pull inspect_mock.assert_called_with(imgref.canonical)
async def test_push_non_existent_image(self, patched_tag: Any, make_client: _MakeClient) -> None: patched_tag.side_effect = DockerError(404, {"message": "Mocked error"}) image = self.parser.parse_as_neuro_image( "image://bob/image:bananas-no-more") local_image = self.parser.parse_as_local_image("bananas:latest") async with make_client("https://api.localhost.localdomain") as client: with pytest.raises(ValueError, match=r"not found"): await client.images.push(local_image, image)
def _parse_commit_started_chunk(job_id: str, obj: Dict[str, Any], parse: Parser) -> ImageCommitStarted: _raise_for_invalid_commit_chunk(obj, expect_started=True) details_json = obj.get("details", {}) image = details_json.get("image") if not image: error_details = {"message": "Missing required details: 'image'"} raise DockerError(400, error_details) return ImageCommitStarted(job_id, parse.remote_image(image))
async def test_auto_pull_none_when_missing(agent, mocker): behavior = AutoPullBehavior.NONE inspect_mock = AsyncMock(side_effect=DockerError( status=404, data={'message': 'Simulated missing image'})) mocker.patch.object(agent.docker.images, 'inspect', new=inspect_mock) with pytest.raises(ImageNotAvailable) as e: await agent.check_image(imgref, query_digest, behavior) assert e.value.args[0] is imgref inspect_mock.assert_called_with(imgref.canonical)
async def test_pull_image_from_foreign_repo( self, patched_pull: Any, make_client: _MakeClient) -> None: patched_pull.side_effect = DockerError(403, {"message": "Mocked error"}) image = self.parser.parse_as_neuro_image( "image://bob/image:not-your-bananas") local_image = self.parser.parse_as_local_image("bananas:latest") async with make_client("https://api.localhost.localdomain") as client: with pytest.raises(AuthorizationError): await client.images.pull(image, local_image)
async def test_push_image_to_foreign_repo( self, patched_push: Any, patched_tag: Any, make_client: _MakeClient) -> None: patched_tag.return_value = True patched_push.side_effect = DockerError(403, {"message": "Mocked error"}) image = self.parser.parse_as_neuro_image( "image://bob/image:bananas-no-more") local_image = self.parser.parse_as_local_image("bananas:latest") async with make_client("https://api.localhost.localdomain") as client: with pytest.raises(AuthorizationError): await client.images.push(local_image, image)
def _docker(self) -> aiodocker.Docker: if not self.__docker: try: self.__docker = aiodocker.Docker() except ValueError as error: if re.match( r".*Either DOCKER_HOST or local sockets are not available.*", f"{error}", ): raise DockerError( 900, { "message": "Docker engine is not available. " "Please specify DOCKER_HOST variable " "if you are using remote docker engine" }, ) raise return self.__docker
def _raise_on_error_chunk(obj: Dict[str, Any]) -> None: if "error" in obj.keys(): error_details = obj.get("errorDetail", {"message": "Unknown error"}) raise DockerError(900, error_details)