Best Python code snippet using autotest_python
test_linux_log_parser.py
Source:test_linux_log_parser.py  
1import os2from django.test import TestCase3from squad.plugins.linux_log_parser import Plugin4from squad.core.models import Group5def read_sample_file(name):6    if not name.startswith('/'):7        name = os.path.join(os.path.dirname(__file__), 'linux_log_parser', name)8    return open(name).read()9class TestLinuxLogParser(TestCase):10    def setUp(self):11        group = Group.objects.create(slug='mygroup')12        self.project = group.projects.create(slug='myproject', enabled_plugins_list='example')13        self.build = self.project.builds.create(version='1')14        self.env = self.project.environments.create(slug='myenv')15        self.plugin = Plugin()16    def new_testrun(self, logfile, job_id='999'):17        log = read_sample_file(logfile)18        testrun = self.build.test_runs.create(environment=self.env, job_id=job_id)19        testrun.save_log_file(log)20        return testrun21    def test_detects_oops(self):22        testrun = self.new_testrun('oops.log')23        self.plugin.postprocess_testrun(testrun)24        test = testrun.tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-oops')25        self.assertFalse(test.result)26        self.assertIsNotNone(test.log)27        self.assertNotIn('Linux version 4.4.89-01529-gb29bace', test.log)28        self.assertIn('Internal error: Oops - BUG: 0 [#1] PREEMPT SMP', test.log)29        self.assertNotIn('Kernel panic', test.log)30    def test_detects_kernel_panic(self):31        testrun = self.new_testrun('kernelpanic.log')32        self.plugin.postprocess_testrun(testrun)33        test = testrun.tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-panic')34        self.assertFalse(test.result)35        self.assertIsNotNone(test.log)36        self.assertNotIn('Booting Linux', test.log)37        self.assertIn('Kernel panic - not syncing', test.log)38        self.assertIn('Attempted to kill init! exitcode=0x00000009', test.log)39        self.assertNotIn('Internal error: Oops', test.log)40    def test_detects_kernel_exception(self):41        testrun = self.new_testrun('kernelexceptiontrace.log')42        self.plugin.postprocess_testrun(testrun)43        test = testrun.tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-exception')44        self.assertFalse(test.result)45        self.assertIsNotNone(test.log)46        self.assertNotIn('Booting Linux', test.log)47        self.assertIn('WARNING: CPU: 0 PID: 1 at kernel/smp.c:912 smp_call_function_many_cond+0x3c4/0x3c8', test.log)48        self.assertIn('5fe0: 0000000b be963e80 b6f142d9 b6f0e648 60000030 ffffffff"}', test.log)49        self.assertNotIn('Internal error: Oops', test.log)50    def test_detects_kernel_kasan(self):51        testrun = self.new_testrun('kasan.log')52        self.plugin.postprocess_testrun(testrun)53        test = testrun.tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-kasan')54        self.assertFalse(test.result)55        self.assertIsNotNone(test.log)56        self.assertNotIn('Booting Linux', test.log)57        self.assertIn('==================================================================', test.log)58        self.assertIn('BUG: KASAN: slab-out-of-bounds in kmalloc_oob_right+0x190/0x3b8', test.log)59        self.assertIn('Write of size 1 at addr c6aaf473 by task kunit_try_catch/191', test.log)60        self.assertNotIn('Internal error: Oops', test.log)61    def test_detects_kernel_kfence(self):62        testrun = self.new_testrun('kfence.log')63        self.plugin.postprocess_testrun(testrun)64        test = testrun.tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-kfence')65        self.assertFalse(test.result)66        self.assertIsNotNone(test.log)67        self.assertNotIn('Booting Linux', test.log)68        self.assertIn('==================================================================', test.log)69        self.assertIn('BUG: KFENCE: memory corruption in kfree+0x8c/0x174', test.log)70        self.assertIn('Corrupted memory at 0x00000000c5d55ff8 [ ! ! ! . . . . . . . . . . . . . ] (in kfence-#214):', test.log)71        self.assertNotIn('Internal error: Oops', test.log)72    def test_detects_kernel_bug(self):73        testrun = self.new_testrun('oops.log')74        self.plugin.postprocess_testrun(testrun)75        test = testrun.tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-bug')76        self.assertFalse(test.result)77        self.assertIsNotNone(test.log)78        self.assertNotIn('Booting Linux', test.log)79        self.assertIn('] BUG:', test.log)80        self.assertNotIn('Internal error: Oops', test.log)81        testrun = self.new_testrun('kernel_bug_and_invalid_opcode.log', job_id='1000')82        self.plugin.postprocess_testrun(testrun)83        test = testrun.tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-bug')84        self.assertFalse(test.result)85        self.assertIsNotNone(test.log)86        self.assertNotIn('Booting Linux', test.log)87        self.assertIn('] kernel BUG at', test.log)88        self.assertNotIn('] BUG:', test.log)89        self.assertNotIn('Internal error: Oops', test.log)90    def test_detects_kernel_invalid_opcode(self):91        testrun = self.new_testrun('kernel_bug_and_invalid_opcode.log')92        self.plugin.postprocess_testrun(testrun)93        test = testrun.tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-invalid-opcode')94        self.assertFalse(test.result)95        self.assertIsNotNone(test.log)96        self.assertNotIn('Booting Linux', test.log)97        self.assertIn('] invalid opcode:', test.log)98        self.assertNotIn('] BUG:', test.log)99        self.assertNotIn('Internal error: Oops', test.log)100    def test_detects_multiple(self):101        testrun = self.new_testrun('multiple_issues_dmesg.log')102        self.plugin.postprocess_testrun(testrun)103        tests = testrun.tests104        test_panic = tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-panic')105        test_exception = tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-exception')106        test_warning = tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-warning')107        test_oops = tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-oops')108        test_fault = tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-fault')109        self.assertFalse(test_panic.result)110        self.assertNotIn('Boot CPU', test_panic.log)111        self.assertIn('Kernel panic - not syncing', test_panic.log)112        self.assertFalse(test_exception.result)113        self.assertNotIn('Boot CPU', test_exception.log)114        self.assertIn('------------[ cut here ]------------', test_exception.log)115        self.assertFalse(test_warning.result)116        self.assertNotIn('Boot CPU', test_warning.log)117        self.assertNotIn('Kernel panic - not syncing', test_warning.log)118        self.assertNotIn('------------[ cut here ]------------', test_warning.log)119        self.assertNotIn('Unhandled fault:', test_warning.log)120        self.assertNotIn('Oops', test_warning.log)121        self.assertIn('WARNING: CPU', test_warning.log)122        self.assertFalse(test_oops.result)123        self.assertNotIn('Boot CPU', test_oops.log)124        self.assertNotIn('Kernel panic - not syncing', test_oops.log)125        self.assertNotIn('------------[ cut here ]------------', test_oops.log)126        self.assertNotIn('WARNING: CPU', test_oops.log)127        self.assertNotIn('Unhandled fault:', test_oops.log)128        self.assertIn('Oops', test_oops.log)129        self.assertFalse(test_fault.result)130        self.assertNotIn('Boot CPU', test_fault.log)131        self.assertNotIn('Kernel panic - not syncing', test_fault.log)132        self.assertNotIn('------------[ cut here ]------------', test_fault.log)133        self.assertNotIn('WARNING: CPU', test_fault.log)134        self.assertNotIn('Oops', test_fault.log)135        self.assertIn('Unhandled fault:', test_fault.log)136    def test_pass_if_nothing_is_found(self):137        testrun = self.new_testrun('/dev/null')138        self.plugin.postprocess_testrun(testrun)139        tests = testrun.tests140        test_panic = tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-panic')141        test_exception = tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-exception')142        test_warning = tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-warning')143        test_oops = tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-oops')144        test_fault = tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-fault')145        self.assertTrue(test_panic.result)146        self.assertTrue(test_exception.result)147        self.assertTrue(test_warning.result)148        self.assertTrue(test_oops.result)149        self.assertTrue(test_fault.result)150    def test_two_testruns_distinct_test_names(self):151        testrun1 = self.new_testrun('/dev/null', 'job1')152        testrun2 = self.new_testrun('/dev/null', 'job2')153        self.plugin.postprocess_testrun(testrun1)154        self.plugin.postprocess_testrun(testrun2)155        self.assertNotEqual(testrun1.tests.all(), testrun2.tests.all())156    def test_rcu_warning(self):157        testrun = self.new_testrun('rcu_warning.log')158        self.plugin.postprocess_testrun(testrun)159        tests = testrun.tests160        test_panic = tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-panic')161        test_exception = tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-exception')162        test_warning = tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-warning')163        test_oops = tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-oops')164        test_fault = tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-fault')165        self.assertTrue(test_panic.result)166        self.assertTrue(test_exception.result)167        self.assertTrue(test_oops.result)168        self.assertTrue(test_fault.result)169        self.assertFalse(test_warning.result)170        self.assertIn('WARNING: suspicious RCU usage', test_warning.log)171    def test_no_string(self):172        testrun = self.build.test_runs.create(environment=self.env, job_id='1111')173        self.plugin.postprocess_testrun(testrun)174        tests = testrun.tests.filter(result=False)175        self.assertEqual(0, tests.count())176    def test_metadata_creation(self):177        log = 'Kernel panic - not syncing'178        testrun = self.build.test_runs.create(environment=self.env, job_id='999')179        testrun.save_log_file(log)180        self.plugin.postprocess_testrun(testrun)181        test = testrun.tests.get(suite__slug='log-parser-test', metadata__name='check-kernel-panic')182        self.assertIsNotNone(test.metadata)183    def test_boot_log(self):184        testrun = self.new_testrun('oops.log')185        self.plugin.postprocess_testrun(testrun)186        test = testrun.tests.get(suite__slug='log-parser-boot', metadata__name='check-kernel-oops')187        self.assertFalse(test.result)188        self.assertIsNotNone(test.log)189        self.assertNotIn('Linux version 4.4.89-01529-gb29bace', test.log)190        self.assertIn('Internal error: Oops - BUG: 0 [#1] PREEMPT SMP', test.log)191        self.assertNotIn('Kernel panic', test.log)192    def test_sha_name(self):193        testrun = self.new_testrun('oops.log')194        self.plugin.postprocess_testrun(testrun)195        test = testrun.tests.get(suite__slug='log-parser-boot', metadata__name='check-kernel-oops')196        self.assertFalse(test.result)197        self.assertIsNotNone(test.log)198        self.assertNotIn('Linux version 4.4.89-01529-gb29bace', test.log)199        self.assertIn('Internal error: Oops - BUG: 0 [#1] PREEMPT SMP', test.log)200        self.assertNotIn('Kernel panic', test.log)201        # Now check if a test with sha digest in the name202        test = testrun.tests.get(suite__slug='log-parser-boot', metadata__name='check-kernel-oops-ed958a6ced291792084f3de4166e5d52676eb6b21f67a1f1585bf7028df50581')203        self.assertFalse(test.result)204        self.assertIsNotNone(test.log)...task1.py
Source:task1.py  
2def oops():3    a_list = [0, 1, 2, 3]4    return a_list[4]5    6def test_oops():7    try:8        oops()9    except IndexError:10        print("Unknown index")11        12test_oops()13# ÐбÑобка помилка KeyError14def oops1():15    a_dict = {0: "zero", 1: "one", 2: "two", 3: "three"}16    return a_dict[4]17    18def test_oops1():19    try:20        oops1()21    except KeyError:22        print("Unknown key")...test_oops.py
Source:test_oops.py  
1def test_oops(client):2    url = f'/fake-path'3    response = client.get(url)...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!!
