Skip to content

Commit

Permalink
test(client): adopt new client in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
heiruwu committed Dec 4, 2023
1 parent 53fd882 commit acae811
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 61 deletions.
7 changes: 4 additions & 3 deletions instill/clients/instance.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from typing import Union
import grpc

import grpc

import instill.protogen.core.mgmt.v1alpha.mgmt_public_service_pb2_grpc as mgmt_service
import instill.protogen.model.model.v1alpha.model_public_service_pb2_grpc as model_service
import instill.protogen.vdp.pipeline.v1alpha.pipeline_public_service_pb2_grpc as pipeline_service
import instill.protogen.core.mgmt.v1alpha.mgmt_public_service_pb2_grpc as mgmt_service


class InstillInstance:
def __init__(self, stub, url: str, token: str, secure: bool, asyncio: bool):
self.url: str = url
self.token: str = token
self.asyncio: bool = asyncio
if not secure:
Expand All @@ -26,7 +27,7 @@ def __init__(self, stub, url: str, token: str, secure: bool, asyncio: bool):
call_creds = grpc.access_token_call_credentials(token)
creds = grpc.composite_channel_credentials(ssl_creds, call_creds)
channel = grpc.secure_channel(target=url, credentials=creds)
self.metadata = ""
self.metadata = (("", ""),)
if asyncio:
async_channel = grpc.aio.secure_channel(target=url, credentials=creds)
self.channel: grpc.Channel = channel
Expand Down
3 changes: 2 additions & 1 deletion instill/clients/mgmt.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# pylint: disable=no-member,wrong-import-position
from typing import Dict

import instill.protogen.common.healthcheck.v1alpha.healthcheck_pb2 as healthcheck

# mgmt
import instill.protogen.core.mgmt.v1alpha.metric_pb2 as metric_interface
import instill.protogen.core.mgmt.v1alpha.mgmt_pb2 as mgmt_interface
import instill.protogen.core.mgmt.v1alpha.mgmt_public_service_pb2_grpc as mgmt_service
from instill.clients.base import Client

# common
from instill.clients.constant import DEFAULT_INSTANCE
from instill.clients.instance import InstillInstance
from instill.clients.base import Client
from instill.configuration import global_config
from instill.utils.error_handler import grpc_handler

Expand Down
11 changes: 5 additions & 6 deletions instill/clients/model.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
# pylint: disable=no-member,wrong-import-position
import time
from typing import Iterable, Tuple, Union, Dict
from typing import Dict, Iterable, Tuple, Union

from google.longrunning import operations_pb2
from google.protobuf import field_mask_pb2

# common
import instill.protogen.common.healthcheck.v1alpha.healthcheck_pb2 as healthcheck
import instill.protogen.model.model.v1alpha.model_definition_pb2 as model_definition_interface

# model
import instill.protogen.model.model.v1alpha.model_pb2 as model_interface
import instill.protogen.model.model.v1alpha.model_public_service_pb2_grpc as model_service
import instill.protogen.model.model.v1alpha.model_definition_pb2 as model_definition_interface

# common
import instill.protogen.common.healthcheck.v1alpha.healthcheck_pb2 as healthcheck
from instill.clients.base import Client
from instill.clients.constant import DEFAULT_INSTANCE
from instill.clients.instance import InstillInstance
from instill.clients.base import Client
from instill.configuration import global_config
from instill.utils.error_handler import grpc_handler

Expand Down
10 changes: 5 additions & 5 deletions instill/clients/pipeline.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# pylint: disable=no-member,wrong-import-position
from typing import Iterable, Tuple, Union, Dict
from typing import Dict, Iterable, Tuple, Union

from google.longrunning import operations_pb2
from google.protobuf import field_mask_pb2

# common
import instill.protogen.common.healthcheck.v1alpha.healthcheck_pb2 as healthcheck

# pipeline
import instill.protogen.vdp.pipeline.v1alpha.connector_pb2 as connector_interface
import instill.protogen.vdp.pipeline.v1alpha.operator_definition_pb2 as operator_interface
import instill.protogen.vdp.pipeline.v1alpha.pipeline_pb2 as pipeline_interface
import instill.protogen.vdp.pipeline.v1alpha.pipeline_public_service_pb2_grpc as pipeline_service

# common
import instill.protogen.common.healthcheck.v1alpha.healthcheck_pb2 as healthcheck
from instill.clients.base import Client
from instill.clients.constant import DEFAULT_INSTANCE
from instill.clients.instance import InstillInstance
from instill.clients.base import Client
from instill.configuration import global_config
from instill.utils.error_handler import grpc_handler

Expand Down
115 changes: 92 additions & 23 deletions instill/tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,118 @@
# pylint: disable=redefined-outer-name,unused-variable,expression-not-assigned,no-name-in-module

from collections import defaultdict

import instill.protogen.core.mgmt.v1alpha.mgmt_public_service_pb2_grpc as mgmt_service
import instill.protogen.model.model.v1alpha.model_public_service_pb2_grpc as model_service
import instill.protogen.vdp.pipeline.v1alpha.pipeline_public_service_pb2_grpc as pipeline_service
from instill.clients import MgmtClient, ModelClient, PipelineClient
from instill.clients.instance import InstillInstance


def describe_client():
def describe_instance():
def when_not_set(expect):
mgmt_client = MgmtClient()
mgmt_client = MgmtClient(False)
expect(mgmt_client.instance) == ""
model_client = ModelClient(namespace="")
model_client = ModelClient(namespace="", asyncio=False)
expect(model_client.instance) == ""
pipeline_client = PipelineClient(namespace="")
pipeline_client = PipelineClient(namespace="", asyncio=False)
expect(pipeline_client.instance) == ""

def when_set_correct_type(expect):
mgmt_client = MgmtClient()
mgmt_client = MgmtClient(False)
mgmt_client.instance = "staging"
expect(mgmt_client.instance) == "staging"
model_client = ModelClient(namespace="")
model_client = ModelClient(namespace="", asyncio=False)
model_client.instance = "staging"
expect(model_client.instance) == "staging"
pipeline_client = PipelineClient(namespace="")
pipeline_client = PipelineClient(namespace="", asyncio=False)
pipeline_client.instance = "staging"
expect(pipeline_client.instance) == "staging"

def describe_host():
def when_not_set(expect):
mgmt_client = MgmtClient()
mgmt_client = MgmtClient(False)
expect(mgmt_client.hosts) is None
model_client = ModelClient(namespace="")
model_client = ModelClient(namespace="", asyncio=False)
expect(model_client.hosts) is None
pipeline_client = PipelineClient(namespace="")
pipeline_client = PipelineClient(namespace="", asyncio=False)
expect(pipeline_client.hosts) is None

def when_set_correct_type(expect):
mgmt_client = MgmtClient()
d = defaultdict(dict) # type: ignore
d["test_instance"] = dict({"url": "test_url"})
mgmt_client.hosts = d
expect(mgmt_client.hosts["test_instance"]["url"]) == "test_url"
model_client = ModelClient(namespace="")
model_client.hosts = d
expect(model_client.hosts["test_instance"]["url"]) == "test_url"
pipeline_client = PipelineClient(namespace="")
pipeline_client.hosts = d
expect(pipeline_client.hosts["test_instance"]["url"]) == "test_url"
def when_set_correct_type_url(expect):
mgmt_instance = {
"test_instance": InstillInstance(
mgmt_service.MgmtPublicServiceStub,
"test_url",
"token",
False,
False,
)
}
mgmt_client = MgmtClient(False)
mgmt_client.hosts = mgmt_instance
expect(mgmt_client.hosts["test_instance"].url) == "test_url"

model_instance = {
"test_instance": InstillInstance(
model_service.ModelPublicServiceStub,
"test_url",
"token",
False,
False,
)
}
model_client = ModelClient(namespace="", asyncio=False)
model_client.hosts = model_instance
expect(model_client.hosts["test_instance"].url) == "test_url"

pipeline_instance = {
"test_instance": InstillInstance(
pipeline_service.PipelinePublicServiceStub,
"test_url",
"token",
False,
False,
)
}
pipeline_client = PipelineClient(namespace="", asyncio=False)
pipeline_client.hosts = pipeline_instance
expect(pipeline_client.hosts["test_instance"].url) == "test_url"

def when_set_correct_type_token(expect):
mgmt_instance = {
"test_instance": InstillInstance(
mgmt_service.MgmtPublicServiceStub,
"test_url",
"token",
False,
False,
)
}
mgmt_client = MgmtClient(False)
mgmt_client.hosts = mgmt_instance
expect(mgmt_client.hosts["test_instance"].token) == "token"

model_instance = {
"test_instance": InstillInstance(
model_service.ModelPublicServiceStub,
"test_url",
"token",
False,
False,
)
}
model_client = ModelClient(namespace="", asyncio=False)
model_client.hosts = model_instance
expect(model_client.hosts["test_instance"].token) == "token"

pipeline_instance = {
"test_instance": InstillInstance(
pipeline_service.PipelinePublicServiceStub,
"test_url",
"token",
False,
False,
)
}
pipeline_client = PipelineClient(namespace="", asyncio=False)
pipeline_client.hosts = pipeline_instance
expect(pipeline_client.hosts["test_instance"].token) == "token"
115 changes: 92 additions & 23 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,118 @@
# pylint: disable=redefined-outer-name,unused-variable,expression-not-assigned,no-name-in-module

from collections import defaultdict

import instill.protogen.core.mgmt.v1alpha.mgmt_public_service_pb2_grpc as mgmt_service
import instill.protogen.model.model.v1alpha.model_public_service_pb2_grpc as model_service
import instill.protogen.vdp.pipeline.v1alpha.pipeline_public_service_pb2_grpc as pipeline_service
from instill.clients import MgmtClient, ModelClient, PipelineClient
from instill.clients.instance import InstillInstance


def describe_client():
def describe_instance():
def when_not_set(expect):
mgmt_client = MgmtClient()
mgmt_client = MgmtClient(False)
expect(mgmt_client.instance) == ""
model_client = ModelClient(namespace="")
model_client = ModelClient(namespace="", asyncio=False)
expect(model_client.instance) == ""
pipeline_client = PipelineClient(namespace="")
pipeline_client = PipelineClient(namespace="", asyncio=False)
expect(pipeline_client.instance) == ""

def when_set_correct_type(expect):
mgmt_client = MgmtClient()
mgmt_client = MgmtClient(False)
mgmt_client.instance = "staging"
expect(mgmt_client.instance) == "staging"
model_client = ModelClient(namespace="")
model_client = ModelClient(namespace="", asyncio=False)
model_client.instance = "staging"
expect(model_client.instance) == "staging"
pipeline_client = PipelineClient(namespace="")
pipeline_client = PipelineClient(namespace="", asyncio=False)
pipeline_client.instance = "staging"
expect(pipeline_client.instance) == "staging"

def describe_host():
def when_not_set(expect):
mgmt_client = MgmtClient()
mgmt_client = MgmtClient(False)
expect(mgmt_client.hosts) is None
model_client = ModelClient(namespace="")
model_client = ModelClient(namespace="", asyncio=False)
expect(model_client.hosts) is None
pipeline_client = PipelineClient(namespace="")
pipeline_client = PipelineClient(namespace="", asyncio=False)
expect(pipeline_client.hosts) is None

def when_set_correct_type(expect):
mgmt_client = MgmtClient()
d = defaultdict(dict) # type: ignore
d["test_instance"] = dict({"url": "test_url"})
mgmt_client.hosts = d
expect(mgmt_client.hosts["test_instance"]["url"]) == "test_url"
model_client = ModelClient(namespace="")
model_client.hosts = d
expect(model_client.hosts["test_instance"]["url"]) == "test_url"
pipeline_client = PipelineClient(namespace="")
pipeline_client.hosts = d
expect(pipeline_client.hosts["test_instance"]["url"]) == "test_url"
def when_set_correct_type_url(expect):
mgmt_instance = {
"test_instance": InstillInstance(
mgmt_service.MgmtPublicServiceStub,
"test_url",
"token",
False,
False,
)
}
mgmt_client = MgmtClient(False)
mgmt_client.hosts = mgmt_instance
expect(mgmt_client.hosts["test_instance"].url) == "test_url"

model_instance = {
"test_instance": InstillInstance(
model_service.ModelPublicServiceStub,
"test_url",
"token",
False,
False,
)
}
model_client = ModelClient(namespace="", asyncio=False)
model_client.hosts = model_instance
expect(model_client.hosts["test_instance"].url) == "test_url"

pipeline_instance = {
"test_instance": InstillInstance(
pipeline_service.PipelinePublicServiceStub,
"test_url",
"token",
False,
False,
)
}
pipeline_client = PipelineClient(namespace="", asyncio=False)
pipeline_client.hosts = pipeline_instance
expect(pipeline_client.hosts["test_instance"].url) == "test_url"

def when_set_correct_type_token(expect):
mgmt_instance = {
"test_instance": InstillInstance(
mgmt_service.MgmtPublicServiceStub,
"test_url",
"token",
False,
False,
)
}
mgmt_client = MgmtClient(False)
mgmt_client.hosts = mgmt_instance
expect(mgmt_client.hosts["test_instance"].token) == "token"

model_instance = {
"test_instance": InstillInstance(
model_service.ModelPublicServiceStub,
"test_url",
"token",
False,
False,
)
}
model_client = ModelClient(namespace="", asyncio=False)
model_client.hosts = model_instance
expect(model_client.hosts["test_instance"].token) == "token"

pipeline_instance = {
"test_instance": InstillInstance(
pipeline_service.PipelinePublicServiceStub,
"test_url",
"token",
False,
False,
)
}
pipeline_client = PipelineClient(namespace="", asyncio=False)
pipeline_client.hosts = pipeline_instance
expect(pipeline_client.hosts["test_instance"].token) == "token"

0 comments on commit acae811

Please sign in to comment.