#!/usr/bin/env python3
"""Tests for fetch_dms_arn_formats.py, on pages in the AWS DMS User Guide's markup and Service Reference documents."""
from __future__ import annotations
import json
import pytest
import fetch_dms_arn_formats as f
SYNTAX = ('
An ARN for an AWS DMS resource uses the following syntax:
arn:aws:dms:'
'region:account\n number:'
'resourcetype:resourcename
')
NAME = ('resourcename is the '
'resource name\n assigned to the AWS DMS resource. This is a generated arbitrary string.
')
ROW = ('| {kind} | arn:aws:dms:'
'region:\n account:{word}:\n resourcename'
'\n |
')
def page(syntax: str = SYNTAX, rows: tuple[tuple[str, str], ...] = (("Endpoint", "endpoint"),)) -> str:
table = ('| AWS DMS resource type | ARN format |
'
+ "".join(ROW.format(kind=kind, word=word) for kind, word in rows) + "
")
return f'Constructing an ARN
{syntax}
{table}
' \
f''
def reference(*formats: tuple[str, str]) -> str:
return json.dumps({"Name": "dms", "Resources": [{"Name": name, "ARNFormats": [arn]} for name, arn in formats]})
ENDPOINT = ("Endpoint", "arn:${Partition}:dms:${Region}:${Account}:endpoint:*")
def test_the_syntax_its_resource_name_and_its_table_are_copied_as_they_stand():
built = f.build(page(), reference(ENDPOINT))
assert built["syntax"] == {"sentence": "An ARN for an AWS DMS resource uses the following syntax:",
"format": "arn:aws:dms::::"}
assert built["resourceName"] == ("resourcename is the resource name assigned to the AWS DMS resource. This is a "
"generated arbitrary string.")
assert built["table"] == [{"resourceType": "Endpoint",
"format": "arn:aws:dms:::endpoint:"}]
def test_a_reference_format_ending_in_a_wildcard_takes_the_syntax_placeholder_there():
built = f.build(page(), reference(ENDPOINT, ("Certificate", "arn:${Partition}:dms:${Region}:${Account}:cert:*"),
("Other", "arn:${Partition}:dms:${Region}:${Account}:other/${OtherId}")))
assert built["arnFormats"] == {
"Certificate": "arn:${Partition}:dms:${Region}:${Account}:cert:${resourcename}",
"Endpoint": "arn:${Partition}:dms:${Region}:${Account}:endpoint:${resourcename}"}
def test_a_reworded_syntax_is_refused():
reworded = SYNTAX.replace("resourcename", "resourceid").replace(
"resourcetype:", "type:")
with pytest.raises(ValueError, match="in no shape it used"):
f.build(page(syntax=reworded), reference(ENDPOINT))
with pytest.raises(ValueError, match="before a syntax"):
f.build(page(syntax="DMS ARNs look like this.
"), reference(ENDPOINT))
def test_a_table_row_no_reference_format_has_is_refused():
with pytest.raises(ValueError, match="no longer agree"):
f.build(page(rows=(("Endpoint", "endpoint"), ("Widget", "widget"))), reference(ENDPOINT))
def test_stale_compares_everything_but_the_date():
built = f.build(page(), reference(ENDPOINT))
assert not f.stale(dict(built, _source={"retrieved": "2000-01-01"}), page(), reference(ENDPOINT))
assert f.stale(built, page(), reference(ENDPOINT, ("Certificate",
"arn:${Partition}:dms:${Region}:${Account}:cert:*")))
ENDPOINT_EXAMPLE = "arn:aws:dms:us-east-1:123456789012:endpoint:RAAR3R22XSH46S3PWLC3NJAWKM"
def recording(operation: str, request: dict, answered: dict) -> str:
kind = f.CREATE_RE.match(operation).group("type")
return json.dumps({"steps": [{"service": "dms", "operation": operation, "input": {"body": request},
"output": {"status": 200, "body": {kind: answered}}}]})
def test_a_create_aws_answers_with_an_arn_it_does_not_tie_to_the_request_is_recorded_as_minted():
answers = f.create_answers({"Endpoint"}, [("https://wire/b1.json", recording(
"CreateEndpoint", {"EndpointIdentifier": "{{endpoint_id}}", "Password": "{{secret}}"},
{"EndpointArn": "", "EndpointIdentifier": "{{endpoint_id}}"}))],
("https://botocore/examples-1.json", json.dumps({"examples": {"CreateEndpoint": [{
"input": {"EndpointIdentifier": "test-endpoint-1"},
"output": {"Endpoint": {"EndpointArn": ENDPOINT_EXAMPLE}}}]}})))
assert answers == {"CreateEndpoint": {"type": "Endpoint", "arn": "minted", "evidence": [
{"source": "https://wire/b1.json", "request": {"EndpointIdentifier": "{{endpoint_id}}"},
"answered": {"EndpointArn": ""}},
{"source": "https://botocore/examples-1.json", "request": {"EndpointIdentifier": "test-endpoint-1"},
"answered": {"EndpointArn": ENDPOINT_EXAMPLE}}]}}
def test_a_create_aws_answers_with_an_arn_ending_in_a_request_member_is_recorded_as_named_by_it():
answers = f.create_answers({"ReplicationSubnetGroup"}, [("https://wire/sg.json", recording(
"CreateReplicationSubnetGroup", {"ReplicationSubnetGroupIdentifier": "{{subnet_group_id}}"},
{"ReplicationSubnetGroupArn": "arn:aws:dms:{{region}}:{{account}}:subgrp:{{subnet_group_id}}"}))],
("https://botocore/examples-1.json", "{}"))
assert answers["CreateReplicationSubnetGroup"]["arn"] == "named"
assert answers["CreateReplicationSubnetGroup"]["member"] == "ReplicationSubnetGroupIdentifier"
def test_a_create_the_evidence_gives_both_ways_is_refused():
named = recording("CreateEndpoint", {"EndpointIdentifier": "{{endpoint_id}}"},
{"EndpointArn": "arn:aws:dms:{{region}}:{{account}}:endpoint:{{endpoint_id}}"})
minted = recording("CreateEndpoint", {"EndpointIdentifier": "{{endpoint_id}}"},
{"EndpointArn": ""})
with pytest.raises(ValueError, match="both"):
f.create_answers({"Endpoint"}, [("https://wire/a.json", named), ("https://wire/b.json", minted)],
("https://botocore/examples-1.json", "{}"))
def test_an_answer_of_another_type_or_service_is_not_read():
answers = f.create_answers({"Endpoint"}, [("https://wire/t.json", json.dumps({"steps": [
{"service": "sns", "operation": "CreateTopic", "input": {"body": {}},
"output": {"status": 200, "body": {"Topic": {"TopicArn": ""}}}}]}))],
("https://botocore/examples-1.json", "{}"))
assert answers == {}