blob: 5aba4f4119cbcfbbc7e4615a3f414797326d9237 [file] [log] [blame]
showardb6d16622009-05-26 19:35:29 +00001import atexit, datetime, os, tempfile, unittest
2import common
3from autotest_lib.frontend import setup_test_environment
4from autotest_lib.frontend import thread_local
jamesrendd855242010-03-02 22:23:44 +00005from autotest_lib.frontend.afe import models, model_attributes
showardb6d16622009-05-26 19:35:29 +00006from autotest_lib.client.common_lib.test_utils import mock
7
8class FrontendTestMixin(object):
showardb6d16622009-05-26 19:35:29 +00009 def _fill_in_test_data(self):
10 """Populate the test database with some hosts and labels."""
jamesren76fcf192010-04-21 20:39:50 +000011 if models.DroneSet.drone_sets_enabled():
12 models.DroneSet.objects.create(
13 name=models.DroneSet.default_drone_set_name())
14
showardb6d16622009-05-26 19:35:29 +000015 acl_group = models.AclGroup.objects.create(name='my_acl')
showardfd8b89f2010-01-20 19:06:30 +000016 acl_group.users.add(models.User.current_user())
showardb6d16622009-05-26 19:35:29 +000017
showarded2afea2009-07-07 20:54:07 +000018 self.hosts = [models.Host.objects.create(hostname=hostname)
19 for hostname in
20 ('host1', 'host2', 'host3', 'host4', 'host5', 'host6',
21 'host7', 'host8', 'host9')]
showardb6d16622009-05-26 19:35:29 +000022
showarded2afea2009-07-07 20:54:07 +000023 acl_group.hosts = self.hosts
showardb6d16622009-05-26 19:35:29 +000024 models.AclGroup.smart_get('Everyone').hosts = []
25
showard2ca64c92009-12-10 21:41:02 +000026 self.labels = [models.Label.objects.create(name=name) for name in
27 ('label1', 'label2', 'label3', 'label4', 'label5',
28 'label6', 'label7', 'label8')]
showardb6d16622009-05-26 19:35:29 +000029
30 platform = models.Label.objects.create(name='myplatform', platform=True)
showarded2afea2009-07-07 20:54:07 +000031 for host in self.hosts:
mbligh1ef218d2009-08-03 16:57:56 +000032 host.labels.add(platform)
showardb6d16622009-05-26 19:35:29 +000033
34 atomic_group1 = models.AtomicGroup.objects.create(
35 name='atomic1', max_number_of_machines=2)
36 atomic_group2 = models.AtomicGroup.objects.create(
37 name='atomic2', max_number_of_machines=2)
38
showard2ca64c92009-12-10 21:41:02 +000039 self.label3 = self.labels[2]
showardb6d16622009-05-26 19:35:29 +000040 self.label3.only_if_needed = True
41 self.label3.save()
showard2ca64c92009-12-10 21:41:02 +000042 self.label4 = self.labels[3]
showardb6d16622009-05-26 19:35:29 +000043 self.label4.atomic_group = atomic_group1
44 self.label4.save()
showard2ca64c92009-12-10 21:41:02 +000045 self.label5 = self.labels[4]
showardb6d16622009-05-26 19:35:29 +000046 self.label5.atomic_group = atomic_group1
47 self.label5.save()
showard2ca64c92009-12-10 21:41:02 +000048 self.hosts[0].labels.add(self.labels[0]) # label1
49 self.hosts[1].labels.add(self.labels[1]) # label2
50 self.label6 = self.labels[5]
51 self.label7 = self.labels[6]
52 self.label8 = self.labels[7]
showard6157c632009-07-06 20:19:31 +000053 self.label8.atomic_group = atomic_group2
54 self.label8.save()
showardb6d16622009-05-26 19:35:29 +000055 for hostnum in xrange(4,7): # host5..host7
showarded2afea2009-07-07 20:54:07 +000056 self.hosts[hostnum].labels.add(self.label4) # an atomic group lavel
57 self.hosts[hostnum].labels.add(self.label6) # a normal label
58 self.hosts[6].labels.add(self.label7)
showardb6d16622009-05-26 19:35:29 +000059 for hostnum in xrange(7,9): # host8..host9
showarded2afea2009-07-07 20:54:07 +000060 self.hosts[hostnum].labels.add(self.label5) # an atomic group lavel
61 self.hosts[hostnum].labels.add(self.label6) # a normal label
62 self.hosts[hostnum].labels.add(self.label7)
showardb6d16622009-05-26 19:35:29 +000063
64
showard26b7ec72009-12-21 22:43:57 +000065 def _frontend_common_setup(self, fill_data=True):
showardb6d16622009-05-26 19:35:29 +000066 self.god = mock.mock_god()
showard78f5b012009-12-23 00:05:59 +000067 setup_test_environment.set_up()
showard26b7ec72009-12-21 22:43:57 +000068 if fill_data:
69 self._fill_in_test_data()
showardb6d16622009-05-26 19:35:29 +000070
71
72 def _frontend_common_teardown(self):
73 setup_test_environment.tear_down()
showard98ead172009-06-22 18:13:24 +000074 thread_local.set_user(None)
showardb6d16622009-05-26 19:35:29 +000075 self.god.unstub_all()
76
77
78 def _create_job(self, hosts=[], metahosts=[], priority=0, active=False,
jamesren76fcf192010-04-21 20:39:50 +000079 synchronous=False, atomic_group=None, hostless=False,
80 drone_set=None):
showardb6d16622009-05-26 19:35:29 +000081 """
82 Create a job row in the test database.
83
84 @param hosts - A list of explicit host ids for this job to be
85 scheduled on.
86 @param metahosts - A list of label ids for each host that this job
87 should be scheduled on (meta host scheduling).
88 @param priority - The job priority (integer).
89 @param active - bool, mark this job as running or not in the database?
90 @param synchronous - bool, if True use synch_count=2 otherwise use
91 synch_count=1.
92 @param atomic_group - An atomic group id for this job to schedule on
93 or None if atomic scheduling is not required. Each metahost
94 becomes a request to schedule an entire atomic group.
95 This does not support creating an active atomic group job.
showard01339bf2009-12-21 22:20:28 +000096 @param hostless - if True, this job is intended to be hostless (in that
97 case, hosts, metahosts, and atomic_group must all be empty)
showard77182562009-06-10 00:16:05 +000098
99 @returns A Django frontend.afe.models.Job instance.
showardb6d16622009-05-26 19:35:29 +0000100 """
jamesren76fcf192010-04-21 20:39:50 +0000101 if not drone_set:
102 drone_set = (models.DroneSet.default_drone_set_name()
103 and models.DroneSet.get_default())
104
showardb6d16622009-05-26 19:35:29 +0000105 assert not (atomic_group and active) # TODO(gps): support this
106 synch_count = synchronous and 2 or 1
107 created_on = datetime.datetime(2008, 1, 1)
108 status = models.HostQueueEntry.Status.QUEUED
109 if active:
110 status = models.HostQueueEntry.Status.RUNNING
111 job = models.Job.objects.create(
showardfd8b89f2010-01-20 19:06:30 +0000112 name='test', owner='autotest_system', priority=priority,
showardb6d16622009-05-26 19:35:29 +0000113 synch_count=synch_count, created_on=created_on,
jamesren76fcf192010-04-21 20:39:50 +0000114 reboot_before=model_attributes.RebootBefore.NEVER,
115 drone_set=drone_set)
showardb6d16622009-05-26 19:35:29 +0000116 for host_id in hosts:
117 models.HostQueueEntry.objects.create(job=job, host_id=host_id,
118 status=status,
119 atomic_group_id=atomic_group)
120 models.IneligibleHostQueue.objects.create(job=job, host_id=host_id)
121 for label_id in metahosts:
122 models.HostQueueEntry.objects.create(job=job, meta_host_id=label_id,
123 status=status,
124 atomic_group_id=atomic_group)
125 if atomic_group and not (metahosts or hosts):
126 # Create a single HQE to request the atomic group of hosts even if
127 # no metahosts or hosts are supplied.
128 models.HostQueueEntry.objects.create(job=job,
129 status=status,
130 atomic_group_id=atomic_group)
showarda9545c02009-12-18 22:44:26 +0000131
132 if hostless:
133 assert not (hosts or metahosts or atomic_group)
134 models.HostQueueEntry.objects.create(job=job, status=status)
showardb6d16622009-05-26 19:35:29 +0000135 return job
136
137
138 def _create_job_simple(self, hosts, use_metahost=False,
jamesren76fcf192010-04-21 20:39:50 +0000139 priority=0, active=False, drone_set=None):
showardb6d16622009-05-26 19:35:29 +0000140 """An alternative interface to _create_job"""
141 args = {'hosts' : [], 'metahosts' : []}
142 if use_metahost:
143 args['metahosts'] = hosts
144 else:
145 args['hosts'] = hosts
jamesren76fcf192010-04-21 20:39:50 +0000146 return self._create_job(priority=priority, active=active,
147 drone_set=drone_set, **args)