| 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 # _pid_is_alive should not be called here and it is an error if it is. |
| 62 # pylint: disable=redundant-unittest-assert |
| 63 self.mock( |
| 64 master, '_pid_is_alive', |
| 65 lambda _x: self.assertTrue(False)) # pragma: no cover |
| 66 # There is no twistd.pid in chromium.webkit. |
| 67 self.assertFalse(master.buildbot_is_running(self.chromium_webkit)) |
| 68 |
| 69 def testNoActionsLog(self): |
| 70 last_boot = master.get_last_boot(self.chromium_webkit) |
| 71 self.assertIsNone(last_boot) |
| 72 |
| 73 def testGetLastBoot(self): |
| 74 last_boot = master.get_last_boot(self.chromium_fyi) |
| 75 |
| 76 # Apr 23 2015 11:01:40 PDT. |
| 77 self.assertEqual(last_boot, 1429812100) |
| 78 |
| 79 def testGetLastNoNewBuilds(self): |
| 80 last_no_new_builds = master.get_last_no_new_builds(self.chromium_fyi) |
| 81 |
| 82 # Apr 23 2015 11:01:50 PDT. |
| 83 self.assertEqual(last_no_new_builds, 1429812110) |
| 84 |
| 85 def testGetLastNoNewBuildsNotThere(self): |
| 86 last_no_new_builds = master.get_last_no_new_builds(self.chromium_webkit) |
| 87 self.assertIsNone(last_no_new_builds) |
| 88 |
| 89 def testGetLastNoNewBuildsButStarted(self): |
| 90 last_no_new_builds = master.get_last_no_new_builds(self.chromium_linux) |
| 91 self.assertIsNone(last_no_new_builds) |
| 92 |
| 93 def testGetLastBootNotThere(self): |
| 94 # 'make wait' is not in the sample actions.log. |
| 95 last_make_wait = master._get_last_action(self.chromium_fyi, 'make wait') |
| 96 self.assertIsNone(last_make_wait) |
| 97 |
| 98 def testMasterWebPort(self): |
| 99 master_port = master._get_master_web_port(self.chromium_fyi) |
| 100 self.assertEquals(master_port, 8011) |
| 101 self.assertEquals(len(self.calls), 1) |
| 102 self.assertTrue(any(x.endswith('mastermap.py') for x in self.calls[0])) |
| 103 |
| 104 def testNoSuchMaster(self): |
| 105 master_port = master._get_master_web_port(self.chromium_webkit) |
| 106 self.assertIsNone(master_port) |
| 107 |
| 108 def testMasterMapInternal(self): |
| 109 master._get_master_web_port(self.supersecret) |
| 110 self.assertEquals(len(self.calls), 1) |
| 111 self.assertTrue( |
| 112 any(x.endswith('mastermap_internal.py') for x in self.calls[0])) |
| 113 |
| 114 def testAcceptingBuilds(self): |
| 115 self.mock(requests, 'get', self.requests_handler) |
| 116 self.assertTrue(master.get_accepting_builds(self.chromium_fyi)) |
| 117 |
| 118 def testNotAcceptingBuilds(self): |
| 119 self.res = self.Response( |
| 120 status_code=200, |
| 121 json=lambda: {'accepting_builds': False}) |
| 122 self.mock(requests, 'get', self.requests_handler) |
| 123 self.assertFalse(master.get_accepting_builds(self.chromium_fyi)) |
| 124 |
| 125 def testAcceptingBuildsNoMaster(self): |
| 126 self.assertIsNone(master.get_accepting_builds(self.chromium_webkit)) |
| 127 |
| 128 def testBadStatusCode(self): |
| 129 # We shouldn't get to the JSON function since we hit 404. |
| 130 # pylint: disable=redundant-unittest-assert |
| 131 self.res = self.Response( |
| 132 status_code=404, |
| 133 json=lambda: self.assertTrue(False)) # pragma: no cover |
| 134 self.mock(requests, 'get', self.requests_handler) |
| 135 self.assertFalse(master.get_accepting_builds(self.chromium_fyi)) |
| 136 |
| 137 def testBadJson(self): |
| 138 def raiser(): |
| 139 raise simplejson.scanner.JSONDecodeError('bad json', '', 0) |
| 140 self.res = self.Response( |
| 141 status_code=200, |
| 142 json=raiser) |
| 143 self.mock(requests, 'get', self.requests_handler) |
| 144 self.assertFalse(master.get_accepting_builds(self.chromium_fyi)) |
| 145 |
| 146 def testTimeout(self): |
| 147 def timeout(*_args, **_kwargs): |
| 148 raise requests.exceptions.Timeout('timeout') |
| 149 self.mock(requests, 'get', timeout) |
| 150 self.assertFalse(master.get_accepting_builds(self.chromium_fyi)) |
| 151 |
| 152 class TestMasterManipulation(auto_stub.TestCase): |
| 153 def setUp(self): |
| 154 super(TestMasterManipulation, self).setUp() |
| 155 self.chromium_fyi = os.path.join(DATA_DIR, 'master.chromium.fyi') |
| 156 |
| 157 def testWithGclientSyncEnabled(self): |
| 158 actions = list(master.convert_action_items_to_cli(( |
| 159 master.GclientSync, |
| 160 master.MakeStop, |
| 161 master.MakeWait, |
| 162 master.MakeStart, |
| 163 master.MakeNoNewBuilds), |
| 164 self.chromium_fyi, |
| 165 enable_gclient=True)) |
| 166 self.assertEquals( |
| 167 [a['cmd'] for a in actions], |
| 168 [ |
| 169 ['gclient', 'sync', '--reset', '--force', '--auto_rebase'], |
| 170 ['make', 'stop'], |
| 171 ['make', 'wait'], |
| 172 ['make', 'start'], |
| 173 ['make', 'no-new-builds'], |
| 174 ], |
| 175 ) |
| 176 |
| 177 def testWithGclientSyncDisabled(self): |
| 178 actions = list(master.convert_action_items_to_cli(( |
| 179 master.GclientSync, |
| 180 master.MakeStop), |
| 181 self.chromium_fyi)) |
| 182 |
| 183 self.assertEquals( |
| 184 [a['cmd'] for a in actions], |
| 185 [ |
| 186 ['make', 'stop'], |
| 187 ], |
| 188 ) |
| 189 |
| 190 def testInvalid(self): |
| 191 with self.assertRaises(ValueError): |
| 192 list(master.convert_action_items_to_cli((-100,), self.chromium_fyi)) |
| OLD | NEW |