run.py 4.3 KB
Newer Older
F
felixhjh 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# coding:utf-8
# Copyright (c) 2020  PaddlePaddle Authors. All Rights Reserved.
#
# Licensed 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.
'''
This module is used to check whether the running environment for PaddleServing is configured correctly.

Two test cases are set for verifying the smooth of environment, fit a line test case for C++ Serving environment and uci for Pipeline Serving enviroment

Usage: export PYTHON_EXECUTABLE=/usr/local/bin/python3.6
       python3.6 -m paddle_serving_server.serve check
'''

F
felixhjh 已提交
24 25
import sys
import os
F
felixhjh 已提交
26
import pytest
F
felixhjh 已提交
27

F
felixhjh 已提交
28
inference_test_cases = ["test_fit_a_line.py::TestFitALine::test_inference"]
29 30 31 32 33 34 35 36
cpp_test_cases = [
    "test_fit_a_line.py::TestFitALine::test_cpu",
    "test_fit_a_line.py::TestFitALine::test_gpu"
]
pipeline_test_cases = [
    "test_uci_pipeline.py::TestUCIPipeline::test_cpu",
    "test_uci_pipeline.py::TestUCIPipeline::test_gpu"
]
F
felixhjh 已提交
37
log_files = ["PipelineServingLogs", "log", "stderr.log", "stdout.log"]
F
felixhjh 已提交
38

39

F
felixhjh 已提交
40 41
def set_serving_log_path():
    if 'SERVING_LOG_PATH' not in os.environ:
42
        serving_log_path = os.path.expanduser(os.getcwd()) + '/'
43 44
        os.environ['SERVING_LOG_PATH'] = serving_log_path

F
felixhjh 已提交
45

F
felixhjh 已提交
46 47 48 49 50 51 52
def mv_log_to_new_dir(dir_path):
    import shutil
    if not os.path.exists(dir_path):
        os.mkdir(dir_path)
    serving_log_path = os.environ['SERVING_LOG_PATH']
    for file_name in log_files:
        file_path = os.path.join(serving_log_path, file_name)
F
felixhjh 已提交
53
        dir_path_temp = os.path.join(dir_path, file_name)
F
felixhjh 已提交
54
        if os.path.exists(file_path):
55 56
            shutil.move(file_path, dir_path_temp)

F
felixhjh 已提交
57

F
felixhjh 已提交
58
def run_test_cases(cases_list, case_type, is_open_std):
F
felixhjh 已提交
59 60 61
    old_stdout, old_stderr = sys.stdout, sys.stderr
    real_path = os.path.dirname(os.path.realpath(__file__))
    for case in cases_list:
F
felixhjh 已提交
62 63 64
        if is_open_std is False:
            sys.stdout = open('/dev/null', 'w')
            sys.stderr = open('/dev/null', 'w')
F
felixhjh 已提交
65 66 67 68
        args_str = "--disable-warnings " + str(real_path) + "/" + case
        args = args_str.split(" ")
        res = pytest.main(args)
        sys.stdout, sys.stderr = old_stdout, old_stderr
F
felixhjh 已提交
69
        case_name = case.split('_')[-1]
F
felixhjh 已提交
70 71 72 73
        serving_log_path = os.environ['SERVING_LOG_PATH']
        dir_name = str(case_type) + '_' + case.split(':')[-1]
        new_dir_path = os.path.join(serving_log_path, dir_name)
        mv_log_to_new_dir(new_dir_path)
F
felixhjh 已提交
74
        if res == 0:
75 76
            print("{} {} environment running success".format(case_type,
                                                             case_name))
F
felixhjh 已提交
77 78
        elif res == 1:
            if case_name == "inference":
79 80 81
                print(
                    "{} {} environment running failure. Please refer to https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/install/pip/linux-pip.html to configure environment".
                    format(case_type, case_name))
82
                os._exit(0)
F
felixhjh 已提交
83
            else:
84 85 86 87
                print(
                    "{} {} environment running failure, if you need this environment, please refer to https://github.com/PaddlePaddle/Serving/blob/develop/doc/Install_CN.md".
                    format(case_type, case_name))

F
felixhjh 已提交
88

F
felixhjh 已提交
89
def unset_env(key):
F
felixhjh 已提交
90
    del os.environ[key]
F
felixhjh 已提交
91

92

F
felixhjh 已提交
93
def check_env(mode):
F
felixhjh 已提交
94
    set_serving_log_path()
F
felixhjh 已提交
95
    if 'https_proxy' in os.environ or 'http_proxy' in os.environ:
96 97
        unset_env("https_proxy")
        unset_env("http_proxy")
F
felixhjh 已提交
98
    if 'GREP_OPTIONS' in os.environ:
99 100 101
        unset_env("GREP_OPTIONS")
    is_open_std = False
    if mode == "debug":
F
felixhjh 已提交
102
        is_open_std = True
103
    if mode == "all" or mode == "inference" or mode == "debug":
F
felixhjh 已提交
104
        run_test_cases(inference_test_cases, "PaddlePaddle", is_open_std)
105
    if mode == "all" or mode == "cpp" or mode == "debug":
F
felixhjh 已提交
106
        run_test_cases(cpp_test_cases, "C++", is_open_std)
107
    if mode == "all" or mode == "pipeline" or mode == "debug":
F
felixhjh 已提交
108 109
        run_test_cases(pipeline_test_cases, "Pipeline", is_open_std)

110

F
felixhjh 已提交
111 112
if __name__ == '__main__':
    check_env("debug")