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