Skip to content

Commit

Permalink
Techdebt: Update scaffolding to use mock_aws (#7221)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblommers committed Jan 27, 2024
1 parent dea4a98 commit 8199a88
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 46 deletions.
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ SERVICE_NAME = "default"
TEST_NAMES = "*"

ifeq ($(TEST_SERVER_MODE), true)
# exclude test_kinesisvideoarchivedmedia
# because testing with moto_server is difficult with data-endpoint
TEST_EXCLUDE := --ignore tests/test_kinesisvideoarchivedmedia --ignore tests/test_acm --ignore tests/test_amp --ignore tests/test_awslambda --ignore tests/test_batch --ignore tests/test_ec2 --ignore tests/test_sqs
# Exclude parallel tests
TEST_EXCLUDE := --ignore tests/test_acm --ignore tests/test_amp --ignore tests/test_awslambda --ignore tests/test_batch --ignore tests/test_ec2 --ignore tests/test_sqs
# Parallel tests will be run separate
PARALLEL_TESTS := ./tests/test_acm/ ./tests/test_acmpca/ ./tests/test_amp/ ./tests/test_awslambda ./tests/test_batch ./tests/test_ec2 ./tests/test_sqs
else
Expand Down
35 changes: 7 additions & 28 deletions scripts/scaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import re
import inspect
import importlib
import subprocess
from lxml import etree

import click
Expand All @@ -47,6 +48,7 @@
INPUT_IGNORED_IN_BACKEND = ["Marker", "PageSize"]
OUTPUT_IGNORED_IN_BACKEND = ["NextMarker"]

root_dir = subprocess.check_output(["git", "rev-parse", "--show-toplevel"]).decode().strip()

def print_progress(title, body, color):
"""Prints a color-code message describing current state of progress."""
Expand Down Expand Up @@ -143,29 +145,6 @@ def render_template(tmpl_dir, tmpl_filename, context, service, alt_filename=None
fhandle.write(rendered)


def append_mock_to_init_py(service):
"""Update __init_.py to add line to load the mock service."""
path = os.path.join(os.path.dirname(__file__), "..", "moto", "__init__.py")
with open(path, encoding="utf-8") as fhandle:
lines = [_.replace("\n", "") for _ in fhandle.readlines()]

escaped_service = get_escaped_service(service)
if any(_ for _ in lines if _.startswith(f"^mock_{escaped_service} = lazy_load")):
return
filtered_lines = [_ for _ in lines if re.match("^mock_.*lazy_load(.*)$", _)]
last_import_line_index = lines.index(filtered_lines[-1])

new_line = (
f"mock_{escaped_service} = lazy_load("
f'".{escaped_service}", "mock_{escaped_service}", boto3_name="{service}")'
)
lines.insert(last_import_line_index + 1, new_line)

body = "\n".join(lines) + "\n"
with open(path, "w", encoding="utf-8") as fhandle:
fhandle.write(body)


def initialize_service(service, api_protocol):
"""Create lib and test dirs if they don't exist."""
lib_dir = get_lib_dir(service)
Expand Down Expand Up @@ -213,8 +192,6 @@ def initialize_service(service, api_protocol):
else None
)
render_template(tmpl_dir, tmpl_filename, tmpl_context, service, alt_filename)
# append mock to initi files
append_mock_to_init_py(service)


def to_upper_camel_case(string):
Expand Down Expand Up @@ -340,7 +317,7 @@ def get_func_in_tests(service, operation):
escaped_service = get_escaped_service(service)
random_region = random.choice(["us-east-2", "eu-west-1", "ap-southeast-1"])
body = "\n\n"
body += f"@mock_{escaped_service}\n"
body += f"@mock_aws\n"
body += f"def test_{operation}():\n"
body += f" client = boto3.client(\"{service}\", region_name=\"{random_region}\")\n"
body += f" resp = client.{operation}()\n"
Expand Down Expand Up @@ -593,7 +570,7 @@ def insert_url(service, operation, api_protocol): # pylint: disable=too-many-lo

# generate url pattern
if api_protocol == "rest-json":
new_line = ' "{0}/.*$": response.dispatch,'
new_line = f' "{0}/.*$": {service_class}Response.dispatch,'
elif api_protocol == "rest-xml":
new_line = f' "{{0}}{uri}$": {service_class}Response.{operation},'
else:
Expand Down Expand Up @@ -674,13 +651,15 @@ def main():
"yellow",
)

click.echo("Updating backend index...")
subprocess.check_output([f"{root_dir}/scripts/update_backend_index.py"]).decode().strip()

click.echo(
"\n"
"Please select another operation, or Ctrl-X/Ctrl-C to cancel."
"\n\n"
"Remaining steps after development is complete:\n"
'- Run scripts/implementation_coverage.py,\n'
"- Run scripts/update_backend_index.py."
"\n"
)

Expand Down
6 changes: 1 addition & 5 deletions scripts/template/lib/__init__.py.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
"""{{ escaped_service }} module initialization; sets value for base decorator."""
from .models import {{ escaped_service }}_backends
from ..core.models import base_decorator

mock_{{ escaped_service }} = base_decorator({{ escaped_service }}_backends)
from .models import {{ escaped_service }}_backends #noqa: F401
4 changes: 0 additions & 4 deletions scripts/template/lib/urls.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,5 @@ url_bases = [
r"https?://{{ endpoint_prefix }}\.(.+)\.amazonaws\.com",
]

{% if api_protocol == 'rest-json' %}
response = {{ service_class }}Response()
{% endif %}

url_paths = {
}
2 changes: 1 addition & 1 deletion scripts/template/test/test_service.py.j2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Unit tests for {{ escaped_service }}-supported APIs."""
import boto3

from moto import mock_{{ escaped_service }}
from moto import mock_aws

# See our Development Tips on writing tests for hints on how to write good tests:
# http://docs.getmoto.org/en/latest/docs/contributing/development_tips/tests.html
5 changes: 1 addition & 4 deletions scripts/update_backend_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import black
import pprint

import moto

output_file = "moto/backend_index.py"

script_dir = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -25,7 +23,6 @@


def iter_backend_url_patterns():
path = os.path.dirname(moto.__file__)
for backend in list_of_moto_modules():
# Special case
if backend == "moto_api":
Expand Down Expand Up @@ -55,7 +52,7 @@ def build_backend_url_pattern_index():

def main():
with open(output_path, "w") as fd:
fd.write("# autogenerated by %s\n" % __file__)
fd.write("# autogenerated by moto/scripts/update_backend_index.py\n")
fd.write("import re\n")

print("build backend_url_patterns")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from datetime import timedelta
from unittest import SkipTest

import boto3

from moto import mock_aws
from moto import mock_aws, settings
from moto.core.utils import utcnow


@mock_aws
def test_get_hls_streaming_session_url():
if settings.TEST_SERVER_MODE:
raise SkipTest("Can't mock KinesisVideo, as DataEndpoint is set to real URL")
region_name = "ap-northeast-1"
kvs_client = boto3.client("kinesisvideo", region_name=region_name)
stream_name = "my-stream"
Expand All @@ -30,6 +33,8 @@ def test_get_hls_streaming_session_url():

@mock_aws
def test_get_dash_streaming_session_url():
if settings.TEST_SERVER_MODE:
raise SkipTest("Can't mock KinesisVideo, as DataEndpoint is set to real URL")
region_name = "ap-northeast-1"
kvs_client = boto3.client("kinesisvideo", region_name=region_name)
stream_name = "my-stream"
Expand All @@ -52,6 +57,8 @@ def test_get_dash_streaming_session_url():

@mock_aws
def test_get_clip():
if settings.TEST_SERVER_MODE:
raise SkipTest("Can't mock KinesisVideo, as DataEndpoint is set to real URL")
region_name = "ap-northeast-1"
kvs_client = boto3.client("kinesisvideo", region_name=region_name)
stream_name = "my-stream"
Expand Down

0 comments on commit 8199a88

Please sign in to comment.