summaryrefslogtreecommitdiff
path: root/scheduler/monitor_db_unittest.py
blob: 240eb5aa4911b78ed3e4b9b15c4c04c275d1c296 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
#!/usr/bin/python

import unittest, time, subprocess, os, StringIO, tempfile, datetime, shutil
import common
import MySQLdb
from autotest_lib.frontend import setup_django_environment
from autotest_lib.frontend import setup_test_environment
from autotest_lib.client.common_lib import global_config, host_protections
from autotest_lib.client.common_lib.test_utils import mock
from autotest_lib.database import database_connection, migrate
from autotest_lib.frontend import thread_local
from autotest_lib.frontend.afe import models
from autotest_lib.scheduler import monitor_db, drone_manager, email_manager
from autotest_lib.scheduler import scheduler_config

_DEBUG = False


class DummyAgent(object):
    _is_running = False
    _is_done = False
    num_processes = 1
    host_ids = []
    queue_entry_ids = []

    def is_running(self):
        return self._is_running


    def tick(self):
        self._is_running = True


    def is_done(self):
        return self._is_done


    def set_done(self, done):
        self._is_done = done
        self._is_running = not done


class IsRow(mock.argument_comparator):
    def __init__(self, row_id):
        self.row_id = row_id


    def is_satisfied_by(self, parameter):
        return list(parameter)[0] == self.row_id


    def __str__(self):
        return 'row with id %s' % self.row_id


class IsAgentWithTask(mock.argument_comparator):
        def __init__(self, task):
            self._task = task


        def is_satisfied_by(self, parameter):
            if not isinstance(parameter, monitor_db.Agent):
                return False
            tasks = list(parameter.queue.queue)
            if len(tasks) != 1:
                return False
            return tasks[0] == self._task


def _set_host_and_qe_ids(agent_or_task, id_list=None):
    if id_list is None:
        id_list = []
    agent_or_task.host_ids = agent_or_task.queue_entry_ids = id_list


class BaseSchedulerTest(unittest.TestCase):
    _config_section = 'AUTOTEST_WEB'
    _test_db_initialized = False

    def _do_query(self, sql):
        self._database.execute(sql)


    @classmethod
    def _initialize_test_db(cls):
        if cls._test_db_initialized:
            return
        temp_fd, cls._test_db_file = tempfile.mkstemp(suffix='.monitor_test')
        os.close(temp_fd)
        setup_test_environment.set_test_database(cls._test_db_file)
        setup_test_environment.run_syncdb()
        cls._test_db_backup = setup_test_environment.backup_test_database()
        cls._test_db_initialized = True


    def _open_test_db(self):
        self._initialize_test_db()
        setup_test_environment.restore_test_database(self._test_db_backup)
        self._database = (
            database_connection.DatabaseConnection.get_test_database(
                self._test_db_file))
        self._database.connect()
        self._database.debug = _DEBUG


    def _close_test_db(self):
        self._database.disconnect()


    def _set_monitor_stubs(self):
        # Clear the instance cache as this is a brand new database.
        monitor_db.DBObject._clear_instance_cache()
        monitor_db._db = self._database
        monitor_db._drone_manager._results_dir = '/test/path'
        monitor_db._drone_manager._temporary_directory = '/test/path/tmp'


    def _fill_in_test_data(self):
        """Populate the test database with some hosts and labels."""
        user = models.User.objects.create(login='my_user')
        acl_group = models.AclGroup.objects.create(name='my_acl')
        acl_group.users.add(user)

        hosts = [models.Host.objects.create(hostname=hostname) for hostname in
                 ('host1', 'host2', 'host3', 'host4', 'host5', 'host6',
                  'host7', 'host8', 'host9')]

        acl_group.hosts = hosts
        models.AclGroup.smart_get('Everyone').hosts = []

        labels = [models.Label.objects.create(name=name) for name in
                  ('label1', 'label2', 'label3', 'label4', 'label5', 'label6',
                   'label7')]

        atomic_group1 = models.AtomicGroup.objects.create(
                name='atomic1', max_number_of_machines=2)
        atomic_group2 = models.AtomicGroup.objects.create(
                name='atomic2', max_number_of_machines=2)

        self.label3 = labels[2]
        self.label3.only_if_needed = True
        self.label3.save()
        self.label4 = labels[3]
        self.label4.atomic_group = atomic_group1
        self.label4.save()
        self.label5 = labels[4]
        self.label5.atomic_group = atomic_group1
        self.label5.save()
        hosts[0].labels.add(labels[0])  # label1
        hosts[1].labels.add(labels[1])  # label2
        self.label6 = labels[5]
        self.label7 = labels[6]
        for hostnum in xrange(4,7):  # host5..host7
            hosts[hostnum].labels.add(self.label4)  # an atomic group lavel
            hosts[hostnum].labels.add(self.label6)  # a normal label
        hosts[6].labels.add(self.label7)
        for hostnum in xrange(7,9):  # host8..host9
            hosts[hostnum].labels.add(self.label5)  # an atomic group lavel
            hosts[hostnum].labels.add(self.label6)  # a normal label
            hosts[hostnum].labels.add(self.label7)


    def _setup_dummy_user(self):
        user = models.User.objects.create(login='dummy', access_level=100)
        thread_local.set_user(user)


    def setUp(self):
        self.god = mock.mock_god()
        self._open_test_db()
        self._fill_in_test_data()
        self._set_monitor_stubs()
        self._dispatcher = monitor_db.Dispatcher()
        self._setup_dummy_user()


    def tearDown(self):
        self._close_test_db()
        self.god.unstub_all()


    def _create_job(self, hosts=[], metahosts=[], priority=0, active=False,
                    synchronous=False, atomic_group=None):
        """
        Create a job row in the test database.

        @param hosts - A list of explicit host ids for this job to be
                scheduled on.
        @param metahosts - A list of label ids for each host that this job
                should be scheduled on (meta host scheduling).
        @param priority - The job priority (integer).
        @param active - bool, mark this job as running or not in the database?
        @param synchronous - bool, if True use synch_count=2 otherwise use
                synch_count=1.
        @param atomic_group - An atomic group id for this job to schedule on
                or None if atomic scheduling is not required.  Each metahost
                becomes a request to schedule an entire atomic group.
                This does not support creating an active atomic group job.
        """
        assert not (atomic_group and active)  # TODO(gps): support this
        synch_count = synchronous and 2 or 1
        created_on = datetime.datetime(2008, 1, 1)
        status = models.HostQueueEntry.Status.QUEUED
        if active:
            status = models.HostQueueEntry.Status.RUNNING
        job = models.Job.objects.create(
            name='test', owner='my_user', priority=priority,
            synch_count=synch_count, created_on=created_on,
            reboot_before=models.RebootBefore.NEVER)
        for host_id in hosts:
            models.HostQueueEntry.objects.create(job=job, host_id=host_id,
                                                 status=status,
                                                 atomic_group_id=atomic_group)
            models.IneligibleHostQueue.objects.create(job=job, host_id=host_id)
        for label_id in metahosts:
            models.HostQueueEntry.objects.create(job=job, meta_host_id=label_id,
                                                 status=status,
                                                 atomic_group_id=atomic_group)
        if atomic_group and not (metahosts or hosts):
            # Create a single HQE to request the atomic group of hosts even if
            # no metahosts or hosts are supplied.
            models.HostQueueEntry.objects.create(job=job,
                                                 status=status,
                                                 atomic_group_id=atomic_group)
        return job


    def _create_job_simple(self, hosts, use_metahost=False,
                          priority=0, active=False):
        """An alternative interface to _create_job"""
        args = {'hosts' : [], 'metahosts' : []}
        if use_metahost:
            args['metahosts'] = hosts
        else:
            args['hosts'] = hosts
        return self._create_job(priority=priority, active=active, **args)


    def _update_hqe(self, set, where=''):
        query = 'UPDATE host_queue_entries SET ' + set
        if where:
            query += ' WHERE ' + where
        self._do_query(query)


class DBObjectTest(BaseSchedulerTest):
    # It may seem odd to subclass BaseSchedulerTest for this but it saves us
    # duplicating some setup work for what we want to test.


    def test_compare_fields_in_row(self):
        host = monitor_db.Host(id=1)
        fields = list(host._fields)
        row_data = [getattr(host, fieldname) for fieldname in fields]
        self.assertEqual({}, host._compare_fields_in_row(row_data))
        row_data[fields.index('hostname')] = 'spam'
        self.assertEqual({'hostname': ('host1', 'spam')},
                         host._compare_fields_in_row(row_data))
        row_data[fields.index('id')] = 23
        self.assertEqual({'hostname': ('host1', 'spam'), 'id': (1, 23)},
                         host._compare_fields_in_row(row_data))


    def test_always_query(self):
        host_a = monitor_db.Host(id=2)
        self.assertEqual(host_a.hostname, 'host2')
        self._do_query('UPDATE hosts SET hostname="host2-updated" WHERE id=2')
        host_b = monitor_db.Host(id=2, always_query=True)
        self.assert_(host_a is host_b, 'Cached instance not returned.')
        self.assertEqual(host_a.hostname, 'host2-updated',
                         'Database was not re-queried')

        # If either of these are called, a query was made when it shouldn't be.
        host_a._compare_fields_in_row = lambda _: self.fail('eek! a query!')
        host_a._update_fields_from_row = host_a._compare_fields_in_row
        host_c = monitor_db.Host(id=2, always_query=False)
        self.assert_(host_a is host_c, 'Cached instance not returned')


    def test_delete(self):
        host = monitor_db.Host(id=3)
        host.delete()
        host = self.assertRaises(monitor_db.DBError, monitor_db.Host, id=3,
                                 always_query=False)
        host = self.assertRaises(monitor_db.DBError, monitor_db.Host, id=3,
                                 always_query=True)

    def test_save(self):
        # Dummy Job to avoid creating a one in the HostQueueEntry __init__.
        class MockJob(object):
            def __init__(self, id):
                pass
            def tag(self):
                return 'MockJob'
        self.god.stub_with(monitor_db, 'Job', MockJob)
        hqe = monitor_db.HostQueueEntry(
                new_record=True,
                row=[0, 1, 2, 'Queued', None, 0, 0, 0, '.', None, False, None])
        hqe.save()
        new_id = hqe.id
        # Force a re-query and verify that the correct data was stored.
        monitor_db.DBObject._clear_instance_cache()
        hqe = monitor_db.HostQueueEntry(id=new_id)
        self.assertEqual(hqe.id, new_id)
        self.assertEqual(hqe.job_id, 1)
        self.assertEqual(hqe.host_id, 2)
        self.assertEqual(hqe.status, 'Queued')
        self.assertEqual(hqe.meta_host, None)
        self.assertEqual(hqe.active, False)
        self.assertEqual(hqe.complete, False)
        self.assertEqual(hqe.deleted, False)
        self.assertEqual(hqe.execution_subdir, '.')
        self.assertEqual(hqe.atomic_group_id, None)
        self.assertEqual(hqe.started_on, None)


class DispatcherSchedulingTest(BaseSchedulerTest):
    _jobs_scheduled = []


    def tearDown(self):
        super(DispatcherSchedulingTest, self).tearDown()


    def _set_monitor_stubs(self):
        super(DispatcherSchedulingTest, self)._set_monitor_stubs()

        def job_run_stub(job_self, queue_entry):
            """Return a dummy for testing.  Called by HostQueueEntry.run()."""
            self._record_job_scheduled(job_self.id, queue_entry.host.id)
            queue_entry.set_status('Starting')
            return DummyAgent()

        self.god.stub_with(monitor_db.Job, 'run', job_run_stub)

        def hqe_queue_log_record_stub(self, log_line):
            """No-Op to avoid calls down to the _drone_manager during tests."""

        self.god.stub_with(monitor_db.HostQueueEntry, 'queue_log_record',
                           hqe_queue_log_record_stub)


    def _record_job_scheduled(self, job_id, host_id):
        record = (job_id, host_id)
        self.assert_(record not in self._jobs_scheduled,
                     'Job %d scheduled on host %d twice' %
                     (job_id, host_id))
        self._jobs_scheduled.append(record)


    def _assert_job_scheduled_on(self, job_id, host_id):
        record = (job_id, host_id)
        self.assert_(record in self._jobs_scheduled,
                     'Job %d not scheduled on host %d as expected\n'
                     'Jobs scheduled: %s' %
                     (job_id, host_id, self._jobs_scheduled))
        self._jobs_scheduled.remove(record)


    def _assert_job_scheduled_on_number_of(self, job_id, host_ids, number):
        """Assert job was scheduled on exactly number hosts out of a set."""
        found = []
        for host_id in host_ids:
            record = (job_id, host_id)
            if record in self._jobs_scheduled:
                found.append(record)
                self._jobs_scheduled.remove(record)
        if len(found) < number:
            self.fail('Job %d scheduled on fewer than %d hosts in %s.\n'
                      'Jobs scheduled: %s' % (job_id, number, host_ids, found))
        elif len(found) > number:
            self.fail('Job %d scheduled on more than %d hosts in %s.\n'
                      'Jobs scheduled: %s' % (job_id, number, host_ids, found))


    def _check_for_extra_schedulings(self):
        if len(self._jobs_scheduled) != 0:
            self.fail('Extra jobs scheduled: ' +
                      str(self._jobs_scheduled))


    def _convert_jobs_to_metahosts(self, *job_ids):
        sql_tuple = '(' + ','.join(str(i) for i in job_ids) + ')'
        self._do_query('UPDATE host_queue_entries SET '
                       'meta_host=host_id, host_id=NULL '
                       'WHERE job_id IN ' + sql_tuple)


    def _lock_host(self, host_id):
        self._do_query('UPDATE hosts SET locked=1 WHERE id=' +
                       str(host_id))


    def setUp(self):
        super(DispatcherSchedulingTest, self).setUp()
        self._jobs_scheduled = []


    def _test_basic_scheduling_helper(self, use_metahosts):
        'Basic nonmetahost scheduling'
        self._create_job_simple([1], use_metahosts)
        self._create_job_simple([2], use_metahosts)
        self._dispatcher._schedule_new_jobs()
        self._assert_job_scheduled_on(1, 1)
        self._assert_job_scheduled_on(2, 2)
        self._check_for_extra_schedulings()


    def _test_priorities_helper(self, use_metahosts):
        'Test prioritization ordering'
        self._create_job_simple([1], use_metahosts)
        self._create_job_simple([2], use_metahosts)
        self._create_job_simple([1,2], use_metahosts)
        self._create_job_simple([1], use_metahosts, priority=1)
        self._dispatcher._schedule_new_jobs()
        self._assert_job_scheduled_on(4, 1) # higher priority
        self._assert_job_scheduled_on(2, 2) # earlier job over later
        self._check_for_extra_schedulings()


    def _test_hosts_ready_helper(self, use_metahosts):
        """
        Only hosts that are status=Ready, unlocked and not invalid get
        scheduled.
        """
        self._create_job_simple([1], use_metahosts)
        self._do_query('UPDATE hosts SET status="Running" WHERE id=1')
        self._dispatcher._schedule_new_jobs()
        self._check_for_extra_schedulings()

        self._do_query('UPDATE hosts SET status="Ready", locked=1 '
                       'WHERE id=1')
        self._dispatcher._schedule_new_jobs()
        self._check_for_extra_schedulings()

        self._do_query('UPDATE hosts SET locked=0, invalid=1 '
                       'WHERE id=1')
        self._dispatcher._schedule_new_jobs()
        if not use_metahosts:
            self._assert_job_scheduled_on(1, 1)
        self._check_for_extra_schedulings()


    def _test_hosts_idle_helper(self, use_metahosts):
        'Only idle hosts get scheduled'
        self._create_job(hosts=[1], active=True)
        self._create_job_simple([1], use_metahosts)
        self._dispatcher._schedule_new_jobs()
        self._check_for_extra_schedulings()


    def _test_obey_ACLs_helper(self, use_metahosts):
        self._do_query('DELETE FROM acl_groups_hosts WHERE host_id=1')
        self._create_job_simple([1], use_metahosts)
        self._dispatcher._schedule_new_jobs()
        self._check_for_extra_schedulings()


    def test_basic_scheduling(self):
        self._test_basic_scheduling_helper(False)


    def test_priorities(self):
        self._test_priorities_helper(False)


    def test_hosts_ready(self):
        self._test_hosts_ready_helper(False)


    def test_hosts_idle(self):
        self._test_hosts_idle_helper(False)


    def test_obey_ACLs(self):
        self._test_obey_ACLs_helper(False)


    def test_non_metahost_on_invalid_host(self):
        """
        Non-metahost entries can get scheduled on invalid hosts (this is how
        one-time hosts work).
        """
        self._do_query('UPDATE hosts SET invalid=1')
        self._test_basic_scheduling_helper(False)


    def test_metahost_scheduling(self):
        """
        Basic metahost scheduling
        """
        self._test_basic_scheduling_helper(True)


    def test_metahost_priorities(self):
        self._test_priorities_helper(True)


    def test_metahost_hosts_ready(self):
        self._test_hosts_ready_helper(True)


    def test_metahost_hosts_idle(self):
        self._test_hosts_idle_helper(True)


    def test_metahost_obey_ACLs(self):
        self._test_obey_ACLs_helper(True)


    def _setup_test_only_if_needed_labels(self):
        # apply only_if_needed label3 to host1
        models.Host.smart_get('host1').labels.add(self.label3)
        return self._create_job_simple([1], use_metahost=True)


    def test_only_if_needed_labels_avoids_host(self):
        job = self._setup_test_only_if_needed_labels()
        # if the job doesn't depend on label3, there should be no scheduling
        self._dispatcher._schedule_new_jobs()
        self._check_for_extra_schedulings()


    def test_only_if_needed_labels_schedules(self):
        job = self._setup_test_only_if_needed_labels()
        job.dependency_labels.add(self.label3)
        self._dispatcher._schedule_new_jobs()
        self._assert_job_scheduled_on(1, 1)
        self._check_for_extra_schedulings()


    def test_only_if_needed_labels_via_metahost(self):
        job = self._setup_test_only_if_needed_labels()
        job.dependency_labels.add(self.label3)
        # should also work if the metahost is the only_if_needed label
        self._do_query('DELETE FROM jobs_dependency_labels')
        self._create_job(metahosts=[3])
        self._dispatcher._schedule_new_jobs()
        self._assert_job_scheduled_on(2, 1)
        self._check_for_extra_schedulings()


    def test_nonmetahost_over_metahost(self):
        """
        Non-metahost entries should take priority over metahost entries
        for the same host
        """
        self._create_job(metahosts=[1])
        self._create_job(hosts=[1])
        self._dispatcher._schedule_new_jobs()
        self._assert_job_scheduled_on(2, 1)
        self._check_for_extra_schedulings()


    def test_metahosts_obey_blocks(self):
        """
        Metahosts can't get scheduled on hosts already scheduled for
        that job.
        """
        self._create_job(metahosts=[1], hosts=[1])
        # make the nonmetahost entry complete, so the metahost can try
        # to get scheduled
        self._update_hqe(set='complete = 1', where='host_id=1')
        self._dispatcher._schedule_new_jobs()
        self._check_for_extra_schedulings()


    # TODO(gps): These should probably live in their own TestCase class
    # specific to testing HostScheduler methods directly.  It was convenient
    # to put it here for now to share existing test environment setup code.
    def test_HostScheduler_check_atomic_group_labels(self):
        normal_job = self._create_job(metahosts=[0])
        atomic_job = self._create_job(atomic_group=1)
        # Indirectly initialize the internal state of the host scheduler.
        self._dispatcher._refresh_pending_queue_entries()

        atomic_hqe = monitor_db.HostQueueEntry(id=atomic_job.id)
        normal_hqe = monitor_db.HostQueueEntry(id=normal_job.id)

        host_scheduler = self._dispatcher._host_scheduler
        self.assertTrue(host_scheduler._check_atomic_group_labels(
                [self.label4.id], atomic_hqe))
        self.assertFalse(host_scheduler._check_atomic_group_labels(
                [self.label4.id], normal_hqe))
        self.assertFalse(host_scheduler._check_atomic_group_labels(
                [self.label5.id, self.label6.id, self.label7.id], normal_hqe))
        self.assertTrue(host_scheduler._check_atomic_group_labels(
                [self.label4.id, self.label6.id], atomic_hqe))
        self.assertRaises(monitor_db.SchedulerError,
                          host_scheduler._check_atomic_group_labels,
                          [self.label4.id, self.label5.id],
                          atomic_hqe)


    def test_HostScheduler_get_host_atomic_group_id(self):
        self._create_job(metahosts=[self.label6.id])
        # Indirectly initialize the internal state of the host scheduler.
        self._dispatcher._refresh_pending_queue_entries()

        # Test the host scheduler
        host_scheduler = self._dispatcher._host_scheduler
        self.assertRaises(monitor_db.SchedulerError,
                          host_scheduler._get_host_atomic_group_id,
                          [self.label4.id, self.label5.id])
        self.assertEqual(None, host_scheduler._get_host_atomic_group_id([]))
        self.assertEqual(None, host_scheduler._get_host_atomic_group_id(
                [self.label3.id, self.label7.id, self.label6.id]))
        self.assertEqual(1, host_scheduler._get_host_atomic_group_id(
                [self.label4.id, self.label7.id, self.label6.id]))
        self.assertEqual(1, host_scheduler._get_host_atomic_group_id(
                [self.label7.id, self.label5.id]))


    def test_atomic_group_hosts_blocked_from_non_atomic_jobs(self):
        # Create a job scheduled to run on label6.
        self._create_job(metahosts=[self.label6.id])
        self._dispatcher._schedule_new_jobs()
        # label6 only has hosts that are in atomic groups associated with it,
        # there should be no scheduling.
        self._check_for_extra_schedulings()


    def test_atomic_group_hosts_blocked_from_non_atomic_jobs_explicit(self):
        # Create a job scheduled to run on label5.  This is an atomic group
        # label but this job does not request atomic group scheduling.
        self._create_job(metahosts=[self.label5.id])
        self._dispatcher._schedule_new_jobs()
        # label6 only has hosts that are in atomic groups associated with it,
        # there should be no scheduling.
        self._check_for_extra_schedulings()


    def test_atomic_group_scheduling_basics(self):
        # Create jobs scheduled to run on an atomic group.
        job_a = self._create_job(synchronous=True, metahosts=[self.label4.id],
                         atomic_group=1)
        job_b = self._create_job(synchronous=True, metahosts=[self.label5.id],
                         atomic_group=1)
        self._dispatcher._schedule_new_jobs()
        # atomic_group.max_number_of_machines was 2 so we should run on 2.
        self._assert_job_scheduled_on_number_of(job_a.id, (5, 6, 7), 2)
        self._assert_job_scheduled_on(job_b.id, 8)  # label5
        self._assert_job_scheduled_on(job_b.id, 9)  # label5
        self._check_for_extra_schedulings()

        # The three host label4 atomic group still has one host available.
        # That means a job with a synch_count of 1 asking to be scheduled on
        # the atomic group can still use the final machine.
        #
        # This may seem like a somewhat odd use case.  It allows the use of an
        # atomic group as a set of machines to run smaller jobs within (a set
        # of hosts configured for use in network tests with eachother perhaps?)
        onehost_job = self._create_job(atomic_group=1)
        self._dispatcher._schedule_new_jobs()
        self._assert_job_scheduled_on_number_of(onehost_job.id, (5, 6, 7), 1)
        self._check_for_extra_schedulings()

        # No more atomic groups have hosts available, no more jobs should
        # be scheduled.
        self._create_job(atomic_group=1)
        self._dispatcher._schedule_new_jobs()
        self._check_for_extra_schedulings()


    def test_atomic_group_scheduling_obeys_acls(self):
        # Request scheduling on a specific atomic label but be denied by ACLs.
        self._do_query('DELETE FROM acl_groups_hosts WHERE host_id in (8,9)')
        job = self._create_job(metahosts=[self.label5.id], atomic_group=1)
        self._dispatcher._schedule_new_jobs()
        self._check_for_extra_schedulings()


    def test_atomic_group_scheduling_dependency_label_exclude(self):
        # A dependency label that matches no hosts in the atomic group.
        job_a = self._create_job(atomic_group=1)
        job_a.dependency_labels.add(self.label3)
        self._dispatcher._schedule_new_jobs()
        self._check_for_extra_schedulings()


    def test_atomic_group_scheduling_metahost_dependency_label_exclude(self):
        # A metahost and dependency label that excludes too many hosts.
        job_b = self._create_job(synchronous=True, metahosts=[self.label4.id],
                                 atomic_group=1)
        job_b.dependency_labels.add(self.label7)
        self._dispatcher._schedule_new_jobs()
        self._check_for_extra_schedulings()


    def test_atomic_group_scheduling_dependency_label_match(self):
        # A dependency label that exists on enough atomic group hosts in only
        # one of the two atomic group labels.
        job_c = self._create_job(synchronous=True, atomic_group=1)
        job_c.dependency_labels.add(self.label7)
        self._dispatcher._schedule_new_jobs()
        self._assert_job_scheduled_on_number_of(job_c.id, (8, 9), 2)
        self._check_for_extra_schedulings()


    def test_atomic_group_scheduling_no_metahost(self):
        # Force it to schedule on the other group for a reliable test.
        self._do_query('UPDATE hosts SET invalid=1 WHERE id=9')
        # An atomic job without a metahost.
        job = self._create_job(synchronous=True, atomic_group=1)
        self._dispatcher._schedule_new_jobs()
        self._assert_job_scheduled_on_number_of(job.id, (5, 6, 7), 2)
        self._check_for_extra_schedulings()


    def test_atomic_group_scheduling_partial_group(self):
        # Make one host in labels[3] unavailable so that there are only two
        # hosts left in the group.
        self._do_query('UPDATE hosts SET status="Repair Failed" WHERE id=5')
        job = self._create_job(synchronous=True, metahosts=[self.label4.id],
                         atomic_group=1)
        self._dispatcher._schedule_new_jobs()
        # Verify that it was scheduled on the 2 ready hosts in that group.
        self._assert_job_scheduled_on(job.id, 6)
        self._assert_job_scheduled_on(job.id, 7)
        self._check_for_extra_schedulings()


    def test_atomic_group_scheduling_not_enough_available(self):
        # Mark some hosts in each atomic group label as not usable.
        # One host running, another invalid in the first group label.
        self._do_query('UPDATE hosts SET status="Running" WHERE id=5')
        self._do_query('UPDATE hosts SET invalid=1 WHERE id=6')
        # One host invalid in the second group label.
        self._do_query('UPDATE hosts SET invalid=1 WHERE id=9')
        # Nothing to schedule when no group label has enough (2) good hosts..
        self._create_job(atomic_group=1, synchronous=True)
        self._dispatcher._schedule_new_jobs()
        # There are not enough hosts in either atomic group,
        # No more scheduling should occur.
        self._check_for_extra_schedulings()

        # Now create an atomic job that has a synch count of 1.  It should
        # schedule on exactly one of the hosts.
        onehost_job = self._create_job(atomic_group=1)
        self._dispatcher._schedule_new_jobs()
        self._assert_job_scheduled_on_number_of(onehost_job.id, (7, 8), 1)


    def test_atomic_group_scheduling_no_valid_hosts(self):
        self._do_query('UPDATE hosts SET invalid=1 WHERE id in (8,9)')
        self._create_job(synchronous=True, metahosts=[self.label5.id],
                         atomic_group=1)
        self._dispatcher._schedule_new_jobs()
        # no hosts in the selected group and label are valid.  no schedulings.
        self._check_for_extra_schedulings()


    def test_atomic_group_scheduling_metahost_works(self):
        # Test that atomic group scheduling also obeys metahosts.
        self._create_job(metahosts=[0], atomic_group=1)
        self._dispatcher._schedule_new_jobs()
        # There are no atomic group hosts that also have that metahost.
        self._check_for_extra_schedulings()

        job_b = self._create_job(metahosts=[self.label5.id], atomic_group=1)
        self._dispatcher._schedule_new_jobs()
        self._assert_job_scheduled_on(job_b.id, 8)
        self._assert_job_scheduled_on(job_b.id, 9)
        self._check_for_extra_schedulings()


    def test_atomic_group_skips_ineligible_hosts(self):
        # Test hosts marked ineligible for this job are not eligible.
        # How would this ever happen anyways?
        job = self._create_job(metahosts=[self.label4.id], atomic_group=1)
        models.IneligibleHostQueue.objects.create(job=job, host_id=5)
        models.IneligibleHostQueue.objects.create(job=job, host_id=6)
        models.IneligibleHostQueue.objects.create(job=job, host_id=7)
        self._dispatcher._schedule_new_jobs()
        # No scheduling should occur as all desired hosts were ineligible.
        self._check_for_extra_schedulings()


    def test_atomic_group_scheduling_fail(self):
        # If synch_count is > the atomic group number of machines, the job
        # should be aborted immediately.
        model_job = self._create_job(synchronous=True, atomic_group=1)
        model_job.synch_count = 4
        model_job.save()
        job = monitor_db.Job(id=model_job.id)
        self._dispatcher._schedule_new_jobs()
        self._check_for_extra_schedulings()
        queue_entries = job.get_host_queue_entries()
        self.assertEqual(1, len(queue_entries))
        self.assertEqual(queue_entries[0].status,
                         models.HostQueueEntry.Status.ABORTED)


    def test_atomic_group_no_labels_no_scheduling(self):
        # Never schedule on atomic groups marked invalid.
        job = self._create_job(metahosts=[self.label5.id], synchronous=True,
                               atomic_group=1)
        # Deleting an atomic group via the frontend marks it invalid and
        # removes all label references to the group.  The job now references
        # an invalid atomic group with no labels associated with it.
        self.label5.atomic_group.invalid = True
        self.label5.atomic_group.save()
        self.label5.atomic_group = None
        self.label5.save()

        self._dispatcher._schedule_new_jobs()
        self._check_for_extra_schedulings()


    def test_schedule_directly_on_atomic_group_host_fail(self):
        # Scheduling a job directly on hosts in an atomic group must
        # fail to avoid users inadvertently holding up the use of an
        # entire atomic group by using the machines individually.
        job = self._create_job(hosts=[5])
        self._dispatcher._schedule_new_jobs()
        self._check_for_extra_schedulings()


    def test_schedule_directly_on_atomic_group_host(self):
        # Scheduling a job directly on one host in an atomic group will
        # work when the atomic group is listed on the HQE in addition
        # to the host (assuming the sync count is 1).
        job = self._create_job(hosts=[5], atomic_group=1)
        self._dispatcher._schedule_new_jobs()
        self._assert_job_scheduled_on(job.id, 5)
        self._check_for_extra_schedulings()


    def test_schedule_directly_on_atomic_group_hosts_sync2(self):
        job = self._create_job(hosts=[5,8], atomic_group=1, synchronous=True)
        self._dispatcher._schedule_new_jobs()
        self._assert_job_scheduled_on(job.id, 5)
        self._assert_job_scheduled_on(job.id, 8)
        self._check_for_extra_schedulings()


    def test_schedule_directly_on_atomic_group_hosts_wrong_group(self):
        job = self._create_job(hosts=[5,8], atomic_group=2, synchronous=True)
        self._dispatcher._schedule_new_jobs()
        self._check_for_extra_schedulings()


    def test_only_schedule_queued_entries(self):
        self._create_job(metahosts=[1])
        self._update_hqe(set='active=1, host_id=2')
        self._dispatcher._schedule_new_jobs()
        self._check_for_extra_schedulings()


    def test_no_ready_hosts(self):
        self._create_job(hosts=[1])
        self._do_query('UPDATE hosts SET status="Repair Failed"')
        self._dispatcher._schedule_new_jobs()
        self._check_for_extra_schedulings()


class DispatcherThrottlingTest(BaseSchedulerTest):
    """
    Test that the dispatcher throttles:
     * total number of running processes
     * number of processes started per cycle
    """
    _MAX_RUNNING = 3
    _MAX_STARTED = 2

    def setUp(self):
        super(DispatcherThrottlingTest, self).setUp()
        scheduler_config.config.max_processes_per_drone = self._MAX_RUNNING
        scheduler_config.config.max_processes_started_per_cycle = (
            self._MAX_STARTED)

        def fake_max_runnable_processes(fake_self):
            running = sum(agent.num_processes
                          for agent in self._agents
                          if agent.is_running())
            return self._MAX_RUNNING - running
        self.god.stub_with(drone_manager.DroneManager, 'max_runnable_processes',
                           fake_max_runnable_processes)


    def _setup_some_agents(self, num_agents):
        self._agents = [DummyAgent() for i in xrange(num_agents)]
        self._dispatcher._agents = list(self._agents)


    def _run_a_few_cycles(self):
        for i in xrange(4):
            self._dispatcher._handle_agents()


    def _assert_agents_started(self, indexes, is_started=True):
        for i in indexes:
            self.assert_(self._agents[i].is_running() == is_started,
                         'Agent %d %sstarted' %
                         (i, is_started and 'not ' or ''))


    def _assert_agents_not_started(self, indexes):
        self._assert_agents_started(indexes, False)


    def test_throttle_total(self):
        self._setup_some_agents(4)
        self._run_a_few_cycles()
        self._assert_agents_started([0, 1, 2])
        self._assert_agents_not_started([3])


    def test_throttle_per_cycle(self):
        self._setup_some_agents(3)
        self._dispatcher._handle_agents()
        self._assert_agents_started([0, 1])
        self._assert_agents_not_started([2])


    def test_throttle_with_synchronous(self):
        self._setup_some_agents(2)
        self._agents[0].num_processes = 3
        self._run_a_few_cycles()
        self._assert_agents_started([0])
        self._assert_agents_not_started([1])


    def test_large_agent_starvation(self):
        """
        Ensure large agents don't get starved by lower-priority agents.
        """
        self._setup_some_agents(3)
        self._agents[1].num_processes = 3
        self._run_a_few_cycles()
        self._assert_agents_started([0])
        self._assert_agents_not_started([1, 2])

        self._agents[0].set_done(True)
        self._run_a_few_cycles()
        self._assert_agents_started([1])
        self._assert_agents_not_started([2])


    def test_zero_process_agent(self):
        self._setup_some_agents(5)
        self._agents[4].num_processes = 0
        self._run_a_few_cycles()
        self._assert_agents_started([0, 1, 2, 4])
        self._assert_agents_not_started([3])


class FindAbortTest(BaseSchedulerTest):
    """
    Test the dispatcher abort functionality.
    """
    def _check_host_agent(self, agent, host_id):
        self.assert_(isinstance(agent, monitor_db.Agent))
        tasks = list(agent.queue.queue)
        self.assertEquals(len(tasks), 2)
        cleanup, verify = tasks

        self.assert_(isinstance(cleanup, monitor_db.CleanupTask))
        self.assertEquals(cleanup.host.id, host_id)

        self.assert_(isinstance(verify, monitor_db.VerifyTask))
        self.assertEquals(verify.host.id, host_id)


    def _check_agents(self, agents):
        agents = list(agents)
        self.assertEquals(len(agents), 3)
        self.assertEquals(agents[0], self._agent)
        self._check_host_agent(agents[1], 1)
        self._check_host_agent(agents[2], 2)


    def _common_setup(self):
        self._create_job(hosts=[1, 2])
        self._update_hqe(set='aborted=1')
        self._agent = self.god.create_mock_class(monitor_db.Agent, 'old_agent')
        _set_host_and_qe_ids(self._agent, [1, 2])
        self._agent.abort.expect_call()
        self._agent.abort.expect_call() # gets called once for each HQE
        self._dispatcher.add_agent(self._agent)


    def test_find_aborting(self):
        self._common_setup()
        self._dispatcher._find_aborting()
        self.god.check_playback()


    def test_find_aborting_verifying(self):
        self._common_setup()
        self._update_hqe(set='active=1, status="Verifying"')

        self._dispatcher._find_aborting()

        self._check_agents(self._dispatcher._agents)
        self.god.check_playback()


class JobTimeoutTest(BaseSchedulerTest):
    def _test_synch_start_timeout_helper(self, expect_abort,
                                         set_created_on=True, set_active=True,
                                         set_acl=True):
        scheduler_config.config.synch_job_start_timeout_minutes = 60
        job = self._create_job(hosts=[1, 2])
        if set_active:
            hqe = job.hostqueueentry_set.filter(host__id=1)[0]
            hqe.status = 'Pending'
            hqe.active = 1
            hqe.save()

        everyone_acl = models.AclGroup.smart_get('Everyone')
        host1 = models.Host.smart_get(1)
        if set_acl:
            everyone_acl.hosts.add(host1)
        else:
            everyone_acl.hosts.remove(host1)

        job.created_on = datetime.datetime.now()
        if set_created_on:
            job.created_on -= datetime.timedelta(minutes=100)
        job.save()

        cleanup = self._dispatcher._periodic_cleanup
        cleanup._abort_jobs_past_synch_start_timeout()

        for hqe in job.hostqueueentry_set.all():
            self.assertEquals(hqe.aborted, expect_abort)


    def test_synch_start_timeout_helper(self):
        # no abort if any of the condition aren't met
        self._test_synch_start_timeout_helper(False, set_created_on=False)
        self._test_synch_start_timeout_helper(False, set_active=False)
        self._test_synch_start_timeout_helper(False, set_acl=False)
        # abort if all conditions are met
        self._test_synch_start_timeout_helper(True)


class PidfileRunMonitorTest(unittest.TestCase):
    execution_tag = 'test_tag'
    pid = 12345
    process = drone_manager.Process('myhost', pid)
    num_tests_failed = 1

    def setUp(self):
        self.god = mock.mock_god()
        self.mock_drone_manager = self.god.create_mock_class(
            drone_manager.DroneManager, 'drone_manager')
        self.god.stub_with(monitor_db, '_drone_manager',
                           self.mock_drone_manager)
        self.god.stub_function(email_manager.manager, 'enqueue_notify_email')

        self.pidfile_id = object()

        (self.mock_drone_manager.get_pidfile_id_from
             .expect_call(self.execution_tag,
                          pidfile_name=monitor_db._AUTOSERV_PID_FILE)
             .and_return(self.pidfile_id))
        self.mock_drone_manager.register_pidfile.expect_call(self.pidfile_id)

        self.monitor = monitor_db.PidfileRunMonitor()
        self.monitor.attach_to_existing_process(self.execution_tag)


    def tearDown(self):
        self.god.unstub_all()


    def setup_pidfile(self, pid=None, exit_code=None, tests_failed=None,
                      use_second_read=False):
        contents = drone_manager.PidfileContents()
        if pid is not None:
            contents.process = drone_manager.Process('myhost', pid)
        contents.exit_status = exit_code
        contents.num_tests_failed = tests_failed
        self.mock_drone_manager.get_pidfile_contents.expect_call(
            self.pidfile_id, use_second_read=use_second_read).and_return(
            contents)


    def set_not_yet_run(self):
        self.setup_pidfile()


    def set_empty_pidfile(self):
        self.setup_pidfile()


    def set_running(self, use_second_read=False):
        self.setup_pidfile(self.pid, use_second_read=use_second_read)


    def set_complete(self, error_code, use_second_read=False):
        self.setup_pidfile(self.pid, error_code, self.num_tests_failed,
                           use_second_read=use_second_read)


    def _check_monitor(self, expected_pid, expected_exit_status,
                       expected_num_tests_failed):
        if expected_pid is None:
            self.assertEquals(self.monitor._state.process, None)
        else:
            self.assertEquals(self.monitor._state.process.pid, expected_pid)
        self.assertEquals(self.monitor._state.exit_status, expected_exit_status)
        self.assertEquals(self.monitor._state.num_tests_failed,
                          expected_num_tests_failed)


        self.god.check_playback()


    def _test_read_pidfile_helper(self, expected_pid, expected_exit_status,
                                  expected_num_tests_failed):
        self.monitor._read_pidfile()
        self._check_monitor(expected_pid, expected_exit_status,
                            expected_num_tests_failed)


    def _get_expected_tests_failed(self, expected_exit_status):
        if expected_exit_status is None:
            expected_tests_failed = None
        else:
            expected_tests_failed = self.num_tests_failed
        return expected_tests_failed


    def test_read_pidfile(self):
        self.set_not_yet_run()
        self._test_read_pidfile_helper(None, None, None)

        self.set_empty_pidfile()
        self._test_read_pidfile_helper(None, None, None)

        self.set_running()
        self._test_read_pidfile_helper(self.pid, None, None)

        self.set_complete(123)
        self._test_read_pidfile_helper(self.pid, 123, self.num_tests_failed)


    def test_read_pidfile_error(self):
        self.mock_drone_manager.get_pidfile_contents.expect_call(
            self.pidfile_id, use_second_read=False).and_return(
            drone_manager.InvalidPidfile('error'))
        self.assertRaises(monitor_db.PidfileRunMonitor._PidfileException,
                          self.monitor._read_pidfile)
        self.god.check_playback()


    def setup_is_running(self, is_running):
        self.mock_drone_manager.is_process_running.expect_call(
            self.process).and_return(is_running)


    def _test_get_pidfile_info_helper(self, expected_pid, expected_exit_status,
                                      expected_num_tests_failed):
        self.monitor._get_pidfile_info()
        self._check_monitor(expected_pid, expected_exit_status,
                            expected_num_tests_failed)


    def test_get_pidfile_info(self):
        """
        normal cases for get_pidfile_info
        """
        # running
        self.set_running()
        self.setup_is_running(True)
        self._test_get_pidfile_info_helper(self.pid, None, None)

        # exited during check
        self.set_running()
        self.setup_is_running(False)
        self.set_complete(123, use_second_read=True) # pidfile gets read again
        self._test_get_pidfile_info_helper(self.pid, 123, self.num_tests_failed)

        # completed
        self.set_complete(123)
        self._test_get_pidfile_info_helper(self.pid, 123, self.num_tests_failed)


    def test_get_pidfile_info_running_no_proc(self):
        """
        pidfile shows process running, but no proc exists
        """
        # running but no proc
        self.set_running()
        self.setup_is_running(False)
        self.set_running(use_second_read=True)
        email_manager.manager.enqueue_notify_email.expect_call(
            mock.is_string_comparator(), mock.is_string_comparator())
        self._test_get_pidfile_info_helper(self.pid, 1, 0)
        self.assertTrue(self.monitor.lost_process)


    def test_get_pidfile_info_not_yet_run(self):
        """
        pidfile hasn't been written yet
        """
        self.set_not_yet_run()
        self._test_get_pidfile_info_helper(None, None, None)


    def test_process_failed_to_write_pidfile(self):
        self.set_not_yet_run()
        email_manager.manager.enqueue_notify_email.expect_call(
            mock.is_string_comparator(), mock.is_string_comparator())
        self.monitor._start_time = time.time() - monitor_db.PIDFILE_TIMEOUT - 1
        self._test_get_pidfile_info_helper(None, 1, 0)
        self.assertTrue(self.monitor.lost_process)


class AgentTest(unittest.TestCase):
    def setUp(self):
        self.god = mock.mock_god()
        self._dispatcher = self.god.create_mock_class(monitor_db.Dispatcher,
                                                      'dispatcher')


    def tearDown(self):
        self.god.unstub_all()


    def _create_mock_task(self, name):
        task = self.god.create_mock_class(monitor_db.AgentTask, name)
        _set_host_and_qe_ids(task)
        return task

    def _create_agent(self, tasks):
        agent = monitor_db.Agent(tasks)
        agent.dispatcher = self._dispatcher
        return agent


    def _finish_agent(self, agent):
        while not agent.is_done():
            agent.tick()


    def test_agent(self):
        task1 = self._create_mock_task('task1')
        task2 = self._create_mock_task('task2')
        task3 = self._create_mock_task('task3')
        task1.poll.expect_call()
        task1.is_done.expect_call().and_return(False)
        task1.poll.expect_call()
        task1.is_done.expect_call().and_return(True)
        task1.is_done.expect_call().and_return(True)
        task1.success = True

        task2.poll.expect_call()
        task2.is_done.expect_call().and_return(True)
        task2.is_done.expect_call().and_return(True)
        task2.success = False
        task2.failure_tasks = [task3]

        self._dispatcher.add_agent.expect_call(IsAgentWithTask(task3))

        agent = self._create_agent([task1, task2])
        self._finish_agent(agent)
        self.god.check_playback()


    def _test_agent_abort_helper(self, ignore_abort=False):
        task1 = self._create_mock_task('task1')
        task2 = self._create_mock_task('task2')
        task1.poll.expect_call()
        task1.is_done.expect_call().and_return(False)
        task1.abort.expect_call()
        if ignore_abort:
            task1.aborted = False # task ignores abort; execution continues

            task1.poll.expect_call()
            task1.is_done.expect_call().and_return(True)
            task1.is_done.expect_call().and_return(True)
            task1.success = True

            task2.poll.expect_call()
            task2.is_done.expect_call().and_return(True)
            task2.is_done.expect_call().and_return(True)
            task2.success = True
        else:
            task1.aborted = True
            task2.abort.expect_call()
            task2.aborted = True

        agent = self._create_agent([task1, task2])
        agent.tick()
        agent.abort()
        self._finish_agent(agent)
        self.god.check_playback()


    def test_agent_abort(self):
        self._test_agent_abort_helper()
        self._test_agent_abort_helper(True)


    def _test_agent_abort_before_started_helper(self, ignore_abort=False):
        task = self._create_mock_task('task')
        task.abort.expect_call()
        if ignore_abort:
            task.aborted = False
            task.poll.expect_call()
            task.is_done.expect_call().and_return(True)
            task.is_done.expect_call().and_return(True)
            task.success = True
        else:
            task.aborted = True

        agent = self._create_agent([task])
        agent.abort()
        self._finish_agent(agent)
        self.god.check_playback()


    def test_agent_abort_before_started(self):
        self._test_agent_abort_before_started_helper()
        self._test_agent_abort_before_started_helper(True)


class AgentTasksTest(unittest.TestCase):
    TEMP_DIR = '/abspath/tempdir'
    RESULTS_DIR = '/results/dir'
    HOSTNAME = 'myhost'
    DUMMY_PROCESS = object()
    HOST_PROTECTION = host_protections.default
    PIDFILE_ID = object()
    JOB_OWNER = 'test_owner'
    JOB_NAME = 'test_job_name'
    JOB_AUTOSERV_PARAMS = set(['-u', JOB_OWNER, '-l', JOB_NAME])

    def setUp(self):
        self.god = mock.mock_god()
        self.god.stub_with(drone_manager.DroneManager, 'get_temporary_path',
                           mock.mock_function('get_temporary_path',
                                              default_return_val='tempdir'))
        self.god.stub_function(drone_manager.DroneManager,
                               'copy_results_on_drone')
        self.god.stub_function(drone_manager.DroneManager,
                               'copy_to_results_repository')
        self.god.stub_function(drone_manager.DroneManager,
                               'get_pidfile_id_from')

        def dummy_absolute_path(self, path):
            return '/abspath/' + path
        self.god.stub_with(drone_manager.DroneManager, 'absolute_path',
                           dummy_absolute_path)

        self.god.stub_class_method(monitor_db.PidfileRunMonitor, 'run')
        self.god.stub_class_method(monitor_db.PidfileRunMonitor, 'exit_code')
        self.god.stub_class_method(monitor_db.PidfileRunMonitor, 'get_process')
        def mock_has_process(unused):
            return True
        self.god.stub_with(monitor_db.PidfileRunMonitor, 'has_process',
                           mock_has_process)
        self.host = self.god.create_mock_class(monitor_db.Host, 'host')
        self.host.id = 1
        self.host.hostname = self.HOSTNAME
        self.host.protection = self.HOST_PROTECTION
        self.queue_entry = self.god.create_mock_class(
            monitor_db.HostQueueEntry, 'queue_entry')
        self.job = self.god.create_mock_class(monitor_db.Job, 'job')
        self.job.owner = self.JOB_OWNER
        self.job.name = self.JOB_NAME
        self.queue_entry.id = 1
        self.queue_entry.job = self.job
        self.queue_entry.host = self.host
        self.queue_entry.meta_host = None
        self._dispatcher = self.god.create_mock_class(monitor_db.Dispatcher,
                                                      'dispatcher')


    def tearDown(self):
        self.god.unstub_all()


    def run_task(self, task, success):
        """
        Do essentially what an Agent would do, but protect againt
        infinite looping from test errors.
        """
        if not getattr(task, 'agent', None):
            task.agent = object()
        count = 0
        while not task.is_done():
            count += 1
            if count > 10:
                print 'Task failed to finish'
                # in case the playback has clues to why it
                # failed
                self.god.check_playback()
                self.fail()
            task.poll()
        self.assertEquals(task.success, success)


    def setup_run_monitor(self, exit_status, copy_log_file=True):
        monitor_db.PidfileRunMonitor.run.expect_call(
            mock.is_instance_comparator(list),
            'tempdir',
            nice_level=monitor_db.AUTOSERV_NICE_LEVEL,
            log_file=mock.anything_comparator(),
            pidfile_name=monitor_db._AUTOSERV_PID_FILE,
            paired_with_pidfile=None)
        monitor_db.PidfileRunMonitor.exit_code.expect_call()
        monitor_db.PidfileRunMonitor.exit_code.expect_call().and_return(
            exit_status)

        if copy_log_file:
            self._setup_move_logfile()


    def _setup_move_logfile(self, copy_on_drone=False,
                            include_destination=False):
        monitor_db.PidfileRunMonitor.get_process.expect_call().and_return(
            self.DUMMY_PROCESS)
        if copy_on_drone:
            self.queue_entry.execution_tag.expect_call().and_return('tag')
            drone_manager.DroneManager.copy_results_on_drone.expect_call(
                self.DUMMY_PROCESS, source_path=mock.is_string_comparator(),
                destination_path=mock.is_string_comparator())
        elif include_destination:
            drone_manager.DroneManager.copy_to_results_repository.expect_call(
                self.DUMMY_PROCESS, mock.is_string_comparator(),
                destination_path=mock.is_string_comparator())
        else:
            drone_manager.DroneManager.copy_to_results_repository.expect_call(
                self.DUMMY_PROCESS, mock.is_string_comparator())


    def _test_repair_task_helper(self, success):
        self.host.set_status.expect_call('Repairing')
        if success:
            self.setup_run_monitor(0)
            self.host.set_status.expect_call('Ready')
        else:
            self.setup_run_monitor(1)
            self.host.set_status.expect_call('Repair Failed')

        task = monitor_db.RepairTask(self.host)
        self.assertEquals(task.failure_tasks, [])
        self.run_task(task, success)

        expected_protection = host_protections.Protection.get_string(
            host_protections.default)
        expected_protection = host_protections.Protection.get_attr_name(
            expected_protection)

        self.assertTrue(set(task.cmd) >=
                        set([monitor_db._autoserv_path, '-p', '-R', '-m',
                             self.HOSTNAME, '-r', self.TEMP_DIR,
                             '--host-protection', expected_protection]))
        self.god.check_playback()


    def test_repair_task(self):
        self._test_repair_task_helper(True)
        self._test_repair_task_helper(False)


    def _test_repair_task_with_queue_entry_helper(self, parse_failed_repair):
        self.god.stub_class(monitor_db, 'FinalReparseTask')
        self.god.stub_class(monitor_db, 'Agent')
        self.god.stub_class_method(monitor_db.TaskWithJobKeyvals,
                                   '_write_keyval_after_job')
        agent = DummyAgent()
        agent.dispatcher = self._dispatcher

        self.host.set_status.expect_call('Repairing')
        self.queue_entry.requeue.expect_call()
        self.setup_run_monitor(1)
        self.host.set_status.expect_call('Repair Failed')
        self.queue_entry.update_from_database.expect_call()
        self.queue_entry.set_execution_subdir.expect_call()
        monitor_db.TaskWithJobKeyvals._write_keyval_after_job.expect_call(
            'job_queued', mock.is_instance_comparator(int))
        monitor_db.TaskWithJobKeyvals._write_keyval_after_job.expect_call(
            'job_finished', mock.is_instance_comparator(int))
        self._setup_move_logfile(copy_on_drone=True)
        self.queue_entry.execution_tag.expect_call().and_return('tag')
        self._setup_move_logfile()
        self.job.parse_failed_repair = parse_failed_repair
        if parse_failed_repair:
            reparse_task = monitor_db.FinalReparseTask.expect_new(
                [self.queue_entry])
            reparse_agent = monitor_db.Agent.expect_new([reparse_task],
                                                        num_processes=0)
            self._dispatcher.add_agent.expect_call(reparse_agent)
        self.queue_entry.handle_host_failure.expect_call()

        task = monitor_db.RepairTask(self.host, self.queue_entry)
        task.agent = agent
        self.queue_entry.status = 'Queued'
        self.job.created_on = datetime.datetime(2009, 1, 1)
        self.run_task(task, False)
        self.assertTrue(set(task.cmd) >= self.JOB_AUTOSERV_PARAMS)
        self.god.check_playback()


    def test_repair_task_with_queue_entry(self):
        self._test_repair_task_with_queue_entry_helper(True)
        self._test_repair_task_with_queue_entry_helper(False)


    def setup_verify_expects(self, success, use_queue_entry):
        if use_queue_entry:
            self.queue_entry.set_status.expect_call('Verifying')
        self.host.set_status.expect_call('Verifying')
        if success:
            self.setup_run_monitor(0)
            self.host.set_status.expect_call('Ready')
        else:
            self.setup_run_monitor(1)
            if use_queue_entry and not self.queue_entry.meta_host:
                self.queue_entry.set_execution_subdir.expect_call()
                self.queue_entry.execution_tag.expect_call().and_return('tag')
                self._setup_move_logfile(include_destination=True)


    def _check_verify_failure_tasks(self, verify_task):
        self.assertEquals(len(verify_task.failure_tasks), 1)
        repair_task = verify_task.failure_tasks[0]
        self.assert_(isinstance(repair_task, monitor_db.RepairTask))
        self.assertEquals(verify_task.host, repair_task.host)
        if verify_task.queue_entry:
            self.assertEquals(repair_task.queue_entry_to_fail,
                              verify_task.queue_entry)
        else:
            self.assertEquals(repair_task.queue_entry_to_fail, None)


    def _test_verify_task_helper(self, success, use_queue_entry=False,
                                 use_meta_host=False):
        self.setup_verify_expects(success, use_queue_entry)

        if use_queue_entry:
            task = monitor_db.VerifyTask(queue_entry=self.queue_entry)
        else:
            task = monitor_db.VerifyTask(host=self.host)
        self._check_verify_failure_tasks(task)
        self.run_task(task, success)
        self.assertTrue(set(task.cmd) >=
                        set([monitor_db._autoserv_path, '-p', '-v', '-m',
                             self.HOSTNAME, '-r', self.TEMP_DIR]))
        if use_queue_entry:
            self.assertTrue(set(task.cmd) >= self.JOB_AUTOSERV_PARAMS)
        self.god.check_playback()


    def test_verify_task_with_host(self):
        self._test_verify_task_helper(True)
        self._test_verify_task_helper(False)


    def test_verify_task_with_queue_entry(self):
        self._test_verify_task_helper(True, use_queue_entry=True)
        self._test_verify_task_helper(False, use_queue_entry=True)


    def test_verify_task_with_metahost(self):
        self.queue_entry.meta_host = 1
        self.test_verify_task_with_queue_entry()


    def _setup_post_job_task_expects(self, autoserv_success, hqe_status,
                                     hqe_aborted=False):
        self.queue_entry.execution_tag.expect_call().and_return('tag')
        self.pidfile_monitor = monitor_db.PidfileRunMonitor.expect_new()
        self.pidfile_monitor.pidfile_id = self.PIDFILE_ID
        self.pidfile_monitor.attach_to_existing_process.expect_call('tag')
        if autoserv_success:
            code = 0
        else:
            code = 1
        self.queue_entry.update_from_database.expect_call()
        self.queue_entry.aborted = hqe_aborted
        if not hqe_aborted:
            self.pidfile_monitor.exit_code.expect_call().and_return(code)

        self.queue_entry.set_status.expect_call(hqe_status)


    def _setup_pre_parse_expects(self, autoserv_success):
        self._setup_post_job_task_expects(autoserv_success, 'Parsing')


    def _setup_post_parse_expects(self, autoserv_success):
        if autoserv_success:
            status = 'Completed'
        else:
            status = 'Failed'
        self.queue_entry.set_status.expect_call(status)


    def _setup_post_job_run_monitor(self, pidfile_name):
        self.pidfile_monitor.has_process.expect_call().and_return(True)
        autoserv_pidfile_id = object()
        self.monitor = monitor_db.PidfileRunMonitor.expect_new()
        self.monitor.run.expect_call(
            mock.is_instance_comparator(list),
            'tag',
            nice_level=monitor_db.AUTOSERV_NICE_LEVEL,
            log_file=mock.anything_comparator(),
            pidfile_name=pidfile_name,
            paired_with_pidfile=self.PIDFILE_ID)
        self.monitor.exit_code.expect_call()
        self.monitor.exit_code.expect_call().and_return(0)
        self._expect_copy_results()


    def _expect_copy_results(self, monitor=None, queue_entry=None):
        if monitor is None:
            monitor = self.monitor
        monitor.has_process.expect_call().and_return(True)
        if queue_entry:
            queue_entry.execution_tag.expect_call().and_return('tag')
        monitor.get_process.expect_call().and_return(self.DUMMY_PROCESS)
        drone_manager.DroneManager.copy_to_results_repository.expect_call(
                self.DUMMY_PROCESS, mock.is_string_comparator())


    def _test_final_reparse_task_helper(self, autoserv_success=True):
        self._setup_pre_parse_expects(autoserv_success)
        self._setup_post_job_run_monitor(monitor_db._PARSER_PID_FILE)
        self._setup_post_parse_expects(autoserv_success)

        task = monitor_db.FinalReparseTask([self.queue_entry])
        self.run_task(task, True)

        self.god.check_playback()
        cmd = [monitor_db._parser_path, '--write-pidfile', '-l', '2', '-r',
               '-o', '-P', '/abspath/tag']
        self.assertEquals(task.cmd, cmd)


    def test_final_reparse_task(self):
        self.god.stub_class(monitor_db, 'PidfileRunMonitor')
        self._test_final_reparse_task_helper()
        self._test_final_reparse_task_helper(autoserv_success=False)


    def test_final_reparse_throttling(self):
        self.god.stub_class(monitor_db, 'PidfileRunMonitor')
        self.god.stub_function(monitor_db.FinalReparseTask,
                               '_can_run_new_parse')

        self._setup_pre_parse_expects(True)
        monitor_db.FinalReparseTask._can_run_new_parse.expect_call().and_return(
            False)
        monitor_db.FinalReparseTask._can_run_new_parse.expect_call().and_return(
            True)
        self._setup_post_job_run_monitor(monitor_db._PARSER_PID_FILE)
        self._setup_post_parse_expects(True)

        task = monitor_db.FinalReparseTask([self.queue_entry])
        self.run_task(task, True)
        self.god.check_playback()


    def _setup_gather_logs_expects(self, autoserv_killed=True,
                                   hqe_aborted=False):
        self.god.stub_class(monitor_db, 'PidfileRunMonitor')
        self.god.stub_class(monitor_db, 'FinalReparseTask')
        self._setup_post_job_task_expects(not autoserv_killed, 'Gathering',
                                          hqe_aborted)
        if hqe_aborted:
            exit_code = None
        elif autoserv_killed:
            exit_code = 271
        else:
            exit_code = 0
        self.pidfile_monitor.exit_code.expect_call().and_return(exit_code)
        if exit_code != 0:
            self._setup_post_job_run_monitor('.collect_crashinfo_execute')
        self.pidfile_monitor.has_process.expect_call().and_return(True)
        self._expect_copy_results(monitor=self.pidfile_monitor,
                                  queue_entry=self.queue_entry)
        parse_task = monitor_db.FinalReparseTask.expect_new([self.queue_entry])
        _set_host_and_qe_ids(parse_task)
        self._dispatcher.add_agent.expect_call(IsAgentWithTask(parse_task))


    def _run_gather_logs_task(self):
        task = monitor_db.GatherLogsTask(self.job, [self.queue_entry])
        task.agent = DummyAgent()
        task.agent.dispatcher = self._dispatcher
        self.run_task(task, True)
        self.god.check_playback()


    def test_gather_logs_task(self):
        self._setup_gather_logs_expects()
        # no rebooting for this basic test
        self.job.reboot_after = models.RebootAfter.NEVER
        self.host.set_status.expect_call('Ready')

        self._run_gather_logs_task()


    def test_gather_logs_task_successful_autoserv(self):
        # When Autoserv exits successfully, no collect_crashinfo stage runs
        self._setup_gather_logs_expects(autoserv_killed=False)
        self.job.reboot_after = models.RebootAfter.NEVER
        self.host.set_status.expect_call('Ready')

        self._run_gather_logs_task()


    def _setup_gather_task_cleanup_expects(self):
        self.god.stub_class(monitor_db, 'CleanupTask')
        cleanup_task = monitor_db.CleanupTask.expect_new(host=self.host)
        _set_host_and_qe_ids(cleanup_task)
        self._dispatcher.add_agent.expect_call(IsAgentWithTask(cleanup_task))


    def test_gather_logs_reboot_hosts(self):
        self._setup_gather_logs_expects()
        self.job.reboot_after = models.RebootAfter.ALWAYS
        self._setup_gather_task_cleanup_expects()

        self._run_gather_logs_task()


    def test_gather_logs_reboot_on_abort(self):
        self._setup_gather_logs_expects(hqe_aborted=True)
        self.job.reboot_after = models.RebootAfter.NEVER
        self._setup_gather_task_cleanup_expects()

        self._run_gather_logs_task()


    def _test_cleanup_task_helper(self, success, use_queue_entry=False):
        if use_queue_entry:
            self.queue_entry.get_host.expect_call().and_return(self.host)
        self.host.set_status.expect_call('Cleaning')
        if success:
            self.setup_run_monitor(0)
            self.host.set_status.expect_call('Ready')
            self.host.update_field.expect_call('dirty', 0)
        else:
            self.setup_run_monitor(1)
            if use_queue_entry and not self.queue_entry.meta_host:
                self.queue_entry.set_execution_subdir.expect_call()
                self.queue_entry.execution_tag.expect_call().and_return('tag')
                self._setup_move_logfile(include_destination=True)

        if use_queue_entry:
            task = monitor_db.CleanupTask(queue_entry=self.queue_entry)
        else:
            task = monitor_db.CleanupTask(host=self.host)
        self.assertEquals(len(task.failure_tasks), 1)
        repair_task = task.failure_tasks[0]
        self.assert_(isinstance(repair_task, monitor_db.RepairTask))
        if use_queue_entry:
            self.assertEquals(repair_task.queue_entry_to_fail, self.queue_entry)

        self.run_task(task, success)

        self.god.check_playback()
        self.assert_(set(task.cmd) >=
                        set([monitor_db._autoserv_path, '-p', '--cleanup', '-m',
                             self.HOSTNAME, '-r', self.TEMP_DIR]))
        if use_queue_entry:
            self.assertTrue(set(task.cmd) >= self.JOB_AUTOSERV_PARAMS)

    def test_cleanup_task(self):
        self._test_cleanup_task_helper(True)
        self._test_cleanup_task_helper(False)


    def test_cleanup_task_with_queue_entry(self):
        self._test_cleanup_task_helper(False, True)


class HostTest(BaseSchedulerTest):
    def test_cmp_for_sort(self):
        expected_order = [
                'alice', 'Host1', 'host2', 'host3', 'host09', 'HOST010',
                'host10', 'host11', 'yolkfolk']
        hostname_idx = list(monitor_db.Host._fields).index('hostname')
        row = [None] * len(monitor_db.Host._fields)
        hosts = []
        for hostname in expected_order:
            row[hostname_idx] = hostname
            hosts.append(monitor_db.Host(row=row, new_record=True))

        host1 = hosts[expected_order.index('Host1')]
        host010 = hosts[expected_order.index('HOST010')]
        host10 = hosts[expected_order.index('host10')]
        host3 = hosts[expected_order.index('host3')]
        alice = hosts[expected_order.index('alice')]
        self.assertEqual(0, monitor_db.Host.cmp_for_sort(host10, host10))
        self.assertEqual(1, monitor_db.Host.cmp_for_sort(host10, host010))
        self.assertEqual(-1, monitor_db.Host.cmp_for_sort(host010, host10))
        self.assertEqual(-1, monitor_db.Host.cmp_for_sort(host1, host10))
        self.assertEqual(-1, monitor_db.Host.cmp_for_sort(host1, host010))
        self.assertEqual(-1, monitor_db.Host.cmp_for_sort(host3, host10))
        self.assertEqual(-1, monitor_db.Host.cmp_for_sort(host3, host010))
        self.assertEqual(1, monitor_db.Host.cmp_for_sort(host3, host1))
        self.assertEqual(-1, monitor_db.Host.cmp_for_sort(host1, host3))
        self.assertEqual(-1, monitor_db.Host.cmp_for_sort(alice, host3))
        self.assertEqual(1, monitor_db.Host.cmp_for_sort(host3, alice))
        self.assertEqual(0, monitor_db.Host.cmp_for_sort(alice, alice))

        hosts.sort(cmp=monitor_db.Host.cmp_for_sort)
        self.assertEqual(expected_order, [h.hostname for h in hosts])

        hosts.reverse()
        hosts.sort(cmp=monitor_db.Host.cmp_for_sort)
        self.assertEqual(expected_order, [h.hostname for h in hosts])


class HostQueueEntryTest(BaseSchedulerTest):
    def _create_hqe(self, dependency_labels=(), **create_job_kwargs):
        job = self._create_job(**create_job_kwargs)
        for label in dependency_labels:
            job.dependency_labels.add(label)
        hqes = list(monitor_db.HostQueueEntry.fetch(where='job_id=%d' % job.id))
        self.assertEqual(1, len(hqes))
        return hqes[0]

    def _check_hqe_labels(self, hqe, expected_labels):
        expected_labels = set(expected_labels)
        label_names = set(label.name for label in hqe.get_labels())
        self.assertEqual(expected_labels, label_names)

    def test_get_labels_empty(self):
        hqe = self._create_hqe(hosts=[1])
        labels = list(hqe.get_labels())
        self.assertEqual([], labels)

    def test_get_labels_metahost(self):
        hqe = self._create_hqe(metahosts=[2])
        self._check_hqe_labels(hqe, ['label2'])

    def test_get_labels_dependancies(self):
        hqe = self._create_hqe(dependency_labels=(self.label3, self.label4),
                               metahosts=[1])
        self._check_hqe_labels(hqe, ['label1', 'label3', 'label4'])


class JobTest(BaseSchedulerTest):
    def setUp(self):
        super(JobTest, self).setUp()
        self.god.stub_with(
            drone_manager.DroneManager, 'attach_file_to_execution',
            mock.mock_function('attach_file_to_execution',
                               default_return_val='/test/path/tmp/foo'))


    def _setup_directory_expects(self, execution_subdir):
        # XXX(gps): um... this function does -nothing-
        job_path = os.path.join('.', '1-my_user')
        results_dir = os.path.join(job_path, execution_subdir)


    def _test_run_helper(self, expect_agent=True, expect_starting=False,
                         expect_pending=False):
        if expect_starting:
            expected_status = models.HostQueueEntry.Status.STARTING
        elif expect_pending:
            expected_status = models.HostQueueEntry.Status.PENDING
        else:
            expected_status = models.HostQueueEntry.Status.VERIFYING
        job = monitor_db.Job.fetch('id = 1').next()
        queue_entry = monitor_db.HostQueueEntry.fetch('id = 1').next()
        agent = job.run(queue_entry)

        self.god.check_playback()
        self.assertEquals(models.HostQueueEntry.smart_get(1).status,
                          expected_status)

        if not expect_agent:
            self.assertEquals(agent, None)
            return

        self.assert_(isinstance(agent, monitor_db.Agent))
        tasks = list(agent.queue.queue)
        return tasks


    def _check_verify_task(self, verify_task):
        self.assert_(isinstance(verify_task, monitor_db.VerifyTask))
        self.assertEquals(verify_task.queue_entry.id, 1)


    def _check_pending_task(self, pending_task):
        self.assert_(isinstance(pending_task, monitor_db.SetEntryPendingTask))
        self.assertEquals(pending_task._queue_entry.id, 1)


    def test_run_asynchronous(self):
        self._create_job(hosts=[1, 2])

        tasks = self._test_run_helper()

        self.assertEquals(len(tasks), 2)
        verify_task, pending_task = tasks
        self._check_verify_task(verify_task)
        self._check_pending_task(pending_task)


    def test_run_asynchronous_skip_verify(self):
        job = self._create_job(hosts=[1, 2])
        job.run_verify = False
        job.save()
        self._setup_directory_expects('host1')

        tasks = self._test_run_helper()

        self.assertEquals(len(tasks), 1)
        pending_task = tasks[0]
        self._check_pending_task(pending_task)


    def test_run_synchronous_verify(self):
        self._create_job(hosts=[1, 2], synchronous=True)

        tasks = self._test_run_helper()
        self.assertEquals(len(tasks), 2)
        verify_task, pending_task = tasks
        self._check_verify_task(verify_task)
        self._check_pending_task(pending_task)


    def test_run_synchronous_skip_verify(self):
        job = self._create_job(hosts=[1, 2], synchronous=True)
        job.run_verify = False
        job.save()

        tasks = self._test_run_helper()
        self.assertEquals(len(tasks), 1)
        self._check_pending_task(tasks[0])


    def test_run_synchronous_ready(self):
        self._create_job(hosts=[1, 2], synchronous=True)
        self._update_hqe("status='Pending', execution_subdir=''")
        self._setup_directory_expects('group0')

        tasks = self._test_run_helper(expect_starting=True)
        self.assertEquals(len(tasks), 1)
        queue_task = tasks[0]

        self.assert_(isinstance(queue_task, monitor_db.QueueTask))
        self.assertEquals(queue_task.job.id, 1)
        hqe_ids = [hqe.id for hqe in queue_task.queue_entries]
        self.assertEquals(hqe_ids, [1, 2])


    def test_run_synchronous_atomic_group_ready(self):
        self._create_job(hosts=[5, 6], atomic_group=1, synchronous=True)
        self._update_hqe("status='Pending', execution_subdir=''")

        tasks = self._test_run_helper(expect_starting=True)
        self.assertEquals(len(tasks), 1)
        queue_task = tasks[0]

        self.assert_(isinstance(queue_task, monitor_db.QueueTask))
        # Atomic group jobs that do not a specific label in the atomic group
        # will use the atomic group name as their group name.
        self.assertEquals(queue_task.group_name, 'atomic1')


    def test_run_synchronous_atomic_group_with_label_ready(self):
        job = self._create_job(hosts=[5, 6], atomic_group=1, synchronous=True)
        job.dependency_labels.add(self.label4)
        self._update_hqe("status='Pending', execution_subdir=''")

        tasks = self._test_run_helper(expect_starting=True)
        self.assertEquals(len(tasks), 1)
        queue_task = tasks[0]

        self.assert_(isinstance(queue_task, monitor_db.QueueTask))
        # Atomic group jobs that also specify a label in the atomic group
        # will use the label name as their group name.
        self.assertEquals(queue_task.group_name, 'label4')


    def test_reboot_before_always(self):
        job = self._create_job(hosts=[1])
        job.reboot_before = models.RebootBefore.ALWAYS
        job.save()

        tasks = self._test_run_helper()
        self.assertEquals(len(tasks), 3)
        cleanup_task = tasks[0]
        self.assert_(isinstance(cleanup_task, monitor_db.CleanupTask))
        self.assertEquals(cleanup_task.host.id, 1)


    def _test_reboot_before_if_dirty_helper(self, expect_reboot):
        job = self._create_job(hosts=[1])
        job.reboot_before = models.RebootBefore.IF_DIRTY
        job.save()

        tasks = self._test_run_helper()
        self.assertEquals(len(tasks), expect_reboot and 3 or 2)
        if expect_reboot:
            cleanup_task = tasks[0]
            self.assert_(isinstance(cleanup_task, monitor_db.CleanupTask))
            self.assertEquals(cleanup_task.host.id, 1)

    def test_reboot_before_if_dirty(self):
        models.Host.smart_get(1).update_object(dirty=True)
        self._test_reboot_before_if_dirty_helper(True)


    def test_reboot_before_not_dirty(self):
        models.Host.smart_get(1).update_object(dirty=False)
        self._test_reboot_before_if_dirty_helper(False)


    def test_next_group_name(self):
        django_job = self._create_job(metahosts=[1])
        job = monitor_db.Job(id=django_job.id)
        self.assertEqual('group0', job._next_group_name())

        for hqe in django_job.hostqueueentry_set.filter():
            hqe.execution_subdir = 'my_rack.group0'
            hqe.save()
        self.assertEqual('my_rack.group1', job._next_group_name('my/rack'))


class TopLevelFunctionsTest(unittest.TestCase):
    def test_autoserv_command_line(self):
        machines = 'abcd12,efgh34'
        results_dir = '/fake/path'
        extra_args = ['-Z', 'hello']
        expected_command_line = [monitor_db._autoserv_path, '-p',
                                 '-m', machines, '-r', results_dir]

        command_line = monitor_db._autoserv_command_line(
                machines, results_dir, extra_args)
        self.assertEqual(expected_command_line + extra_args, command_line)

        class FakeJob(object):
            owner = 'Bob'
            name = 'fake job name'

        command_line = monitor_db._autoserv_command_line(
                machines, results_dir, extra_args=[], job=FakeJob())
        self.assertEqual(expected_command_line +
                         ['-u', FakeJob.owner, '-l', FakeJob.name],
                         command_line)


if __name__ == '__main__':
    unittest.main()