1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright (C) 2024 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15# 运行环境: python 3.10+, pytest, pytest-repeat, pytest-testreport
16# 准备文件:package.zip
17# pip install pytest pytest-testreport pytest-repeat
18# python hdc_normal_test.py
19
20
21import argparse
22import logging
23import os
24import time
25import pytest
26
27
28from dev_hdc_test import GP
29from dev_hdc_test import check_library_installation
30from dev_hdc_test import check_hdc_cmd, check_hdc_targets, get_local_path, get_remote_path
31from dev_hdc_test import check_soft_local, check_soft_remote
32from dev_hdc_test import check_app_uninstall, prepare_source, make_multiprocess_file
33from dev_hdc_test import execute_lines_in_file, hdc_get_key, rmdir, pytest_run
34
35
36logging.basicConfig(level=logging.INFO,
37                format='%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s',
38                datefmt='%d %b %Y %H:%M:%S',
39                   )
40
41
42def test_list_targets():
43    assert check_hdc_targets()
44
45
46@pytest.mark.repeat(5)
47def test_soft_link():
48    assert check_soft_local(get_local_path('small'),
49                            get_local_path('soft_small'),
50                            get_remote_path('it_small_soft')
51                            )
52    assert check_soft_remote('it_small_soft',
53                             get_remote_path('it_soft_small'),
54                             get_local_path('recv_soft_small')
55                            )
56
57
58@pytest.mark.repeat(1)
59def test_mix_file():
60    muti_num = 5 # the count of multiprocess file
61    assert make_multiprocess_file(get_local_path('small'), get_remote_path('it_small'), 'send', muti_num, "file")
62    assert make_multiprocess_file(get_local_path('small_recv'), get_remote_path('it_small'), 'recv', muti_num, "file")
63    assert make_multiprocess_file(get_local_path('medium'), get_remote_path('it_medium'), 'send', muti_num, "file")
64    assert make_multiprocess_file(get_local_path('medium_recv'), get_remote_path('it_medium'), 'recv', muti_num, "file")
65
66
67
68def test_recv_dir():
69    assert make_multiprocess_file(get_local_path('package'), get_remote_path(''), 'send', 1, "dir")
70    assert check_hdc_cmd(f"shell mv {get_remote_path('package')} {get_remote_path('it_package')}")
71    assert make_multiprocess_file(get_local_path(''), get_remote_path('it_package'), 'recv', 1, "dir")
72    if os.path.exists(get_local_path('it_package')):
73        rmdir(get_local_path('it_package'))
74
75
76def test_te_case():
77    execute_lines_in_file('te.txt')
78
79
80def test_hap_install():
81    assert check_hdc_cmd(f"install -s {get_local_path('libA_v10001.hsp')}",
82                            bundle="com.example.liba")
83
84    assert check_hdc_cmd(f"install -s {get_local_path('libB_v10001.hsp')}",
85                            bundle="com.example.libb")
86
87    app_name_default_a = "com.example.liba"
88    app_name_default_b = "com.example.libb"
89    assert check_app_uninstall(app_name_default_a, "-s")
90    assert check_app_uninstall(app_name_default_b, "-s")
91
92
93def test_shell_print():
94    check_hdc_cmd("shell echo 'hello world'")
95
96
97def test_shell_rm():
98    check_hdc_cmd("shell rm -rf data/local/tmp/it_*")
99
100
101def test_shell_ls():
102    check_hdc_cmd("shell ls data/local/tmp")
103
104
105def test_file_smap():
106    pid = hdc_get_key("shell pidof hdcd")
107    check_hdc_cmd(f"file recv proc/{pid}/smaps resource/smaps")
108
109
110def test_shell_mkdir():
111    check_hdc_cmd("shell mkdir -p data/local/tmp/it")
112
113
114def test_shell_rmdir():
115    check_hdc_cmd("shell rmdir data/local/tmp/it")
116
117
118def setup_class():
119    print("setting up env ...")
120    check_hdc_cmd("shell rm -rf data/local/tmp/it_*")
121    GP.load()
122
123
124def teardown_class():
125    pass
126
127
128def run_main():
129    if check_library_installation("pytest"):
130        exit(1)
131
132    if check_library_installation("pytest-testreport"):
133        exit(1)
134
135    if check_library_installation("pytest-repeat"):
136        exit(1)
137
138    GP.init()
139    if not os.path.exists(GP.local_path):
140        prepare_source()
141
142    choice_default = ""
143    parser = argparse.ArgumentParser()
144    parser.add_argument('--count', type=int, default=1,
145                        help='test times')
146    parser.add_argument('--verbose', '-v', default=__file__,
147                        help='filename')
148    parser.add_argument('--desc', '-d', default='Test for function.',
149                        help='Add description on report')
150    args = parser.parse_args()
151
152    pytest_run(args)
153
154
155if __name__ == "__main__":
156    run_main()