• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15"""
16test MindSpore vlog interface
17"""
18
19import subprocess
20import pytest
21import os
22import shutil
23
24
25def check_output(vlog_v, expect_output, is_expect=True):
26    """set VLOG_v to vlog_v and check output """
27    cmd = f"VLOG_v={vlog_v} python -c 'import mindspore as ms'"
28    s = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
29    out = s.stdout.read().decode("UTF-8")
30    s.stdout.close()
31
32    matched = False
33    lines = out.split('\n')
34    for line in lines:
35        if line.find(expect_output) > 0:
36            matched = True
37            break
38    if is_expect:
39        assert matched, f'`VLOG_v={vlog_v}` expect `{expect_output}` fail'
40    else:
41        assert not matched, '`VLOG_v={vlog_v}` unexpected `{expect_output}` fail'
42
43
44@pytest.mark.level0
45@pytest.mark.platform_arm_ascend_training
46@pytest.mark.platform_x86_ascend_training
47@pytest.mark.env_onecard
48def test_vlog():
49    """
50    Feature: test mindspore vlog interface
51    Description: check whether mindspore vlog can work properly
52    Expectation: vlog can work properly
53    """
54    # test invalid VLOG_v value
55    check_output('xxx', 'Value of environment var VLOG_v is invalid:')
56    check_output('2147483648', 'Value of environment var VLOG_v is invalid:')
57    check_output('(1,2147483648)', 'Value of environment var VLOG_v is invalid:')
58    check_output('(,)', 'Value of environment var VLOG_v is invalid:')
59    check_output('0', 'Value of environment var VLOG_v is invalid:')
60
61    # test valid VLOG_v value and expect some outputs
62    check_output('(1,2147483647)', ': log level for printing vlog tags already been used')
63    check_output('(,2147483647)', ': log level for printing vlog tags already been used')
64    check_output('(1,)', ': log level for printing vlog tags already been used')
65    check_output('20000', ': log level for printing vlog tags already been used')
66
67    # test valid VLOG_v value and unexpected some outputs
68    check_output('(5,3)', ': log level for printing vlog tags already been used', False)
69    check_output('(,20)', ': log level for printing vlog tags already been used', False)
70    check_output('(20001,)', ': log level for printing vlog tags already been used', False)
71    check_output('50', ': log level for printing vlog tags already been used', False)
72
73
74@pytest.mark.level0
75@pytest.mark.env_onecard
76def test_vlog_to_file():
77    """
78    Feature: test mindspore vlog interface
79    Description: check whether mindspore vlog can work properly when log to file
80    Expectation: vlog can work properly
81    """
82    current_dir = os.getcwd()
83    log_path = f'{current_dir}/glogdir/111/222'
84    cmd = f"GLOG_logtostderr=0 GLOG_log_dir={log_path} VLOG_v='(1,)' python -c 'import mindspore as ms'"
85    retcode, _ = subprocess.getstatusoutput(cmd)
86    assert retcode == 0
87
88    log_file = f'{log_path}/rank_0/logs/mindspore.INFO'
89    matched = False
90    if os.path.exists(log_file):
91        with open(log_file, 'r') as f:
92            line = f.readline()
93            while line:
94                if line.find(': log level for printing vlog tags already been used') > 0:
95                    matched = True
96                    break
97                line = f.readline()
98    shutil.rmtree(f'{current_dir}/glogdir')
99    assert matched
100