fb98f5516fe51e5a251ea8f8829e3665e7468bef
[amitay/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.sqldb import setup_schema
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_hosts(self, hosts):
61         f = open(os.path.join(self.path, "web", "hosts.list"), "w")
62         try:
63             for name, platform in hosts.iteritems():
64                 f.write("%s: %s\n" % (name, platform))
65         finally:
66             f.close()
67
68     def write_trees(self, trees):
69         f = open(os.path.join(self.path, "web", "trees.conf"), "w")
70         try:
71             for t in trees:
72                 f.write("[%s]\n" % t)
73                 for k, v in trees[t].iteritems():
74                     f.write("%s = %s\n" % (k, v))
75                 f.write("\n")
76         finally:
77             f.close()
78
79     def setUp(self):
80         super(BuildFarmTestCase, self).setUp()
81         self.path = tempfile.mkdtemp()
82
83         for subdir in ["data", "data/upload", "data/oldrevs", "cache", "web", "lcov", "lcov/data"]:
84             os.mkdir(os.path.join(self.path, subdir))
85
86         db = database.create_database("sqlite:"+os.path.join(self.path, "hostdb.sqlite"))
87         store = Store(db)
88         setup_schema(store)
89         store.flush()
90
91     def tearDown(self):
92         shutil.rmtree(self.path)
93         super(BuildFarmTestCase, self).tearDown()