Best Python code snippet using localstack_python
fixtures.py
Source:fixtures.py  
...503            return check_set.get("ExecutionStatus") == "EXECUTE_COMPLETE"504        return _inner505    return _is_change_set_finished506@pytest.fixture507def wait_until_lambda_ready(lambda_client):508    def _wait_until_ready(function_name: str, qualifier: str = None):509        def _is_not_pending():510            kwargs = {}511            if qualifier:512                kwargs["Qualifier"] = qualifier513            try:514                result = (515                    lambda_client.get_function(FunctionName=function_name)["Configuration"]["State"]516                    != "Pending"517                )518                LOG.debug(f"lambda state result: {result=}")519                return result520            except Exception as e:521                LOG.error(e)522                raise523        wait_until(_is_not_pending)524    return _wait_until_ready525role_assume_policy = """526{527  "Version": "2012-10-17",528  "Statement": [529    {530      "Effect": "Allow",531      "Principal": {532        "Service": "lambda.amazonaws.com"533      },534      "Action": "sts:AssumeRole"535    }536  ]537}538""".strip()539role_policy = """540{541    "Version": "2012-10-17",542    "Statement": [543        {544            "Effect": "Allow",545            "Action": [546                "logs:CreateLogGroup",547                "logs:CreateLogStream",548                "logs:PutLogEvents"549            ],550            "Resource": [551                "*"552            ]553        }554    ]555}556""".strip()557@pytest.fixture558def create_lambda_function(lambda_client, iam_client, wait_until_lambda_ready):559    lambda_arns = []560    role_names = []561    policy_arns = []562    def _create_lambda_function(*args, **kwargs):563        kwargs["client"] = lambda_client564        func_name = kwargs.get("func_name")565        assert func_name566        del kwargs["func_name"]567        if not kwargs.get("role"):568            role_name = f"lambda-autogenerated-{short_uid()}"569            role_names.append(role_name)570            role = iam_client.create_role(571                RoleName=role_name, AssumeRolePolicyDocument=role_assume_policy572            )["Role"]573            policy_name = f"lambda-autogenerated-{short_uid()}"574            policy_arn = iam_client.create_policy(575                PolicyName=policy_name, PolicyDocument=role_policy576            )["Policy"]["Arn"]577            policy_arns.append(policy_arn)578            iam_client.attach_role_policy(RoleName=role_name, PolicyArn=policy_arn)579            kwargs["role"] = role["Arn"]580        def _create_function():581            resp = testutil.create_lambda_function(func_name, **kwargs)582            lambda_arns.append(resp["CreateFunctionResponse"]["FunctionArn"])583            wait_until_lambda_ready(function_name=func_name)584            return resp585        # @AWS, takes about 10s until the role/policy is "active", until then it will fail586        # localstack should normally not require the retries and will just continue here587        return retry(_create_function, retries=3, sleep=4)588    yield _create_lambda_function589    for arn in lambda_arns:590        try:591            lambda_client.delete_function(FunctionName=arn)592        except Exception:593            LOG.debug(f"Unable to delete function {arn=} in cleanup")594    for role_name in role_names:595        try:596            iam_client.delete_role(RoleName=role_name)597        except Exception:...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
