Best Python code snippet using autotest_python
models_test.py
Source:models_test.py
1#!/usr/bin/python2import unittest3import common4from autotest_lib.frontend import setup_django_environment5from autotest_lib.frontend.afe import frontend_test_utils6from autotest_lib.frontend.afe import models, model_attributes, model_logic7from autotest_lib.client.common_lib import global_config8class AclGroupTest(unittest.TestCase,9 frontend_test_utils.FrontendTestMixin):10 def setUp(self):11 self._frontend_common_setup()12 def tearDown(self):13 self._frontend_common_teardown()14 def _check_acls(self, host, acl_name_list):15 actual_acl_names = [acl_group.name for acl_group16 in host.aclgroup_set.all()]17 self.assertEquals(set(actual_acl_names), set(acl_name_list))18 def test_on_host_membership_change(self):19 host1, host2 = self.hosts[1:3]20 everyone_acl = models.AclGroup.objects.get(name='Everyone')21 host1.aclgroup_set.clear()22 self._check_acls(host1, [])23 host2.aclgroup_set.add(everyone_acl)24 self._check_acls(host2, ['Everyone', 'my_acl'])25 models.AclGroup.on_host_membership_change()26 self._check_acls(host1, ['Everyone'])27 self._check_acls(host2, ['my_acl'])28class HostTest(unittest.TestCase,29 frontend_test_utils.FrontendTestMixin):30 def setUp(self):31 self._frontend_common_setup()32 def tearDown(self):33 self._frontend_common_teardown()34 def test_add_host_previous_one_time_host(self):35 # ensure that when adding a host which was previously used as a one-time36 # host, the status isn't reset, since this can interfere with the37 # scheduler.38 host = models.Host.create_one_time_host('othost')39 self.assertEquals(host.invalid, True)40 self.assertEquals(host.status, models.Host.Status.READY)41 host.status = models.Host.Status.RUNNING42 host.save()43 host2 = models.Host.add_object(hostname='othost')44 self.assertEquals(host2.id, host.id)45 self.assertEquals(host2.status, models.Host.Status.RUNNING)46class SpecialTaskUnittest(unittest.TestCase,47 frontend_test_utils.FrontendTestMixin):48 def setUp(self):49 self._frontend_common_setup()50 def tearDown(self):51 self._frontend_common_teardown()52 def _create_task(self):53 return models.SpecialTask.objects.create(54 host=self.hosts[0], task=models.SpecialTask.Task.VERIFY,55 requested_by=models.User.current_user())56 def test_execution_path(self):57 task = self._create_task()58 self.assertEquals(task.execution_path(), 'hosts/host1/1-verify')59 def test_status(self):60 task = self._create_task()61 self.assertEquals(task.status, 'Queued')62 task.update_object(is_active=True)63 self.assertEquals(task.status, 'Running')64 task.update_object(is_active=False, is_complete=True, success=True)65 self.assertEquals(task.status, 'Completed')66 task.update_object(success=False)67 self.assertEquals(task.status, 'Failed')68 def test_activate(self):69 task = self._create_task()70 task.activate()71 self.assertTrue(task.is_active)72 self.assertFalse(task.is_complete)73 def test_finish(self):74 task = self._create_task()75 task.activate()76 task.finish(True)77 self.assertFalse(task.is_active)78 self.assertTrue(task.is_complete)79 self.assertTrue(task.success)80 def test_requested_by_from_queue_entry(self):81 job = self._create_job(hosts=[0])82 task = models.SpecialTask.objects.create(83 host=self.hosts[0], task=models.SpecialTask.Task.VERIFY,84 queue_entry=job.hostqueueentry_set.all()[0])85 self.assertEquals(task.requested_by.login, 'autotest_system')86class HostQueueEntryUnittest(unittest.TestCase,87 frontend_test_utils.FrontendTestMixin):88 def setUp(self):89 self._frontend_common_setup()90 def tearDown(self):91 self._frontend_common_teardown()92 def test_execution_path(self):93 entry = self._create_job(hosts=[1]).hostqueueentry_set.all()[0]94 entry.execution_subdir = 'subdir'95 entry.save()96 self.assertEquals(entry.execution_path(), '1-autotest_system/subdir')97class ModelWithInvalidTest(unittest.TestCase,98 frontend_test_utils.FrontendTestMixin):99 def setUp(self):100 self._frontend_common_setup()101 def tearDown(self):102 self._frontend_common_teardown()103 def test_model_with_invalid_delete(self):104 self.assertFalse(self.hosts[0].invalid)105 self.hosts[0].delete()106 self.assertTrue(self.hosts[0].invalid)107 self.assertTrue(models.Host.objects.get(id=self.hosts[0].id))108 def test_model_with_invalid_delete_queryset(self):109 for host in self.hosts:110 self.assertFalse(host.invalid)111 hosts = models.Host.objects.all()112 hosts.delete()113 self.assertEqual(hosts.count(), len(self.hosts))114 for host in hosts:115 self.assertTrue(host.invalid)116 def test_cloned_queryset_delete(self):117 """118 Make sure that a cloned queryset maintains the custom delete()119 """120 to_delete = ('host1', 'host2')121 for host in self.hosts:122 self.assertFalse(host.invalid)123 hosts = models.Host.objects.all().filter(hostname__in=to_delete)124 hosts.delete()125 all_hosts = models.Host.objects.all()126 self.assertEqual(all_hosts.count(), len(self.hosts))127 for host in all_hosts:128 if host.hostname in to_delete:129 self.assertTrue(130 host.invalid,131 '%s.invalid expected to be True' % host.hostname)132 else:133 self.assertFalse(134 host.invalid,135 '%s.invalid expected to be False' % host.hostname)136 def test_normal_delete(self):137 job = self._create_job(hosts=[1])138 self.assertEqual(1, models.Job.objects.all().count())139 job.delete()140 self.assertEqual(0, models.Job.objects.all().count())141 def test_normal_delete_queryset(self):142 self._create_job(hosts=[1])143 self._create_job(hosts=[2])144 self.assertEqual(2, models.Job.objects.all().count())145 models.Job.objects.all().delete()146 self.assertEqual(0, models.Job.objects.all().count())147class KernelTest(unittest.TestCase, frontend_test_utils.FrontendTestMixin):148 def setUp(self):149 self._frontend_common_setup()150 def tearDown(self):151 self._frontend_common_teardown()152 def test_create_kernels_none(self):153 self.assertEqual(None, models.Kernel.create_kernels(None))154 def test_create_kernels(self):155 self.god.stub_function(models.Kernel, '_create')156 num_kernels = 3157 kernel_list = [object() for _ in range(num_kernels)]158 result = [object() for _ in range(num_kernels)]159 for kernel, response in zip(kernel_list, result):160 models.Kernel._create.expect_call(kernel).and_return(response)161 self.assertEqual(result, models.Kernel.create_kernels(kernel_list))162 self.god.check_playback()163 def test_create(self):164 kernel = models.Kernel._create({'version': 'version'})165 self.assertEqual(kernel.version, 'version')166 self.assertEqual(kernel.cmdline, '')167 self.assertEqual(kernel, models.Kernel._create({'version': 'version'}))168class ParameterizedJobTest(unittest.TestCase,169 frontend_test_utils.FrontendTestMixin):170 def setUp(self):171 self._frontend_common_setup()172 def tearDown(self):173 self._frontend_common_teardown()174 def test_job(self):175 global_config.global_config.override_config_value(176 'AUTOTEST_WEB', 'parameterized_jobs', 'True')177 test = models.Test.objects.create(178 name='name', author='author', test_class='class',179 test_category='category',180 test_type=model_attributes.TestTypes.SERVER, path='path')181 parameterized_job = models.ParameterizedJob.objects.create(test=test)182 job = self._create_job(hosts=[1], control_file=None,183 parameterized_job=parameterized_job)184 self.assertEqual(job, parameterized_job.job())185class JobTest(unittest.TestCase, frontend_test_utils.FrontendTestMixin):186 def setUp(self):187 self._frontend_common_setup()188 def tearDown(self):189 self._frontend_common_teardown()190 def test_check_parameterized_jobs_no_args(self):191 self.assertRaises(Exception, models.Job.check_parameterized_job,192 control_file=None, parameterized_job=None)193 def test_check_parameterized_jobs_both_args(self):194 self.assertRaises(Exception, models.Job.check_parameterized_job,195 control_file=object(), parameterized_job=object())196 def test_check_parameterized_jobs_disabled(self):197 self.assertRaises(Exception, models.Job.check_parameterized_job,198 control_file=None, parameterized_job=object())199 def test_check_parameterized_jobs_enabled(self):200 global_config.global_config.override_config_value(201 'AUTOTEST_WEB', 'parameterized_jobs', 'True')202 self.assertRaises(Exception, models.Job.check_parameterized_job,203 control_file=object(), parameterized_job=None)204if __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!!