def test_delete_bentos_with_tags(bento_service): yc = get_yatai_client() yc.repository.delete(prune=True) version_one = uuid.uuid4().hex[0:8] version_two = uuid.uuid4().hex[0:8] version_three = uuid.uuid4().hex[0:8] bento_service.save(version=version_one) bento_service.save(version=version_two) bento_service.save(version=version_three) yc.repository.delete(bento_tag=f'{bento_service.name}:{version_one}') bentos = yc.repository.list() assert len(bentos) == 2 runner = CliRunner() cli = create_bentoml_cli() print(cli.commands['delete']) runner.invoke( cli.commands['delete'], [ f'{bento_service.name}:{version_two},{bento_service.name}:{version_three}', '-y', ], ) bentos = yc.repository.list() assert len(bentos) == 0 # Clean up existing bentos yc.repository.delete(prune=True)
def test_delete_multiple_bentos_by_tag_from_cli(example_bento_service_class, yatai_client): svc1 = example_bento_service_class() svc1.save() svc2 = example_bento_service_class() svc2.save() svc3 = example_bento_service_class() svc3.save() runner = CliRunner() cli = create_bentoml_cli() result = runner.invoke( cli.commands['delete'], [svc1.tag, '-y'], ) assert result.exit_code == 0 assert (len( yatai_client.repository.list( bento_name=example_bento_service_class.name)) == 2) result = runner.invoke(cli.commands['delete'], [f'{svc2.tag},{svc3.tag}', '-y']) assert result.exit_code == 0 assert (len( yatai_client.repository.list( bento_name=example_bento_service_class.name)) == 0)
def test_track_cli_with_keyboard_interrupt(bento_bundle_path): with patch('bentoml.cli.click_utils.track') as mock: mock.side_effect = mock_track_func runner = CliRunner() cli = create_bentoml_cli() runner.invoke(cli.commands['serve'], [bento_bundle_path]) _, properties = mock.call_args_list[0][0] assert properties['return_code'] == 2 assert properties['error_type'] == 'KeyboardInterrupt' assert properties['command'] == 'serve'
def test_run_command_with_input_file(bento_archive_path): input_path = generate_test_input_file() runner = CliRunner() cli = create_bentoml_cli() run_cmd = cli.commands["<API_NAME>"] result = runner.invoke(run_cmd, ['predict', bento_archive_path, '--input', input_path]) assert result.exit_code == 0 result_json = json.loads(result.output) assert result_json['age']['0'] == 6
def test_track_cli_with_click_exception(): with patch('bentoml.cli.click_utils.track') as mock: mock.side_effect = mock_track_func runner = CliRunner() cli = create_bentoml_cli() runner.invoke(cli.commands['azure-functions'], ['update', 'mock-deployment-name']) _, properties = mock.call_args_list[0][0] assert properties['command'] == 'update' assert properties['command_group'] == 'azure-functions' assert properties['error_type'] == 'AttributeError' assert properties['return_code'] == 1
def test_track_cli_usage(bento_service, bento_bundle_path): with patch('bentoml.cli.click_utils.track') as mock: mock.side_effect = mock_track_func runner = CliRunner() cli = create_bentoml_cli() runner.invoke(cli.commands['info'], [f'{bento_service.name}:{bento_service.version}']) event_name, properties = mock.call_args_list[0][0] assert event_name == 'bentoml-cli' assert properties['command'] == 'info' assert properties['return_code'] == 0 assert properties['duration']
def test_run_command_with_input_file(bento_archive_path): input_path = generate_test_input_file() runner = CliRunner() cli = create_bentoml_cli() run_cmd = cli.commands["<API_NAME>"] result = runner.invoke( run_cmd, ["predict", bento_archive_path, "--input", input_path, "-o", "json"]) assert result.exit_code == 0 result_json = json.loads(result.output) assert result_json[0]["age"] == 6
def test_label_selectors_on_cli_get(bento_service): runner = CliRunner() cli = create_bentoml_cli() unique_label_value = uuid.uuid4().hex bento_service.save(labels={'test_id': unique_label_value}) success_result = runner.invoke( cli.commands['get'], [bento_service.name, '--labels', f'test_id in ({unique_label_value})'], ) assert success_result.exit_code == 0 assert f'{bento_service.name}:{bento_service.version}' in success_result.output
def test_delete_all_bentos_from_cli(example_bento_service_class, yatai_client): svc1 = example_bento_service_class() svc1.save() svc2 = example_bento_service_class() svc2.save() svc3 = example_bento_service_class() svc3.save() assert len(yatai_client.repository.list()) == 3 runner = CliRunner() cli = create_bentoml_cli() result = runner.invoke( cli.commands['delete'], ['--all', '-y'], ) assert result.exit_code == 0 assert len(yatai_client.repository.list()) == 0
def test_label_selectors_on_cli_list(bento_service): runner = CliRunner() cli = create_bentoml_cli() failed_result = runner.invoke(cli.commands['list'], ['--labels', '"incorrect label query"']) assert failed_result.exit_code == 1 assert type(failed_result.exception) == AssertionError assert str(failed_result.exception) == 'Operator "label" is invalid' unique_label_value = uuid.uuid4().hex bento_service.save( labels={ 'test_id': unique_label_value, 'example_key': 'values_can_contains_and-and.s', }) success_result = runner.invoke( cli.commands['list'], ['--labels', f'test_id in ({unique_label_value})']) assert success_result.exit_code == 0 assert f'{bento_service.name}:{bento_service.version}' in success_result.output
def test_delete_all_bentos(bento_service): bento_service.save(version=uuid.uuid4().hex[0:8]) bento_service.save(version=uuid.uuid4().hex[0:8]) bento_service.save(version=uuid.uuid4().hex[0:8]) yc = get_yatai_client() yc.repository.delete(prune=True) bentos = yc.repository.list() assert len(bentos) == 0 bento_service.save(version=uuid.uuid4().hex[0:8]) bento_service.save(version=uuid.uuid4().hex[0:8]) bento_service.save(version=uuid.uuid4().hex[0:8]) runner = CliRunner() cli = create_bentoml_cli() runner.invoke( cli.commands['delete'], ['--all', '-y'], ) bentos = yc.repository.list() assert len(bentos) == 0
configure_logging() from bentoml.saved_bundle import load_from_dir, save_to_dir # noqa: E402 from bentoml.service import ( # noqa: E402 BentoService, api_decorator as api, env_decorator as env, web_static_content_decorator as web_static_content, artifacts_decorator as artifacts, ver_decorator as ver, save, ) from bentoml.cli import create_bentoml_cli commandline_interface = create_bentoml_cli() load = load_from_dir __all__ = [ "__version__", "api", "artifacts", "env", "web_static_content", "ver", "BentoService", # backward compatible "load", "load_from_dir", "save",
import os import sys from bentoml import archive from bentoml.cli import create_bentoml_cli __VERSION__ = "0.0.2019_04_22_e5d9f7d0" __module_path = os.path.abspath(os.path.dirname(__file__)) TextClassificationService = archive.load_bento_service_class(__module_path) cli=create_bentoml_cli(__module_path) def load(): return archive.load(__module_path) __all__ = ['__version__', 'TextClassificationService', 'load']
def test_deployment_labels(): runner = CliRunner() cli = create_bentoml_cli() failed_result = runner.invoke( cli.commands['lambda'], [ 'deploy', 'failed-name', '-b', 'ExampleBentoService:version', '--labels', 'test=abc', ], ) assert failed_result.exit_code == 2 with mock.patch( 'bentoml.yatai.deployment.aws_lambda.operator.AwsLambdaDeploymentOperator.add' ) as mock_operator_add: bento_name = 'MockService' bento_version = 'MockVersion' deployment_name = f'test-label-{uuid.uuid4().hex[:8]}' deployment_namespace = 'test-namespace' mocked_deployment_pb = Deployment(name=deployment_name, namespace=deployment_namespace) mocked_deployment_pb.spec.bento_name = bento_name mocked_deployment_pb.spec.bento_version = bento_version mocked_deployment_pb.spec.operator = DeploymentSpec.AWS_LAMBDA mocked_deployment_pb.spec.aws_lambda_operator_config.memory_size = 1000 mocked_deployment_pb.spec.aws_lambda_operator_config.timeout = 60 mocked_deployment_pb.spec.aws_lambda_operator_config.region = 'us-west-2' mock_operator_add.return_value = ApplyDeploymentResponse( status=Status.OK(), deployment=mocked_deployment_pb) success_result = runner.invoke( cli.commands['lambda'], [ 'deploy', deployment_name, '-b', f'{bento_name}:{bento_version}', '--namespace', deployment_namespace, '--labels', 'created_by:admin,cicd:passed', '--region', 'us-west-2', ], ) assert success_result.exit_code == 0 list_result = runner.invoke( cli.commands['deployment'], [ 'list', '--labels', 'created_by=admin,cicd NotIn (failed, unsuccessful)', '--output', 'wide', ], ) assert list_result.exit_code == 0 assert deployment_name in list_result.output.strip() assert 'created_by:admin' in list_result.output.strip()
if __name__ == '__main__': from bentoml.cli import create_bentoml_cli create_bentoml_cli()()