|
| 1 | +import json |
| 2 | +from test.aibs_informatics_aws_utils.base import AwsBaseTest |
| 3 | + |
| 4 | +import boto3 |
| 5 | +from moto import mock_iam, mock_lambda_simple, mock_sts |
| 6 | +from pytest import raises |
| 7 | + |
| 8 | +from aibs_informatics_aws_utils.lambda_ import ( |
| 9 | + get_lambda_function_file_systems, |
| 10 | + get_lambda_function_url, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +@mock_sts |
| 15 | +class LambdaTests(AwsBaseTest): |
| 16 | + def setUp(self) -> None: |
| 17 | + super().setUp() |
| 18 | + self.set_aws_credentials() |
| 19 | + |
| 20 | + def get_role_arn(self) -> str: |
| 21 | + return boto3.client("iam").create_role( |
| 22 | + RoleName="foo", |
| 23 | + AssumeRolePolicyDocument=json.dumps( |
| 24 | + { |
| 25 | + "Version": "2012-10-17", |
| 26 | + "Statement": [ |
| 27 | + { |
| 28 | + "Effect": "Allow", |
| 29 | + "Principal": {"Service": "lambda.amazonaws.com"}, |
| 30 | + "Action": "sts:AssumeRole", |
| 31 | + } |
| 32 | + ], |
| 33 | + } |
| 34 | + ), |
| 35 | + )["Role"]["Arn"] |
| 36 | + |
| 37 | + @mock_iam |
| 38 | + @mock_lambda_simple |
| 39 | + def test__get_lambda_function_file_systems__no_file_systems(self): |
| 40 | + # Set up lambda |
| 41 | + lambda_client = boto3.client("lambda") |
| 42 | + file_system_configs = [ |
| 43 | + { |
| 44 | + "Arn": "arn:aws:elasticfilesystem:us-west-2:123456789012:access-point/fsap-1234abcd", |
| 45 | + "LocalMountPath": "/mnt/efs1", |
| 46 | + }, |
| 47 | + { |
| 48 | + "Arn": "arn:aws:elasticfilesystem:us-west-2:123456789012:access-point/fsap-1234abcd2", |
| 49 | + "LocalMountPath": "/mnt/efs2", |
| 50 | + }, |
| 51 | + ] |
| 52 | + lambda_client.create_function( |
| 53 | + FunctionName="test", |
| 54 | + Runtime="python3.8", |
| 55 | + Handler="test", |
| 56 | + Role=self.get_role_arn(), |
| 57 | + Code={"ZipFile": b"bar"}, |
| 58 | + Environment={"Variables": {"TEST": "test"}}, |
| 59 | + # NOTE: FileSystemConfigs is not supported by moto yet, so this is meaningless |
| 60 | + FileSystemConfigs=file_system_configs, |
| 61 | + ) |
| 62 | + # HACK: moto doesn't support FileSystemConfigs yet, so we have to patch it in |
| 63 | + # here we will fetch the actual response and then add the FileSystemConfigs |
| 64 | + response = lambda_client.get_function_configuration(FunctionName="test") |
| 65 | + with self.stub(lambda_client) as lambda_stubber: |
| 66 | + lambda_stubber.add_response( |
| 67 | + "get_function_configuration", |
| 68 | + { |
| 69 | + **response, |
| 70 | + **{"FileSystemConfigs": file_system_configs}, |
| 71 | + }, |
| 72 | + expected_params={"FunctionName": "test"}, |
| 73 | + ) |
| 74 | + self.create_patch( |
| 75 | + "aibs_informatics_aws_utils.lambda_.get_lambda_client", return_value=lambda_client |
| 76 | + ) |
| 77 | + |
| 78 | + actual_file_system_configs = get_lambda_function_file_systems("test") |
| 79 | + self.assertListEqual(actual_file_system_configs, file_system_configs) |
| 80 | + |
| 81 | + @mock_iam |
| 82 | + @mock_lambda_simple |
| 83 | + def test__get_lambda_function_url__with_url(self): |
| 84 | + lambda_client = boto3.client("lambda") |
| 85 | + lambda_client.create_function( |
| 86 | + FunctionName="test", |
| 87 | + Runtime="python3.8", |
| 88 | + Handler="test", |
| 89 | + Role=self.get_role_arn(), |
| 90 | + Code={"ZipFile": b"bar"}, |
| 91 | + Environment={"Variables": {"TEST": "test"}}, |
| 92 | + ) |
| 93 | + response = lambda_client.create_function_url_config( |
| 94 | + FunctionName="test", AuthType="AWS_IAM" |
| 95 | + ) |
| 96 | + |
| 97 | + assert get_lambda_function_url("test") == response["FunctionUrl"] |
| 98 | + |
| 99 | + @mock_iam |
| 100 | + @mock_lambda_simple |
| 101 | + def test__get_lambda_function_url__handles_no_url(self): |
| 102 | + lambda_client = boto3.client("lambda") |
| 103 | + lambda_client.create_function( |
| 104 | + FunctionName="test", |
| 105 | + Runtime="python3.8", |
| 106 | + Handler="test", |
| 107 | + Role=self.get_role_arn(), |
| 108 | + Code={"ZipFile": b"bar"}, |
| 109 | + Environment={"Variables": {"TEST": "test"}}, |
| 110 | + ) |
| 111 | + |
| 112 | + @mock_iam |
| 113 | + @mock_lambda_simple |
| 114 | + def test__get_lambda_function_url__handles_invalid_function_name(self): |
| 115 | + with raises(ValueError): |
| 116 | + get_lambda_function_url("@#$#$@#$@#$") |
0 commit comments