@@ -263,6 +263,85 @@ def test_add_new_relic_apm_lambda_mode(aws_credentials, mock_function_config):
263263 )
264264
265265
266+ def test_install_apm (aws_credentials , mock_function_config ):
267+ mock_session = MagicMock ()
268+ mock_session .region_name = "us-east-1"
269+ expected_tags_after_tagging = {
270+ "NR.Apm.Lambda.Mode" : "true" ,
271+ }
272+
273+ with patch (
274+ "newrelic_lambda_cli.layers._get_license_key_outputs"
275+ ) as mock_get_license_key_outputs :
276+ mock_client = mock_session .client .return_value
277+
278+ mock_client .get_function .reset_mock (return_value = True )
279+
280+ config = mock_function_config ("python3.12" )
281+ mock_client .get_function .return_value = config
282+
283+ mock_get_license_key_outputs .return_value = ("license_arn" , "12345" , "policy" )
284+
285+ try :
286+ install (
287+ layer_install (
288+ session = mock_session ,
289+ aws_region = "us-east-1" ,
290+ nr_account_id = 12345 ,
291+ apm = True ,
292+ ),
293+ "APMLambda" ,
294+ )
295+ except UsageError as e :
296+ print (f"UsageError: { e } " )
297+
298+ mock_client .get_function .reset_mock ()
299+ config = mock_function_config ("python3.12" )
300+ mock_client .get_function .return_value = config
301+ mock_client .list_tags .return_value = {"Tags" : expected_tags_after_tagging }
302+ assert (
303+ install (
304+ layer_install (nr_account_id = 12345 , session = mock_session ), "APMLambda"
305+ )
306+ is True
307+ )
308+
309+ mock_client .assert_has_calls ([call .get_function (FunctionName = "APMLambda" )])
310+ mock_client .assert_has_calls (
311+ [
312+ call .update_function_configuration (
313+ FunctionName = "arn:aws:lambda:us-east-1:5558675309:function:aws-python3-dev-hello" , # noqa
314+ Environment = {
315+ "Variables" : {
316+ "EXISTING_ENV_VAR" : "Hello World" ,
317+ "NEW_RELIC_ACCOUNT_ID" : "12345" ,
318+ "NEW_RELIC_LAMBDA_HANDLER" : "original_handler" ,
319+ "NEW_RELIC_LAMBDA_EXTENSION_ENABLED" : "false" ,
320+ "NEW_RELIC_APM_LAMBDA_MODE" : "True" ,
321+ }
322+ },
323+ Layers = ANY ,
324+ Handler = "newrelic_lambda_wrapper.handler" ,
325+ )
326+ ]
327+ )
328+
329+ mock_client .assert_has_calls (
330+ [
331+ call .tag_resource (
332+ Resource = "arn:aws:lambda:us-east-1:5558675309:function:aws-python3-dev-hello" ,
333+ Tags = {
334+ "NR.Apm.Lambda.Mode" : "true" ,
335+ },
336+ )
337+ ]
338+ )
339+
340+ tags_from_list_tags = mock_client .list_tags (Resource = "APMLambda" )["Tags" ]
341+
342+ assert tags_from_list_tags == expected_tags_after_tagging
343+
344+
266345@mock_aws
267346def test_add_new_relic_dotnet (aws_credentials , mock_function_config ):
268347 session = boto3 .Session (region_name = "us-east-1" )
0 commit comments