2ce5198bbf146b321f830b46855951c64aa509a1
[build-farm.git] / buildfarm / tests / test_data.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 import os
19 import tempfile
20 import testtools
21 import time
22 import unittest
23
24 from buildfarm import data
25
26 from buildfarm.tests import BuildFarmTestCase
27
28
29 class NonexistantTests(unittest.TestCase):
30
31     def test_nonexistant(self):
32         self.assertRaises(
33             Exception, data.BuildResultStore, "somedirthatdoesn'texist", None)
34
35
36 class ReadTreesFromConfTests(testtools.TestCase):
37
38     def create_file(self, contents):
39         (fd, path) = tempfile.mkstemp()
40         f = os.fdopen(fd, 'w')
41         self.addCleanup(os.remove, path)
42         try:
43             f.write(contents)
44         finally:
45             f.close()
46         return path
47
48     def test_read_trees_from_conf_ko(self):
49         name = self.create_file("""
50 [foo]
51 param1 = fooval1
52 param2 = fooval2
53 param3 = fooval3
54
55 [bar]
56 param1 = barval1
57 param2 = barval2
58 param3 = barval3
59 """)
60         self.assertRaises(
61             Exception, data.read_trees_from_conf, name, None)
62
63     def test_read_trees_from_conf(self):
64         name = self.create_file("""
65 [pidl]
66 scm = git
67 repo = samba.git
68 branch = master
69 subdir = pidl/
70
71 [rsync]
72 scm = git
73 repo = rsync.git
74 branch = HEAD
75 """)
76         t = data.read_trees_from_conf(name)
77         self.assertEquals(
78             t["pidl"].scm,
79             "git")
80
81
82 class BuildResultStoreTests(BuildFarmTestCase):
83
84     def setUp(self):
85         super(BuildResultStoreTests, self).setUp()
86
87         self.write_compilers(["cc"])
88         self.write_hosts(["gwenhwyvar", "charis"])
89         self.write_trees({"tdb": {"scm": "git", "repo": "tdb", "branch": "master"}})
90
91         self.x = data.BuildResultStore(self.path)
92
93     def test_build_fname(self):
94         self.assertEquals(
95             self.x.build_fname("mytree", "myhost", "cc"),
96             "%s/data/upload/build.mytree.myhost.cc" % self.path)
97         self.assertEquals(
98             self.x.build_fname("mytree", "myhost", "cc", 123),
99             "%s/data/oldrevs/build.mytree.myhost.cc-123" % self.path)
100
101     def test_cache_fname(self):
102         self.assertEquals(
103             self.x.cache_fname("mytree", "myhost", "cc", 123),
104             "%s/cache/build.mytree.myhost.cc-123" % self.path)
105         self.assertEquals(
106             self.x.cache_fname("mytree", "myhost", "cc"),
107             "%s/cache/build.mytree.myhost.cc" % self.path)
108
109     def test_build_age_mtime(self):
110         path = self.create_mock_logfile("tdb", "charis", "cc")
111         # Set mtime to something in the past
112         os.utime(path, (time.time(), time.time() - 990))
113         build = self.x.get_build("tdb", "charis", "cc")
114         age = build.age_mtime()
115         self.assertTrue(age >= 990 and age <= 1000, "age was %d" % age)
116
117     def test_get_build_nonexistant(self):
118         self.assertRaises(data.NoSuchBuildError, self.x.get_build, "tdb",
119             "charis", "cc")
120
121     def test_build_age_ctime(self):
122         path = self.create_mock_logfile("tdb", "charis", "cc")
123         # Set mtime to something in the past
124         build = self.x.get_build("tdb", "charis", "cc")
125         age = build.age_ctime()
126         self.assertTrue(age >= 0 and age <= 10, "age was %d" % age)
127
128     def test_read_log(self):
129         path = self.create_mock_logfile("tdb", "charis", "cc",
130             contents="This is what a log file looks like.")
131         build = self.x.get_build("tdb", "charis", "cc")
132         self.assertEquals("This is what a log file looks like.", build.read_log())
133
134     def test_read_err(self):
135         self.create_mock_logfile("tdb", "charis", "cc")
136         path = self.create_mock_logfile("tdb", "charis", "cc",
137             kind="stderr",
138             contents="This is what an stderr file looks like.")
139         build = self.x.get_build("tdb", "charis", "cc")
140         self.assertEquals("This is what an stderr file looks like.", build.read_err())
141
142     def test_revision_details(self):
143         self.create_mock_logfile("tdb", "charis", "cc", contents="""
144 BUILD COMMIT REVISION: 43
145 bla
146 BUILD REVISION: 42
147 BUILD COMMIT TIME: 3 August 2010
148 """)
149         build = self.x.get_build("tdb", "charis", "cc")
150         self.assertEquals(("42", "3 August 2010"), build.revision_details())
151
152     def test_revision_details_no_timestamp(self):
153         self.create_mock_logfile("tdb", "charis", "cc", contents="""
154 BUILD COMMIT REVISION: 43
155 BUILD REVISION: 42
156 BLA
157 """)
158         build = self.x.get_build("tdb", "charis", "cc")
159         self.assertEquals(("42", None), build.revision_details())
160
161     def test_err_count(self):
162         self.create_mock_logfile("tdb", "charis", "cc")
163         self.create_mock_logfile("tdb", "charis", "cc", kind="stderr", contents="""error1
164 error2
165 error3""")
166         build = self.x.get_build("tdb", "charis", "cc")
167         self.assertEquals(3, build.err_count())
168
169     def test_has_host(self):
170         self.assertFalse(self.x.has_host("charis"))
171         self.create_mock_logfile("tdb", "charis", "cc")
172         self.assertTrue(self.x.has_host("charis"))
173
174
175
176 class LogParserTests(unittest.TestCase):
177
178     def test_nothing(self):
179         self.assertEquals(((None, None, None, None, None), set()),
180             data.build_status_from_logs("", ""))
181
182     def test_disk_full(self):
183         self.assertEquals(((None, None, None, None, None), set(["disk full"])),
184             data.build_status_from_logs("foo\nbar\nNo space left on device\nla\n",
185                 ""))
186         self.assertEquals(((None, None, None, None, None), set(["disk full"])),
187             data.build_status_from_logs(
188                 "", "foo\nbar\nNo space left on device\nla\n"))
189
190     def test_timeout(self):
191         self.assertEquals(((None, None, None, None, None), set(["timeout"])),
192             data.build_status_from_logs("foo\nbar\nmaximum runtime exceeded\nla\n",
193                 ""))
194