use storm.
[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 import setup_db
19 import os
20 from storm import database
21 from storm.store import Store
22 from testtools import TestCase
23 import shutil
24 import tempfile
25
26
27 class BuildFarmTestCase(TestCase):
28     """Test case class that provides a build farm data directory and convenience methods.
29     """
30
31     def create_mock_logfile(self, tree, host, compiler, rev=None,
32             kind="stdout", contents="FOO"):
33         basename = "build.%s.%s.%s" % (tree, host, compiler)
34         if rev is not None:
35             basename += "-%s" % rev
36             path = os.path.join(self.path, "data", "oldrevs", basename)
37         else:
38             path = os.path.join(self.path, "data", "upload", basename)
39         if kind == "stdout":
40             path += ".log"
41         elif kind == "stderr":
42             path += ".err"
43         else:
44             raise ValueError("Unknown log kind %r" % kind)
45         f = open(path, 'w+')
46         try:
47             f.write(contents)
48         finally:
49             f.close()
50         return path
51
52     def write_compilers(self, compilers):
53         f = open(os.path.join(self.path, "web", "compilers.list"), "w")
54         try:
55             for compiler in compilers:
56                 f.write("%s\n" % compiler)
57         finally:
58             f.close()
59
60     def write_trees(self, trees):
61         f = open(os.path.join(self.path, "web", "trees.conf"), "w")
62         try:
63             for t in trees:
64                 f.write("[%s]\n" % t)
65                 for k, v in trees[t].iteritems():
66                     f.write("%s = %s\n" % (k, v))
67                 f.write("\n")
68         finally:
69             f.close()
70
71     def setUp(self):
72         super(BuildFarmTestCase, self).setUp()
73         self.path = tempfile.mkdtemp()
74
75         for subdir in ["data", "data/upload", "data/oldrevs", "cache", "web", "lcov", "lcov/data"]:
76             os.mkdir(os.path.join(self.path, subdir))
77
78         db = database.create_database("sqlite:"+os.path.join(self.path, "hostdb.sqlite"))
79         store = Store(db)
80         setup_db(store)
81
82     def tearDown(self):
83         shutil.rmtree(self.path)
84         super(BuildFarmTestCase, self).tearDown()