Richard Barnette | 0ed6075 | 2017-02-02 13:23:30 -0800 | [diff] [blame^] | 1 | import datetime |
| 2 | |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 3 | import common |
| 4 | from autotest_lib.frontend import setup_test_environment |
| 5 | from autotest_lib.frontend import thread_local |
jamesren | dd85524 | 2010-03-02 22:23:44 +0000 | [diff] [blame] | 6 | from autotest_lib.frontend.afe import models, model_attributes |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 7 | from autotest_lib.client.common_lib import global_config |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 8 | from autotest_lib.client.common_lib.test_utils import mock |
| 9 | |
| 10 | class FrontendTestMixin(object): |
Richard Barnette | 0ed6075 | 2017-02-02 13:23:30 -0800 | [diff] [blame^] | 11 | # pylint: disable=missing-docstring |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 12 | def _fill_in_test_data(self): |
| 13 | """Populate the test database with some hosts and labels.""" |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 14 | if models.DroneSet.drone_sets_enabled(): |
| 15 | models.DroneSet.objects.create( |
| 16 | name=models.DroneSet.default_drone_set_name()) |
| 17 | |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 18 | acl_group = models.AclGroup.objects.create(name='my_acl') |
showard | fd8b89f | 2010-01-20 19:06:30 +0000 | [diff] [blame] | 19 | acl_group.users.add(models.User.current_user()) |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 20 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 21 | self.hosts = [models.Host.objects.create(hostname=hostname) |
| 22 | for hostname in |
| 23 | ('host1', 'host2', 'host3', 'host4', 'host5', 'host6', |
| 24 | 'host7', 'host8', 'host9')] |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 25 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 26 | acl_group.hosts = self.hosts |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 27 | models.AclGroup.smart_get('Everyone').hosts = [] |
| 28 | |
showard | 2ca64c9 | 2009-12-10 21:41:02 +0000 | [diff] [blame] | 29 | self.labels = [models.Label.objects.create(name=name) for name in |
| 30 | ('label1', 'label2', 'label3', 'label4', 'label5', |
Alex Miller | 4a19369 | 2013-08-21 13:59:01 -0700 | [diff] [blame] | 31 | 'label6', 'label7', 'label8', 'unused')] |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 32 | |
| 33 | platform = models.Label.objects.create(name='myplatform', platform=True) |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 34 | for host in self.hosts: |
mbligh | 1ef218d | 2009-08-03 16:57:56 +0000 | [diff] [blame] | 35 | host.labels.add(platform) |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 36 | |
| 37 | atomic_group1 = models.AtomicGroup.objects.create( |
| 38 | name='atomic1', max_number_of_machines=2) |
| 39 | atomic_group2 = models.AtomicGroup.objects.create( |
| 40 | name='atomic2', max_number_of_machines=2) |
| 41 | |
showard | 2ca64c9 | 2009-12-10 21:41:02 +0000 | [diff] [blame] | 42 | self.label3 = self.labels[2] |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 43 | self.label3.only_if_needed = True |
| 44 | self.label3.save() |
showard | 2ca64c9 | 2009-12-10 21:41:02 +0000 | [diff] [blame] | 45 | self.label4 = self.labels[3] |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 46 | self.label4.atomic_group = atomic_group1 |
| 47 | self.label4.save() |
showard | 2ca64c9 | 2009-12-10 21:41:02 +0000 | [diff] [blame] | 48 | self.label5 = self.labels[4] |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 49 | self.label5.atomic_group = atomic_group1 |
| 50 | self.label5.save() |
showard | 2ca64c9 | 2009-12-10 21:41:02 +0000 | [diff] [blame] | 51 | self.hosts[0].labels.add(self.labels[0]) # label1 |
| 52 | self.hosts[1].labels.add(self.labels[1]) # label2 |
| 53 | self.label6 = self.labels[5] |
| 54 | self.label7 = self.labels[6] |
| 55 | self.label8 = self.labels[7] |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 56 | self.label8.atomic_group = atomic_group2 |
| 57 | self.label8.save() |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 58 | for hostnum in xrange(4,7): # host5..host7 |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 59 | self.hosts[hostnum].labels.add(self.label4) # an atomic group lavel |
| 60 | self.hosts[hostnum].labels.add(self.label6) # a normal label |
| 61 | self.hosts[6].labels.add(self.label7) |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 62 | for hostnum in xrange(7,9): # host8..host9 |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 63 | self.hosts[hostnum].labels.add(self.label5) # an atomic group lavel |
| 64 | self.hosts[hostnum].labels.add(self.label6) # a normal label |
| 65 | self.hosts[hostnum].labels.add(self.label7) |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 66 | |
| 67 | |
Prashanth B | 4ec9867 | 2014-05-15 10:44:54 -0700 | [diff] [blame] | 68 | def _frontend_common_setup(self, fill_data=True, setup_tables=True): |
jamesren | 06e59f1 | 2010-05-24 17:05:06 +0000 | [diff] [blame] | 69 | self.god = mock.mock_god(ut=self) |
Prashanth B | 4ec9867 | 2014-05-15 10:44:54 -0700 | [diff] [blame] | 70 | if setup_tables: |
| 71 | setup_test_environment.set_up() |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 72 | global_config.global_config.override_config_value( |
Eric Li | 8a12e80 | 2011-02-17 14:24:13 -0800 | [diff] [blame] | 73 | 'SERVER', 'rpc_logging', 'False') |
Prashanth B | 4ec9867 | 2014-05-15 10:44:54 -0700 | [diff] [blame] | 74 | if fill_data and setup_tables: |
showard | 26b7ec7 | 2009-12-21 22:43:57 +0000 | [diff] [blame] | 75 | self._fill_in_test_data() |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | def _frontend_common_teardown(self): |
| 79 | setup_test_environment.tear_down() |
showard | 98ead17 | 2009-06-22 18:13:24 +0000 | [diff] [blame] | 80 | thread_local.set_user(None) |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 81 | self.god.unstub_all() |
| 82 | |
| 83 | |
| 84 | def _create_job(self, hosts=[], metahosts=[], priority=0, active=False, |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 85 | synchronous=False, atomic_group=None, hostless=False, |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 86 | drone_set=None, control_file='control', |
Richard Barnette | 0ed6075 | 2017-02-02 13:23:30 -0800 | [diff] [blame^] | 87 | owner='autotest_system', parent_job_id=None): |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 88 | """ |
| 89 | Create a job row in the test database. |
| 90 | |
| 91 | @param hosts - A list of explicit host ids for this job to be |
| 92 | scheduled on. |
| 93 | @param metahosts - A list of label ids for each host that this job |
| 94 | should be scheduled on (meta host scheduling). |
| 95 | @param priority - The job priority (integer). |
| 96 | @param active - bool, mark this job as running or not in the database? |
| 97 | @param synchronous - bool, if True use synch_count=2 otherwise use |
| 98 | synch_count=1. |
| 99 | @param atomic_group - An atomic group id for this job to schedule on |
| 100 | or None if atomic scheduling is not required. Each metahost |
| 101 | becomes a request to schedule an entire atomic group. |
| 102 | This does not support creating an active atomic group job. |
showard | 01339bf | 2009-12-21 22:20:28 +0000 | [diff] [blame] | 103 | @param hostless - if True, this job is intended to be hostless (in that |
| 104 | case, hosts, metahosts, and atomic_group must all be empty) |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame] | 105 | @param owner - The owner of the job. Aclgroups from which a job can |
| 106 | acquire hosts change with the aclgroups of the owners. |
Prashanth B | 2c1a22a | 2014-04-02 17:30:51 -0700 | [diff] [blame] | 107 | @param parent_job_id - The id of a parent_job. If a job with the id |
| 108 | doesn't already exist one will be created. |
| 109 | |
| 110 | @raises model.DoesNotExist: If parent_job_id is specified but a job with |
| 111 | id=parent_job_id does not exist. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 112 | |
| 113 | @returns A Django frontend.afe.models.Job instance. |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 114 | """ |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 115 | if not drone_set: |
| 116 | drone_set = (models.DroneSet.default_drone_set_name() |
| 117 | and models.DroneSet.get_default()) |
| 118 | |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 119 | assert not (atomic_group and active) # TODO(gps): support this |
| 120 | synch_count = synchronous and 2 or 1 |
| 121 | created_on = datetime.datetime(2008, 1, 1) |
| 122 | status = models.HostQueueEntry.Status.QUEUED |
| 123 | if active: |
| 124 | status = models.HostQueueEntry.Status.RUNNING |
Prashanth B | 2c1a22a | 2014-04-02 17:30:51 -0700 | [diff] [blame] | 125 | |
| 126 | parent_job = (models.Job.objects.get(id=parent_job_id) |
| 127 | if parent_job_id else None) |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 128 | job = models.Job.objects.create( |
Prashanth B | 489b91d | 2014-03-15 12:17:16 -0700 | [diff] [blame] | 129 | name='test', owner=owner, priority=priority, |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 130 | synch_count=synch_count, created_on=created_on, |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 131 | reboot_before=model_attributes.RebootBefore.NEVER, |
jamesren | 4a41e01 | 2010-07-16 22:33:48 +0000 | [diff] [blame] | 132 | drone_set=drone_set, control_file=control_file, |
Richard Barnette | 0ed6075 | 2017-02-02 13:23:30 -0800 | [diff] [blame^] | 133 | parent_job=parent_job, require_ssp=None) |
beeps | 7d8273b | 2013-11-06 09:44:34 -0800 | [diff] [blame] | 134 | |
| 135 | # Update the job's dependencies to include the metahost. |
| 136 | for metahost_label in metahosts: |
| 137 | dep = models.Label.objects.get(id=metahost_label) |
| 138 | job.dependency_labels.add(dep) |
| 139 | |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 140 | for host_id in hosts: |
| 141 | models.HostQueueEntry.objects.create(job=job, host_id=host_id, |
| 142 | status=status, |
| 143 | atomic_group_id=atomic_group) |
| 144 | models.IneligibleHostQueue.objects.create(job=job, host_id=host_id) |
| 145 | for label_id in metahosts: |
| 146 | models.HostQueueEntry.objects.create(job=job, meta_host_id=label_id, |
| 147 | status=status, |
| 148 | atomic_group_id=atomic_group) |
| 149 | if atomic_group and not (metahosts or hosts): |
| 150 | # Create a single HQE to request the atomic group of hosts even if |
| 151 | # no metahosts or hosts are supplied. |
| 152 | models.HostQueueEntry.objects.create(job=job, |
| 153 | status=status, |
| 154 | atomic_group_id=atomic_group) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 155 | |
| 156 | if hostless: |
| 157 | assert not (hosts or metahosts or atomic_group) |
| 158 | models.HostQueueEntry.objects.create(job=job, status=status) |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 159 | return job |
| 160 | |
| 161 | |
| 162 | def _create_job_simple(self, hosts, use_metahost=False, |
Prashanth B | 2c1a22a | 2014-04-02 17:30:51 -0700 | [diff] [blame] | 163 | priority=0, active=False, drone_set=None, |
| 164 | parent_job_id=None): |
showard | b6d1662 | 2009-05-26 19:35:29 +0000 | [diff] [blame] | 165 | """An alternative interface to _create_job""" |
| 166 | args = {'hosts' : [], 'metahosts' : []} |
| 167 | if use_metahost: |
| 168 | args['metahosts'] = hosts |
| 169 | else: |
| 170 | args['hosts'] = hosts |
Prashanth B | 2c1a22a | 2014-04-02 17:30:51 -0700 | [diff] [blame] | 171 | return self._create_job( |
| 172 | priority=priority, active=active, drone_set=drone_set, |
| 173 | parent_job_id=parent_job_id, **args) |