bc66ab1b726935bc50c2e46c1e3d9e5474cee563
[build-farm.git] / buildfarm / tests / __init__.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.build import Build
19 from buildfarm.sqldb import setup_schema
20 import os
21 from storm import database
22 from storm.store import Store
23 from testtools import TestCase
24 import shutil
25 import tempfile
26 import unittest
27
28
29 class BuildFarmTestCase(TestCase):
30     """Test case class that provides a build farm data directory and convenience methods.
31     """
32
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)
40         return log_path
41
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)
45         if rev is not None:
46             basename += "-%s" % rev
47             path = os.path.join(self.path, "data", "oldrevs", basename)
48         else:
49             path = os.path.join(self.path, "data", "upload", basename)
50         if kind == "stdout":
51             path += ".log"
52         elif kind == "stderr":
53             path += ".err"
54         else:
55             raise ValueError("Unknown log kind %r" % kind)
56         f = open(path, 'w+')
57         try:
58             f.write(contents)
59         finally:
60             f.close()
61         if mtime is not None:
62             os.utime(path, (mtime, mtime))
63         return path
64
65     def write_compilers(self, compilers):
66         f = open(os.path.join(self.path, "web", "compilers.list"), "w")
67         try:
68             for compiler in compilers:
69                 f.write("%s\n" % compiler)
70         finally:
71             f.close()
72
73     def write_hosts(self, hosts):
74         for host in hosts:
75             self.buildfarm.hostdb.createhost(host)
76
77     def write_trees(self, trees):
78         f = open(os.path.join(self.path, "web", "trees.conf"), "w")
79         try:
80             for t in trees:
81                 f.write("[%s]\n" % t)
82                 for k, v in trees[t].iteritems():
83                     f.write("%s = %s\n" % (k, v))
84                 f.write("\n")
85         finally:
86             f.close()
87
88     def setUp(self):
89         super(BuildFarmTestCase, self).setUp()
90         self.path = tempfile.mkdtemp()
91
92         for subdir in ["data", "data/upload", "data/oldrevs", "db", "web", "lcov", "lcov/data"]:
93             os.mkdir(os.path.join(self.path, subdir))
94
95         self.db_url = "sqlite:"+os.path.join(self.path, "db", "hostdb.sqlite")
96         db = database.create_database(self.db_url)
97         store = Store(db)
98         setup_schema(store)
99         store.commit()
100         self.write_compilers([])
101         self.write_hosts({})
102
103     def tearDown(self):
104         shutil.rmtree(self.path)
105         super(BuildFarmTestCase, self).tearDown()
106
107
108 def test_suite():
109     names = [
110         '__init__',
111         'test_build',
112         'test_history',
113         'test_hostdb',
114         'test_sqldb',
115         'test_util',
116         ]
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)
122     return result