Release 0.4.2
[rebuildd/rebuildd.git] / tests / TestJob.py
1 #!/usr/bin/python
2 import sys  
3   
4 sys.path.insert(0, "..")  
5 sys.path.insert(0, ".")  
6 from RebuilddTestSetup import rebuildd_global_test_setup
7 import unittest, types, os
8 import sqlobject
9 from rebuildd.RebuilddConfig import RebuilddConfig
10 from rebuildd.Package import Package
11 from rebuildd.Job import Job
12 from rebuildd.JobStatus import JobStatus
13
14 class TestJob(unittest.TestCase):
15
16     def setUp(self):
17         rebuildd_global_test_setup()
18         self.job = Job(package=Package(name="bash", version="3.1dfsg-8"), arch="alpha", dist="sid")
19
20     def test_init(self):
21         self.assert_(type(self.job) is Job)
22
23     def test_setattr(self):
24         self.job.status = JobStatus.UNKNOWN
25         self.assert_(self.job.status == JobStatus.UNKNOWN)
26         self.job.status = JobStatus.WAIT
27         self.assert_(self.job.status == JobStatus.WAIT)
28
29     def test_open_logfile(self):
30         file = open(self.job.logfile, "w")
31         self.assert_(file is not None)
32         filero = open(self.job.logfile, "r")
33         self.assert_(filero is not None)
34         file.close()
35         filero.close()
36         os.unlink(file.name)
37
38     def test_status_on_doquit(self):
39         self.job.do_quit.set()
40         self.job.start()
41         self.job.join()
42         self.assert_(self.job.status == JobStatus.WAIT_LOCKED)
43
44     def test_build_success(self):
45         self.job.do_quit.clear()
46         RebuilddConfig().set('build', 'source_cmd', '/bin/true')
47         RebuilddConfig().set('build', 'build_cmd', '/bin/true')
48         RebuilddConfig().set('build', 'post_build_cmd', '/bin/true')
49         self.job.start()
50         self.job.join()
51         self.assert_(self.job.status == JobStatus.BUILD_OK)
52
53     def test_build_failure_source(self):
54         self.job.do_quit.clear()
55         RebuilddConfig().set('build', 'source_cmd', '/bin/false')
56         RebuilddConfig().set('build', 'build_cmd', '/bin/true')
57         RebuilddConfig().set('build', 'post_build_cmd', '/bin/true')
58         self.job.start()
59         self.job.join()
60         self.assert_(self.job.status == JobStatus.SOURCE_FAILED)
61
62     def test_build_failure_build(self):
63         self.job.do_quit.clear()
64         RebuilddConfig().set('build', 'source_cmd', '/bin/true %s %s %s')
65         RebuilddConfig().set('build', 'build_cmd', '/bin/false %s %s %s %s')
66         RebuilddConfig().set('build', 'post_build_cmd', '/bin/true %s %s %s %s')
67         self.job.start()
68         self.job.join()
69         self.assert_(self.job.status == JobStatus.BUILD_FAILED)
70
71     def test_build_failure_post_build(self):
72         RebuilddConfig().set('build', 'source_cmd', '/bin/true %s %s %s')
73         RebuilddConfig().set('build', 'build_cmd', '/bin/true %s %s %s %s')
74         RebuilddConfig().set('build', 'post_build_cmd', '/bin/false %s %s %s %s')
75         self.job.start()
76         self.job.join()
77         self.assert_(self.job.status == JobStatus.POST_BUILD_FAILED)
78
79     def test_send_build_log(self):
80         self.assert_(self.job.send_build_log() is False)
81         self.job.status = JobStatus.BUILD_OK
82         self.assert_(self.job.send_build_log() is True)
83         self.assert_(self.job.status is JobStatus.BUILD_OK)
84         self.job.status = JobStatus.BUILD_FAILED
85         self.assert_(self.job.send_build_log() is True)
86         self.assert_(self.job.status is JobStatus.BUILD_FAILED)
87
88 if __name__ == '__main__':
89     suite = unittest.TestLoader().loadTestsFromTestCase(TestJob)
90     unittest.TextTestRunner(verbosity=2).run(suite)