def get_image_name(uses: str) -> str: """The image can be provided in different formats by the user. This function converts it to an image name which can be understood by k8s. It uses the Hub api to get the image name and the latest tag on Docker Hub. If you don't want to rebuild image on Jina Hub, you can set `JINA_HUB_NO_IMAGE_REBUILD` environment variable. :param uses: image name :return: normalized image name """ try: rebuild_image = 'JINA_HUB_NO_IMAGE_REBUILD' not in os.environ scheme, name, tag, secret = parse_hub_uri(uses) meta_data, _ = HubIO.fetch_meta(name, tag, secret=secret, rebuild_image=rebuild_image, force=True) image_name = meta_data.image_name return image_name except Exception: if uses.startswith('docker'): # docker:// is a valid requirement and user may want to put its own image return uses.replace('docker://', '') raise
def test_fetch_with_authorization(mocker, monkeypatch, auth_token): mock = mocker.Mock() def _mock_post(url, json, headers): mock(url=url, json=json, headers=headers) return FetchMetaMockResponse(response_code=200) monkeypatch.setattr(requests, 'post', _mock_post) HubIO.fetch_meta('dummy_mwu_encoder', tag=None, force=True) assert mock.call_count == 1 _, kwargs = mock.call_args_list[0] assert kwargs['headers'].get('Authorization') == f'token {auth_token}'
def test_fetch_with_no_image(mocker, monkeypatch): mock = mocker.Mock() def _mock_post(url, json, headers=None): mock(url=url, json=json) return FetchMetaMockResponse(response_code=200, no_image=True) monkeypatch.setattr(requests, 'post', _mock_post) with pytest.raises(Exception) as exc_info: HubIO.fetch_meta('dummy_mwu_encoder', tag=None, force=True) assert exc_info.match('No image found for executor "dummy_mwu_encoder"') executor, _ = HubIO.fetch_meta( 'dummy_mwu_encoder', tag=None, image_required=False, force=True ) assert executor.image_name is None assert mock.call_count == 2
def test_fetch_with_retry(mocker, monkeypatch): mock = mocker.Mock() mock_response = FetchMetaMockResponse(response_code=200, fail_count=3) def _mock_post(url, json, headers=None): mock(url=url, json=json) return mock_response monkeypatch.setattr(requests, 'post', _mock_post) with pytest.raises(Exception) as exc_info: # failing 3 times, so it should raise an error HubIO.fetch_meta('dummy_mwu_encoder', tag=None, force=True) assert exc_info.match('{"message": "Internal server error"}') mock_response = FetchMetaMockResponse(response_code=200, fail_count=2) # failing 2 times, it must succeed on 3rd time executor, _ = HubIO.fetch_meta('dummy_mwu_encoder', tag=None, force=True) assert executor.uuid == 'dummy_mwu_encoder' assert mock.call_count == 6 # mock must be called 3+3
def get_image_name(uses: str) -> str: """The image can be provided in different formats by the user. This function converts it to an image name which can be understood by k8s. It uses the Hub api to get the image name and the latest tag on Docker Hub. :param uses: image name :return: normalized image name """ try: scheme, name, tag, secret = parse_hub_uri(uses) meta_data = HubIO.fetch_meta(name, tag, secret=secret) image_name = meta_data.image_name return image_name except Exception: return uses.replace('docker://', '')
def get_image_name(uses: str) -> str: """The image can be provided in different formats by the user. This function converts it to an image name which can be understood by k8s. It uses the Hub api to get the image name and the latest tag on Docker Hub. :param uses: image name :return: normalized image name """ try: scheme, name, tag, secret = parse_hub_uri(uses) meta_data, _ = HubIO.fetch_meta(name, tag, secret=secret, force=True) image_name = meta_data.image_name return image_name except Exception: if uses.startswith('docker'): # docker:// is a valid requirement and user may want to put its own image return uses.replace('docker://', '') raise