2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2010
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.
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.
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.
18 from buildfarm.build import Build
19 from buildfarm.sqldb import setup_schema
21 from storm import database
22 from storm.store import Store
23 from testtools import TestCase
29 class BuildFarmTestCase(TestCase):
30 """Test case class that provides a build farm data directory and convenience methods.
33 def upload_mock_logfile(self, store, tree, host, compiler,
34 stdout_contents="", stderr_contents=None, mtime=None):
35 log_path = self.create_mock_logfile(tree, host, compiler, contents=stdout_contents, mtime=mtime)
36 if stderr_contents is not None:
37 err_path = self.create_mock_logfile(tree, host, compiler, kind="stderr", contents=stderr_contents, mtime=mtime)
38 build = Build(log_path[:-4], tree, host, compiler)
39 store.upload_build(build)
42 def create_mock_logfile(self, tree, host, compiler, rev=None,
43 kind="stdout", contents="FOO", mtime=None):
44 basename = "build.%s.%s.%s" % (tree, host, compiler)
46 basename += "-%s" % rev
47 path = os.path.join(self.path, "data", "oldrevs", basename)
49 path = os.path.join(self.path, "data", "upload", basename)
52 elif kind == "stderr":
55 raise ValueError("Unknown log kind %r" % kind)
62 os.utime(path, (mtime, mtime))
65 def write_compilers(self, compilers):
66 f = open(os.path.join(self.path, "web", "compilers.list"), "w")
68 for compiler in compilers:
69 f.write("%s\n" % compiler)
73 def write_hosts(self, hosts):
75 self.buildfarm.hostdb.createhost(host)
77 def write_trees(self, trees):
78 f = open(os.path.join(self.path, "web", "trees.conf"), "w")
82 for k, v in trees[t].iteritems():
83 f.write("%s = %s\n" % (k, v))
89 super(BuildFarmTestCase, self).setUp()
90 self.path = tempfile.mkdtemp()
92 for subdir in ["data", "data/upload", "data/oldrevs", "db", "web", "lcov", "lcov/data"]:
93 os.mkdir(os.path.join(self.path, subdir))
95 self.db_url = "sqlite:"+os.path.join(self.path, "db", "hostdb.sqlite")
96 db = database.create_database(self.db_url)
100 self.write_compilers([])
104 shutil.rmtree(self.path)
105 super(BuildFarmTestCase, self).tearDown()
117 module_names = ['buildfarm.tests.' + name for name in names]
118 result = unittest.TestSuite()
119 loader = unittest.TestLoader()
120 suite = loader.loadTestsFromNames(module_names)
121 result.addTests(suite)