15ba71b47Sopenharmony_ci#!/usr/bin/env python3
25ba71b47Sopenharmony_ci#-*- coding: utf-8 -*-
35ba71b47Sopenharmony_ci
45ba71b47Sopenharmony_ci# Copyright (c) 2024 Huawei Device Co., Ltd.
55ba71b47Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
65ba71b47Sopenharmony_ci# you may not use this file except in compliance with the License.
75ba71b47Sopenharmony_ci# You may obtain a copy of the License at
85ba71b47Sopenharmony_ci#
95ba71b47Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
105ba71b47Sopenharmony_ci#
115ba71b47Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
125ba71b47Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
135ba71b47Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
145ba71b47Sopenharmony_ci# See the License for the specific language governing permissions and
155ba71b47Sopenharmony_ci# limitations under the License.
165ba71b47Sopenharmony_ci
175ba71b47Sopenharmony_ciimport time
185ba71b47Sopenharmony_cifrom devicetest.core.test_case import TestCase, CheckPoint
195ba71b47Sopenharmony_cifrom hypium import UiDriver
205ba71b47Sopenharmony_cifrom hypium.action.host import host
215ba71b47Sopenharmony_ciimport subprocess
225ba71b47Sopenharmony_ciimport shlex
235ba71b47Sopenharmony_ci
245ba71b47Sopenharmony_ci
255ba71b47Sopenharmony_cidef run_command_with_timeout(command, timeout):
265ba71b47Sopenharmony_ci    proc = subprocess.Popen(shlex.split(command))
275ba71b47Sopenharmony_ci    start_time = time.monotonic()
285ba71b47Sopenharmony_ci    while proc.poll() is None:
295ba71b47Sopenharmony_ci        time.sleep(0.1)
305ba71b47Sopenharmony_ci        if time.monotonic() - start_time > timeout:
315ba71b47Sopenharmony_ci            proc.kill()
325ba71b47Sopenharmony_ci            return -1
335ba71b47Sopenharmony_ci    return proc.returncode
345ba71b47Sopenharmony_ci
355ba71b47Sopenharmony_ci
365ba71b47Sopenharmony_ciclass case26_init001(TestCase):
375ba71b47Sopenharmony_ci
385ba71b47Sopenharmony_ci    def __init__(self, configs):
395ba71b47Sopenharmony_ci        self.TAG = self.__class__.__name__
405ba71b47Sopenharmony_ci        TestCase.__init__(self, self.TAG, configs)
415ba71b47Sopenharmony_ci        self.tests = [
425ba71b47Sopenharmony_ci            "test_step"
435ba71b47Sopenharmony_ci        ]
445ba71b47Sopenharmony_ci        self.driver = UiDriver(self.device1)
455ba71b47Sopenharmony_ci        self.sn = self.device1.device_sn
465ba71b47Sopenharmony_ci
475ba71b47Sopenharmony_ci    def setup(self):
485ba71b47Sopenharmony_ci        self.log.info("case26_init001 start")
495ba71b47Sopenharmony_ci
505ba71b47Sopenharmony_ci    def test_step(self):
515ba71b47Sopenharmony_ci        driver = self.driver
525ba71b47Sopenharmony_ci        CheckPoint("First kill samgr, enter fastboot or restart")
535ba71b47Sopenharmony_ci        result = driver.System.get_pid("samgr")
545ba71b47Sopenharmony_ci        assert result is not None
555ba71b47Sopenharmony_ci        time.sleep(1)
565ba71b47Sopenharmony_ci        host.shell("hdc -t {} shell kill -9 `pidof samgr`".format(self.sn))
575ba71b47Sopenharmony_ci        command = "fastboot reboot"
585ba71b47Sopenharmony_ci        timeout = 3
595ba71b47Sopenharmony_ci        current_number = 0
605ba71b47Sopenharmony_ci        max_number = 10
615ba71b47Sopenharmony_ci        return_code = -1
625ba71b47Sopenharmony_ci        while current_number < max_number and return_code == -1:
635ba71b47Sopenharmony_ci            return_code = run_command_with_timeout(command, timeout)
645ba71b47Sopenharmony_ci            current_number += 1
655ba71b47Sopenharmony_ci        if return_code == 0:
665ba71b47Sopenharmony_ci            time.sleep(30)
675ba71b47Sopenharmony_ci
685ba71b47Sopenharmony_ci        CheckPoint("Second kill samgr, enter fastboot or restart")
695ba71b47Sopenharmony_ci        result = driver.System.get_pid("samgr")
705ba71b47Sopenharmony_ci        assert result is not None
715ba71b47Sopenharmony_ci        host.shell("hdc -t {} shell kill -9 `pidof samgr`".format(self.sn))
725ba71b47Sopenharmony_ci        current_number = 0
735ba71b47Sopenharmony_ci        return_code = -1
745ba71b47Sopenharmony_ci        while current_number < max_number and return_code == -1:
755ba71b47Sopenharmony_ci            return_code = run_command_with_timeout(command, timeout)
765ba71b47Sopenharmony_ci            current_number += 1
775ba71b47Sopenharmony_ci        if return_code == 0:
785ba71b47Sopenharmony_ci            time.sleep(30)
795ba71b47Sopenharmony_ci
805ba71b47Sopenharmony_ci    def teardown(self):
815ba71b47Sopenharmony_ci        self.log.info("case26_init001 done")
82