showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 1 | import atexit, datetime, os, tempfile, unittest |
| 2 | import common |
| 3 | from autotest_lib.frontend import setup_test_environment |
| 4 | from autotest_lib.frontend import thread_local |
| 5 | from autotest_lib.frontend.afe import models |
| 6 | from autotest_lib.client.common_lib.test_utils import mock |
| 7 | |
| 8 | class FrontendTestMixin(object): |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 9 | def _fill_in_test_data(self): |
| 10 | """Populate the test database with some hosts and labels.""" |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 11 | acl_group = models.AclGroup.objects.create(name='my_acl') |
showard | 98ead17 | 2009-06-22 18:13:24 +0000 | [diff] [blame] | 12 | acl_group.users.add(self.user) |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 13 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 14 | self.hosts = [models.Host.objects.create(hostname=hostname) |
| 15 | for hostname in |
| 16 | ('host1', 'host2', 'host3', 'host4', 'host5', 'host6', |
| 17 | 'host7', 'host8', 'host9')] |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 18 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 19 | acl_group.hosts = self.hosts |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 20 | models.AclGroup.smart_get('Everyone').hosts = [] |
| 21 | |
showard | 2ca64c9 | 2009-12-10 21:41:02 +0000 | [diff] [blame] | 22 | self.labels = [models.Label.objects.create(name=name) for name in |
| 23 | ('label1', 'label2', 'label3', 'label4', 'label5', |
| 24 | 'label6', 'label7', 'label8')] |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 25 | |
| 26 | platform = models.Label.objects.create(name='myplatform', platform=True) |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 27 | for host in self.hosts: |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 28 | host.labels.add(platform) |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 29 | |
| 30 | atomic_group1 = models.AtomicGroup.objects.create( |
| 31 | name='atomic1', max_number_of_machines=2) |
| 32 | atomic_group2 = models.AtomicGroup.objects.create( |
| 33 | name='atomic2', max_number_of_machines=2) |
| 34 | |
showard | 2ca64c9 | 2009-12-10 21:41:02 +0000 | [diff] [blame] | 35 | self.label3 = self.labels[2] |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 36 | self.label3.only_if_needed = True |
| 37 | self.label3.save() |
showard | 2ca64c9 | 2009-12-10 21:41:02 +0000 | [diff] [blame] | 38 | self.label4 = self.labels[3] |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 39 | self.label4.atomic_group = atomic_group1 |
| 40 | self.label4.save() |
showard | 2ca64c9 | 2009-12-10 21:41:02 +0000 | [diff] [blame] | 41 | self.label5 = self.labels[4] |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 42 | self.label5.atomic_group = atomic_group1 |
| 43 | self.label5.save() |
showard | 2ca64c9 | 2009-12-10 21:41:02 +0000 | [diff] [blame] | 44 | self.hosts[0].labels.add(self.labels[0]) # label1 |
| 45 | self.hosts[1].labels.add(self.labels[1]) # label2 |
| 46 | self.label6 = self.labels[5] |
| 47 | self.label7 = self.labels[6] |
| 48 | self.label8 = self.labels[7] |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 49 | self.label8.atomic_group = atomic_group2 |
| 50 | self.label8.save() |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 51 | for hostnum in xrange(4,7): # host5..host7 |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 52 | self.hosts[hostnum].labels.add(self.label4) # an atomic group lavel |
| 53 | self.hosts[hostnum].labels.add(self.label6) # a normal label |
| 54 | self.hosts[6].labels.add(self.label7) |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 55 | for hostnum in xrange(7,9): # host8..host9 |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 56 | self.hosts[hostnum].labels.add(self.label5) # an atomic group lavel |
| 57 | self.hosts[hostnum].labels.add(self.label6) # a normal label |
| 58 | self.hosts[hostnum].labels.add(self.label7) |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 59 | |
| 60 | |
| 61 | def _setup_dummy_user(self): |
showard | 98ead17 | 2009-06-22 18:13:24 +0000 | [diff] [blame] | 62 | self.user = models.User.objects.create(login='my_user', access_level=0) |
| 63 | thread_local.set_user(self.user) |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 64 | |
| 65 | |
showard | 26b7ec7 | 2009-12-21 22:43:57 +0000 | [diff] [blame] | 66 | def _frontend_common_setup(self, fill_data=True): |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 67 | self.god = mock.mock_god() |
showard | 78f5b01 | 2009-12-23 00:05:59 +0000 | [diff] [blame^] | 68 | setup_test_environment.set_up() |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 69 | self._setup_dummy_user() |
showard | 26b7ec7 | 2009-12-21 22:43:57 +0000 | [diff] [blame] | 70 | if fill_data: |
| 71 | self._fill_in_test_data() |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 72 | |
| 73 | |
| 74 | def _frontend_common_teardown(self): |
| 75 | setup_test_environment.tear_down() |
showard | 98ead17 | 2009-06-22 18:13:24 +0000 | [diff] [blame] | 76 | thread_local.set_user(None) |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 77 | self.god.unstub_all() |
| 78 | |
| 79 | |
| 80 | def _create_job(self, hosts=[], metahosts=[], priority=0, active=False, |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 81 | synchronous=False, atomic_group=None, hostless=False): |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 82 | """ |
| 83 | Create a job row in the test database. |
| 84 | |
| 85 | @param hosts - A list of explicit host ids for this job to be |
| 86 | scheduled on. |
| 87 | @param metahosts - A list of label ids for each host that this job |
| 88 | should be scheduled on (meta host scheduling). |
| 89 | @param priority - The job priority (integer). |
| 90 | @param active - bool, mark this job as running or not in the database? |
| 91 | @param synchronous - bool, if True use synch_count=2 otherwise use |
| 92 | synch_count=1. |
| 93 | @param atomic_group - An atomic group id for this job to schedule on |
| 94 | or None if atomic scheduling is not required. Each metahost |
| 95 | becomes a request to schedule an entire atomic group. |
| 96 | This does not support creating an active atomic group job. |
showard | 01339bf | 2009-12-21 22:20:28 +0000 | [diff] [blame] | 97 | @param hostless - if True, this job is intended to be hostless (in that |
| 98 | case, hosts, metahosts, and atomic_group must all be empty) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 99 | |
| 100 | @returns A Django frontend.afe.models.Job instance. |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 101 | """ |
| 102 | assert not (atomic_group and active) # TODO(gps): support this |
| 103 | synch_count = synchronous and 2 or 1 |
| 104 | created_on = datetime.datetime(2008, 1, 1) |
| 105 | status = models.HostQueueEntry.Status.QUEUED |
| 106 | if active: |
| 107 | status = models.HostQueueEntry.Status.RUNNING |
| 108 | job = models.Job.objects.create( |
| 109 | name='test', owner='my_user', priority=priority, |
| 110 | synch_count=synch_count, created_on=created_on, |
| 111 | reboot_before=models.RebootBefore.NEVER) |
| 112 | for host_id in hosts: |
| 113 | models.HostQueueEntry.objects.create(job=job, host_id=host_id, |
| 114 | status=status, |
| 115 | atomic_group_id=atomic_group) |
| 116 | models.IneligibleHostQueue.objects.create(job=job, host_id=host_id) |
| 117 | for label_id in metahosts: |
| 118 | models.HostQueueEntry.objects.create(job=job, meta_host_id=label_id, |
| 119 | status=status, |
| 120 | atomic_group_id=atomic_group) |
| 121 | if atomic_group and not (metahosts or hosts): |
| 122 | # Create a single HQE to request the atomic group of hosts even if |
| 123 | # no metahosts or hosts are supplied. |
| 124 | models.HostQueueEntry.objects.create(job=job, |
| 125 | status=status, |
| 126 | atomic_group_id=atomic_group) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 127 | |
| 128 | if hostless: |
| 129 | assert not (hosts or metahosts or atomic_group) |
| 130 | models.HostQueueEntry.objects.create(job=job, status=status) |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 131 | return job |
| 132 | |
| 133 | |
| 134 | def _create_job_simple(self, hosts, use_metahost=False, |
| 135 | priority=0, active=False): |
| 136 | """An alternative interface to _create_job""" |
| 137 | args = {'hosts' : [], 'metahosts' : []} |
| 138 | if use_metahost: |
| 139 | args['metahosts'] = hosts |
| 140 | else: |
| 141 | args['hosts'] = hosts |
| 142 | return self._create_job(priority=priority, active=active, **args) |