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