Chromium Code Reviews| 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 unittest | |
| 6 | |
| 7 from infra.libs.buildbot import master | |
| 8 from infra.libs.time_functions import timestamp | |
| 9 from infra.services.master_manager_launcher import desired_state_parser | |
| 10 from testing_support import auto_stub | |
| 11 | |
| 12 | |
| 13 class TestDesiredStateValidation(auto_stub.TestCase): | |
| 14 def setUp(self): | |
| 15 super(TestDesiredStateValidation, self).setUp() | |
| 16 | |
| 17 self.mock(timestamp, 'utcnow_ts', lambda: 5000) | |
| 18 | |
| 19 def testValidState(self): | |
| 20 self.assertTrue(desired_state_parser.desired_master_state_is_valid({ | |
| 21 'master.chromium.fyi': [ | |
| 22 {'desired_state': 'running', 'transition_time_utc': 4000}, | |
| 23 {'desired_state': 'offline', 'transition_time_utc': 6000}, | |
| 24 ]})) | |
| 25 | |
| 26 def testNoDesiredState(self): | |
| 27 self.assertFalse(desired_state_parser.desired_master_state_is_valid({ | |
| 28 'master.chromium.fyi': [ | |
| 29 {'transition_time_utc': 4000}, | |
| 30 {'desired_state': 'offline', 'transition_time_utc': 6000}, | |
| 31 ]})) | |
| 32 | |
| 33 def testNoTransitionTime(self): | |
| 34 self.assertFalse(desired_state_parser.desired_master_state_is_valid({ | |
| 35 'master.chromium.fyi': [ | |
| 36 {'desired_state': 'running', 'transition_time_utc': 4000}, | |
| 37 {'desired_state': 'offline'}, | |
| 38 ]})) | |
| 39 | |
| 40 def testTransitionTimeInvalid(self): | |
| 41 self.assertFalse(desired_state_parser.desired_master_state_is_valid({ | |
| 42 'master.chromium.fyi': [ | |
| 43 {'desired_state': 'running', 'transition_time_utc': 'boats'}, | |
| 44 {'desired_state': 'offline', 'transition_time_utc': 'llama'}, | |
| 45 ]})) | |
| 46 | |
| 47 def testNotSorted(self): | |
| 48 self.assertFalse(desired_state_parser.desired_master_state_is_valid({ | |
| 49 'master.chromium.fyi': [ | |
| 50 {'desired_state': 'offline', 'transition_time_utc': 6000}, | |
| 51 {'desired_state': 'running', 'transition_time_utc': 4000}, | |
| 52 ]})) | |
| 53 | |
| 54 def testInvalidState(self): | |
| 55 self.assertFalse(desired_state_parser.desired_master_state_is_valid({ | |
| 56 'master.chromium.fyi': [ | |
| 57 {'desired_state': 'pajamas', 'transition_time_utc': 4000}, | |
| 58 {'desired_state': 'offline', 'transition_time_utc': 6000}, | |
| 59 ]})) | |
| 60 | |
| 61 def testUncertainPresent(self): | |
| 62 self.assertFalse(desired_state_parser.desired_master_state_is_valid({ | |
| 63 'master.chromium.fyi': [ | |
| 64 {'desired_state': 'running', 'transition_time_utc': 6000}, | |
| 65 {'desired_state': 'offline', 'transition_time_utc': 8000}, | |
| 66 ]})) | |
| 67 | |
| 68 | |
| 69 class TestMasterStateLookup(unittest.TestCase): | |
| 70 STATES = [ | |
| 71 {'desired_state': 'pajamas', 'transition_time_utc': 4000}, | |
| 72 {'desired_state': 'offline', 'transition_time_utc': 6000}, | |
| 73 ] | |
| 74 | |
| 75 def testUnknownPast(self): | |
| 76 state = desired_state_parser.get_master_state(self.STATES, now=300) | |
| 77 self.assertIsNone(state) | |
| 78 | |
| 79 def testMiddle(self): | |
| 80 state = desired_state_parser.get_master_state(self.STATES, now=4500) | |
| 81 self.assertEqual(state, self.STATES[0]) | |
| 82 | |
| 83 def testEnd(self): | |
| 84 state = desired_state_parser.get_master_state(self.STATES, now=8000) | |
| 85 self.assertEqual(state, self.STATES[1]) | |
| 86 | |
| 87 | |
| 88 class TestHostnameLookup(auto_stub.TestCase): | |
| 89 def setUp(self): | |
| 90 super(TestHostnameLookup, self).setUp() | |
| 91 | |
| 92 self.mock(master, 'get_mastermap_for_host', lambda _x, _y: [ | |
| 93 {'dirname': 'master.chromium', 'internal': False}, | |
| 94 {'dirname': 'master.chromium.fyi', 'internal': False}, | |
| 95 {'dirname': 'master.supersecret', 'internal': True}, | |
| 96 {'dirname': 'master.ultrasecret', 'internal': True}, | |
| 97 ]) | |
| 98 | |
| 99 | |
| 100 def testHostnameLookup(self): | |
| 101 """Test that selected masters are triggered and all else are ignored.""" | |
| 102 desired_state = { | |
| 103 'master.chromium.fyi': [ | |
| 104 {'desired_state': 'running', 'transition_time_utc': 4000}, | |
| 105 ], | |
| 106 'master.supersecret': [ | |
| 107 {'desired_state': 'running', 'transition_time_utc': 4000}, | |
| 108 ], | |
| 109 } | |
| 110 triggered, ignored = desired_state_parser.get_masters_for_host( | |
| 111 desired_state, | |
| 112 'bananas/', | |
| 113 'impenetrablefortress.cool' | |
| 114 ) | |
| 115 | |
| 116 | |
|
agable
2015/05/07 17:59:31
nit: two blank lines
ghost stip (do not use)
2015/05/07 19:49:40
my guess is you meant 'one blank line,' which I've
| |
| 117 self.assertEqual(len(triggered), 2) | |
| 118 self.assertEqual(len(ignored), 2) | |
| 119 | |
| 120 self.assertEqual(sorted(ignored), [ | |
| 121 'master.chromium', | |
| 122 'master.ultrasecret', | |
| 123 ]) | |
| 124 | |
| 125 for master_dict in triggered: | |
| 126 self.assertIn(master_dict['dirname'], desired_state) | |
| OLD | NEW |