reboot.py 3.6 KB
Newer Older
A
air9 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/env python
# coding: utf-8

# Copyright (c) 2020 Huawei Technologies Co., Ltd.
# oec-hardware is licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Create: 2020-04-01

import datetime

from .document import Document, FactoryDocument
from .env import CertEnv
from .command import Command, CertCommandError


C
cuixucui 已提交
22 23 24 25
class Reboot(object):
    """
    Special for restart tasks, so that the test can be continued after the machine is restarted
    """
A
air9 已提交
26 27 28 29 30 31 32
    def __init__(self, testname, job, rebootup):
        self.testname = testname
        self.rebootup = rebootup
        self.job = job
        self.reboot = dict()

    def clean(self):
C
cuixucui 已提交
33 34 35 36
        """
        Remove reboot file
        :return:
        """
A
air9 已提交
37 38 39 40 41 42 43 44
        if not (self.job and self.testname):
            return

        for test in self.job.test_factory:
            if test["run"] and self.testname == test["name"]:
                test["reboot"] = False

        Command("rm -rf %s" % CertEnv.rebootfile).run(ignore_errors=True)
T
theprocess 已提交
45
        Command("systemctl disable oech").run(ignore_errors=True)
A
air9 已提交
46 47

    def setup(self):
C
cuixucui 已提交
48 49 50 51
        """
        Reboot  setuping
        :return:
        """
A
air9 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        if not (self.job and self.testname):
            print("Error: invalid reboot input.")
            return False

        self.job.save_result()
        for test in self.job.test_factory:
            if test["run"] and self.testname == test["name"]:
                test["reboot"] = True
                test["status"] = "FAIL"
        if not FactoryDocument(CertEnv.factoryfile, self.job.test_factory).save():
            print("Error: save testfactory doc fail before reboot.")
            return False

        self.reboot["job_id"] = self.job.job_id
        self.reboot["time"] = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        self.reboot["test"] = self.testname
        self.reboot["rebootup"] = self.rebootup
        if not Document(CertEnv.rebootfile, self.reboot).save():
            print("Error: save reboot doc fail.")
            return False

        try:
            Command("systemctl daemon-reload").run_quiet()
T
theprocess 已提交
75
            Command("systemctl enable oech").run_quiet()
C
cuixucui 已提交
76
        except OSError as e:
T
theprocess 已提交
77
            print("Error: enable oech.service fail.")
A
air9 已提交
78 79 80 81 82
            return False

        return True

    def check(self):
C
cuixucui 已提交
83 84 85 86
        """
        Reboot file check
        :return:
        """
A
air9 已提交
87 88 89 90 91 92 93 94 95 96 97
        doc = Document(CertEnv.rebootfile)
        if not doc.load():
            print("Error: reboot file load fail.")
            return False

        try:
            self.testname = doc.document["test"]
            self.reboot = doc.document
            self.job.job_id = self.reboot["job_id"]
            self.job.subtests_filter = self.reboot["rebootup"]
            time_reboot = datetime.datetime.strptime(self.reboot["time"], "%Y%m%d%H%M%S")
C
cuixucui 已提交
98
        except KeyError:
A
air9 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111
            print("Error: reboot file format not as expect.")
            return False

        time_now = datetime.datetime.now()
        time_delta = (time_now - time_reboot).seconds
        cmd = Command("last reboot -s '%s seconds ago'" % time_delta)
        reboot_list = cmd.get_str("^reboot .*$", single_line=False, return_list=True)
        if len(reboot_list) != 1:
            print("Errot:reboot times check fail.")
            return False

        return True