3 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 """Unittests for lab status."""
15 sys.path.insert(0, constants.SOURCE_ROOT)
16 from chromite.buildbot import lab_status
17 from chromite.lib import cros_test_lib
20 class TestLabStatus(cros_test_lib.MockTestCase):
21 """Class that tests GetLabStatus and CheckLabStatus."""
24 self.PatchObject(time, 'sleep')
25 self.PatchObject(urllib, 'urlopen')
27 def _LabStatusFile(self, message, general_state):
28 """Returns a file-like object with the status message written in it."""
30 my_response.json = '{"message": "%s", "general_state": "%s"}' % (
31 message, general_state)
34 def _TestGetLabStatusHelper(self, lab_message, general_state, expected_return,
35 max_attempts=5, failed_attempts=0):
36 """Tests whether we get correct lab status.
39 lab_message: A message describing lab status and
40 disabled boards, e.g. "Lab is Up [stumpy, kiev]"
41 general_state: Current lab state, e.g. 'open'.
42 expected_return: The expected return of GetLabStatus,
43 e.g. {'lab_is_up': True, 'message': 'Lab is up'}.
44 max_attempts: Max attempts GetLabStatus will make to get lab status.
45 failed_attempts: Number of failed attempts we want to mock.
47 return_status = self._LabStatusFile(lab_message, general_state)
48 urlopen_side_effect = [500 for _ in range(failed_attempts)]
49 if failed_attempts < max_attempts:
50 urlopen_side_effect.append(200)
51 call_count = failed_attempts + 1
53 call_count = max_attempts
54 return_status.getcode.side_effect = urlopen_side_effect
55 urllib.urlopen.return_value = return_status
56 return_status.read.return_value = return_status.json
57 self.assertEqual(lab_status.GetLabStatus(max_attempts), expected_return)
58 # pylint: disable=E1101
59 self.assertEqual(urllib.urlopen.call_count, call_count)
61 def testGetLabStatusWithOneAttempt(self):
62 """Tests that GetLabStatus succeeds with one attempt."""
63 expected_return = {'lab_is_up': True, 'message': 'Lab is up'}
64 self._TestGetLabStatusHelper('Lab is up', 'open', expected_return)
66 def testGetLabStatusWithMultipleAttempts(self):
67 """Tests that GetLabStatus succeeds after multiple tries."""
68 expected_return = {'lab_is_up': True, 'message': 'Lab is up'}
69 self._TestGetLabStatusHelper('Lab is up', 'open', expected_return,
70 max_attempts=5, failed_attempts=3)
72 def testGetLabStatusFailsWithMultipleAttempts(self):
73 """Tests that GetLabStatus fails after multiple tries."""
74 expected_return = {'lab_is_up': True, 'message': ''}
75 self._TestGetLabStatusHelper('dummy_msg', 'dummy_state', expected_return,
76 max_attempts=5, failed_attempts=5)
78 def _BuildLabStatus(self, lab_is_up, disabled_boards=None):
79 """Build a dictionary representing the lab status."""
81 message = 'Lab is Open'
83 message += '[%s]' % (', '.join(disabled_boards))
85 message = 'Lab is Down (For some reason it is down.)'
87 status = {'lab_is_up': lab_is_up, 'message': message}
90 def _TestCheckLabStatusHelper(self, lab_is_up):
91 """Tests CheckLabStatus runs properly."""
92 self.PatchObject(lab_status, 'GetLabStatus')
93 status = self._BuildLabStatus(lab_is_up)
94 lab_status.GetLabStatus.return_value = status
96 lab_status.CheckLabStatus()
98 self.assertRaises(lab_status.LabIsDownException,
99 lab_status.CheckLabStatus)
101 def testCheckLabStatusWhenLabUp(self):
102 """Tests CheckLabStatus runs properly when lab is up."""
103 self._TestCheckLabStatusHelper(True)
105 def testCheckLabStatusWhenLabDown(self):
106 """Tests CheckLabStatus runs properly when lab is down."""
107 self._TestCheckLabStatusHelper(False)
109 def testCheckLabStatusWhenBoardsDisabled(self):
110 """Tests CheckLabStatus runs properly when some boards are disabled."""
111 self.PatchObject(lab_status, 'GetLabStatus')
112 status = self._BuildLabStatus(True, ['stumpy', 'kiev', 'x85-alex'])
113 lab_status.GetLabStatus.return_value = status
114 lab_status.CheckLabStatus('lumpy')
115 self.assertRaises(lab_status.BoardIsDisabledException,
116 lab_status.CheckLabStatus,
120 if __name__ == '__main__':