|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 | + |
| 15 | +"""Tests for CLI argument parsing.""" |
| 16 | + |
| 17 | +import pytest |
| 18 | +from mcp_proxy_for_aws.cli import parse_args |
| 19 | +from unittest.mock import patch |
| 20 | + |
| 21 | + |
| 22 | +class TestParseArgs: |
| 23 | + """Tests for parse_args function.""" |
| 24 | + |
| 25 | + @patch('sys.argv', ['mcp-proxy-for-aws', 'https://test.example.com']) |
| 26 | + def test_parse_args_minimal(self): |
| 27 | + """Test parsing with minimal required arguments.""" |
| 28 | + args = parse_args() |
| 29 | + |
| 30 | + assert args.endpoint == 'https://test.example.com' |
| 31 | + assert args.service is None |
| 32 | + assert args.profile is None |
| 33 | + assert args.region is None |
| 34 | + assert args.read_only is False |
| 35 | + assert args.log_level == 'INFO' |
| 36 | + assert args.retries == 0 |
| 37 | + assert args.timeout == 180.0 |
| 38 | + assert args.connect_timeout == 60.0 |
| 39 | + assert args.read_timeout == 120.0 |
| 40 | + assert args.write_timeout == 180.0 |
| 41 | + |
| 42 | + @patch( |
| 43 | + 'sys.argv', |
| 44 | + [ |
| 45 | + 'mcp-proxy-for-aws', |
| 46 | + 'https://test.example.com', |
| 47 | + '--service', |
| 48 | + 'lambda', |
| 49 | + '--profile', |
| 50 | + 'prod', |
| 51 | + '--region', |
| 52 | + 'eu-west-1', |
| 53 | + '--read-only', |
| 54 | + '--log-level', |
| 55 | + 'WARNING', |
| 56 | + '--retries', |
| 57 | + '3', |
| 58 | + '--timeout', |
| 59 | + '300', |
| 60 | + '--connect-timeout', |
| 61 | + '30', |
| 62 | + '--read-timeout', |
| 63 | + '90', |
| 64 | + '--write-timeout', |
| 65 | + '120', |
| 66 | + ], |
| 67 | + ) |
| 68 | + def test_parse_args_with_all_options(self): |
| 69 | + """Test parsing with all options to verify no conflicts.""" |
| 70 | + args = parse_args() |
| 71 | + |
| 72 | + assert args.endpoint == 'https://test.example.com' |
| 73 | + assert args.service == 'lambda' |
| 74 | + assert args.profile == 'prod' |
| 75 | + assert args.region == 'eu-west-1' |
| 76 | + assert args.read_only is True |
| 77 | + assert args.log_level == 'WARNING' |
| 78 | + assert args.retries == 3 |
| 79 | + assert args.timeout == 300.0 |
| 80 | + assert args.connect_timeout == 30.0 |
| 81 | + assert args.read_timeout == 90.0 |
| 82 | + assert args.write_timeout == 120.0 |
| 83 | + |
| 84 | + @patch('sys.argv', ['mcp-proxy-for-aws']) |
| 85 | + def test_parse_args_missing_endpoint(self): |
| 86 | + """Test parsing fails when endpoint is missing.""" |
| 87 | + with pytest.raises(SystemExit): |
| 88 | + parse_args() |
| 89 | + |
| 90 | + @patch.dict('os.environ', {'AWS_PROFILE': 'env-profile'}) |
| 91 | + @patch('sys.argv', ['mcp-proxy-for-aws', 'https://test.example.com']) |
| 92 | + def test_parse_args_profile_from_env(self): |
| 93 | + """Test that profile is read from AWS_PROFILE environment variable.""" |
| 94 | + args = parse_args() |
| 95 | + |
| 96 | + assert args.endpoint == 'https://test.example.com' |
| 97 | + assert args.profile == 'env-profile' |
| 98 | + |
| 99 | + @patch.dict('os.environ', {'AWS_PROFILE': 'env-profile'}) |
| 100 | + @patch( |
| 101 | + 'sys.argv', ['mcp-proxy-for-aws', 'https://test.example.com', '--profile', 'cli-profile'] |
| 102 | + ) |
| 103 | + def test_parse_args_profile_cli_overrides_env(self): |
| 104 | + """Test that CLI profile argument overrides environment variable.""" |
| 105 | + args = parse_args() |
| 106 | + |
| 107 | + assert args.endpoint == 'https://test.example.com' |
| 108 | + assert args.profile == 'cli-profile' |
| 109 | + |
| 110 | + @patch('sys.argv', ['mcp-proxy-for-aws', 'https://test.example.com', '--retries', '0']) |
| 111 | + def test_parse_args_retries_minimum(self): |
| 112 | + """Test parsing with minimum valid retries value (boundary).""" |
| 113 | + args = parse_args() |
| 114 | + |
| 115 | + assert args.endpoint == 'https://test.example.com' |
| 116 | + assert args.retries == 0 |
| 117 | + |
| 118 | + @patch('sys.argv', ['mcp-proxy-for-aws', 'https://test.example.com', '--retries', '10']) |
| 119 | + def test_parse_args_retries_maximum(self): |
| 120 | + """Test parsing with maximum valid retries value (boundary).""" |
| 121 | + args = parse_args() |
| 122 | + |
| 123 | + assert args.endpoint == 'https://test.example.com' |
| 124 | + assert args.retries == 10 |
| 125 | + |
| 126 | + @patch('sys.argv', ['mcp-proxy-for-aws', 'https://test.example.com', '--retries', '11']) |
| 127 | + def test_parse_args_invalid_retries(self): |
| 128 | + """Test parsing fails with retries value above maximum (boundary).""" |
| 129 | + with pytest.raises(SystemExit): |
| 130 | + parse_args() |
| 131 | + |
| 132 | + @patch('sys.argv', ['mcp-proxy-for-aws', 'https://test.example.com', '--log-level', 'INVALID']) |
| 133 | + def test_parse_args_invalid_log_level(self): |
| 134 | + """Test parsing fails with invalid log level (choices validation).""" |
| 135 | + with pytest.raises(SystemExit): |
| 136 | + parse_args() |
| 137 | + |
| 138 | + @patch('sys.argv', ['mcp-proxy-for-aws', 'https://test.example.com', '--timeout', '-1']) |
| 139 | + def test_parse_args_negative_timeout(self): |
| 140 | + """Test parsing fails with negative timeout (within_range validation).""" |
| 141 | + with pytest.raises(SystemExit): |
| 142 | + parse_args() |
0 commit comments