Best Python code snippet using avocado_python
ltp.py
Source:ltp.py
...25from avocado.utils.partition import Partition26from avocado.utils.software_manager import SoftwareManager27def clear_dmesg():28 process.run("dmesg -c ", sudo=True)29def collect_dmesg(obj):30 obj.whiteboard = process.system_output("dmesg").decode()31class LTP(Test):32 """33 LTP (Linux Test Project) testsuite34 :param args: Extra arguments ("runltp" can use with35 "-f $test")36 """37 failed_tests = list()38 mem_tests = ['-f mm', '-f hugetlb']39 @staticmethod40 def mount_point(mount_dir):41 lines = genio.read_file('/proc/mounts').rstrip('\t\r\0').splitlines()42 for substr in lines:43 mop = substr.split(" ")[1]44 if mop == mount_dir:45 return True46 return False47 def check_thp(self):48 if 'thp_file_alloc' in genio.read_file('/proc/vm'49 'stat').rstrip('\t\r\n\0'):50 self.thp = True51 return self.thp52 def setup_tmpfs_dir(self):53 # check for THP page cache54 self.check_thp()55 if not os.path.isdir(self.mount_dir):56 os.makedirs(self.mount_dir)57 self.device = None58 if not self.mount_point(self.mount_dir):59 if self.thp:60 self.device = Partition(61 device="none", mountpoint=self.mount_dir,62 mount_options="huge=always")63 else:64 self.device = Partition(65 device="none", mountpoint=self.mount_dir)66 self.device.mount(mountpoint=self.mount_dir,67 fstype="tmpfs", mnt_check=False)68 def setUp(self):69 smg = SoftwareManager()70 dist = distro.detect()71 self.args = self.params.get('args', default='')72 self.mem_leak = self.params.get('mem_leak', default=0)73 deps = ['gcc', 'make', 'automake', 'autoconf', 'psmisc']74 deps.extend(['numactl-devel'])75 self.ltpbin_dir = self.mount_dir = None76 self.thp = False77 if self.args in self.mem_tests:78 self.mount_dir = self.params.get('tmpfs_mount_dir', default=None)79 if self.mount_dir:80 self.setup_tmpfs_dir()81 over_commit = self.params.get('overcommit', default=True)82 if not over_commit:83 process.run('echo 2 > /proc/sys/vm/overcommit_memory',84 shell=True, ignore_status=True)85 for package in deps:86 if not smg.check_installed(package) and not smg.install(package):87 self.cancel('%s is needed for the test to be run' % package)88 clear_dmesg()89 self.tmppart = Partition("/dev/vdb", mountpoint="/var/tmp")90 mounted_devs = [line.split()[0]91 for line in process.getoutput('mount').splitlines()]92 if self.tmppart.device not in mounted_devs:93 self.tmppart.mkfs(fstype="ext4")94 self.tmppart.mount()95 #url = "https://github.com/linux-test-project/ltp/archive/master.zip"96 #tarball = self.fetch_asset("ltp-master.zip", locations=[url])97 tarball = self.get_data("ltp-full-20210927.tar.bz2")98 archive.extract(tarball, self.teststmpdir)99 self.version = os.path.basename(tarball.split('.tar.')[0])100 ltp_dir = os.path.join(self.teststmpdir, self.version)101 os.chdir(ltp_dir)102 build.make(ltp_dir, extra_args='autotools')103 if not self.ltpbin_dir:104 self.ltpbin_dir = os.path.join(self.teststmpdir, 'bin')105 # if not os.path.exists(self.ltpbin_dir):106 # os.mkdir(self.ltpbin_dir)107 os.makedirs(self.ltpbin_dir, exist_ok=True)108 process.system('./configure --prefix=%s' % self.ltpbin_dir)109 build.make(ltp_dir)110 build.make(ltp_dir, extra_args='install')111 def test(self):112 logfile = os.path.join(self.logdir, 'ltp.log')113 failcmdfile = os.path.join(self.logdir, 'failcmdfile')114 skipfilepath = self.get_data('skipfile')115 os.chmod(self.teststmpdir, 0o755)116 self.args += (" -q -p -l %s -C %s -d %s -S %s"117 % (logfile, failcmdfile, self.teststmpdir,118 skipfilepath))119 if self.mem_leak:120 self.args += " -M %s" % self.mem_leak121 cmd = "%s %s" % (os.path.join(self.ltpbin_dir, 'runltp'), self.args)122 process.run(cmd, ignore_status=True)123 # Walk the ltp.log and try detect failed tests from lines like these:124 # msgctl04 FAIL 2125 with open(logfile, 'r') as file_p:126 lines = file_p.readlines()127 for line in lines:128 if 'FAIL' in line:129 value = re.split(r'\s+', line)130 self.failed_tests.append(value[0])131 collect_dmesg(self)132 if self.failed_tests:133 self.fail("LTP tests failed: %s" % self.failed_tests)134 def tearDown(self):135 if self.mount_dir:...
dmesg.py
Source:dmesg.py
...31 verbose=False, shell=True, sudo=True)32 if status:33 raise DmesgError(34 "Unable to clear dmesg as some issue while clearing")35def collect_dmesg(output_file=None):36 """Function collect dmesg and save in file.37 The dmesg operation is a privileged user task.38 This function needs sudo permissions enabled on the target host39 :param output_file : File use for save dmesg output if not provided it use40 tmp file which located in system /tmp path41 :type output_file: str42 :return: file which contain dmesg43 :rtype: str44 """45 if output_file is None:46 _, output_file = tempfile.mkstemp(suffix=".-%s" % time.strftime("%Y-%m-%d:%H:%M:%S"),47 dir=tempfile.gettempdir())48 dmesg = process.system_output(49 "dmesg", ignore_status=True, sudo=True).decode()50 genio.write_file(output_file, dmesg)51 if not os.path.isfile(output_file):52 raise DmesgError("{} is not a valid file.".format(output_file))53 return output_file54def collect_errors_dmesg(patterns):55 """Check patterns in dmesg.56 :param patterns : List variable to search in dmesg57 :return error log in form of list58 :rtype: list of str59 """60 error = []61 dmesg_log_file = collect_dmesg(None)62 for fail_pattern in patterns:63 for log in genio.read_file(dmesg_log_file).splitlines():64 if fail_pattern in log:65 error.append(log)66 return error67def collect_errors_by_level(output_file=None, level_check=5):68 """Verify dmesg having severity level of OS issue(s).69 :param output_file: The file used to save dmesg70 :type output_file: str71 :param level_check: level of severity of issues to be checked72 1 - emerg73 2 - emerg,alert74 3 - emerg,alert,crit75 4 - emerg,alert,crit,err...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!