|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# OpenPOWER Automated Test Project |
| 3 | +# |
| 4 | +# Contributors Listed Below - COPYRIGHT 2024 |
| 5 | +# [+] International Business Machines Corp. |
| 6 | +# |
| 7 | +# |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 17 | +# implied. See the License for the specific language governing |
| 18 | +# permissions and limitations under the License. |
| 19 | +import json |
| 20 | +import os |
| 21 | +import re |
| 22 | +import unittest |
| 23 | +from urllib.parse import urlparse |
| 24 | +import subprocess |
| 25 | +import OpTestConfiguration |
| 26 | +import OpTestLogger |
| 27 | +import configparser |
| 28 | +import sys |
| 29 | +from testcases import Build_bisector |
| 30 | +from testcases import Email_git |
| 31 | +from common.OpTestSystem import OpSystemState |
| 32 | +from common.OpTestSOL import OpSOLMonitorThread |
| 33 | +from common.Exceptions import CommandFailed |
| 34 | +log = OpTestLogger.optest_logger_glob.get_logger(__name__) |
| 35 | +class Test_build(unittest.TestCase): |
| 36 | + """ |
| 37 | + Test case for building kernel and calling Build_bisector.py in case of build failure |
| 38 | + """ |
| 39 | + |
| 40 | + def setUp(self): |
| 41 | + """ |
| 42 | + Set up the test environment. |
| 43 | + Initializes test parameters and checks required configurations. |
| 44 | + """ |
| 45 | + self.conf = OpTestConfiguration.conf |
| 46 | + self.cv_HOST = self.conf.host() |
| 47 | + self.cv_SYSTEM = self.conf.system() |
| 48 | + self.connection = self.cv_SYSTEM.cv_HOST.get_ssh_connection() |
| 49 | + self.console_thread = OpSOLMonitorThread(1, "console") |
| 50 | + self.host_cmd_timeout = self.conf.args.host_cmd_timeout |
| 51 | + self.repo = self.conf.args.git_repo |
| 52 | + self.repo_reference = self.conf.args.git_repo_reference |
| 53 | + self.branch = self.conf.args.git_branch |
| 54 | + self.home = self.conf.args.git_home |
| 55 | + self.config = self.conf.args.git_repoconfig |
| 56 | + self.good_commit = self.conf.args.good_commit |
| 57 | + self.bad_commit = self.conf.args.bad_commit |
| 58 | + self.bisect_script = self.conf.args.bisect_script |
| 59 | + self.bisect_category = self.conf.args.bisect_category |
| 60 | + self.append_kernel_cmdline = self.conf.args.append_kernel_cmdline |
| 61 | + self.linux_path = os.path.join(self.home, "linux") |
| 62 | + self.bisect_flag = self.conf.args.bisect_flag |
| 63 | + if not self.repo: |
| 64 | + self.fail("Provide git repo of kernel to install") |
| 65 | + if not (self.conf.args.host_ip and self.conf.args.host_user and self.conf.args.host_password): |
| 66 | + self.fail( |
| 67 | + "Provide host ip user details refer, --host-{ip,user,password}") |
| 68 | + |
| 69 | + def build_kernel(self): |
| 70 | + """ |
| 71 | + Build and install the Linux kernel. |
| 72 | + """ |
| 73 | + self.connection.run_command("wget http://ltc-jenkins.aus.stglabs.ibm.com:81/abdul/ioci/kernel_config -o linux/.config") |
| 74 | + self.connection.run_command("cd linux && make olddefconfig") |
| 75 | + try: |
| 76 | + build_command = "make -j && make modules_install && make install" |
| 77 | + err=self.connection.run_command(build_command, timeout=self.host_cmd_timeout) |
| 78 | + log.info("Kernel build successful") |
| 79 | + return 0,err |
| 80 | + except CommandFailed as e: |
| 81 | + log.error("Kernel build failed: {}".format(e)) |
| 82 | + return 4,e |
| 83 | + |
| 84 | + def Store_loc ( self, er) : |
| 85 | + """ |
| 86 | + To get location of file in which error is introduced |
| 87 | + """ |
| 88 | + pattern = r"([\w\d_]+\/(?:(?:[\w\d_]+\/)*[\w\d_]+\b))" |
| 89 | + matches = [match.group(1) for match in re.finditer(pattern,er)] |
| 90 | + return matches |
| 91 | + |
| 92 | + def runTest(self): |
| 93 | + self.connection.run_command("if [ -d {} ]; then rm -rf {}; fi".format(self.home,self.home)) |
| 94 | + self.connection.run_command("if [ ! -d {} ]; then mkdir -p {}; fi".format(self.home,self.home)) |
| 95 | + self.connection.run_command("cd {}".format(self.home)) |
| 96 | + if not self.branch: |
| 97 | + self.branch='master' |
| 98 | + log.info("CD DONE") |
| 99 | + self.connection.run_command("git clone --depth 1 {} -b {}".format(self.repo, self.branch)) |
| 100 | + self.connection.run_command("git clone -b {} {} linux".format( self.branch, self.repo),timeout=3000) |
| 101 | + self.connection.run_command("cd linux") |
| 102 | + commit = self.connection.run_command(" git log -1 --format=%H | sed -r 's/\x1B\[[0-9:]*[JKsu]//g'") |
| 103 | + self.connection.run_command("cd ..") |
| 104 | + error = self.build_kernel() |
| 105 | + log.info("COMMAND RUN") |
| 106 | + exit_code = error[0] |
| 107 | + errVal = str(error[1]) |
| 108 | + log.info("printing the exit code '{}'".format(exit_code)) |
| 109 | + entry=[] |
| 110 | + if exit_code != 0: |
| 111 | + entry = self.Store_loc(errVal)[-1] |
| 112 | + badCommit = commit[-1] |
| 113 | + if self.bisect_flag == '1': |
| 114 | + log.info("BUILD_BISECTOR CALLED") |
| 115 | + bisect = Build_bisector.Buil_bisector() |
| 116 | + bisect.setUp() |
| 117 | + res = bisect.runTest() |
| 118 | + log.info("BUILD_BISECTOR END") |
| 119 | + emaili=res[0] |
| 120 | + commiti=res[1] |
| 121 | + log.info("COMMIT REVERT HAS TO BE CHECKED MANUALLY") |
| 122 | + else : |
| 123 | + emaili="" |
| 124 | + commiti=commit[-1] |
| 125 | + else : |
| 126 | + emaili="" |
| 127 | + commiti=commit[-1] |
| 128 | + with open('output.json','w') as f: |
| 129 | + json.dump({"exit_code":exit_code,"email":emaili,"commit": commiti,"error":entry,"flag":self.bisect_flag},f) |
| 130 | + if exit_code != 0: |
| 131 | + email = Email_git.Email_git() |
| 132 | + email.setUp() |
| 133 | + email.runTest() |
| 134 | + return exit_code |
0 commit comments