From 93c64dab5859ec36d6eb65a4f16b933e7f8be794 Mon Sep 17 00:00:00 2001 From: laminar Date: Sun, 5 Feb 2023 11:12:11 +0800 Subject: [PATCH] add ut Signed-off-by: laminar --- .../src/main/python/python_instance_main.py | 63 +++++++++-------- .../test/python/test_python_instance_main.py | 68 +++++++++++++++++++ .../python/test_python_runtime_config.ini | 8 +++ 3 files changed, 109 insertions(+), 30 deletions(-) create mode 100644 pulsar-functions/instance/src/test/python/test_python_instance_main.py create mode 100644 pulsar-functions/instance/src/test/python/test_python_runtime_config.ini diff --git a/pulsar-functions/instance/src/main/python/python_instance_main.py b/pulsar-functions/instance/src/main/python/python_instance_main.py index e9922d00d08731..2d6520b2e99e9e 100755 --- a/pulsar-functions/instance/src/main/python/python_instance_main.py +++ b/pulsar-functions/instance/src/main/python/python_instance_main.py @@ -56,6 +56,38 @@ def atexit_function(signo, _frame): to_run = False +def generate_arguments_parser(): + parser = argparse.ArgumentParser(description='Pulsar Functions Python Instance') + parser.add_argument('--function_details', required=False, help='Function Details Json String') + parser.add_argument('--py', required=False, help='Full Path of Function Code File') + parser.add_argument('--instance_id', required=False, help='Instance Id') + parser.add_argument('--function_id', required=False, help='Function Id') + parser.add_argument('--function_version', required=False, help='Function Version') + parser.add_argument('--pulsar_serviceurl', required=False, help='Pulsar Service Url') + parser.add_argument('--client_auth_plugin', required=False, help='Client authentication plugin') + parser.add_argument('--client_auth_params', required=False, help='Client authentication params') + parser.add_argument('--use_tls', required=False, help='Use tls') + parser.add_argument('--tls_allow_insecure_connection', required=False, help='Tls allow insecure connection') + parser.add_argument('--hostname_verification_enabled', required=False, help='Enable hostname verification') + parser.add_argument('--tls_trust_cert_path', required=False, help='Tls trust cert file path') + parser.add_argument('--port', required=False, help='Instance Port', type=int) + parser.add_argument('--metrics_port', required=False, help="Port metrics will be exposed on", type=int) + parser.add_argument('--max_buffered_tuples', required=False, help='Maximum number of Buffered tuples') + parser.add_argument('--logging_directory', required=False, help='Logging Directory') + parser.add_argument('--logging_file', required=False, help='Log file name') + parser.add_argument('--logging_level', required=False, help='Logging level') + parser.add_argument('--logging_config_file', required=False, help='Config file for logging') + parser.add_argument('--expected_healthcheck_interval', required=False, help='Expected time in seconds between health checks', type=int) + parser.add_argument('--secrets_provider', required=False, help='The classname of the secrets provider') + parser.add_argument('--secrets_provider_config', required=False, help='The config that needs to be passed to secrets provider') + parser.add_argument('--install_usercode_dependencies', required=False, help='For packaged python like wheel files, do we need to install all dependencies', type=bool) + parser.add_argument('--dependency_repository', required=False, help='For packaged python like wheel files, which repository to pull the dependencies from') + parser.add_argument('--extra_dependency_repository', required=False, help='For packaged python like wheel files, any extra repository to pull the dependencies from') + parser.add_argument('--state_storage_serviceurl', required=False, help='Managed State Storage Service Url') + parser.add_argument('--cluster_name', required=False, help='The name of the cluster this instance is running on') + parser.add_argument('--config_file', required=False, default="", help='Configuration file name', type=str) + return parser + def merge_arguments(args, config_file): """ This function is used to merge arguments passed in via the command line @@ -120,36 +152,7 @@ def main(): signal.signal(signal.SIGHUP, atexit_function) signal.signal(signal.SIGINT, atexit_function) - parser = argparse.ArgumentParser(description='Pulsar Functions Python Instance') - parser.add_argument('--function_details', required=False, help='Function Details Json String') - parser.add_argument('--py', required=False, help='Full Path of Function Code File') - parser.add_argument('--instance_id', required=False, help='Instance Id') - parser.add_argument('--function_id', required=False, help='Function Id') - parser.add_argument('--function_version', required=False, help='Function Version') - parser.add_argument('--pulsar_serviceurl', required=False, help='Pulsar Service Url') - parser.add_argument('--client_auth_plugin', required=False, help='Client authentication plugin') - parser.add_argument('--client_auth_params', required=False, help='Client authentication params') - parser.add_argument('--use_tls', required=False, help='Use tls') - parser.add_argument('--tls_allow_insecure_connection', required=False, help='Tls allow insecure connection') - parser.add_argument('--hostname_verification_enabled', required=False, help='Enable hostname verification') - parser.add_argument('--tls_trust_cert_path', required=False, help='Tls trust cert file path') - parser.add_argument('--port', required=False, help='Instance Port', type=int) - parser.add_argument('--metrics_port', required=False, help="Port metrics will be exposed on", type=int) - parser.add_argument('--max_buffered_tuples', required=False, help='Maximum number of Buffered tuples') - parser.add_argument('--logging_directory', required=False, help='Logging Directory') - parser.add_argument('--logging_file', required=False, help='Log file name') - parser.add_argument('--logging_level', required=False, help='Logging level') - parser.add_argument('--logging_config_file', required=False, help='Config file for logging') - parser.add_argument('--expected_healthcheck_interval', required=False, help='Expected time in seconds between health checks', type=int) - parser.add_argument('--secrets_provider', required=False, help='The classname of the secrets provider') - parser.add_argument('--secrets_provider_config', required=False, help='The config that needs to be passed to secrets provider') - parser.add_argument('--install_usercode_dependencies', required=False, help='For packaged python like wheel files, do we need to install all dependencies', type=bool) - parser.add_argument('--dependency_repository', required=False, help='For packaged python like wheel files, which repository to pull the dependencies from') - parser.add_argument('--extra_dependency_repository', required=False, help='For packaged python like wheel files, any extra repository to pull the dependencies from') - parser.add_argument('--state_storage_serviceurl', required=False, help='Managed State Storage Service Url') - parser.add_argument('--cluster_name', required=False, help='The name of the cluster this instance is running on') - parser.add_argument('--config_file', required=False, default="", help='Configuration file name', type=str) - + parser = generate_arguments_parser() args = parser.parse_args() merge_arguments(args, args.config_file) validate_arguments(args) diff --git a/pulsar-functions/instance/src/test/python/test_python_instance_main.py b/pulsar-functions/instance/src/test/python/test_python_instance_main.py new file mode 100644 index 00000000000000..e350ea99b39203 --- /dev/null +++ b/pulsar-functions/instance/src/test/python/test_python_instance_main.py @@ -0,0 +1,68 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + + +# DEPENDENCIES: unittest2 +import python_instance_main + +import log +import unittest + +class TestContextImpl(unittest.TestCase): + + def Any(cls): + class Any(cls): + def __eq__(self, other): + return True + return Any() + + def setUp(self): + log.init_logger("INFO", "foo", "logging.ini") + + def test_arguments(self): + parser = python_instance_main.generate_arguments_parser() + argv = [ + "--function_details", "test_function_details", + "--py", "test_py", + "--instance_id", "test_instance_id", + "--function_id", "test_function_id", + "--function_version", "test_function_version", + "--pulsar_serviceurl", "test_pulsar_serviceurl", + "--client_auth_plugin", "test_client_auth_plugin", + "--client_auth_params", "test_client_auth_params", + "--tls_allow_insecure_connection", "true", + "--hostname_verification_enabled", "true", + "--tls_trust_cert_path", "test_tls_trust_cert_path", + "--port", "1000", + "--metrics_port", "1001", + "--max_buffered_tuples", "100", + "--config_file", "test_python_runtime_config.ini" + ] + args = parser.parse_args(argv) + python_instance_main.merge_arguments(args, args.config_file) + # argument from command line test + self.assertEqual(args.function_details, "test_function_details") + # argument from config file test + self.assertEqual(args.use_tls, "true") + # argument read priority test + self.assertEqual(args.port, 1000) + # mandatory argument test + self.assertEqual(args.expected_healthcheck_interval, "50") + # optional argument test + self.assertEqual(args.secrets_provider, None) diff --git a/pulsar-functions/instance/src/test/python/test_python_runtime_config.ini b/pulsar-functions/instance/src/test/python/test_python_runtime_config.ini new file mode 100644 index 00000000000000..d9148985749f6f --- /dev/null +++ b/pulsar-functions/instance/src/test/python/test_python_runtime_config.ini @@ -0,0 +1,8 @@ +[DEFAULT] +port=5000 +metrics_port=5001 +use_tls=true +logging_directory=test_logging_directory +logging_file=test_logging_file +logging_config_file=test_logging_config_file +expected_healthcheck_interval=50 \ No newline at end of file