blob: c2ec7288941de6ff76e4abb72afeec52b53f4cda [file] [log] [blame]
jamesren883492a2010-02-12 00:45:18 +00001#!/usr/bin/python
2
3import common
4import unittest
5from autotest_lib.client.common_lib.test_utils import mock
jamesrenb55378a2010-03-02 22:19:49 +00006from autotest_lib.frontend import setup_django_environment
7from autotest_lib.frontend import setup_test_environment
8from autotest_lib.scheduler import metahost_scheduler, scheduler_models
jamesren883492a2010-02-12 00:45:18 +00009
10class LabelMetahostSchedulerTest(unittest.TestCase):
11 def setUp(self):
12 self.god = mock.mock_god()
13 self.scheduling_utility = self.god.create_mock_class(
14 metahost_scheduler.HostSchedulingUtility, 'utility')
15 self.metahost_scheduler = metahost_scheduler.LabelMetahostScheduler()
16
17
jamesrenb55378a2010-03-02 22:19:49 +000018 def tearDown(self):
19 self.god.unstub_all()
20
21
jamesren883492a2010-02-12 00:45:18 +000022 def entry(self):
jamesrenb55378a2010-03-02 22:19:49 +000023 return self.god.create_mock_class(scheduler_models.HostQueueEntry,
24 'entry')
jamesren883492a2010-02-12 00:45:18 +000025
26
27 def test_can_schedule_metahost(self):
28 entry = self.entry()
29 entry.meta_host = None
30 self.assertFalse(self.metahost_scheduler.can_schedule_metahost(entry))
31
32 entry.meta_host = 1
33 self.assert_(self.metahost_scheduler.can_schedule_metahost(entry))
34
35
36 def test_schedule_metahost(self):
37 entry = self.entry()
38 entry.meta_host = 1
39 host = object()
40
41 self.scheduling_utility.hosts_in_label.expect_call(1).and_return(
42 [2, 3, 4, 5])
43 # 2 is in ineligible_hosts
44 (self.scheduling_utility.ineligible_hosts_for_entry.expect_call(entry)
45 .and_return([2]))
46 self.scheduling_utility.is_host_usable.expect_call(2).and_return(True)
47 # 3 is unusable
48 self.scheduling_utility.is_host_usable.expect_call(3).and_return(False)
49 self.scheduling_utility.remove_host_from_label.expect_call(3, 1)
50 # 4 is ineligible for the job
51 self.scheduling_utility.is_host_usable.expect_call(4).and_return(True)
52 (self.scheduling_utility.is_host_eligible_for_job.expect_call(4, entry)
53 .and_return(False))
54 # 5 runs
55 self.scheduling_utility.is_host_usable.expect_call(5).and_return(True)
56 (self.scheduling_utility.is_host_eligible_for_job.expect_call(5, entry)
57 .and_return(True))
58 self.scheduling_utility.remove_host_from_label.expect_call(5, 1)
59 self.scheduling_utility.pop_host.expect_call(5).and_return(host)
60 entry.set_host.expect_call(host)
61
62 self.metahost_scheduler.schedule_metahost(entry,
63 self.scheduling_utility)
64 self.god.check_playback()
65
66
67 def test_no_hosts(self):
68 entry = self.entry()
69 entry.meta_host = 1
70
71 self.scheduling_utility.hosts_in_label.expect_call(1).and_return(())
72 (self.scheduling_utility.ineligible_hosts_for_entry.expect_call(entry)
73 .and_return(()))
74
75 self.metahost_scheduler.schedule_metahost(entry,
76 self.scheduling_utility)
77 self.god.check_playback()
78
79
80if __name__ == '__main__':
81 unittest.main()