| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import collections |
| 6 import json |
| 7 import os |
| 8 import requests |
| 9 import simplejson |
| 10 import subprocess |
| 11 |
| 12 from infra.libs.buildbot import master |
| 13 from testing_support import auto_stub |
| 14 |
| 15 |
| 16 DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data') |
| 17 |
| 18 |
| 19 class TestMasterInformation(auto_stub.TestCase): |
| 20 def setUp(self): |
| 21 super(TestMasterInformation, self).setUp() |
| 22 self.calls = [] |
| 23 self.master_data = [{ |
| 24 'alt_port': 8211, |
| 25 'buildbot_url': 'http://build.chromium.org/p/chromium.fyi/', |
| 26 'dirname': 'master.chromium.fyi', |
| 27 'fullhost': 'master1.golo.chromium.org', |
| 28 'host': 'master1.golo', |
| 29 'internal': False, |
| 30 'name': 'ChromiumFYI', |
| 31 'port': 8011, |
| 32 'slave_port': 8111 |
| 33 }] |
| 34 def _handle_check_output(*args): |
| 35 self.calls.append(args[0]) |
| 36 return json.dumps(self.master_data) |
| 37 self.mock(subprocess, 'check_output', _handle_check_output) |
| 38 |
| 39 self.chromium_fyi = os.path.join(DATA_DIR, 'master.chromium.fyi') |
| 40 self.chromium_webkit = os.path.join(DATA_DIR, 'master.chromium.webkit') |
| 41 self.chromium_linux = os.path.join(DATA_DIR, 'master.chromium.linux') |
| 42 self.supersecret = os.path.join( |
| 43 DATA_DIR, 'build_internal', 'masters', 'master.chromium.supersecret') |
| 44 |
| 45 self.Response = collections.namedtuple('Response', ['status_code', 'json']) |
| 46 self.res = self.Response( |
| 47 status_code=200, |
| 48 json=lambda: {'accepting_builds': True}) |
| 49 |
| 50 self.requests_handler = lambda *_args, **_kwargs: self.res |
| 51 |
| 52 def testPidIsRunning(self): |
| 53 self.mock(master, '_pid_is_alive', lambda _x: True) |
| 54 self.assertTrue(master.buildbot_is_running(self.chromium_fyi)) |
| 55 |
| 56 def testPidIsNotRunning(self): |
| 57 self.mock(master, '_pid_is_alive', lambda _x: False) |
| 58 self.assertFalse(master.buildbot_is_running(self.chromium_fyi)) |
| 59 |
| 60 def testPidfileNotThere(self): |
| 61 # Mock _pid_is_alive to true -- it should never be called and is an error if |
| 62 # it is. |
| 63 # pylint: disable=redundant-unittest-assert |
| 64 self.mock( |
| 65 master, '_pid_is_alive', |
| 66 lambda _x: self.assertTrue(False)) # pragma: no cover |
| 67 # There is no twistd.pid in chromium.webkit. |
| 68 self.assertFalse(master.buildbot_is_running(self.chromium_webkit)) |
| 69 |
| 70 def testNoActionsLog(self): |
| 71 last_boot = master.get_last_boot(self.chromium_webkit) |
| 72 self.assertIsNone(last_boot) |
| 73 |
| 74 def testGetLastBoot(self): |
| 75 last_boot = master.get_last_boot(self.chromium_fyi) |
| 76 |
| 77 # Apr 23 2015 11:01:40 PDT. |
| 78 self.assertEqual(last_boot, 1429812100) |
| 79 |
| 80 def testGetLastNoNewBuilds(self): |
| 81 last_no_new_builds = master.get_last_no_new_builds(self.chromium_fyi) |
| 82 |
| 83 # Apr 23 2015 11:01:50 PDT. |
| 84 self.assertEqual(last_no_new_builds, 1429812110) |
| 85 |
| 86 def testGetLastNoNewBuildsNotThere(self): |
| 87 last_no_new_builds = master.get_last_no_new_builds(self.chromium_webkit) |
| 88 self.assertIsNone(last_no_new_builds) |
| 89 |
| 90 def testGetLastNoNewBuildsButStarted(self): |
| 91 last_no_new_builds = master.get_last_no_new_builds(self.chromium_linux) |
| 92 self.assertIsNone(last_no_new_builds) |
| 93 |
| 94 def testGetLastBootNotThere(self): |
| 95 # 'make wait' is not in the sample actions.log. |
| 96 last_make_wait = master._get_last_action(self.chromium_fyi, 'make wait') |
| 97 self.assertIsNone(last_make_wait) |
| 98 |
| 99 def testMasterWebPort(self): |
| 100 master_port = master._get_master_web_port(self.chromium_fyi) |
| 101 self.assertEquals(master_port, 8011) |
| 102 self.assertEquals(len(self.calls), 1) |
| 103 self.assertTrue(any(x.endswith('mastermap.py') for x in self.calls[0])) |
| 104 |
| 105 def testNoSuchMaster(self): |
| 106 master_port = master._get_master_web_port(self.chromium_webkit) |
| 107 self.assertIsNone(master_port) |
| 108 |
| 109 def testMasterMapInternal(self): |
| 110 master._get_master_web_port(self.supersecret) |
| 111 self.assertEquals(len(self.calls), 1) |
| 112 self.assertTrue( |
| 113 any(x.endswith('mastermap_internal.py') for x in self.calls[0])) |
| 114 |
| 115 def testAcceptingBuilds(self): |
| 116 self.mock(requests, 'get', self.requests_handler) |
| 117 self.assertTrue(master.get_accepting_builds(self.chromium_fyi)) |
| 118 |
| 119 def testNotAcceptingBuilds(self): |
| 120 self.res = self.Response( |
| 121 status_code=200, |
| 122 json=lambda: {'accepting_builds': False}) |
| 123 self.mock(requests, 'get', self.requests_handler) |
| 124 self.assertFalse(master.get_accepting_builds(self.chromium_fyi)) |
| 125 |
| 126 def testAcceptingBuildsNoMaster(self): |
| 127 self.assertIsNone(master.get_accepting_builds(self.chromium_webkit)) |
| 128 |
| 129 def testBadStatusCode(self): |
| 130 # We shouldn't get to the JSON function since we hit 404. |
| 131 # pylint: disable=redundant-unittest-assert |
| 132 self.res = self.Response( |
| 133 status_code=404, |
| 134 json=lambda: self.assertTrue(False)) # pragma: no cover |
| 135 self.mock(requests, 'get', self.requests_handler) |
| 136 self.assertFalse(master.get_accepting_builds(self.chromium_fyi)) |
| 137 |
| 138 def testBadJson(self): |
| 139 def raiser(): |
| 140 raise simplejson.scanner.JSONDecodeError('bad json', '', 0) |
| 141 self.res = self.Response( |
| 142 status_code=200, |
| 143 json=raiser) |
| 144 self.mock(requests, 'get', self.requests_handler) |
| 145 self.assertFalse(master.get_accepting_builds(self.chromium_fyi)) |
| 146 |
| 147 def testTimeout(self): |
| 148 def timeout(*_args, **_kwargs): |
| 149 raise requests.exceptions.Timeout('timeout') |
| 150 self.mock(requests, 'get', timeout) |
| 151 self.assertFalse(master.get_accepting_builds(self.chromium_fyi)) |
| 152 |
| 153 class TestMasterManipulation(auto_stub.TestCase): |
| 154 def setUp(self): |
| 155 super(TestMasterManipulation, self).setUp() |
| 156 self.chromium_fyi = os.path.join(DATA_DIR, 'master.chromium.fyi') |
| 157 |
| 158 def testWithGclientSyncEnabled(self): |
| 159 actions = list(master.convert_action_items_to_cli(( |
| 160 master.GclientSync, |
| 161 master.MakeStop, |
| 162 master.MakeWait, |
| 163 master.MakeStart, |
| 164 master.MakeNoNewBuilds), |
| 165 self.chromium_fyi, |
| 166 enable_gclient=True)) |
| 167 self.assertEquals( |
| 168 [a['cmd'] for a in actions], |
| 169 [ |
| 170 ['gclient', 'sync', '--reset', '--force', '--auto_rebase'], |
| 171 ['make', 'stop'], |
| 172 ['make', 'wait'], |
| 173 ['make', 'start'], |
| 174 ['make', 'no-new-builds'], |
| 175 ], |
| 176 ) |
| 177 |
| 178 def testWithGclientSyncDisabled(self): |
| 179 actions = list(master.convert_action_items_to_cli(( |
| 180 master.GclientSync, |
| 181 master.MakeStop), |
| 182 self.chromium_fyi)) |
| 183 |
| 184 self.assertEquals( |
| 185 [a['cmd'] for a in actions], |
| 186 [ |
| 187 ['make', 'stop'], |
| 188 ], |
| 189 ) |
| 190 |
| 191 def testInvalid(self): |
| 192 with self.assertRaises(ValueError): |
| 193 list(master.convert_action_items_to_cli((-100,), self.chromium_fyi)) |
| OLD | NEW |