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