1#!/usr/bin/env python3
2#-*- coding: utf-8 -*-
3
4# Copyright (c) 2024 Huawei Device Co., Ltd.
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import time
18from devicetest.core.test_case import TestCase, CheckPoint
19from hypium import UiDriver
20from hypium.action.host import host
21import subprocess
22import shlex
23
24
25def run_command_with_timeout(command, timeout):
26    proc = subprocess.Popen(shlex.split(command))
27    start_time = time.monotonic()
28    while proc.poll() is None:
29        time.sleep(0.1)
30        if time.monotonic() - start_time > timeout:
31            proc.kill()
32            return -1
33    return proc.returncode
34
35
36class case26_init001(TestCase):
37
38    def __init__(self, configs):
39        self.TAG = self.__class__.__name__
40        TestCase.__init__(self, self.TAG, configs)
41        self.tests = [
42            "test_step"
43        ]
44        self.driver = UiDriver(self.device1)
45        self.sn = self.device1.device_sn
46
47    def setup(self):
48        self.log.info("case26_init001 start")
49
50    def test_step(self):
51        driver = self.driver
52        CheckPoint("First kill samgr, enter fastboot or restart")
53        result = driver.System.get_pid("samgr")
54        assert result is not None
55        time.sleep(1)
56        host.shell("hdc -t {} shell kill -9 `pidof samgr`".format(self.sn))
57        command = "fastboot reboot"
58        timeout = 3
59        current_number = 0
60        max_number = 10
61        return_code = -1
62        while current_number < max_number and return_code == -1:
63            return_code = run_command_with_timeout(command, timeout)
64            current_number += 1
65        if return_code == 0:
66            time.sleep(30)
67
68        CheckPoint("Second kill samgr, enter fastboot or restart")
69        result = driver.System.get_pid("samgr")
70        assert result is not None
71        host.shell("hdc -t {} shell kill -9 `pidof samgr`".format(self.sn))
72        current_number = 0
73        return_code = -1
74        while current_number < max_number and return_code == -1:
75            return_code = run_command_with_timeout(command, timeout)
76            current_number += 1
77        if return_code == 0:
78            time.sleep(30)
79
80    def teardown(self):
81        self.log.info("case26_init001 done")
82