Add tests for new min_age argument.
[build-farm.git] / buildfarm / tests / test_buildfarm.py
1 #!/usr/bin/python
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2010
3 #
4 #   This program is free software; you can redistribute it and/or modify
5 #   it under the terms of the GNU General Public License as published by
6 #   the Free Software Foundation; either version 3 of the License, or
7 #   (at your option) any later version.
8 #
9 #   This program is distributed in the hope that it will be useful,
10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #   GNU General Public License for more details.
13 #
14 #   You should have received a copy of the GNU General Public License
15 #   along with this program; if not, write to the Free Software
16 #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 from buildfarm import (
19     BuildFarm,
20     read_trees_from_conf,
21     )
22 from buildfarm.build import NoSuchBuildError
23 from buildfarm.tests import BuildFarmTestCase
24
25 import os
26 from testtools import TestCase
27 import tempfile
28
29
30 class ReadTreesFromConfTests(TestCase):
31
32     def create_file(self, contents):
33         (fd, path) = tempfile.mkstemp()
34         f = os.fdopen(fd, 'w')
35         self.addCleanup(os.remove, path)
36         try:
37             f.write(contents)
38         finally:
39             f.close()
40         return path
41
42     def test_read_trees_from_conf_ko(self):
43         name = self.create_file("""
44 [foo]
45 param1 = fooval1
46 param2 = fooval2
47 param3 = fooval3
48
49 [bar]
50 param1 = barval1
51 param2 = barval2
52 param3 = barval3
53 """)
54         self.assertRaises(
55             Exception, read_trees_from_conf, name, None)
56
57     def test_read_trees_from_conf(self):
58         name = self.create_file("""
59 [pidl]
60 scm = git
61 repo = samba.git
62 branch = master
63 subdir = pidl/
64
65 [rsync]
66 scm = git
67 repo = rsync.git
68 branch = HEAD
69 """)
70         t = read_trees_from_conf(name)
71         self.assertEquals(t["pidl"].scm, "git")
72
73
74 class BuildFarmTests(BuildFarmTestCase):
75
76     def setUp(self):
77         super(BuildFarmTests, self).setUp()
78         self.buildfarm = BuildFarm(self.path)
79         self.write_compilers(["cc"])
80         self.write_hosts({"myhost": "Fedora",
81                           "charis": "Debian"})
82         self.write_trees({"trivial": {"scm": "git", "repo": "git://foo", "branch": "master"},
83                           "other": {"scm": "git", "repo": "other.git", "branch": "HEAD"}})
84         self.buildfarm.commit()
85         self.x = BuildFarm(self.path)
86
87     def test_get_new_builds_empty(self):
88         self.assertEquals([], list(self.x.get_new_builds()))
89
90     def test_get_last_builds_empty(self):
91         self.assertEquals([], list(self.x.get_last_builds()))
92
93     def test_get_tree_builds_empty(self):
94         self.assertEquals([], list(self.x.get_tree_builds("trival")))
95
96     def test_get_tree_builds(self):
97         path = self.upload_mock_logfile(self.x.builds, "tdb", "myhost", "gcc",
98             stdout_contents="BUILD COMMIT REVISION: 12\n", mtime=1200)
99         path = self.upload_mock_logfile(self.x.builds, "tdb", "myhost", "cc",
100             stdout_contents="BUILD COMMIT REVISION: 13\n", mtime=1300)
101         path = self.upload_mock_logfile(self.x.builds, "tdb", "myhost", "cc",
102             stdout_contents="BUILD COMMIT REVISION: 42\n", mtime=4200)
103         builds = list(self.x.get_tree_builds("tdb"))
104         self.assertEquals(["42", "12"], [x.revision for x in builds])
105
106     def test_get_last_builds(self):
107         path = self.upload_mock_logfile(self.x.builds, "other", "myhost", "cc",
108             "BUILD COMMIT REVISION: 12\n", mtime=1200)
109         path = self.upload_mock_logfile(self.x.builds, "trivial", "myhost", "cc",
110             "BUILD COMMIT REVISION: 13\n", mtime=1300)
111         path = self.upload_mock_logfile(self.x.builds, "trivial", "myhost", "cc",
112             "BUILD COMMIT REVISION: 42\n", mtime=4200)
113         builds = list(self.x.get_last_builds())
114         self.assertEquals(2, len(builds))
115         self.assertEquals(4200, builds[0].upload_time)
116         self.assertEquals("42", builds[0].revision_details())
117         self.assertEquals("trivial", builds[0].tree)
118         self.assertEquals(1200, builds[1].upload_time)
119         self.assertEquals("12", builds[1].revision_details())
120         self.assertEquals("other", builds[1].tree)
121
122     def test_get_summary_builds_empty(self):
123         self.assertEquals([], list(self.x.get_summary_builds()))
124
125     def test_get_summary_builds(self):
126         path = self.upload_mock_logfile(self.x.builds, "other", "myhost", "cc",
127             "BUILD COMMIT REVISION: 12\n", mtime=1200)
128         path = self.upload_mock_logfile(self.x.builds, "trivial", "myhost", "cc",
129             "BUILD COMMIT REVISION: 13\n", mtime=1300)
130         path = self.upload_mock_logfile(self.x.builds, "trivial", "myhost", "cc",
131             "BUILD COMMIT REVISION: 42\n", mtime=4200)
132         builds = list(self.x.get_summary_builds())
133         self.assertEquals(2, len(builds))
134         self.assertEquals(4200, builds[0].upload_time)
135         self.assertEquals("42", builds[0].revision_details())
136         self.assertEquals("trivial", builds[0].tree)
137         self.assertEquals(1200, builds[1].upload_time)
138         self.assertEquals("12", builds[1].revision_details())
139         self.assertEquals("other", builds[1].tree)
140         builds = list(self.x.get_summary_builds(min_age=4000))
141         self.assertEquals(1, len(builds))
142         builds = list(self.x.get_summary_builds(min_age=5000))
143         self.assertEquals(0, len(builds))
144
145     def test_get_host_builds_empty(self):
146         self.assertEquals([], list(self.x.get_host_builds("myhost")))
147
148     def test_lcov_status_none(self):
149         self.assertRaises(NoSuchBuildError, self.x.lcov_status, "trivial")
150
151     def test_tree(self):
152         self.assertEquals("trivial", self.x.trees["trivial"].name)
153         tree = self.x.trees["trivial"]
154         self.assertEquals("git", tree.scm)
155         self.assertEquals("git://foo", tree.repo)
156         self.assertEquals("master", tree.branch)
157
158     def test_get_build_rev(self):
159         path = self.upload_mock_logfile(self.x.builds, "tdb", "charis", "cc",
160             stdout_contents="tHIS is what a log file looks like.\n"
161             "BUILD COMMIT REVISION: 12\n")
162         build = self.x.get_build("tdb", "charis", "cc", "12")
163         self.assertEquals("tdb", build.tree)
164         self.assertEquals("charis", build.host)
165         self.assertEquals("cc", build.compiler)
166         self.assertEquals("12", build.revision)
167
168     def test_get_build_no_rev(self):
169         path = self.create_mock_logfile("tdb", "charis", "cc",
170             contents="This is what a log file looks like.")
171         build = self.x.get_build("tdb", "charis", "cc")
172         self.assertEquals("tdb", build.tree)
173         self.assertEquals("charis", build.host)
174         self.assertEquals("cc", build.compiler)
175         self.assertIs(None, build.revision)
176