| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Tests for scripts/tools/buildbot_tool.py""" |
| 7 |
| 8 import StringIO |
| 9 import sys |
| 10 import textwrap |
| 11 import unittest |
| 12 |
| 13 |
| 14 # This adjusts sys.path, so it must be imported before the other modules. |
| 15 import test_env |
| 16 |
| 17 from common import fake_filesystem |
| 18 from tools import buildbot_tool |
| 19 |
| 20 |
| 21 SAMPLE_MASTER_CFG_TEMPLATE = """\ |
| 22 # builders_block |
| 23 %(builders_block)s |
| 24 |
| 25 # git_repo_url |
| 26 %(git_repo_url)s |
| 27 |
| 28 # master_dirname |
| 29 %(master_dirname)s |
| 30 |
| 31 # master_classname |
| 32 %(master_classname)s |
| 33 |
| 34 # master_base_class |
| 35 %(master_base_class)s |
| 36 |
| 37 # master_port |
| 38 %(master_port)s |
| 39 |
| 40 # master_port_alt |
| 41 %(master_port_alt)s |
| 42 |
| 43 # slave_port |
| 44 %(slave_port)s |
| 45 """ |
| 46 |
| 47 SAMPLE_SLAVES_CFG_TEMPLATE = """\ |
| 48 %(slaves_block)s |
| 49 """ |
| 50 |
| 51 SAMPLE_BUILDERS_PY = """\ |
| 52 { |
| 53 "builders": { |
| 54 "Test Linux": { |
| 55 "properties": { |
| 56 "config": "Release" |
| 57 }, |
| 58 "recipe": "test_recipe", |
| 59 "slave_pools": ["main"], |
| 60 "slavebuilddir": "test" |
| 61 } |
| 62 }, |
| 63 "git_repo_url": "https://chromium.googlesource.com/test/test.git", |
| 64 "master_base_class": "Master1", |
| 65 "master_port": 20999, |
| 66 "master_port_alt": 40999, |
| 67 "slave_port": 30999, |
| 68 "slave_pools": { |
| 69 "main": { |
| 70 "slave_data": { |
| 71 "bits": 64, |
| 72 "os": "linux", |
| 73 "version": "precise" |
| 74 }, |
| 75 "slaves": ["vm9999-m1"] |
| 76 } |
| 77 } |
| 78 } |
| 79 """ |
| 80 |
| 81 |
| 82 def _trap_output(): |
| 83 orig_output = (sys.stdout, sys.stderr) |
| 84 sys.stdout = StringIO.StringIO() |
| 85 sys.stderr = StringIO.StringIO() |
| 86 return orig_output |
| 87 |
| 88 |
| 89 def _restore_output(orig_output): |
| 90 out, err = sys.stdout.getvalue(), sys.stderr.getvalue() |
| 91 sys.stdout, sys.stderr = orig_output |
| 92 return out, err |
| 93 |
| 94 |
| 95 def _stub_constants(new_values): |
| 96 orig = {} |
| 97 for k, v in new_values.items(): |
| 98 orig[k] = getattr(buildbot_tool, k) |
| 99 setattr(buildbot_tool, k, v) |
| 100 return orig |
| 101 |
| 102 |
| 103 def _restore_constants(orig_values): |
| 104 for k, v in orig_values.items(): |
| 105 setattr(buildbot_tool, k, v) |
| 106 |
| 107 |
| 108 class GenTest(unittest.TestCase): |
| 109 def test_normal(self): |
| 110 files = { |
| 111 '/build/templates/master.cfg': SAMPLE_MASTER_CFG_TEMPLATE, |
| 112 '/build/templates/slaves.cfg': SAMPLE_SLAVES_CFG_TEMPLATE, |
| 113 '/build/masters/master.test/builders.py': SAMPLE_BUILDERS_PY, |
| 114 } |
| 115 fs = fake_filesystem.FakeFilesystem(files=files.copy()) |
| 116 |
| 117 orig_output = _trap_output() |
| 118 orig_constants = _stub_constants({ |
| 119 'BASE_DIR': '/build', |
| 120 'TEMPLATE_SUBPATH': 'templates', |
| 121 'TEMPLATE_DIR': '/build/templates', |
| 122 }) |
| 123 |
| 124 try: |
| 125 ret = buildbot_tool.main(['gen', '/build/masters/master.test'], fs) |
| 126 finally: |
| 127 out, err = _restore_output(orig_output) |
| 128 _restore_constants(orig_constants) |
| 129 |
| 130 self.assertEqual(ret, 0) |
| 131 self.assertEqual(err, '') |
| 132 self.assertNotEqual(out, '') |
| 133 self.assertEqual(set(fs.files.keys()), |
| 134 set(files.keys() + |
| 135 ['/build/masters/master.test/master.cfg', |
| 136 '/build/masters/master.test/slaves.cfg'])) |
| 137 |
| 138 self.assertMultiLineEqual( |
| 139 fs.read_text_file('/build/masters/master.test/master.cfg'), |
| 140 textwrap.dedent("""\ |
| 141 # builders_block |
| 142 c['builders'].append({ |
| 143 'name': 'Test Linux', |
| 144 'factory': m_annotator.BaseFactory('test_recipe'), |
| 145 'slavebuilddir': 'test'}) |
| 146 |
| 147 |
| 148 # git_repo_url |
| 149 https://chromium.googlesource.com/test/test.git |
| 150 |
| 151 # master_dirname |
| 152 master.test |
| 153 |
| 154 # master_classname |
| 155 Test |
| 156 |
| 157 # master_base_class |
| 158 Master1 |
| 159 |
| 160 # master_port |
| 161 20999 |
| 162 |
| 163 # master_port_alt |
| 164 40999 |
| 165 |
| 166 # slave_port |
| 167 30999 |
| 168 """)) |
| 169 |
| 170 self.assertMultiLineEqual( |
| 171 fs.read_text_file('/build/masters/master.test/slaves.cfg'), |
| 172 textwrap.dedent("""\ |
| 173 slaves = [ |
| 174 { |
| 175 'master': 'Test', |
| 176 'hostname': 'vm9999-m1', |
| 177 'builder': 'Test Linux', |
| 178 'os': 'linux', |
| 179 'version': 'precise', |
| 180 'bits': '64', |
| 181 }, |
| 182 ] |
| 183 """)) |
| 184 |
| 185 def test_not_found(self): |
| 186 files = { |
| 187 '/build/templates/master.cfg': SAMPLE_MASTER_CFG_TEMPLATE, |
| 188 '/build/templates/slaves.cfg': SAMPLE_SLAVES_CFG_TEMPLATE, |
| 189 } |
| 190 fs = fake_filesystem.FakeFilesystem(files=files.copy()) |
| 191 |
| 192 orig_output = _trap_output() |
| 193 orig_constants = _stub_constants({ |
| 194 'BASE_DIR': '/build', |
| 195 'TEMPLATE_SUBPATH': 'templates', |
| 196 'TEMPLATE_DIR': '/build/templates', |
| 197 }) |
| 198 |
| 199 try: |
| 200 ret = buildbot_tool.main(['gen', '/build/masters/master.test'], fs) |
| 201 finally: |
| 202 out, err = _restore_output(orig_output) |
| 203 _restore_constants(orig_constants) |
| 204 |
| 205 self.assertEqual(ret, 1) |
| 206 self.assertEqual(out, '') |
| 207 self.assertEqual(err, '/build/masters/master.test not found\n') |
| 208 |
| 209 def test_bad_template(self): |
| 210 files = { |
| 211 '/build/templates/master.cfg': '%(unknown_key)s', |
| 212 '/build/masters/master.test/builders.py': SAMPLE_BUILDERS_PY, |
| 213 } |
| 214 fs = fake_filesystem.FakeFilesystem(files=files.copy()) |
| 215 |
| 216 orig_output = _trap_output() |
| 217 orig_constants = _stub_constants({ |
| 218 'BASE_DIR': '/build', |
| 219 'TEMPLATE_SUBPATH': 'templates', |
| 220 'TEMPLATE_DIR': '/build/templates', |
| 221 }) |
| 222 |
| 223 try: |
| 224 self.assertRaises(KeyError, |
| 225 buildbot_tool.main, |
| 226 ['gen', '/build/masters/master.test'], |
| 227 fs) |
| 228 finally: |
| 229 _restore_output(orig_output) |
| 230 _restore_constants(orig_constants) |
| 231 |
| 232 |
| 233 class HelpTest(unittest.TestCase): |
| 234 def test_help(self): |
| 235 orig_output = _trap_output() |
| 236 fs = fake_filesystem.FakeFilesystem() |
| 237 try: |
| 238 # We do not care what the output is, just that the commands run. |
| 239 self.assertRaises(SystemExit, buildbot_tool.main, ['--help'], fs) |
| 240 self.assertRaises(SystemExit, buildbot_tool.main, ['help'], fs) |
| 241 self.assertRaises(SystemExit, buildbot_tool.main, ['help', 'gen'], fs) |
| 242 finally: |
| 243 _restore_output(orig_output) |
| 244 |
| 245 |
| 246 if __name__ == '__main__': |
| 247 unittest.TestCase.maxDiff = None |
| 248 unittest.main() |
| OLD | NEW |