|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +Copyright 2021 The Dapr Authors |
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 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 | +""" |
| 15 | +import time |
| 16 | +import unittest |
| 17 | +from unittest.mock import patch, MagicMock |
| 18 | + |
| 19 | +from dapr.clients.health import DaprHealth |
| 20 | +from dapr.conf import settings |
| 21 | +from dapr.version import __version__ |
| 22 | + |
| 23 | + |
| 24 | +class DaprHealthCheckTests(unittest.TestCase): |
| 25 | + @patch.object(settings, 'DAPR_HTTP_ENDPOINT', 'http://domain.com:3500') |
| 26 | + @patch('urllib.request.urlopen') |
| 27 | + def test_wait_until_ready_success(self, mock_urlopen): |
| 28 | + mock_urlopen.return_value.__enter__.return_value = MagicMock(status=200) |
| 29 | + |
| 30 | + try: |
| 31 | + DaprHealth.wait_until_ready() |
| 32 | + except Exception as e: |
| 33 | + self.fail(f'wait_until_ready() raised an exception unexpectedly: {e}') |
| 34 | + |
| 35 | + mock_urlopen.assert_called_once() |
| 36 | + |
| 37 | + called_url = mock_urlopen.call_args[0][0].full_url |
| 38 | + self.assertEqual(called_url, 'http://domain.com:3500/v1.0/healthz/outbound') |
| 39 | + |
| 40 | + # Check headers are properly set |
| 41 | + headers = mock_urlopen.call_args[0][0].headers |
| 42 | + self.assertIn('User-agent', headers) |
| 43 | + self.assertEqual(headers['User-agent'], f'dapr-sdk-python/{__version__}') |
| 44 | + |
| 45 | + @patch.object(settings, 'DAPR_HTTP_ENDPOINT', 'http://domain.com:3500') |
| 46 | + @patch.object(settings, 'DAPR_API_TOKEN', 'mytoken') |
| 47 | + @patch('urllib.request.urlopen') |
| 48 | + def test_wait_until_ready_success_with_api_token(self, mock_urlopen): |
| 49 | + mock_urlopen.return_value.__enter__.return_value = MagicMock(status=200) |
| 50 | + |
| 51 | + try: |
| 52 | + DaprHealth.wait_until_ready() |
| 53 | + except Exception as e: |
| 54 | + self.fail(f'wait_until_ready() raised an exception unexpectedly: {e}') |
| 55 | + |
| 56 | + mock_urlopen.assert_called_once() |
| 57 | + |
| 58 | + # Check headers are properly set |
| 59 | + headers = mock_urlopen.call_args[0][0].headers |
| 60 | + self.assertIn('User-agent', headers) |
| 61 | + self.assertEqual(headers['User-agent'], f'dapr-sdk-python/{__version__}') |
| 62 | + self.assertIn('Dapr-api-token', headers) |
| 63 | + self.assertEqual(headers['Dapr-api-token'], 'mytoken') |
| 64 | + |
| 65 | + @patch.object(settings, 'DAPR_HEALTH_TIMEOUT', 2) |
| 66 | + @patch('urllib.request.urlopen') |
| 67 | + def test_wait_until_ready_timeout(self, mock_urlopen): |
| 68 | + mock_urlopen.return_value.__enter__.return_value = MagicMock(status=500) |
| 69 | + |
| 70 | + start = time.time() |
| 71 | + |
| 72 | + with self.assertRaises(TimeoutError): |
| 73 | + DaprHealth.wait_until_ready() |
| 74 | + |
| 75 | + self.assertGreaterEqual(time.time() - start, 2) |
| 76 | + self.assertGreater(mock_urlopen.call_count, 1) |
0 commit comments