Skip to content

Commit

Permalink
add ut
Browse files Browse the repository at this point in the history
Signed-off-by: laminar <[email protected]>
  • Loading branch information
tpiperatgod committed Feb 5, 2023
1 parent bdd61f9 commit 93c64da
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 30 deletions.
63 changes: 33 additions & 30 deletions pulsar-functions/instance/src/main/python/python_instance_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 93c64da

Please sign in to comment.