aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/test/unit/test_scripts_checkconfig.py
blob: c6bf3bde1134cc7f406ad6e4c2d39e175c3ddc14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# This file is part of Buildbot.  Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

from __future__ import with_statement

import mock
import re
import sys
import os
import textwrap
import cStringIO
from twisted.trial import unittest
from buildbot.test.util import dirs, compat
from buildbot.scripts import base, checkconfig

class TestConfigLoader(dirs.DirsMixin, unittest.TestCase):

    def setUp(self):
        return self.setUpDirs('configdir')

    def tearDown(self):
        return self.tearDownDirs()

    # tests

    def do_test_load(self, config='', other_files={},
                           stdout_re=None, stderr_re=None):
        configFile = os.path.join('configdir', 'master.cfg')
        with open(configFile, "w") as f:
            f.write(config)
        for filename, contents in other_files.iteritems():
            if type(filename) == type(()):
                fn = os.path.join('configdir', *filename)
                dn = os.path.dirname(fn)
                if not os.path.isdir(dn):
                    os.makedirs(dn)
            else:
                fn = os.path.join('configdir', filename)
            with open(fn, "w") as f:
                f.write(contents)

        old_stdout, old_stderr = sys.stdout, sys.stderr
        stdout = sys.stdout = cStringIO.StringIO()
        stderr = sys.stderr = cStringIO.StringIO()
        try:
            checkconfig._loadConfig(
                    basedir='configdir', configFile="master.cfg", quiet=False)
        finally:
            sys.stdout, sys.stderr = old_stdout, old_stderr
        if stdout_re:
            stdout = stdout.getvalue()
            self.failUnless(stdout_re.search(stdout), stdout)
        if stderr_re:
            stderr = stderr.getvalue()
            self.failUnless(stderr_re.search(stderr), stderr)

    def test_success(self):
        len_sys_path = len(sys.path)
        config = textwrap.dedent("""\
                c = BuildmasterConfig = {}
                c['multiMaster'] = True
                c['schedulers'] = []
                from buildbot.config import BuilderConfig
                from buildbot.process.factory import BuildFactory
                c['builders'] = [
                    BuilderConfig('testbuilder', factory=BuildFactory(),
                                  slavename='sl'),
                ]
                from buildbot.buildslave import BuildSlave
                c['slaves'] = [
                    BuildSlave('sl', 'pass'),
                ]
                c['slavePortnum'] = 9989
                """)
        self.do_test_load(config=config,
                stdout_re=re.compile('Config file is good!'))

        # (regression) check that sys.path hasn't changed
        self.assertEqual(len(sys.path), len_sys_path)

    @compat.usesFlushLoggedErrors
    def test_failure_ImportError(self):
        config = textwrap.dedent("""\
                import test_scripts_checkconfig_does_not_exist
                """)
        self.do_test_load(config=config,
                stderr_re=re.compile(
                    'No module named test_scripts_checkconfig_does_not_exist'))
        self.flushLoggedErrors()

    @compat.usesFlushLoggedErrors
    def test_failure_no_slaves(self):
        config = textwrap.dedent("""\
                BuildmasterConfig={}
                """)
        self.do_test_load(config=config,
                stderr_re=re.compile('no slaves'))
        self.flushLoggedErrors()

    def test_success_imports(self):
        config = textwrap.dedent("""\
                from othermodule import port
                c = BuildmasterConfig = {}
                c['schedulers'] = []
                c['builders'] = []
                c['slaves'] = []
                c['slavePortnum'] = port
                """)
        other_files = { 'othermodule.py' : 'port = 9989' }
        self.do_test_load(config=config, other_files=other_files)

    def test_success_import_package(self):
        config = textwrap.dedent("""\
                from otherpackage.othermodule import port
                c = BuildmasterConfig = {}
                c['schedulers'] = []
                c['builders'] = []
                c['slaves'] = []
                c['slavePortnum'] = port
                """)
        other_files = {
            ('otherpackage', '__init__.py') : '',
            ('otherpackage', 'othermodule.py') : 'port = 9989',
        }
        self.do_test_load(config=config, other_files=other_files)


class TestCheckconfig(unittest.TestCase):

    def setUp(self):
        self.loadConfig = mock.Mock(spec=checkconfig._loadConfig, return_value=3)
        self.patch(checkconfig, '_loadConfig', self.loadConfig)

    def test_checkconfig_given_dir(self):
        self.assertEqual(checkconfig.checkconfig(dict(configFile='.')), 3)
        self.loadConfig.assert_called_with(basedir='.', configFile='master.cfg', quiet=None)

    def test_checkconfig_given_file(self):
        config = dict(configFile='master.cfg')
        self.assertEqual(checkconfig.checkconfig(config), 3)
        self.loadConfig.assert_called_with(basedir=os.getcwd(), configFile='master.cfg', quiet=None)

    def test_checkconfig_quiet(self):
        config = dict(configFile='master.cfg', quiet=True)
        self.assertEqual(checkconfig.checkconfig(config), 3)
        self.loadConfig.assert_called_with(basedir=os.getcwd(), configFile='master.cfg', quiet=True)

    def test_checkconfig_syntaxError_quiet(self):
        """
        When C{base.getConfigFileFromTac} raises L{SyntaxError},
        C{checkconfig.checkconfig} return an error.
        """
        mockGetConfig = mock.Mock(spec=base.getConfigFileFromTac,
                side_effect=SyntaxError)
        self.patch(checkconfig, 'getConfigFileFromTac', mockGetConfig)
        config = dict(configFile='.', quiet=True)
        self.assertEqual(checkconfig.checkconfig(config), 1)