|
| 1 | +# -------------------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | +# -------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +import unittest |
| 7 | +import tempfile |
| 8 | +import os |
| 9 | +from unittest.mock import patch, MagicMock, call |
| 10 | +from azext_fleet.custom import _convert_kubeconfig_to_azurecli |
| 11 | + |
| 12 | + |
| 13 | +# Test constants |
| 14 | +KUBELOGIN_BINARY = 'kubelogin' |
| 15 | +MOCK_KUBELOGIN_PATH = '/usr/bin/kubelogin' |
| 16 | +KUBELOGIN_TIMEOUT = 60 |
| 17 | +KUBELOGIN_CMD_PREFIX = [KUBELOGIN_BINARY, "convert-kubeconfig", "-l", "azurecli", "--kubeconfig"] |
| 18 | +KUBELOGIN_GITHUB_URL = "https://github.com/Azure/kubelogin" |
| 19 | +KUBECONFIG_SUFFIX = '.kubeconfig' |
| 20 | +STDOUT_PATH = '-' |
| 21 | +CUSTOM_CONFIG_PATH = '/custom/path/to/config' |
| 22 | + |
| 23 | + |
| 24 | +class TestKubeloginConversion(unittest.TestCase): |
| 25 | + """Test cases for kubelogin automatic conversion functionality.""" |
| 26 | + |
| 27 | + @patch('azext_fleet.custom.shutil.which') |
| 28 | + @patch('azext_fleet.custom.subprocess.run') |
| 29 | + @patch('azext_fleet.custom.logger') |
| 30 | + def test_convert_with_kubelogin_available(self, mock_logger, mock_subprocess, mock_which): |
| 31 | + """Test conversion when kubelogin is available.""" |
| 32 | + mock_which.return_value = MOCK_KUBELOGIN_PATH |
| 33 | + mock_subprocess.return_value = MagicMock() |
| 34 | + |
| 35 | + with tempfile.NamedTemporaryFile(mode='w', suffix=KUBECONFIG_SUFFIX, delete=False) as f: |
| 36 | + test_path = f.name |
| 37 | + |
| 38 | + try: |
| 39 | + _convert_kubeconfig_to_azurecli(test_path) |
| 40 | + |
| 41 | + # Verify kubelogin was called with correct arguments |
| 42 | + mock_subprocess.assert_called_once() |
| 43 | + call_args = mock_subprocess.call_args |
| 44 | + self.assertEqual(call_args[0][0], KUBELOGIN_CMD_PREFIX + [test_path]) |
| 45 | + self.assertTrue(call_args[1]['check']) |
| 46 | + self.assertEqual(call_args[1]['timeout'], KUBELOGIN_TIMEOUT) |
| 47 | + |
| 48 | + # Verify success message was logged |
| 49 | + mock_logger.warning.assert_called_with("Converted kubeconfig to use Azure CLI authentication.") |
| 50 | + finally: |
| 51 | + if os.path.exists(test_path): |
| 52 | + os.remove(test_path) |
| 53 | + |
| 54 | + @patch('azext_fleet.custom.shutil.which') |
| 55 | + @patch('azext_fleet.custom.logger') |
| 56 | + def test_convert_with_kubelogin_unavailable(self, mock_logger, mock_which): |
| 57 | + """Test conversion when kubelogin is not available.""" |
| 58 | + mock_which.return_value = None |
| 59 | + |
| 60 | + with tempfile.NamedTemporaryFile(mode='w', suffix=KUBECONFIG_SUFFIX, delete=False) as f: |
| 61 | + test_path = f.name |
| 62 | + |
| 63 | + try: |
| 64 | + _convert_kubeconfig_to_azurecli(test_path) |
| 65 | + |
| 66 | + # Verify warning message was logged |
| 67 | + mock_logger.warning.assert_called_once() |
| 68 | + warning_msg = mock_logger.warning.call_args[0][0] |
| 69 | + self.assertIn("kubeconfig requires kubelogin", warning_msg) |
| 70 | + self.assertIn(KUBELOGIN_GITHUB_URL, warning_msg) |
| 71 | + finally: |
| 72 | + if os.path.exists(test_path): |
| 73 | + os.remove(test_path) |
| 74 | + |
| 75 | + @patch('azext_fleet.custom.shutil.which') |
| 76 | + @patch('azext_fleet.custom.subprocess.run') |
| 77 | + @patch('azext_fleet.custom.logger') |
| 78 | + def test_convert_handles_subprocess_error(self, mock_logger, mock_subprocess, mock_which): |
| 79 | + """Test that subprocess errors are handled gracefully.""" |
| 80 | + mock_which.return_value = MOCK_KUBELOGIN_PATH |
| 81 | + from subprocess import CalledProcessError |
| 82 | + mock_subprocess.side_effect = CalledProcessError(1, KUBELOGIN_BINARY) |
| 83 | + |
| 84 | + with tempfile.NamedTemporaryFile(mode='w', suffix=KUBECONFIG_SUFFIX, delete=False) as f: |
| 85 | + test_path = f.name |
| 86 | + |
| 87 | + try: |
| 88 | + _convert_kubeconfig_to_azurecli(test_path) |
| 89 | + |
| 90 | + # Verify error was logged |
| 91 | + mock_logger.warning.assert_called_once() |
| 92 | + warning_msg = mock_logger.warning.call_args[0][0] |
| 93 | + self.assertIn("Failed to convert kubeconfig with kubelogin", warning_msg) |
| 94 | + finally: |
| 95 | + if os.path.exists(test_path): |
| 96 | + os.remove(test_path) |
| 97 | + |
| 98 | + @patch('azext_fleet.custom.shutil.which') |
| 99 | + @patch('azext_fleet.custom.subprocess.run') |
| 100 | + @patch('azext_fleet.custom.logger') |
| 101 | + def test_convert_handles_timeout(self, mock_logger, mock_subprocess, mock_which): |
| 102 | + """Test that timeout errors are handled gracefully.""" |
| 103 | + mock_which.return_value = MOCK_KUBELOGIN_PATH |
| 104 | + from subprocess import TimeoutExpired |
| 105 | + mock_subprocess.side_effect = TimeoutExpired(KUBELOGIN_BINARY, KUBELOGIN_TIMEOUT) |
| 106 | + |
| 107 | + with tempfile.NamedTemporaryFile(mode='w', suffix=KUBECONFIG_SUFFIX, delete=False) as f: |
| 108 | + test_path = f.name |
| 109 | + |
| 110 | + try: |
| 111 | + _convert_kubeconfig_to_azurecli(test_path) |
| 112 | + |
| 113 | + # Verify timeout was logged |
| 114 | + mock_logger.warning.assert_called_once() |
| 115 | + warning_msg = mock_logger.warning.call_args[0][0] |
| 116 | + self.assertIn("kubelogin command timed out", warning_msg) |
| 117 | + finally: |
| 118 | + if os.path.exists(test_path): |
| 119 | + os.remove(test_path) |
| 120 | + |
| 121 | + @patch('azext_fleet.custom.shutil.which') |
| 122 | + @patch('azext_fleet.custom.subprocess.run') |
| 123 | + @patch('azext_fleet.custom.logger') |
| 124 | + def test_convert_handles_generic_exception(self, mock_logger, mock_subprocess, mock_which): |
| 125 | + """Test that generic exceptions are handled gracefully.""" |
| 126 | + mock_which.return_value = MOCK_KUBELOGIN_PATH |
| 127 | + mock_subprocess.side_effect = Exception("Unexpected error") |
| 128 | + |
| 129 | + with tempfile.NamedTemporaryFile(mode='w', suffix=KUBECONFIG_SUFFIX, delete=False) as f: |
| 130 | + test_path = f.name |
| 131 | + |
| 132 | + try: |
| 133 | + _convert_kubeconfig_to_azurecli(test_path) |
| 134 | + |
| 135 | + # Verify error was logged |
| 136 | + mock_logger.warning.assert_called_once() |
| 137 | + warning_msg = mock_logger.warning.call_args[0][0] |
| 138 | + self.assertIn("Error running kubelogin", warning_msg) |
| 139 | + finally: |
| 140 | + if os.path.exists(test_path): |
| 141 | + os.remove(test_path) |
| 142 | + |
| 143 | + @patch('azext_fleet.custom.shutil.which') |
| 144 | + @patch('azext_fleet.custom.subprocess.run') |
| 145 | + def test_convert_skips_stdout_path(self, mock_subprocess, mock_which): |
| 146 | + """Test that conversion is skipped when path is stdout.""" |
| 147 | + mock_which.return_value = MOCK_KUBELOGIN_PATH |
| 148 | + |
| 149 | + _convert_kubeconfig_to_azurecli(STDOUT_PATH) |
| 150 | + |
| 151 | + # Verify subprocess was not called |
| 152 | + mock_subprocess.assert_not_called() |
| 153 | + |
| 154 | + @patch('azext_fleet.custom.shutil.which') |
| 155 | + @patch('azext_fleet.custom.subprocess.run') |
| 156 | + def test_convert_with_custom_path(self, mock_subprocess, mock_which): |
| 157 | + """Test conversion with custom kubeconfig path.""" |
| 158 | + mock_which.return_value = MOCK_KUBELOGIN_PATH |
| 159 | + mock_subprocess.return_value = MagicMock() |
| 160 | + |
| 161 | + _convert_kubeconfig_to_azurecli(CUSTOM_CONFIG_PATH) |
| 162 | + |
| 163 | + # Verify kubelogin was called with custom path |
| 164 | + call_args = mock_subprocess.call_args |
| 165 | + self.assertEqual(call_args[0][0], KUBELOGIN_CMD_PREFIX + [CUSTOM_CONFIG_PATH]) |
| 166 | + |
| 167 | + |
| 168 | +if __name__ == '__main__': |
| 169 | + unittest.main() |
0 commit comments