|
| 1 | +# Copyright 2022 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""health check reposnser tests.""" |
| 15 | + |
| 16 | +from http.server import HTTPServer |
| 17 | +import threading |
| 18 | +import unittest |
| 19 | + |
| 20 | +import mock |
| 21 | +import requests |
| 22 | + |
| 23 | +from python.bot.startup.health_check_responder import EXPECTED_SCRIPTS |
| 24 | +from python.bot.startup.health_check_responder import RequestHandler |
| 25 | +from python.bot.startup.health_check_responder import RESPONDER_IP |
| 26 | +from python.bot.startup.health_check_responder import RESPONDER_PORT |
| 27 | + |
| 28 | +RESPONDER_ADDR = f'http://{RESPONDER_IP}:{RESPONDER_PORT}' |
| 29 | + |
| 30 | + |
| 31 | +class HealthCheckResponderTest(unittest.TestCase): |
| 32 | + """Test health check responder.""" |
| 33 | + |
| 34 | + def setUp(self): |
| 35 | + """Prepare mock processes and start the responder server thread.""" |
| 36 | + self.mock_run_process = mock.MagicMock() |
| 37 | + self.mock_run_process.cmdline.return_value = ['./' + EXPECTED_SCRIPTS[0]] |
| 38 | + self.mock_run_bot_process = mock.MagicMock() |
| 39 | + self.mock_run_bot_process.cmdline.return_value = [ |
| 40 | + './' + EXPECTED_SCRIPTS[1] |
| 41 | + ] |
| 42 | + |
| 43 | + self.health_check_responder_server = HTTPServer( |
| 44 | + (RESPONDER_IP, RESPONDER_PORT), RequestHandler) |
| 45 | + server_thread = threading.Thread( |
| 46 | + target=self.health_check_responder_server.serve_forever) |
| 47 | + server_thread.start() |
| 48 | + |
| 49 | + def tearDown(self): |
| 50 | + self.health_check_responder_server.shutdown() |
| 51 | + self.health_check_responder_server.server_close() |
| 52 | + |
| 53 | + @mock.patch( |
| 54 | + 'python.bot.startup.health_check_responder.process_handler.psutil') |
| 55 | + def test_healthy(self, mock_psutil): |
| 56 | + """Testcase for both scripts are running.""" |
| 57 | + mock_psutil.process_iter.return_value = [ |
| 58 | + self.mock_run_process, self.mock_run_bot_process |
| 59 | + ] |
| 60 | + |
| 61 | + self.assertEqual(200, requests.get(f'{RESPONDER_ADDR}').status_code) |
| 62 | + |
| 63 | + @mock.patch( |
| 64 | + 'python.bot.startup.health_check_responder.process_handler.psutil') |
| 65 | + def test_run_terminated(self, mock_psutil): |
| 66 | + """Testcase for only the run script is running.""" |
| 67 | + mock_psutil.process_iter.return_value = [self.mock_run_process] |
| 68 | + |
| 69 | + self.assertEqual(500, requests.get(f'{RESPONDER_ADDR}').status_code) |
| 70 | + |
| 71 | + @mock.patch( |
| 72 | + 'python.bot.startup.health_check_responder.process_handler.psutil') |
| 73 | + def test_run_bot_terminated(self, mock_psutil): |
| 74 | + """Testcase for only the run_bot script is running.""" |
| 75 | + mock_psutil.process_iter.return_value = [self.mock_run_bot_process] |
| 76 | + |
| 77 | + self.assertEqual(500, requests.get(f'{RESPONDER_ADDR}').status_code) |
| 78 | + |
| 79 | + @mock.patch( |
| 80 | + 'python.bot.startup.health_check_responder.process_handler.psutil') |
| 81 | + def test_both_terminated(self, mock_psutil): |
| 82 | + """Testcase for neither script is running.""" |
| 83 | + mock_psutil.process_iter.return_value = [] |
| 84 | + self.assertEqual(500, requests.get(f'{RESPONDER_ADDR}').status_code) |
0 commit comments