| 1 |
# |
| 2 |
# (c) 2022 Red Hat Inc. |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from unittest.mock import MagicMock |
| 8 |
from unittest.mock import call |
| 9 |
from unittest.mock import patch |
| 10 |
|
| 11 |
import pytest |
| 12 |
from botocore.exceptions import BotoCoreError |
| 13 |
|
| 14 |
from ansible_collections.amazon.aws.plugins.modules import lambda_layer_info |
| 15 |
|
| 16 |
mod__list_layer_versions = "ansible_collections.amazon.aws.plugins.modules.lambda_layer_info._list_layer_versions" |
| 17 |
mod__list_layers = "ansible_collections.amazon.aws.plugins.modules.lambda_layer_info._list_layers" |
| 18 |
mod_list_layer_versions = "ansible_collections.amazon.aws.plugins.modules.lambda_layer_info.list_layer_versions" |
| 19 |
mod_list_layers = "ansible_collections.amazon.aws.plugins.modules.lambda_layer_info.list_layers" |
| 20 |
|
| 21 |
|
| 22 |
list_layers_paginate_result = { |
| 23 |
"NextMarker": "002", |
| 24 |
"Layers": [ |
| 25 |
{ |
| 26 |
"LayerName": "test-layer-01", |
| 27 |
"LayerArn": "arn:aws:lambda:eu-west-2:123456789012:layer:test-layer-01", |
| 28 |
"LatestMatchingVersion": { |
| 29 |
"LayerVersionArn": "arn:aws:lambda:eu-west-2:123456789012:layer:test-layer-01:1", |
| 30 |
"Version": 1, |
| 31 |
"Description": "lambda layer created for unit tests", |
| 32 |
"CreatedDate": "2022-09-29T10:31:26.341+0000", |
| 33 |
"CompatibleRuntimes": ["nodejs", "nodejs4.3", "nodejs6.10"], |
| 34 |
"LicenseInfo": "MIT", |
| 35 |
"CompatibleArchitectures": ["arm64"], |
| 36 |
}, |
| 37 |
}, |
| 38 |
{ |
| 39 |
"LayerName": "test-layer-02", |
| 40 |
"LayerArn": "arn:aws:lambda:eu-west-2:123456789012:layer:test-layer-02", |
| 41 |
"LatestMatchingVersion": { |
| 42 |
"LayerVersionArn": "arn:aws:lambda:eu-west-2:123456789012:layer:test-layer-02:1", |
| 43 |
"Version": 1, |
| 44 |
"CreatedDate": "2022-09-29T10:31:26.341+0000", |
| 45 |
"CompatibleArchitectures": ["arm64"], |
| 46 |
}, |
| 47 |
}, |
| 48 |
], |
| 49 |
"ResponseMetadata": { |
| 50 |
"http": "true", |
| 51 |
}, |
| 52 |
} |
| 53 |
|
| 54 |
list_layers_result = [ |
| 55 |
{ |
| 56 |
"layer_name": "test-layer-01", |
| 57 |
"layer_arn": "arn:aws:lambda:eu-west-2:123456789012:layer:test-layer-01", |
| 58 |
"layer_version_arn": "arn:aws:lambda:eu-west-2:123456789012:layer:test-layer-01:1", |
| 59 |
"version": 1, |
| 60 |
"description": "lambda layer created for unit tests", |
| 61 |
"created_date": "2022-09-29T10:31:26.341+0000", |
| 62 |
"compatible_runtimes": ["nodejs", "nodejs4.3", "nodejs6.10"], |
| 63 |
"license_info": "MIT", |
| 64 |
"compatible_architectures": ["arm64"], |
| 65 |
}, |
| 66 |
{ |
| 67 |
"layer_name": "test-layer-02", |
| 68 |
"layer_arn": "arn:aws:lambda:eu-west-2:123456789012:layer:test-layer-02", |
| 69 |
"layer_version_arn": "arn:aws:lambda:eu-west-2:123456789012:layer:test-layer-02:1", |
| 70 |
"version": 1, |
| 71 |
"created_date": "2022-09-29T10:31:26.341+0000", |
| 72 |
"compatible_architectures": ["arm64"], |
| 73 |
}, |
| 74 |
] |
| 75 |
|
| 76 |
|
| 77 |
list_layers_versions_paginate_result = { |
| 78 |
"LayerVersions": [ |
| 79 |
{ |
| 80 |
"CompatibleRuntimes": ["python3.7"], |
| 81 |
"CreatedDate": "2022-09-29T10:31:35.977+0000", |
| 82 |
"LayerVersionArn": "arn:aws:lambda:eu-west-2:123456789012:layer:layer-01:2", |
| 83 |
"LicenseInfo": "MIT", |
| 84 |
"Version": 2, |
| 85 |
"CompatibleArchitectures": ["arm64"], |
| 86 |
}, |
| 87 |
{ |
| 88 |
"CompatibleRuntimes": ["python3.7"], |
| 89 |
"CreatedDate": "2022-09-29T10:31:26.341+0000", |
| 90 |
"Description": "lambda layer first version", |
| 91 |
"LayerVersionArn": "arn:aws:lambda:eu-west-2:123456789012:layer:layer-01:1", |
| 92 |
"LicenseInfo": "GPL-3.0-only", |
| 93 |
"Version": 1, |
| 94 |
}, |
| 95 |
], |
| 96 |
"ResponseMetadata": { |
| 97 |
"http": "true", |
| 98 |
}, |
| 99 |
"NextMarker": "001", |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
list_layers_versions_result = [ |
| 104 |
{ |
| 105 |
"compatible_runtimes": ["python3.7"], |
| 106 |
"created_date": "2022-09-29T10:31:35.977+0000", |
| 107 |
"layer_version_arn": "arn:aws:lambda:eu-west-2:123456789012:layer:layer-01:2", |
| 108 |
"license_info": "MIT", |
| 109 |
"version": 2, |
| 110 |
"compatible_architectures": ["arm64"], |
| 111 |
}, |
| 112 |
{ |
| 113 |
"compatible_runtimes": ["python3.7"], |
| 114 |
"created_date": "2022-09-29T10:31:26.341+0000", |
| 115 |
"description": "lambda layer first version", |
| 116 |
"layer_version_arn": "arn:aws:lambda:eu-west-2:123456789012:layer:layer-01:1", |
| 117 |
"license_info": "GPL-3.0-only", |
| 118 |
"version": 1, |
| 119 |
}, |
| 120 |
] |
| 121 |
|
| 122 |
|
| 123 |
@pytest.mark.parametrize( |
| 124 |
"params,call_args", |
| 125 |
[ |
| 126 |
( |
| 127 |
{"compatible_runtime": "nodejs", "compatible_architecture": "arm64"}, |
| 128 |
{"CompatibleRuntime": "nodejs", "CompatibleArchitecture": "arm64"}, |
| 129 |
), |
| 130 |
( |
| 131 |
{ |
| 132 |
"compatible_runtime": "nodejs", |
| 133 |
}, |
| 134 |
{ |
| 135 |
"CompatibleRuntime": "nodejs", |
| 136 |
}, |
| 137 |
), |
| 138 |
({"compatible_architecture": "arm64"}, {"CompatibleArchitecture": "arm64"}), |
| 139 |
({}, {}), |
| 140 |
], |
| 141 |
) |
| 142 |
@patch(mod__list_layers) |
| 143 |
def test_list_layers_with_latest_version(m__list_layers, params, call_args): |
| 144 |
lambda_client = MagicMock() |
| 145 |
|
| 146 |
m__list_layers.return_value = list_layers_paginate_result |
| 147 |
layers = lambda_layer_info.list_layers(lambda_client, **params) |
| 148 |
|
| 149 |
m__list_layers.assert_has_calls([call(lambda_client, **call_args)]) |
| 150 |
assert layers == list_layers_result |
| 151 |
|
| 152 |
|
| 153 |
@pytest.mark.parametrize( |
| 154 |
"params,call_args", |
| 155 |
[ |
| 156 |
( |
| 157 |
{"name": "layer-01", "compatible_runtime": "nodejs", "compatible_architecture": "arm64"}, |
| 158 |
{"LayerName": "layer-01", "CompatibleRuntime": "nodejs", "CompatibleArchitecture": "arm64"}, |
| 159 |
), |
| 160 |
( |
| 161 |
{ |
| 162 |
"name": "layer-01", |
| 163 |
"compatible_runtime": "nodejs", |
| 164 |
}, |
| 165 |
{ |
| 166 |
"LayerName": "layer-01", |
| 167 |
"CompatibleRuntime": "nodejs", |
| 168 |
}, |
| 169 |
), |
| 170 |
( |
| 171 |
{"name": "layer-01", "compatible_architecture": "arm64"}, |
| 172 |
{"LayerName": "layer-01", "CompatibleArchitecture": "arm64"}, |
| 173 |
), |
| 174 |
({"name": "layer-01"}, {"LayerName": "layer-01"}), |
| 175 |
], |
| 176 |
) |
| 177 |
@patch(mod__list_layer_versions) |
| 178 |
def test_list_layer_versions(m__list_layer_versions, params, call_args): |
| 179 |
lambda_client = MagicMock() |
| 180 |
|
| 181 |
m__list_layer_versions.return_value = list_layers_versions_paginate_result |
| 182 |
layers = lambda_layer_info.list_layer_versions(lambda_client, **params) |
| 183 |
|
| 184 |
m__list_layer_versions.assert_has_calls([call(lambda_client, **call_args)]) |
| 185 |
assert layers == list_layers_versions_result |
| 186 |
|
| 187 |
|
| 188 |
def raise_botocore_exception(): |
| 189 |
return BotoCoreError(error="failed", operation="list_layers") |
| 190 |
|
| 191 |
|
| 192 |
def test_get_layer_version_success(): |
| 193 |
aws_layer_version = { |
| 194 |
"CompatibleRuntimes": ["python3.8"], |
| 195 |
"Content": { |
| 196 |
"CodeSha256": "vqxKx6nTW31obVcB4MYaTWv5H3fBQTn2PHklL9+mF9E=", |
| 197 |
"CodeSize": 9492621, |
| 198 |
"Location": "https://test.s3.us-east-1.amazonaws.com/snapshots/123456789012/test-79b29d149e06?versionId=nmEKA3ZgiP7hce3J", |
| 199 |
}, |
| 200 |
"CreatedDate": "2022-12-05T10:47:32.379+0000", |
| 201 |
"Description": "Python units test layer", |
| 202 |
"LayerArn": "arn:aws:lambda:us-east-1:123456789012:layer:test", |
| 203 |
"LayerVersionArn": "arn:aws:lambda:us-east-1:123456789012:layer:test:2", |
| 204 |
"LicenseInfo": "GPL-3.0-only", |
| 205 |
"Version": 2, |
| 206 |
"ResponseMetadata": {"some-metadata": "some-result"}, |
| 207 |
} |
| 208 |
|
| 209 |
ansible_layer_version = { |
| 210 |
"compatible_runtimes": ["python3.8"], |
| 211 |
"content": { |
| 212 |
"code_sha256": "vqxKx6nTW31obVcB4MYaTWv5H3fBQTn2PHklL9+mF9E=", |
| 213 |
"code_size": 9492621, |
| 214 |
"location": "https://test.s3.us-east-1.amazonaws.com/snapshots/123456789012/test-79b29d149e06?versionId=nmEKA3ZgiP7hce3J", |
| 215 |
}, |
| 216 |
"created_date": "2022-12-05T10:47:32.379+0000", |
| 217 |
"description": "Python units test layer", |
| 218 |
"layer_arn": "arn:aws:lambda:us-east-1:123456789012:layer:test", |
| 219 |
"layer_version_arn": "arn:aws:lambda:us-east-1:123456789012:layer:test:2", |
| 220 |
"license_info": "GPL-3.0-only", |
| 221 |
"version": 2, |
| 222 |
} |
| 223 |
|
| 224 |
lambda_client = MagicMock() |
| 225 |
lambda_client.get_layer_version.return_value = aws_layer_version |
| 226 |
|
| 227 |
layer_name = "test" |
| 228 |
layer_version = 2 |
| 229 |
|
| 230 |
assert [ansible_layer_version] == lambda_layer_info.get_layer_version(lambda_client, layer_name, layer_version) |
| 231 |
lambda_client.get_layer_version.assert_called_once_with(LayerName=layer_name, VersionNumber=layer_version) |
| 232 |
|
| 233 |
|
| 234 |
def test_get_layer_version_failure(): |
| 235 |
lambda_client = MagicMock() |
| 236 |
lambda_client.get_layer_version.side_effect = raise_botocore_exception() |
| 237 |
|
| 238 |
layer_name = MagicMock() |
| 239 |
layer_version = MagicMock() |
| 240 |
|
| 241 |
with pytest.raises(lambda_layer_info.LambdaLayerInfoFailure): |
| 242 |
lambda_layer_info.get_layer_version(lambda_client, layer_name, layer_version) |
| 243 |
|
| 244 |
|
| 245 |
@pytest.mark.parametrize( |
| 246 |
"params", |
| 247 |
[ |
| 248 |
({"name": "test-layer", "compatible_runtime": "nodejs", "compatible_architecture": "arm64"}), |
| 249 |
({"compatible_runtime": "nodejs", "compatible_architecture": "arm64"}), |
| 250 |
], |
| 251 |
) |
| 252 |
@patch(mod__list_layers) |
| 253 |
@patch(mod__list_layer_versions) |
| 254 |
def test_list_layers_with_failure(m__list_layer_versions, m__list_layers, params): |
| 255 |
lambda_client = MagicMock() |
| 256 |
|
| 257 |
if "name" in params: |
| 258 |
m__list_layer_versions.side_effect = raise_botocore_exception() |
| 259 |
test_function = lambda_layer_info.list_layer_versions |
| 260 |
else: |
| 261 |
m__list_layers.side_effect = raise_botocore_exception() |
| 262 |
test_function = lambda_layer_info.list_layers |
| 263 |
|
| 264 |
with pytest.raises(lambda_layer_info.LambdaLayerInfoFailure): |
| 265 |
test_function(lambda_client, **params) |
| 266 |
|
| 267 |
|
| 268 |
def raise_layer_info_exception(exc, msg): |
| 269 |
return lambda_layer_info.LambdaLayerInfoFailure(exc=exc, msg=msg) |
| 270 |
|
| 271 |
|
| 272 |
@pytest.mark.parametrize( |
| 273 |
"params,failure", |
| 274 |
[ |
| 275 |
({"name": "test-layer", "compatible_runtime": "nodejs", "compatible_architecture": "arm64"}, False), |
| 276 |
({"compatible_runtime": "nodejs", "compatible_architecture": "arm64"}, False), |
| 277 |
({"name": "test-layer", "compatible_runtime": "nodejs", "compatible_architecture": "arm64"}, True), |
| 278 |
], |
| 279 |
) |
| 280 |
@patch(mod_list_layers) |
| 281 |
@patch(mod_list_layer_versions) |
| 282 |
def test_execute_module(m_list_layer_versions, m_list_layers, params, failure): |
| 283 |
lambda_client = MagicMock() |
| 284 |
|
| 285 |
module = MagicMock() |
| 286 |
module.params = params |
| 287 |
module.exit_json.side_effect = SystemExit(1) |
| 288 |
module.fail_json_aws.side_effect = SystemExit(2) |
| 289 |
|
| 290 |
method_called, method_not_called = m_list_layers, m_list_layer_versions |
| 291 |
if "name" in params: |
| 292 |
method_not_called, method_called = m_list_layers, m_list_layer_versions |
| 293 |
|
| 294 |
if failure: |
| 295 |
exc = "lambda_layer_exception" |
| 296 |
msg = "this exception has been generated for unit tests" |
| 297 |
|
| 298 |
method_called.side_effect = raise_layer_info_exception(exc, msg) |
| 299 |
|
| 300 |
with pytest.raises(SystemExit): |
| 301 |
lambda_layer_info.execute_module(module, lambda_client) |
| 302 |
|
| 303 |
module.fail_json_aws.assert_called_with(exception=exc, msg=msg) |
| 304 |
|
| 305 |
else: |
| 306 |
result = {"A": "valueA", "B": "valueB"} |
| 307 |
method_called.return_value = result |
| 308 |
|
| 309 |
with pytest.raises(SystemExit): |
| 310 |
lambda_layer_info.execute_module(module, lambda_client) |
| 311 |
|
| 312 |
module.exit_json.assert_called_with(changed=False, layers_versions=result) |
| 313 |
method_called.assert_called_with(lambda_client, **params) |
| 314 |
method_not_called.list_layers.assert_not_called() |