|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Tests for enablement_tools module.""" |
| 5 | + |
| 6 | +import pytest |
| 7 | +from awslabs.cloudwatch_appsignals_mcp_server.enablement_tools import get_enablement_guide |
| 8 | +from unittest.mock import patch |
| 9 | + |
| 10 | + |
| 11 | +# Absolute paths for testing (no need to create real directories) |
| 12 | +ABSOLUTE_PATHS = {'iac': '/tmp/test/infrastructure/cdk', 'app': '/tmp/test/app/src'} |
| 13 | + |
| 14 | + |
| 15 | +class TestGetEnablementGuide: |
| 16 | + """Test get_enablement_guide function.""" |
| 17 | + |
| 18 | + @pytest.mark.asyncio |
| 19 | + async def test_successful_guide_fetch(self, tmp_path, monkeypatch): |
| 20 | + """Test successful guide fetching when template exists.""" |
| 21 | + result = await get_enablement_guide( |
| 22 | + service_platform='ec2', |
| 23 | + service_language='python', |
| 24 | + iac_directory=ABSOLUTE_PATHS['iac'], |
| 25 | + app_directory=ABSOLUTE_PATHS['app'], |
| 26 | + ) |
| 27 | + |
| 28 | + assert '# Application Signals Enablement Guide' in result |
| 29 | + assert 'Placeholder content just to verify the tool can fetch the file.' in result |
| 30 | + assert ABSOLUTE_PATHS['iac'] in result |
| 31 | + assert ABSOLUTE_PATHS['app'] in result |
| 32 | + |
| 33 | + @pytest.mark.asyncio |
| 34 | + async def test_all_valid_platforms(self): |
| 35 | + """Test that all valid platforms are accepted.""" |
| 36 | + valid_platforms = ['ec2', 'ecs', 'lambda', 'eks'] |
| 37 | + |
| 38 | + for platform in valid_platforms: |
| 39 | + result = await get_enablement_guide( |
| 40 | + service_platform=platform, |
| 41 | + service_language='python', |
| 42 | + iac_directory=ABSOLUTE_PATHS['iac'], |
| 43 | + app_directory=ABSOLUTE_PATHS['app'], |
| 44 | + ) |
| 45 | + |
| 46 | + # Should either succeed or say template not found with friendly message |
| 47 | + assert ( |
| 48 | + 'Enablement guide not available' in result |
| 49 | + or '# Application Signals Enablement Guide' in result |
| 50 | + ) |
| 51 | + |
| 52 | + @pytest.mark.asyncio |
| 53 | + async def test_all_valid_languages(self): |
| 54 | + """Test that all valid languages are accepted.""" |
| 55 | + valid_languages = ['python', 'nodejs', 'java', 'dotnet'] |
| 56 | + |
| 57 | + for language in valid_languages: |
| 58 | + result = await get_enablement_guide( |
| 59 | + service_platform='ec2', |
| 60 | + service_language=language, |
| 61 | + iac_directory=ABSOLUTE_PATHS['iac'], |
| 62 | + app_directory=ABSOLUTE_PATHS['app'], |
| 63 | + ) |
| 64 | + |
| 65 | + # Should either succeed or say template not found with friendly message |
| 66 | + assert ( |
| 67 | + 'Enablement guide not available' in result |
| 68 | + or '# Application Signals Enablement Guide' in result |
| 69 | + ) |
| 70 | + |
| 71 | + @pytest.mark.asyncio |
| 72 | + async def test_relative_path_rejected(self): |
| 73 | + """Test that relative paths are rejected with clear error message.""" |
| 74 | + result = await get_enablement_guide( |
| 75 | + service_platform='ec2', |
| 76 | + service_language='python', |
| 77 | + iac_directory='infrastructure/cdk', |
| 78 | + app_directory=ABSOLUTE_PATHS['app'], |
| 79 | + ) |
| 80 | + |
| 81 | + assert 'Error: iac_directory and app_directory must be absolute paths' in result |
| 82 | + assert 'infrastructure/cdk' in result |
| 83 | + |
| 84 | + @pytest.mark.asyncio |
| 85 | + async def test_relative_app_directory_rejected(self): |
| 86 | + """Test that relative app directory is rejected with clear error message.""" |
| 87 | + result = await get_enablement_guide( |
| 88 | + service_platform='ec2', |
| 89 | + service_language='python', |
| 90 | + iac_directory=ABSOLUTE_PATHS['iac'], |
| 91 | + app_directory='app/src', |
| 92 | + ) |
| 93 | + |
| 94 | + assert 'Error: iac_directory and app_directory must be absolute paths' in result |
| 95 | + assert 'app/src' in result |
| 96 | + |
| 97 | + @pytest.mark.asyncio |
| 98 | + async def test_absolute_path_handling(self): |
| 99 | + """Test that absolute paths are handled correctly.""" |
| 100 | + result = await get_enablement_guide( |
| 101 | + service_platform='ec2', |
| 102 | + service_language='python', |
| 103 | + iac_directory=ABSOLUTE_PATHS['iac'], |
| 104 | + app_directory=ABSOLUTE_PATHS['app'], |
| 105 | + ) |
| 106 | + |
| 107 | + assert '# Application Signals Enablement Guide' in result |
| 108 | + assert ABSOLUTE_PATHS['iac'] in result |
| 109 | + assert ABSOLUTE_PATHS['app'] in result |
| 110 | + |
| 111 | + @pytest.mark.asyncio |
| 112 | + async def test_unsupported_language_ruby(self): |
| 113 | + """Test that unsupported language (ruby) returns friendly error message.""" |
| 114 | + result = await get_enablement_guide( |
| 115 | + service_platform='ec2', |
| 116 | + service_language='ruby', |
| 117 | + iac_directory=ABSOLUTE_PATHS['iac'], |
| 118 | + app_directory=ABSOLUTE_PATHS['app'], |
| 119 | + ) |
| 120 | + |
| 121 | + assert 'Enablement guide not available' in result |
| 122 | + assert 'ruby' in result.lower() |
| 123 | + assert 'not currently supported' in result |
| 124 | + |
| 125 | + @pytest.mark.asyncio |
| 126 | + async def test_unsupported_platform_k8s(self): |
| 127 | + """Test that unsupported platform (k8s) returns friendly error message.""" |
| 128 | + result = await get_enablement_guide( |
| 129 | + service_platform='k8s', |
| 130 | + service_language='python', |
| 131 | + iac_directory=ABSOLUTE_PATHS['iac'], |
| 132 | + app_directory=ABSOLUTE_PATHS['app'], |
| 133 | + ) |
| 134 | + |
| 135 | + assert 'Enablement guide not available' in result |
| 136 | + assert 'k8s' in result.lower() |
| 137 | + assert 'not currently supported' in result |
| 138 | + |
| 139 | + @pytest.mark.asyncio |
| 140 | + async def test_case_insensitive_platform(self): |
| 141 | + """Test that uppercase platform names are normalized to lowercase.""" |
| 142 | + result = await get_enablement_guide( |
| 143 | + service_platform='EC2', |
| 144 | + service_language='python', |
| 145 | + iac_directory=ABSOLUTE_PATHS['iac'], |
| 146 | + app_directory=ABSOLUTE_PATHS['app'], |
| 147 | + ) |
| 148 | + |
| 149 | + # Should work the same as lowercase |
| 150 | + assert 'Error: iac_directory and app_directory must be absolute paths' not in result |
| 151 | + assert ( |
| 152 | + '# Application Signals Enablement Guide' in result |
| 153 | + or 'Enablement guide not available' in result |
| 154 | + ) |
| 155 | + |
| 156 | + @pytest.mark.asyncio |
| 157 | + async def test_case_insensitive_language(self): |
| 158 | + """Test that uppercase language names are normalized to lowercase.""" |
| 159 | + result = await get_enablement_guide( |
| 160 | + service_platform='ec2', |
| 161 | + service_language='PYTHON', |
| 162 | + iac_directory=ABSOLUTE_PATHS['iac'], |
| 163 | + app_directory=ABSOLUTE_PATHS['app'], |
| 164 | + ) |
| 165 | + |
| 166 | + # Should work the same as lowercase |
| 167 | + assert 'Error: iac_directory and app_directory must be absolute paths' not in result |
| 168 | + assert ( |
| 169 | + '# Application Signals Enablement Guide' in result |
| 170 | + or 'Enablement guide not available' in result |
| 171 | + ) |
| 172 | + |
| 173 | + @pytest.mark.asyncio |
| 174 | + async def test_whitespace_trimming(self): |
| 175 | + """Test that leading/trailing whitespace is trimmed from inputs.""" |
| 176 | + result = await get_enablement_guide( |
| 177 | + service_platform=' ec2 ', |
| 178 | + service_language=' python ', |
| 179 | + iac_directory=ABSOLUTE_PATHS['iac'], |
| 180 | + app_directory=ABSOLUTE_PATHS['app'], |
| 181 | + ) |
| 182 | + |
| 183 | + # Should work the same as trimmed input |
| 184 | + assert 'Error: iac_directory and app_directory must be absolute paths' not in result |
| 185 | + assert ( |
| 186 | + '# Application Signals Enablement Guide' in result |
| 187 | + or 'Enablement guide not available' in result |
| 188 | + ) |
| 189 | + |
| 190 | + @pytest.mark.asyncio |
| 191 | + async def test_both_paths_relative(self): |
| 192 | + """Test that error message shows both paths when both are relative.""" |
| 193 | + result = await get_enablement_guide( |
| 194 | + service_platform='ec2', |
| 195 | + service_language='python', |
| 196 | + iac_directory='infrastructure/cdk', |
| 197 | + app_directory='app/src', |
| 198 | + ) |
| 199 | + |
| 200 | + assert 'Error: iac_directory and app_directory must be absolute paths' in result |
| 201 | + assert 'infrastructure/cdk' in result |
| 202 | + assert 'app/src' in result |
| 203 | + |
| 204 | + @pytest.mark.asyncio |
| 205 | + async def test_file_read_error(self): |
| 206 | + """Test that file read errors are handled gracefully with helpful message.""" |
| 207 | + with patch('builtins.open', side_effect=PermissionError('Permission denied')): |
| 208 | + result = await get_enablement_guide( |
| 209 | + service_platform='ec2', |
| 210 | + service_language='python', |
| 211 | + iac_directory=ABSOLUTE_PATHS['iac'], |
| 212 | + app_directory=ABSOLUTE_PATHS['app'], |
| 213 | + ) |
| 214 | + |
| 215 | + assert 'Fatal error: Cannot read enablement guide' in result |
| 216 | + assert 'Permission denied' in result |
| 217 | + assert 'file permissions or reinstall' in result |
| 218 | + assert 'issue with the MCP server installation' in result |
0 commit comments