Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set docarray version on load flow data #159

Merged
merged 6 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
max-parallel: 5
fail-fast: false
matrix:
python-version: [3.7]
python-version: ['3.10']
test-path: ${{fromJson(needs.prep-testbed.outputs.matrix)}}
steps:
- uses: actions/checkout@v2
Expand All @@ -48,6 +48,7 @@ jobs:
python -m pip install --upgrade pip
python -m pip install wheel
pip install --no-cache-dir ".[test]"
pip install docarray==0.21.0
sudo apt-get install libsndfile1
- name: Test
id: test
Expand Down
57 changes: 57 additions & 0 deletions jcloud/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ def load_flow_data(path: Union[str, Path], envs: Optional[Dict] = None) -> Dict:
flow_dict = JAML.load(f, substitute=True, context=envs)
if 'jtype' not in flow_dict or flow_dict['jtype'] != 'Flow':
raise ValueError(f'The file `{path}` is not a valid Flow YAML')
flow_dict = check_and_set_jcloud_versions(flow_dict)
flow_dict = stringify_labels(flow_dict)
return flow_dict

Expand Down Expand Up @@ -434,3 +435,59 @@ def exit_if_flow_defines_secret(flow_path):
'\n\t2.Create a Secret for your flow with `jc create secret <secret-name> -f <flow-id> --from-literal <secret-data> --update`.',
'cyan',
)


class JCloudLabelsError(TypeError):
pass


def stringify(v: Any) -> str:
if isinstance(v, str):
return v
elif isinstance(v, int) or isinstance(v, float):
return str(v)
else:
raise JCloudLabelsError(f'labels can\'t be of type {type(v)}')


def stringify_labels(flow_dict: Dict) -> Dict:
global_jcloud_labels = flow_dict.get('jcloud', {}).get('labels', None)
if global_jcloud_labels:
for k, v in flow_dict['jcloud']['labels'].items():
flow_dict['jcloud']['labels'][k] = stringify(v)
gateway_jcloud_labels = (
flow_dict.get('gateway', {}).get('jcloud', {}).get('labels', None)
)
if gateway_jcloud_labels:
for k, v in flow_dict['gateway']['jcloud']['labels'].items():
flow_dict['gateway']['jcloud']['labels'][k] = stringify(v)

executors = flow_dict.get('executors', [])
for idx in range(len(executors)):
executor_jcloud_labels = (
flow_dict['executors'][idx].get('jcloud', {}).get('labels', None)
)
if executor_jcloud_labels:
for k, v in flow_dict['executors'][idx]['jcloud']['labels'].items():
flow_dict['executors'][idx]['jcloud']['labels'][k] = stringify(v)
return flow_dict


def check_and_set_jcloud_versions(flow_dict: Dict) -> Dict:
import docarray
import jina

global_jcloud = flow_dict.get('jcloud', None)
if not global_jcloud:
flow_dict['jcloud'] = {
'docarray': docarray.__version__,
'version': jina.__version__,
}
return flow_dict
docarray_version = global_jcloud.get('docarray', None)
if not docarray_version:
flow_dict['jcloud'].update({'docarray': docarray.__version__})
jina_version = global_jcloud.get('version', None)
if not jina_version:
flow_dict['jcloud'].update({'version': jina.__version__})
return flow_dict
6 changes: 5 additions & 1 deletion jcloud/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
import requests

from .constants import CONSTANTS
from .helper import get_logger, get_filename_envs, load_flow_data
from .helper import (
get_logger,
get_filename_envs,
load_flow_data,
)

GPU_DOCKERFILE = 'Dockerfile.gpu'

Expand Down
25 changes: 19 additions & 6 deletions tests/integration/test_jina_new.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import os
import pip
import shutil
import subprocess

import pytest
from jina import Client, DocumentArray

from jcloud.flow import CloudFlow
from jina import Client

cur_dir = os.path.dirname(os.path.abspath(__file__))


def test_jina_new():
subprocess.run(["jina", "new", os.path.join(cur_dir, "hello-world")])
subprocess.run(['jina', 'new', os.path.join(cur_dir, 'hello-world')])
import docarray

assert os.path.exists(os.path.join(cur_dir, "hello-world"))
assert os.path.isdir(os.path.join(cur_dir, "hello-world"))
print(docarray.__version__)
npitsillos marked this conversation as resolved.
Show resolved Hide resolved
subprocess.run(['pip', 'install', '-U', 'docarray', '-q'])
docarray = __import__('docarray')

print(docarray.__version__)
npitsillos marked this conversation as resolved.
Show resolved Hide resolved
from docarray import DocList
from docarray.documents import TextDoc

assert os.path.exists(os.path.join(cur_dir, 'hello-world'))
assert os.path.isdir(os.path.join(cur_dir, 'hello-world'))

with CloudFlow(path=os.path.join(cur_dir, "hello-world")) as flow:
assert flow.endpoints != {}
Expand All @@ -23,8 +33,11 @@ def test_jina_new():
assert 'gateway (websocket)' in flow.endpoints
gateway = flow.endpoints['gateway (grpc)']

da = Client(host=gateway).post(on="/", inputs=DocumentArray.empty(2))
assert da.texts == ["hello, world!", "goodbye, world!"]
da = Client(host=gateway).post(
on='/', inputs=DocList[TextDoc](TextDoc() for i in range(2))
)
assert da[0].text == ['hello, world!']
assert da[1].text == ['goodbye, world!']

shutil.rmtree(os.path.join(cur_dir, "hello-world"))

Expand Down
28 changes: 28 additions & 0 deletions tests/unit/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
load_flow_data,
JCloudLabelsError,
update_flow_yml_and_write_to_file,
check_and_set_jcloud_versions,
)
from jcloud.env_helper import EnvironmentVariables

Expand Down Expand Up @@ -76,6 +77,33 @@ def test_not_normalized(filename, envs):
assert not normalized(f.name)


@pytest.mark.parametrize(
'flow, flow_dict',
(
('flow-one', {'jtype': 'Flow', 'executors': [{'uses': 'jinahub+docker://E1'}]}),
(
'flow-two',
{
'jtype': 'Flow',
'jcloud': {'docarray': '0.31.0'},
'executors': [{'uses': 'jinahub+docker://E1'}],
},
),
),
)
def test_check_and_set_jcloud_versions(flow, flow_dict):
import docarray
import jina

flow_dict = check_and_set_jcloud_versions(flow_dict)
if flow == 'flow-one':
assert flow_dict['jcloud']['docarray'] == docarray.__version__
assert flow_dict['jcloud']['version'] == jina.__version__
else:
assert flow_dict['jcloud']['docarray'] == '0.31.0'
assert flow_dict['jcloud']['version'] == jina.__version__


def test_failed_flow():
flow_path = Path(cur_dir) / 'flows' / 'failed_flows' / 'failed_flow.yml'

Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from unittest.mock import patch
from jcloud.helper import load_flow_data
from jcloud.normalize import *
from jcloud.helper import JCloudLabelsError


@pytest.fixture
Expand Down