【发布时间】:2022-08-18 22:43:41
【问题描述】:
我在这里使用默认的 Lambda 函数在 AWS 代码中轮换我们的 Aurora 密码:https://github.com/aws-samples/aws-secrets-manager-rotation-lambdas/blob/master/SecretsManagerRDSMariaDBRotationSingleUser/lambda_function.py
我必须在部署此代码之前对其进行测试,但是我不知道该怎么做。有人可以帮忙吗?我知道代码可能完全错误,但只需要一些指导。 我想用 Pytest 测试以下功能。
def test_secret(service_client, arn, token):
\"\"\"Args:
service_client (client): The secrets manager service client
arn (string): The secret ARN or other identifier
token (string): The ClientRequestToken associated with the secret version
Raises:
ResourceNotFoundException: If the secret with the specified arn and stage does not exist
ValueError: If the secret is not valid JSON or valid credentials are found to login to the database
KeyError: If the secret json does not contain the expected keys
\"\"\"
# Try to login with the pending secret, if it succeeds, return
conn = get_connection(get_secret_dict(service_client, arn, \"AWSPENDING\", token))
if conn:
# This is where the lambda will validate the user\'s permissions. Uncomment/modify the below lines to
# tailor these validations to your needs
try:
with conn.cursor() as cur:
cur.execute(\"SELECT NOW()\")
conn.commit()
finally:
conn.close()
logger.info(\"testSecret: Successfully signed into MariaDB DB with AWSPENDING secret in %s.\" % arn)
return
else:
logger.error(\"testSecret: Unable to log into database with pending secret of secret ARN %s\" % arn)
raise ValueError(\"Unable to log into database with pending secret of secret ARN %s\" % arn)
import lambda_function.py as testpass
import boto3
import moto import mock_secretsmanager
#Not sure where to get these values from to mock\"
token = \"akd93939-383838-999388\"
arn = \"secret-arn\"
token = \"9393939302931883487\"
@mock_secretsmanager
def test_testsecret(mock_secret_manager):
conn = boto3.client(\"secretsmanager\", region_name=\"us-east-1\")
test = testpass.test_secret(\"secretsmanager\", arn, token)
assert test
-
请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。
标签: python mocking pytest aws-secrets-manager