How to use cat_file_to_cmd method in autotest

Best Python code snippet using autotest_python

kernel_unittest.py

Source:kernel_unittest.py Github

copy

Full Screen

1#!/usr/bin/python2import unittest, os, time, re, glob, logging3import common4from autotest_lib.client.common_lib.test_utils import mock5from autotest_lib.client.bin import kernel, job, utils, kernelexpand6from autotest_lib.client.bin import kernel_config, boottool, os_dep7class TestAddKernelToBootLoader(unittest.TestCase):8 def add_to_bootloader(self, base_args, args, bootloader_args,9 bootloader_root, tag='image', image='image',10 initrd='initrd'):11 god = mock.mock_god()12 bootloader = god.create_mock_class(boottool.boottool, "boottool")13 # record14 bootloader.remove_kernel.expect_call(tag)15 bootloader.add_kernel.expect_call(image, tag, initrd=initrd,16 args='_dummy_', root=bootloader_root)17 for a in bootloader_args.split():18 bootloader.add_args.expect_call(kernel=tag, args=a)19 bootloader.remove_args.expect_call(kernel=tag, args='_dummy_')20 # run and check21 kernel._add_kernel_to_bootloader(bootloader, base_args, tag, args,22 image, initrd)23 god.check_playback()24 def test_add_kernel_to_bootloader(self):25 self.add_to_bootloader(base_args='baseargs', args='',26 bootloader_args='baseargs', bootloader_root=None)27 self.add_to_bootloader(base_args='arg1 root=/dev/oldroot arg2',28 args='root=/dev/newroot arg3',29 bootloader_args='arg1 arg2 arg3',30 bootloader_root='/dev/newroot')31class TestBootableKernel(unittest.TestCase):32 def setUp(self):33 self.god = mock.mock_god()34 self.god.stub_function(time, "time")35 self.god.stub_function(utils, "system")36 self.god.stub_function(kernel, "_add_kernel_to_bootloader")37 job_ = self.god.create_mock_class(job.job, "job")38 self.kernel = kernel.BootableKernel(job_)39 self.kernel.job.bootloader = self.god.create_mock_class(40 boottool.boottool, "boottool")41 def tearDown(self):42 # note: time.time() can only be unstubbed via tearDown()43 self.god.unstub_all()44 def boot_kernel(self, ident_check):45 notes = "applied_patches"46 when = 147 args = ''48 base_args = 'base_args'49 tag = 'ident'50 subdir = 'subdir'51 self.kernel.image = 'image'52 self.kernel.initrd = 'initrd'53 self.kernel.installed_as = tag54 # record55 args_ = args56 if ident_check:57 time.time.expect_call().and_return(when)58 args_ += " IDENT=%d" % when59 status = ["job.end_reboot_and_verify", when, tag, subdir, notes]60 else:61 status = ["job.end_reboot", subdir, tag, notes]62 self.kernel.job.next_step_prepend.expect_call(status)63 self.kernel.job.config_get.expect_call(64 'boot.default_args').and_return(base_args)65 kernel._add_kernel_to_bootloader.expect_call(66 self.kernel.job.bootloader, base_args, tag,67 args_, self.kernel.image, self.kernel.initrd)68 utils.system.expect_call('touch /fastboot')69 self.kernel.job.start_reboot.expect_call()70 self.kernel.job.reboot.expect_call(tag=tag)71 # run and check72 self.kernel._boot_kernel(args=args, ident_check=ident_check,73 expected_ident=tag, subdir=subdir, notes=notes)74 self.god.check_playback()75 def test_boot_kernel(self):76 self.boot_kernel(ident_check=False)77 self.boot_kernel(ident_check=True)78class TestKernel(unittest.TestCase):79 def setUp(self):80 self.god = mock.mock_god()81 logging.disable(logging.CRITICAL)82 self.god.stub_function(time, "time")83 self.god.stub_function(os, "mkdir")84 self.god.stub_function(os, "chdir")85 self.god.stub_function(os, "symlink")86 self.god.stub_function(os, "remove")87 self.god.stub_function(os.path, "isdir")88 self.god.stub_function(os.path, "exists")89 self.god.stub_function(os.path, "isfile")90 self.god.stub_function(os_dep, "commands")91 self.god.stub_function(kernel, "open")92 self.god.stub_function(utils, "system")93 self.god.stub_function(utils, "system_output")94 self.god.stub_function(utils, "get_file")95 self.god.stub_function(utils, "get_current_kernel_arch")96 self.god.stub_function(utils, "cat_file_to_cmd")97 self.god.stub_function(utils, "force_copy")98 self.god.stub_function(utils, "extract_tarball_to_dir")99 self.god.stub_function(utils, "count_cpus")100 self.god.stub_function(utils, "get_os_vendor")101 self.god.stub_function(kernelexpand, "expand_classic")102 self.god.stub_function(kernel_config, "modules_needed")103 self.god.stub_function(glob, "glob")104 def dummy_mark(filename, msg):105 pass106 self.god.stub_with(kernel, '_mark', dummy_mark)107 self.job = self.god.create_mock_class(job.job, "job")108 self.job.bootloader = self.god.create_mock_class(boottool.boottool,109 "boottool")110 class DummyLoggingManager(object):111 def tee_redirect_debug_dir(self, *args, **kwargs):112 pass113 def restore(self, *args, **kwargs):114 pass115 self.job.logging = DummyLoggingManager()116 self.job.autodir = "autodir"117 self.base_tree = "2.6.24"118 self.tmp_dir = "tmpdir"119 self.subdir = "subdir"120 def tearDown(self):121 self.god.unstub_all()122 def construct_kernel(self):123 self.kernel = kernel.kernel.__new__(kernel.kernel)124 self.god.stub_function(self.kernel, "extract")125 # setup126 self.src_dir = os.path.join(self.tmp_dir, 'src')127 self.build_dir = os.path.join(self.tmp_dir, "build_dir")128 self.config_dir = os.path.join(self.subdir, 'config')129 self.log_dir = os.path.join(self.subdir, 'debug')130 self.results_dir = os.path.join(self.subdir, 'results')131 # record132 os.path.isdir.expect_call(self.src_dir).and_return(True)133 utils.system.expect_call('rm -rf ' + self.src_dir)134 os.path.isdir.expect_call(self.build_dir).and_return(True)135 utils.system.expect_call('rm -rf ' + self.build_dir)136 os.path.exists.expect_call(self.src_dir).and_return(False)137 os.mkdir.expect_call(self.src_dir)138 for path in [self.config_dir, self.log_dir, self.results_dir]:139 os.path.exists.expect_call(path).and_return(True)140 utils.system.expect_call('rm -rf ' + path)141 os.mkdir.expect_call(path)142 logpath = os.path.join(self.log_dir, 'build_log')143 self.logfile = self.god.create_mock_class(file, "file")144 kernel.open.expect_call(logpath, 'w+').and_return(self.logfile)145 utils.get_current_kernel_arch.expect_call().and_return('ia64')146 self.logfile.write.expect_call('BASE: %s\n' % self.base_tree)147 self.kernel.extract.expect_call(self.base_tree)148 # finish creation of kernel object and test (and unstub extract)149 self.kernel.__init__(self.job, self.base_tree, self.subdir,150 self.tmp_dir, "build_dir")151 self.god.check_playback()152 self.god.unstub(self.kernel, "extract")153 def test_constructor(self):154 self.construct_kernel()155 def test_kernelexpand1(self):156 self.construct_kernel()157 ret_val = self.kernel.kernelexpand("/path/to/kernel")158 self.assertEquals(ret_val, ["/path/to/kernel"])159 self.god.check_playback()160 def test_kernel_expand2(self):161 self.construct_kernel()162 kernel = "kernel.tar.gz"163 # record164 self.job.config_get.expect_call('mirror.mirrors').and_return('mirror')165 kernelexpand.expand_classic.expect_call(kernel,166 'mirror').and_return('patches')167 # run168 self.assertEquals(self.kernel.kernelexpand(kernel), 'patches')169 self.god.check_playback()170 def test_kernel_expand3(self):171 self.construct_kernel()172 kernel = "kernel.tar.gz"173 # record174 self.job.config_get.expect_call('mirror.mirrors')175 self.job.config_get.expect_call(176 'mirror.ftp_kernel_org').and_return('mirror')177 korg = 'http://www.kernel.org/pub/linux/kernel'178 mirrors = [179 [ korg + '/v2.6', 'mirror' + '/v2.6' ],180 [ korg + '/people/akpm/patches/2.6', 'mirror' + '/akpm' ],181 [ korg + '/people/mbligh', 'mirror' + '/mbligh' ],182 ]183 kernelexpand.expand_classic.expect_call(kernel,184 mirrors).and_return('patches')185 # run186 self.assertEquals(self.kernel.kernelexpand(kernel), 'patches')187 self.god.check_playback()188 def test_extract1(self):189 self.construct_kernel()190 # setup191 self.god.stub_function(self.kernel, "get_kernel_tree")192 # record193 os.path.exists.expect_call(self.base_tree).and_return(True)194 self.kernel.get_kernel_tree.expect_call(self.base_tree)195 self.job.record.expect_call('GOOD', self.subdir, 'kernel.extract')196 # run197 self.kernel.extract(self.base_tree)198 self.god.check_playback()199 self.god.unstub(self.kernel, "get_kernel_tree")200 def test_extract2(self):201 self.construct_kernel()202 # setup203 self.god.stub_function(self.kernel, "kernelexpand")204 self.god.stub_function(self.kernel, "get_kernel_tree")205 self.god.stub_function(self.kernel, "patch")206 # record207 os.path.exists.expect_call(self.base_tree).and_return(False)208 components = ["component0", "component1"]209 self.kernel.kernelexpand.expect_call(self.base_tree).and_return(210 components)211 self.kernel.get_kernel_tree.expect_call(components[0])212 self.kernel.patch.expect_call(components[1])213 self.job.record.expect_call('GOOD', self.subdir, 'kernel.extract')214 # run215 self.kernel.extract(self.base_tree)216 self.god.check_playback()217 self.god.unstub(self.kernel, "kernelexpand")218 self.god.unstub(self.kernel, "get_kernel_tree")219 self.god.unstub(self.kernel, "patch")220 def test_patch1(self):221 self.construct_kernel()222 patches = ('patch1', 'patch2')223 self.god.stub_function(self.kernel, "apply_patches")224 self.god.stub_function(self.kernel, "get_patches")225 #record226 self.kernel.get_patches.expect_call(patches).and_return(patches)227 self.kernel.apply_patches.expect_call(patches)228 self.job.record.expect_call('GOOD', self.subdir, 'kernel.patch')229 #run230 self.kernel.patch(*patches)231 self.god.check_playback()232 self.god.unstub(self.kernel, "apply_patches")233 self.god.unstub(self.kernel, "get_patches")234 def test_patch2(self):235 self.construct_kernel()236 patches = []237 # record238 self.job.record.expect_call('GOOD', self.subdir, 'kernel.patch')239 # run240 self.kernel.patch(*patches)241 self.god.check_playback()242 def test_config(self):243 self.construct_kernel()244 # setup245 self.god.stub_function(self.kernel, "set_cross_cc")246 self.god.stub_class(kernel_config, "kernel_config")247 # record248 self.kernel.set_cross_cc.expect_call()249 kernel_config.kernel_config.expect_new(self.job, self.build_dir,250 self.config_dir, '', None,251 False, self.base_tree, None)252 self.job.record.expect_call('GOOD', self.subdir, 'kernel.config')253 # run254 self.kernel.config()255 self.god.check_playback()256 self.god.unstub(self.kernel, "set_cross_cc")257 def test_get_patches(self):258 self.construct_kernel()259 # setup260 patches = ['patch1', 'patch2', 'patch3']261 local_patches = []262 # record263 for patch in patches:264 dest = os.path.join(self.src_dir, os.path.basename(patch))265 utils.get_file.expect_call(patch, dest)266 utils.system_output.expect_call(267 'md5sum ' + dest).and_return('md5sum')268 local_patches.append((patch, dest, 'md5sum'))269 # run and check270 self.assertEquals(self.kernel.get_patches(patches), local_patches)271 self.god.check_playback()272 def test_apply_patches(self):273 self.construct_kernel()274 # setup275 patches = []276 patches.append(('patch1', 'patch1.gz', 'md5sum1'))277 patches.append(('patch2', 'patch2.bz2', 'md5sum2'))278 patches.append(('patch3', 'patch3', 'md5sum3'))279 applied_patches = []280 # record281 os.chdir.expect_call(self.build_dir)282 patch_id = "%s %s %s" % ('patch1', 'patch1', 'md5sum1')283 log = "PATCH: " + patch_id + "\n"284 utils.cat_file_to_cmd.expect_call('patch1.gz',285 'patch -p1 > /dev/null')286 self.logfile.write.expect_call(log)287 applied_patches.append(patch_id)288 patch_id = "%s %s %s" % ('patch2', 'patch2', 'md5sum2')289 log = "PATCH: " + patch_id + "\n"290 utils.cat_file_to_cmd.expect_call('patch2.bz2',291 'patch -p1 > /dev/null')292 self.logfile.write.expect_call(log)293 applied_patches.append(patch_id)294 utils.force_copy.expect_call('patch3',295 self.results_dir).and_return('local_patch3')296 self.job.relative_path.expect_call('local_patch3').and_return(297 'rel_local_patch3')298 patch_id = "%s %s %s" % ('patch3', 'rel_local_patch3', 'md5sum3')299 log = "PATCH: " + patch_id + "\n"300 utils.cat_file_to_cmd.expect_call('patch3',301 'patch -p1 > /dev/null')302 self.logfile.write.expect_call(log)303 applied_patches.append(patch_id)304 # run and test305 self.kernel.apply_patches(patches)306 self.assertEquals(self.kernel.applied_patches, applied_patches)307 self.god.check_playback()308 def test_get_kernel_tree1(self):309 self.construct_kernel()310 # record311 os.path.isdir.expect_call(self.base_tree).and_return(True)312 os.symlink.expect_call(self.base_tree, self.build_dir)313 # run and check314 self.kernel.get_kernel_tree(self.base_tree)315 self.god.check_playback()316 def test_get_kernel_tree2(self):317 self.construct_kernel()318 # record319 os.path.isdir.expect_call(self.base_tree).and_return(False)320 os.chdir.expect_call(os.path.dirname(self.src_dir))321 tarball = os.path.join(self.src_dir, os.path.basename(self.base_tree))322 utils.get_file.expect_call(self.base_tree, tarball)323 utils.extract_tarball_to_dir.expect_call(tarball,324 self.build_dir)325 # run and check326 self.kernel.get_kernel_tree(self.base_tree)327 self.god.check_playback()328 def test_extraversion(self):329 self.construct_kernel()330 tag = "tag"331 # setup332 self.god.stub_function(self.kernel, "config")333 # record334 os.chdir.expect_call(self.build_dir)335 extraversion_sub = r's/^CONFIG_LOCALVERSION=\s*"\(.*\)"/CONFIG_LOCALVERSION='336 cfg = self.build_dir + '/.config'337 p = extraversion_sub + '"\\1-%s"/' % tag338 utils.system.expect_call('mv %s %s.old' % (cfg, cfg))339 utils.system.expect_call("sed '%s' < %s.old > %s" % (p, cfg, cfg))340 self.kernel.config.expect_call(make='oldconfig')341 # run and check342 self.kernel.extraversion(tag)343 self.god.check_playback()344 def test_build(self):345 self.construct_kernel()346 self.god.stub_function(self.kernel, "extraversion")347 self.god.stub_function(self.kernel, "set_cross_cc")348 self.god.stub_function(self.kernel, "get_kernel_build_ver")349 self.kernel.build_target = 'build_target'350 # record351 os_dep.commands.expect_call('gcc', 'make')352 logfile = os.path.join(self.log_dir, 'kernel_build')353 os.chdir.expect_call(self.build_dir)354 self.kernel.extraversion.expect_call('autotest')355 self.kernel.set_cross_cc.expect_call()356 utils.system.expect_call('make dep', ignore_status=True)357 utils.count_cpus.expect_call().and_return(4)358 threads = 2 * 4359 build_string = 'make -j %d %s %s' % (threads, '', 'build_target')360 utils.system.expect_call(build_string)361 kernel_config.modules_needed.expect_call('.config').and_return(True)362 utils.system.expect_call('make -j %d modules' % (threads))363 self.kernel.get_kernel_build_ver.expect_call().and_return('2.6.24')364 kernel_version = re.sub('-autotest', '', '2.6.24')365 self.logfile.write.expect_call('BUILD VERSION: %s\n' % kernel_version)366 utils.force_copy.expect_call(self.build_dir+'/System.map',367 self.results_dir)368 self.job.record.expect_call('GOOD', self.subdir, 'kernel.build')369 # run and check370 self.kernel.build()371 self.god.check_playback()372 def test_build_timed(self):373 self.construct_kernel()374 self.god.stub_function(self.kernel, "set_cross_cc")375 self.god.stub_function(self.kernel, "clean")376 # record377 os.chdir.expect_call(self.build_dir)378 self.kernel.set_cross_cc.expect_call()379 self.kernel.clean.expect_call()380 build_string = "/usr/bin/time -o /dev/null make -j 8 vmlinux"381 build_string += ' > /dev/null 2>&1'382 utils.system.expect_call(build_string)383 os.path.isfile.expect_call('vmlinux').and_return(True)384 # run and check385 self.kernel.build_timed(threads=8)386 self.god.check_playback()387 def test_clean(self):388 self.construct_kernel()389 # record390 os.chdir.expect_call(self.build_dir)391 utils.system.expect_call('make clean > /dev/null 2> /dev/null')392 self.job.record.expect_call('GOOD', self.subdir, 'kernel.clean')393 # run and check394 self.kernel.clean()395 self.god.check_playback()396 def test_mkinitrd(self):397 self.construct_kernel()398 # record399 utils.get_os_vendor.expect_call().and_return('Ubuntu')400 os.path.isfile.expect_call('initrd').and_return(True)401 os.remove.expect_call('initrd')402 self.job.config_get.expect_call(403 'kernel.mkinitrd_extra_args').and_return(None)404 args = ''405 glob.glob.expect_call('/lib/modules/2.6.24*').and_return(['2.6.24'])406 os.path.isfile.expect_call('/usr/sbin/mkinitrd').and_return(True)407 cmd = '/usr/sbin/mkinitrd'408 utils.system.expect_call('%s %s -o initrd 2.6.24' % (cmd, args))409 self.job.record.expect_call('GOOD', self.subdir, 'kernel.mkinitrd')410 # run and check411 self.kernel.mkinitrd(version="2.6.24", image="image",412 system_map="system_map", initrd="initrd")413 self.god.check_playback()414 def test_install(self):415 self.construct_kernel()416 tag = 'autotest'417 prefix = '/'418 self.kernel.build_image = None419 self.kernel.build_target = 'build_target'420 self.god.stub_function(self.kernel, "get_kernel_build_ver")421 self.god.stub_function(self.kernel, "mkinitrd")422 # record423 os.chdir.expect_call(self.build_dir)424 os.path.isdir.expect_call(prefix).and_return(False)425 os.mkdir.expect_call(prefix)426 boot_dir = os.path.join(prefix, 'boot')427 os.path.isdir.expect_call(boot_dir).and_return(False)428 os.mkdir.expect_call(boot_dir)429 glob.glob.expect_call(430 'arch/*/boot/' + 'build_target').and_return('')431 build_image = self.kernel.build_target432 utils.force_copy.expect_call('vmlinux',433 '/boot/vmlinux-autotest')434 utils.force_copy.expect_call('build_target',435 '/boot/vmlinuz-autotest')436 utils.force_copy.expect_call('System.map',437 '/boot/System.map-autotest')438 utils.force_copy.expect_call('.config',439 '/boot/config-autotest')440 kernel_config.modules_needed.expect_call('.config').and_return(True)441 utils.system.expect_call('make modules_install INSTALL_MOD_PATH=%s'442 % prefix)443 initrd = boot_dir + '/initrd-' + tag444 self.kernel.get_kernel_build_ver.expect_call().and_return('2.6.24')445 self.kernel.mkinitrd.expect_call('2.6.24', '/boot/vmlinuz-autotest',446 '/boot/System.map-autotest', '/boot/initrd-autotest')447 self.job.record.expect_call('GOOD', self.subdir, 'kernel.install')448 # run and check449 self.kernel.install()450 self.god.check_playback()451 def test_get_kernel_build_arch1(self):452 self.construct_kernel()453 # record454 utils.get_current_kernel_arch.expect_call().and_return("i386")455 # run and check456 self.assertEquals(self.kernel.get_kernel_build_arch(), "i386")457 self.god.check_playback()458 def test_get_kernel_build_arch2(self):459 self.construct_kernel()460 # run and check461 self.assertEquals(self.kernel.get_kernel_build_arch('i586'), "i386")462 self.god.check_playback()463 def test_get_kernel_build_release(self):464 self.construct_kernel()465 mock_file = self.god.create_mock_class(file, "file")466 # record467 for f in [self.build_dir + "/include/linux/version.h",468 self.build_dir + "/include/linux/utsrelease.h"]:469 os.path.exists.expect_call(f).and_return(True)470 kernel.open.expect_call(f, 'r').and_return(mock_file)471 mock_file.readlines.expect_call().and_return("Some lines")472 mock_file.close.expect_call()473 for f in [self.build_dir + "/include/linux/compile.h",474 self.build_dir + "/include/generated/utsrelease.h",475 self.build_dir + "/include/generated/compile.h"]:476 os.path.exists.expect_call(f).and_return(False)477 # run and test478 self.kernel.get_kernel_build_release()479 self.god.check_playback()480 def test_get_kernel_build_ident(self):481 self.construct_kernel()482 self.god.stub_function(self.kernel, "get_kernel_build_release")483 # record484 self.kernel.get_kernel_build_release.expect_call().and_return(485 ("AwesomeRelease", "1.0"))486 # run and check487 self.assertEquals(self.kernel.get_kernel_build_ident(),488 "AwesomeRelease::1.0")489 self.god.check_playback()490 def test_boot(self):491 self.construct_kernel()492 self.god.stub_function(self.kernel, "get_kernel_build_ident")493 self.god.stub_function(self.kernel, "install")494 self.god.stub_function(self.kernel, "_boot_kernel")495 self.kernel.applied_patches = "applied_patches"496 self.kernel.installed_as = None497 args = ''498 expected_ident = 'ident'499 ident = True500 # record501 self.kernel.install.expect_call()502 self.kernel.get_kernel_build_ident.expect_call(503 ).and_return(expected_ident)504 self.kernel._boot_kernel.expect_call(505 args, ident, expected_ident,506 self.subdir, self.kernel.applied_patches)507 # run and check508 self.kernel.boot(args=args, ident=ident)509 self.god.check_playback()510if __name__ == "__main__":...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful