4 import logging
, unittest
5 from autotest_lib
.frontend
import setup_django_environment
6 from autotest_lib
.database
import database_connection
7 from autotest_lib
.frontend
.afe
import frontend_test_utils
, models
8 from autotest_lib
.scheduler
import monitor_db_cleanup
, scheduler_config
9 from autotest_lib
.client
.common_lib
import host_protections
11 class UserCleanupTest(unittest
.TestCase
, frontend_test_utils
.FrontendTestMixin
):
13 logging
.basicConfig(level
=logging
.DEBUG
)
14 self
._frontend
_common
_setup
()
16 database_connection
.DatabaseConnection
.get_test_database())
17 self
._database
.connect(db_type
='django')
18 self
.cleanup
= monitor_db_cleanup
.UserCleanup(self
._database
, 1)
22 self
._frontend
_common
_teardown
()
25 def test_reverify_dead_hosts(self
):
26 # unlimited reverifies
27 self
.god
.stub_with(scheduler_config
.config
,
28 'reverify_max_hosts_at_once', 0)
30 self
.hosts
[i
].status
= models
.Host
.Status
.REPAIR_FAILED
33 self
.hosts
[1].locked
= True
36 self
.hosts
[2].protection
= host_protections
.Protection
.DO_NOT_VERIFY
39 self
.god
.stub_with(self
.cleanup
, '_should_reverify_hosts_now',
41 self
.cleanup
._reverify
_dead
_hosts
()
43 tasks
= models
.SpecialTask
.objects
.all()
44 self
.assertEquals(len(tasks
), 1)
45 self
.assertEquals(tasks
[0].host
.id, 1)
46 self
.assertEquals(tasks
[0].task
, models
.SpecialTask
.Task
.VERIFY
)
49 def test_reverify_dead_hosts_limits(self
):
50 # limit the number of reverifies
51 self
.assertTrue(hasattr(scheduler_config
.config
,
52 'reverify_max_hosts_at_once'))
53 self
.god
.stub_with(scheduler_config
.config
,
54 'reverify_max_hosts_at_once', 2)
55 for i
in (0, 1, 2, 3, 4, 5):
56 self
.hosts
[i
].status
= models
.Host
.Status
.REPAIR_FAILED
59 self
.hosts
[1].locked
= True
62 self
.hosts
[2].protection
= host_protections
.Protection
.DO_NOT_VERIFY
65 self
.god
.stub_with(self
.cleanup
, '_should_reverify_hosts_now',
67 self
.cleanup
._reverify
_dead
_hosts
()
69 tasks
= models
.SpecialTask
.objects
.all()
70 # four hosts need reverifying but our max limit was set to 2
71 self
.assertEquals(len(tasks
), 2)
72 self
.assertTrue(tasks
[0].host
.id in (1, 4, 5, 6))
73 self
.assertTrue(tasks
[1].host
.id in (1, 4, 5, 6))
74 self
.assertEquals(tasks
[0].task
, models
.SpecialTask
.Task
.VERIFY
)
75 self
.assertEquals(tasks
[1].task
, models
.SpecialTask
.Task
.VERIFY
)
78 if __name__
== '__main__':