Best Python code snippet using avocado_python
ltp.py
Source:ltp.py
1#!/usr/bin/env python32# This program is free software; you can redistribute it and/or modify3# it under the terms of the GNU General Public License as published by4# the Free Software Foundation; either version 2 of the License, or5# (at your option) any later version.6#7# This program is distributed in the hope that it will be useful,8# but WITHOUT ANY WARRANTY; without even the implied warranty of9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.10#11# See LICENSE for more details.12#13# Copyright: 2016 IBM14# Author: Santhosh G <santhog4@linux.vnet.ibm.com>15#16# Based on code by Martin Bligh <mbligh@google.com>17# copyright 2006 Google, Inc.18# https://github.com/autotest/autotest-client-tests/tree/master/ltp19import os20import re21import shutil22from avocado import Test23from avocado.utils import build, distro, genio24from avocado.utils import process, archive25from 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:...
test_teststmpdir.py
Source:test_teststmpdir.py
1import os2import tempfile3import shutil4import unittest5from avocado.core import exit_codes6from avocado.core import test7from avocado.utils import process8from avocado.utils import script9from .. import AVOCADO, BASEDIR10INSTRUMENTED_SCRIPT = """import os11import tempfile12from avocado import Test13class MyTest(Test):14 def test1(self):15 tempfile.mkstemp(dir=self.teststmpdir)16 if len(os.listdir(self.teststmpdir)) != 2:17 self.fail()18"""19SIMPLE_SCRIPT = """#!/bin/bash20mktemp ${{{0}}}/XXXXXX21if [ $(ls ${{{0}}} | wc -l) == 1 ]22then23 exit 024else25 exit 126fi27""".format(test.COMMON_TMPDIR_NAME)28class TestsTmpDirTests(unittest.TestCase):29 def setUp(self):30 self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__)31 self.simple_test = script.TemporaryScript(32 'test_simple.sh',33 SIMPLE_SCRIPT)34 self.simple_test.save()35 self.instrumented_test = script.TemporaryScript(36 'test_instrumented.py',37 INSTRUMENTED_SCRIPT)38 self.instrumented_test.save()39 def run_and_check(self, cmd_line, expected_rc, env=None):40 os.chdir(BASEDIR)41 result = process.run(cmd_line, ignore_status=True, env=env)42 self.assertEqual(result.exit_status, expected_rc,43 "Command %s did not return rc "44 "%d:\n%s" % (cmd_line, expected_rc, result))45 return result46 @unittest.skipIf(test.COMMON_TMPDIR_NAME in os.environ,47 "%s already set in os.environ"48 % test.COMMON_TMPDIR_NAME)49 def test_tests_tmp_dir(self):50 """51 Tests whether automatically created teststmpdir is shared across52 all tests.53 """54 cmd_line = ("%s run --sysinfo=off "55 "--job-results-dir %s %s %s"56 % (AVOCADO, self.tmpdir, self.simple_test,57 self.instrumented_test))58 self.run_and_check(cmd_line, exit_codes.AVOCADO_ALL_OK)59 def test_manualy_created(self):60 """61 Tests whether manually set teststmpdir is used and not deleted by62 avocado63 """64 shared_tmp = tempfile.mkdtemp(dir=self.tmpdir)65 cmd = ("%s run --sysinfo=off --job-results-dir %s %%s"66 % (AVOCADO, self.tmpdir))67 self.run_and_check(cmd % self.simple_test, exit_codes.AVOCADO_ALL_OK,68 {test.COMMON_TMPDIR_NAME: shared_tmp})69 self.run_and_check(cmd % self.instrumented_test,70 exit_codes.AVOCADO_ALL_OK,71 {test.COMMON_TMPDIR_NAME: shared_tmp})72 content = os.listdir(shared_tmp)73 self.assertEqual(len(content), 2, "The number of tests in manually "74 "set teststmpdir is not 2 (%s):\n%s"75 % (len(content), content))76 def tearDown(self):77 self.instrumented_test.remove()78 self.simple_test.remove()79 shutil.rmtree(self.tmpdir)80if __name__ == '__main__':...
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!!