PK!cQBrr README.mdpytest-serverless --- Mock local resources for serverless framework. | master | PyPI | Python | Licence | | --- | --- | --- | --- | | [![Build Status](https://travis-ci.org/whisller/pytest-serverless.svg?branch=master)](https://travis-ci.org/whisller/pytest-serverless) | [![PyPI](https://img.shields.io/pypi/v/pytest-serverless.svg)](https://pypi.org/project/pytest-serverless/) | ![](https://img.shields.io/pypi/pyversions/pytest-serverless.svg) | ![](https://img.shields.io/pypi/l/pytest-serverless.svg) | ## Installation ```sh pip install pytest-serverless ``` ## What problem it tries to solve? When building your project with [serverless](https://serverless.com/) most likely you will create [resources](https://serverless.com/framework/docs/providers/aws/guide/resources/) like dynamodb tables, sqs queues, sns topics. During writing tests you will have to mock those in [moto](https://github.com/spulec/moto). This pytest plugin tries to automate this process by reading `serverless.yml` file and create mocks of resources for you. ## Usage Mark your test with `@pytest.mark.usefixtures("serverless")`, and rest will be done by plugin. ```python import boto3 import pytest @pytest.mark.usefixtures("serverless") def test(): table = boto3.resource("dynamodb").Table("my-microservice.my-table") count_of_items = len(table.scan()["Items"]) assert count_of_items == 0 ``` ## Supported resources ### AWS::DynamoDB::Table ### AWS::SQS::Queue ## Issues? Plugin is in early stage of development, so you might find some bugs or missing functionality. If possible create pull request (with tests) that fixes particular problem. PK!X pytest_serverless.pyimport os import re from collections import defaultdict import boto3 from box import Box import pytest import yaml @pytest.fixture() def serverless(): is_serverless = os.path.isfile("serverless.yml") if not is_serverless: raise Exception("No serverless.yml file found!") with open(os.path.join(os.getcwd(), "serverless.yml")) as f: serverless_yml_content = f.read() serverless_yml_content = remove_env_variables(serverless_yml_content) serverless_yml_dict = replace_self_variables(serverless_yml_content) actions_before = [] actions_after = [] resources = defaultdict(list) for resource_name, definition in ( serverless_yml_dict.get("resources", {}).get("Resources", {}).items() ): resources[definition["Type"]].append(definition) if resources.get("AWS::DynamoDB::Table"): dynamodb = handle_dynamodb_table(resources["AWS::DynamoDB::Table"]) actions_before.append(dynamodb[0]) actions_after.append(dynamodb[1]) if resources.get("AWS::SQS::Queue"): sqs = handle_sqs_queue(resources["AWS::SQS::Queue"]) actions_before.append(sqs[0]) actions_after.append(sqs[1]) for action in actions_before: action() yield for action in actions_after: action() def handle_dynamodb_table(resources): from moto import mock_dynamodb2 dynamodb = mock_dynamodb2() def before(): dynamodb.start() for resource_definition in resources: boto3.resource("dynamodb").create_table(**resource_definition["Properties"]) def after(): for resource_definition in resources: boto3.client("dynamodb").delete_table( TableName=resource_definition["Properties"]["TableName"] ) dynamodb.stop() return before, after def handle_sqs_queue(resources): from moto import mock_sqs sqs = mock_sqs() def before(): sqs.start() for resource_definition in resources: boto3.resource("sqs").create_queue(**resource_definition["Properties"]) def after(): sqs_client = boto3.client("sqs") for resource_definition in resources: sqs_client.delete_queue( QueueUrl=sqs_client.get_queue_url( QueueName=resource_definition["Properties"]["QueueName"] )["QueueUrl"] ) sqs.stop() return before, after def find_self_variables_to_replace(content): return re.findall(r"(\${self:([a-zA-Z._\-]+)})", content) def replace_self_variables(serverless_yml_content): variables_to_replace = find_self_variables_to_replace(serverless_yml_content) for variable in variables_to_replace: my_box = Box.from_yaml(serverless_yml_content) try: value = eval(f"my_box.{variable[1]}") serverless_yml_content = serverless_yml_content.replace(variable[0], value) except AttributeError: pass return yaml.safe_load(serverless_yml_content) def find_env_variables_to_replace(content): return re.findall(r"(\${env:([a-zA-Z._\-]+),?(.*)})", content) def remove_env_variables(serverless_yml_content): variables_to_replace = find_env_variables_to_replace(serverless_yml_content) for variable in variables_to_replace: serverless_yml_content = serverless_yml_content.replace(variable[0], "") return serverless_yml_content PK!H08)2pytest_serverless-0.5.0.dist-info/entry_points.txt.,I-.14*N-*K-I-.#DPK!!O..)pytest_serverless-0.5.0.dist-info/LICENSEMIT License Copyright (c) 2019 Daniel Ancuta Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!Hu)GTU'pytest_serverless-0.5.0.dist-info/WHEEL HM K-*ϳR03rOK-J,/R(O-)$qzd&Y)r$UV&UrPK!H7 *pytest_serverless-0.5.0.dist-info/METADATAVMsHϯ TlʎRwT* R#Z3#gd88$D+aE/j#U- j,s9V}~٨. k ae,Bw4U ̴*`7E3#gJtXfme0L)U.2iu: cirxþbP:1ˈk`PVt9`dtQJἌk+XGؐ|Ek % rE*nNx{BkWCҖxH r [fny$eV4A,/2fQ8;mc>X)7 pȨ|{ώt0Z6AN{7AU3o'yi(ci 6LL*Ylo 񆷙@Ms,@ZNp@1*ͰSvC,bH ubtIlC7L)X+r`0Qqݘ˄J Ӛ~+'t;J`f` Xߥˑ!jr[hiݷYG9z5 $0xTuq80vCD]*S2^X{V! _E LkP8E}{&E;oSwcgYM8Q=TZUM;.ÒɢRڶzyY3f1N2ngN8HS4A!c;X19ubU Cs,M,no7-VR/NaQ]1Yן(>(_団b]cge1(4iKkڛW#Wyz2,1c4%*hzN;3K3Md+gK @pӿG+ֹAAPK!HldbF(pytest_serverless-0.5.0.dist-info/RECORDл0~%Xl GaJ %ߊj?&t/ϲf]Ev,Jwi̺>#UDNP!u \8הs rڢ= =R7GBꛟ4 JbyE?چS,T(bB>GTZCLn38r q-DxyYS#I1ZUB p/?,NUX?YRyWNb8'J]K8 uMd|j%2zc_nv NάP@ַ D}xPK!cQBrr README.mdPK!X pytest_serverless.pyPK!H08)2Vpytest_serverless-0.5.0.dist-info/entry_points.txtPK!!O..)pytest_serverless-0.5.0.dist-info/LICENSEPK!Hu)GTU'9pytest_serverless-0.5.0.dist-info/WHEELPK!H7 *pytest_serverless-0.5.0.dist-info/METADATAPK!HldbF(pytest_serverless-0.5.0.dist-info/RECORDPK3\