|
2 | 2 | Tests for samcli.lib.utils.name_utils module
|
3 | 3 | """
|
4 | 4 |
|
5 |
| -import pytest |
| 5 | +from unittest import TestCase |
| 6 | + |
| 7 | +from parameterized import parameterized |
6 | 8 |
|
7 | 9 | from samcli.lib.utils.name_utils import normalize_lambda_function_name, InvalidFunctionNameException
|
8 | 10 |
|
9 | 11 |
|
10 |
| -class TestNormalizeLambdaFunctionName: |
| 12 | +class TestNormalizeLambdaFunctionName(TestCase): |
11 | 13 | """Test cases for normalize_lambda_function_name function"""
|
12 | 14 |
|
13 |
| - def test_returns_function_name_unchanged_when_not_arn(self): |
14 |
| - """Test that regular function names are returned unchanged""" |
15 |
| - function_name = "my-function" |
16 |
| - result = normalize_lambda_function_name(function_name) |
17 |
| - assert result == function_name |
18 |
| - |
19 |
| - def test_extracts_function_name_from_basic_lambda_arn(self): |
20 |
| - """Test extracting function name from basic Lambda ARN""" |
21 |
| - arn = "arn:aws:lambda:us-east-1:123456789012:function:my-function" |
22 |
| - result = normalize_lambda_function_name(arn) |
23 |
| - assert result == "my-function" |
24 |
| - |
25 |
| - def test_extracts_function_name_from_versioned_lambda_arn(self): |
26 |
| - """Test extracting function name from versioned Lambda ARN""" |
27 |
| - arn = "arn:aws:lambda:us-east-1:123456789012:function:my-function:$LATEST" |
28 |
| - result = normalize_lambda_function_name(arn) |
29 |
| - assert result == "my-function" |
30 |
| - |
31 |
| - def test_extracts_function_name_from_numbered_version_lambda_arn(self): |
32 |
| - """Test extracting function name from numbered version Lambda ARN""" |
33 |
| - arn = "arn:aws:lambda:us-east-1:123456789012:function:my-function:1" |
34 |
| - result = normalize_lambda_function_name(arn) |
35 |
| - assert result == "my-function" |
36 |
| - |
37 |
| - def test_extracts_function_name_from_different_partition(self): |
38 |
| - """Test extracting function name from Lambda ARN in different partition""" |
39 |
| - arn = "arn:aws-cn:lambda:cn-north-1:123456789012:function:my-function" |
40 |
| - result = normalize_lambda_function_name(arn) |
41 |
| - assert result == "my-function" |
42 |
| - |
43 |
| - def test_extracts_function_name_from_partial_arn(self): |
44 |
| - """Test extracting function name from partial ARN format""" |
45 |
| - partial_arn = "123456789012:function:my-function" |
46 |
| - result = normalize_lambda_function_name(partial_arn) |
47 |
| - assert result == "my-function" |
48 |
| - |
49 |
| - def test_extracts_function_name_from_partial_arn_with_version(self): |
50 |
| - """Test extracting function name from partial ARN with version""" |
51 |
| - partial_arn = "123456789012:function:my-function:$LATEST" |
52 |
| - result = normalize_lambda_function_name(partial_arn) |
53 |
| - assert result == "my-function" |
54 |
| - |
55 |
| - def test_extracts_function_name_from_partial_arn_with_numeric_version(self): |
56 |
| - """Test extracting function name from partial ARN with numeric version""" |
57 |
| - partial_arn = "123456789012:function:my-function:1" |
58 |
| - result = normalize_lambda_function_name(partial_arn) |
59 |
| - assert result == "my-function" |
60 |
| - |
61 |
| - def test_extracts_function_name_with_alias(self): |
62 |
| - """Test extracting function name from function name with alias""" |
63 |
| - function_with_alias = "my-function:v1" |
64 |
| - result = normalize_lambda_function_name(function_with_alias) |
65 |
| - assert result == "my-function" |
66 |
| - |
67 |
| - def test_extracts_function_name_with_latest_alias(self): |
68 |
| - """Test extracting function name from function name with $LATEST alias""" |
69 |
| - function_with_alias = "my-function:$LATEST" |
70 |
| - result = normalize_lambda_function_name(function_with_alias) |
71 |
| - assert result == "my-function" |
72 |
| - |
73 |
| - def test_raises_exception_for_non_lambda_arn(self): |
74 |
| - """Test that non-Lambda ARNs raise InvalidFunctionNameException""" |
75 |
| - arn = "arn:aws:s3:::my-bucket/my-key" |
76 |
| - with pytest.raises(InvalidFunctionNameException): |
77 |
| - normalize_lambda_function_name(arn) |
78 |
| - |
79 |
| - def test_returns_unchanged_for_invalid_arn(self): |
80 |
| - """Test that invalid ARNs are returned unchanged""" |
81 |
| - invalid_arn = "not-an-arn" |
82 |
| - result = normalize_lambda_function_name(invalid_arn) |
83 |
| - assert result == invalid_arn |
84 |
| - |
85 |
| - def test_raises_exception_for_malformed_arn(self): |
86 |
| - """Test that malformed ARNs raise InvalidFunctionNameException""" |
87 |
| - malformed_arn = "arn:aws:lambda" |
88 |
| - with pytest.raises(InvalidFunctionNameException): |
89 |
| - normalize_lambda_function_name(malformed_arn) |
90 |
| - |
91 |
| - def test_returns_unchanged_for_invalid_partial_arn(self): |
92 |
| - """Test that invalid partial ARNs are returned unchanged""" |
93 |
| - invalid_partial = "123456789012:invalid:my-function" |
94 |
| - result = normalize_lambda_function_name(invalid_partial) |
95 |
| - assert result == invalid_partial |
96 |
| - |
97 |
| - def test_handles_function_name_with_hyphens_and_underscores(self): |
98 |
| - """Test handling function names with special characters""" |
99 |
| - function_name = "my-function_name" |
100 |
| - result = normalize_lambda_function_name(function_name) |
101 |
| - assert result == function_name |
102 |
| - |
103 |
| - def test_extracts_function_name_with_special_characters_from_arn(self): |
104 |
| - """Test extracting function name with special characters from ARN""" |
105 |
| - arn = "arn:aws:lambda:us-east-1:123456789012:function:my-function_name-test" |
106 |
| - result = normalize_lambda_function_name(arn) |
107 |
| - assert result == "my-function_name-test" |
108 |
| - |
109 |
| - def test_extracts_function_name_with_special_characters_from_partial_arn(self): |
110 |
| - """Test extracting function name with special characters from partial ARN""" |
111 |
| - partial_arn = "123456789012:function:my-function_name-test" |
112 |
| - result = normalize_lambda_function_name(partial_arn) |
113 |
| - assert result == "my-function_name-test" |
114 |
| - |
115 |
| - def test_handles_complex_function_names(self): |
116 |
| - """Test handling complex function names with multiple special characters""" |
117 |
| - complex_names = [ |
118 |
| - "my-function-with-many-hyphens", |
119 |
| - "my_function_with_underscores", |
120 |
| - "MyFunctionWithCamelCase", |
121 |
| - "function123WithNumbers", |
122 |
| - "function-with_mixed-chars123", |
| 15 | + @parameterized.expand( |
| 16 | + [ |
| 17 | + # Simple function names (pass-through) |
| 18 | + ("my-function", "my-function"), |
| 19 | + ("function-with_mixed-chars123", "function-with_mixed-chars123"), |
| 20 | + # Full ARNs with versions/aliases |
| 21 | + ("arn:aws:lambda:us-east-1:123456789012:function:my-function", "my-function"), |
| 22 | + ("arn:aws:lambda:us-east-1:123456789012:function:my-function:$LATEST", "my-function"), |
| 23 | + ("arn:aws:lambda:us-east-1:123456789012:function:my-function:1", "my-function"), |
| 24 | + ("arn:aws-cn:lambda:cn-north-1:123456789012:function:my-function", "my-function"), |
| 25 | + # Partial ARNs |
| 26 | + ("123456789012:function:my-function", "my-function"), |
| 27 | + ("123456789012:function:my-function:$LATEST", "my-function"), |
| 28 | + # Function names with versions/aliases |
| 29 | + ("my-function:$LATEST", "my-function"), |
| 30 | + ("my-function:v1", "my-function"), |
| 31 | + # Invalid partial ARN format (should pass through) |
| 32 | + ("123456789012:invalid:my-function", "123456789012:invalid:my-function"), |
123 | 33 | ]
|
124 |
| - |
125 |
| - for name in complex_names: |
126 |
| - # Test plain function name |
127 |
| - result = normalize_lambda_function_name(name) |
128 |
| - assert result == name |
129 |
| - |
130 |
| - # Test with alias |
131 |
| - result = normalize_lambda_function_name(f"{name}:v1") |
132 |
| - assert result == name |
133 |
| - |
134 |
| - # Test with full ARN |
135 |
| - arn = f"arn:aws:lambda:us-east-1:123456789012:function:{name}" |
136 |
| - result = normalize_lambda_function_name(arn) |
137 |
| - assert result == name |
138 |
| - |
139 |
| - # Test with partial ARN |
140 |
| - partial_arn = f"123456789012:function:{name}" |
141 |
| - result = normalize_lambda_function_name(partial_arn) |
142 |
| - assert result == name |
143 |
| - |
144 |
| - def test_raises_exception_for_invalid_function_names(self): |
145 |
| - """Test that invalid function names raise InvalidFunctionNameException""" |
146 |
| - invalid_names = [ |
147 |
| - ":HelloWorld", # Starts with colon |
148 |
| - "HelloWorld:", # Ends with colon (empty alias) |
149 |
| - "", # Empty string |
150 |
| - ":", # Just colon |
151 |
| - "function-with-invalid-chars!", # Invalid characters |
152 |
| - "123456789012:function:", # Partial ARN with empty function name |
| 34 | + ) |
| 35 | + def test_normalize_lambda_function_name(self, input_name, expected_output): |
| 36 | + """Test normalize_lambda_function_name with various inputs""" |
| 37 | + result = normalize_lambda_function_name(input_name) |
| 38 | + self.assertEqual(result, expected_output) |
| 39 | + |
| 40 | + @parameterized.expand( |
| 41 | + [ |
| 42 | + ("arn:aws:s3:::my-bucket/my-key",), |
| 43 | + ("arn:aws:lambda",), |
| 44 | + (":HelloWorld",), |
| 45 | + ("HelloWorld:",), |
| 46 | + ("",), |
| 47 | + (":",), |
| 48 | + ("function-with-invalid-chars!",), |
| 49 | + ("123456789012:function:",), |
153 | 50 | ]
|
| 51 | + ) |
| 52 | + def test_raises_exception_for_invalid_function_names(self, invalid_name): |
| 53 | + """Test that invalid function names raise InvalidFunctionNameException""" |
| 54 | + with self.assertRaises(InvalidFunctionNameException) as exc_info: |
| 55 | + normalize_lambda_function_name(invalid_name) |
154 | 56 |
|
155 |
| - for invalid_name in invalid_names: |
156 |
| - with pytest.raises(InvalidFunctionNameException) as exc_info: |
157 |
| - normalize_lambda_function_name(invalid_name) |
158 |
| - |
159 |
| - # Check that the error message matches AWS Lambda's format |
160 |
| - assert "1 validation error detected" in str(exc_info.value) |
161 |
| - assert f"Value '{invalid_name}' at 'functionName'" in str(exc_info.value) |
162 |
| - assert "failed to satisfy constraint" in str(exc_info.value) |
| 57 | + # Check that the error message matches AWS Lambda's format |
| 58 | + self.assertIn("1 validation error detected", str(exc_info.exception)) |
| 59 | + self.assertIn(f"Value '{invalid_name}' at 'functionName'", str(exc_info.exception)) |
| 60 | + self.assertIn("failed to satisfy constraint", str(exc_info.exception)) |
0 commit comments