2021-01-28 10:47:53 +00:00
|
|
|
import copy
|
2023-11-30 15:55:51 +00:00
|
|
|
import json
|
2021-01-28 10:47:53 +00:00
|
|
|
from string import Template
|
|
|
|
|
|
|
|
import boto3
|
2023-11-30 15:55:51 +00:00
|
|
|
import pytest
|
2021-10-09 10:11:46 +00:00
|
|
|
from botocore.exceptions import ClientError
|
2021-01-28 10:47:53 +00:00
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
from moto import mock_aws
|
2022-08-13 09:49:43 +00:00
|
|
|
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
|
2021-01-28 10:47:53 +00:00
|
|
|
|
|
|
|
archive_template = Template(
|
|
|
|
json.dumps(
|
|
|
|
{
|
|
|
|
"AWSTemplateFormatVersion": "2010-09-09",
|
|
|
|
"Description": "EventBridge Archive Test",
|
|
|
|
"Resources": {
|
|
|
|
"Archive": {
|
|
|
|
"Type": "AWS::Events::Archive",
|
|
|
|
"Properties": {
|
|
|
|
"ArchiveName": "${archive_name}",
|
|
|
|
"SourceArn": {
|
|
|
|
"Fn::Sub": "arn:aws:events:$${AWS::Region}:$${AWS::AccountId}:event-bus/default"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"Outputs": {
|
|
|
|
"Arn": {
|
|
|
|
"Description": "Archive Arn",
|
|
|
|
"Value": {"Fn::GetAtt": ["Archive", "Arn"]},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-18 19:31:29 +00:00
|
|
|
rule_template = Template(
|
|
|
|
json.dumps(
|
|
|
|
{
|
|
|
|
"AWSTemplateFormatVersion": "2010-09-09",
|
|
|
|
"Description": "EventBridge Rule Test",
|
|
|
|
"Resources": {
|
|
|
|
"Rule": {
|
|
|
|
"Type": "AWS::Events::Rule",
|
|
|
|
"Properties": {
|
|
|
|
"Name": "${rule_name}",
|
|
|
|
"EventPattern": {"detail-type": ["SomeDetailType"]},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"Outputs": {
|
|
|
|
"Arn": {
|
|
|
|
"Description": "Rule Arn",
|
|
|
|
"Value": {"Fn::GetAtt": ["Rule", "Arn"]},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-10-09 10:11:46 +00:00
|
|
|
empty = json.dumps(
|
|
|
|
{
|
|
|
|
"AWSTemplateFormatVersion": "2010-09-09",
|
|
|
|
"Description": "EventBridge Rule Test",
|
|
|
|
"Resources": {},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-01-28 10:47:53 +00:00
|
|
|
def test_create_archive():
|
|
|
|
# given
|
|
|
|
cfn_client = boto3.client("cloudformation", region_name="eu-central-1")
|
|
|
|
name = "test-archive"
|
|
|
|
stack_name = "test-stack"
|
|
|
|
template = archive_template.substitute({"archive_name": name})
|
|
|
|
|
|
|
|
# when
|
|
|
|
cfn_client.create_stack(StackName=stack_name, TemplateBody=template)
|
|
|
|
|
|
|
|
# then
|
2022-11-17 22:41:08 +00:00
|
|
|
archive_arn = f"arn:aws:events:eu-central-1:{ACCOUNT_ID}:archive/{name}"
|
2021-01-28 10:47:53 +00:00
|
|
|
stack = cfn_client.describe_stacks(StackName=stack_name)["Stacks"][0]
|
2023-07-27 09:39:24 +00:00
|
|
|
assert stack["Outputs"][0]["OutputValue"] == archive_arn
|
2021-01-28 10:47:53 +00:00
|
|
|
|
|
|
|
events_client = boto3.client("events", region_name="eu-central-1")
|
|
|
|
response = events_client.describe_archive(ArchiveName=name)
|
|
|
|
|
2023-07-27 09:39:24 +00:00
|
|
|
assert response["ArchiveArn"] == archive_arn
|
2021-01-28 10:47:53 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-01-28 10:47:53 +00:00
|
|
|
def test_update_archive():
|
|
|
|
# given
|
|
|
|
cfn_client = boto3.client("cloudformation", region_name="eu-central-1")
|
|
|
|
name = "test-archive"
|
|
|
|
stack_name = "test-stack"
|
|
|
|
template = archive_template.substitute({"archive_name": name})
|
|
|
|
cfn_client.create_stack(StackName=stack_name, TemplateBody=template)
|
|
|
|
|
|
|
|
template_update = copy.deepcopy(json.loads(template))
|
|
|
|
template_update["Resources"]["Archive"]["Properties"]["Description"] = (
|
|
|
|
"test archive"
|
2024-03-21 20:17:29 +00:00
|
|
|
)
|
2021-01-28 10:47:53 +00:00
|
|
|
|
|
|
|
# when
|
|
|
|
cfn_client.update_stack(
|
|
|
|
StackName=stack_name, TemplateBody=json.dumps(template_update)
|
|
|
|
)
|
|
|
|
|
|
|
|
# then
|
|
|
|
events_client = boto3.client("events", region_name="eu-central-1")
|
|
|
|
response = events_client.describe_archive(ArchiveName=name)
|
|
|
|
|
2023-07-27 09:39:24 +00:00
|
|
|
assert (
|
|
|
|
response["ArchiveArn"]
|
|
|
|
== f"arn:aws:events:eu-central-1:{ACCOUNT_ID}:archive/{name}"
|
2021-01-28 10:47:53 +00:00
|
|
|
)
|
2023-07-27 09:39:24 +00:00
|
|
|
assert response["Description"] == "test archive"
|
2021-01-28 10:47:53 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-01-28 10:47:53 +00:00
|
|
|
def test_delete_archive():
|
|
|
|
# given
|
|
|
|
cfn_client = boto3.client("cloudformation", region_name="eu-central-1")
|
|
|
|
name = "test-archive"
|
|
|
|
stack_name = "test-stack"
|
|
|
|
template = archive_template.substitute({"archive_name": name})
|
|
|
|
cfn_client.create_stack(StackName=stack_name, TemplateBody=template)
|
|
|
|
|
|
|
|
# when
|
|
|
|
cfn_client.delete_stack(StackName=stack_name)
|
|
|
|
|
|
|
|
# then
|
|
|
|
events_client = boto3.client("events", region_name="eu-central-1")
|
|
|
|
response = events_client.list_archives(NamePrefix="test")["Archives"]
|
2023-07-27 09:39:24 +00:00
|
|
|
assert len(response) == 0
|
2021-08-18 19:31:29 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-08-18 19:31:29 +00:00
|
|
|
def test_create_rule():
|
|
|
|
# given
|
|
|
|
cfn_client = boto3.client("cloudformation", region_name="eu-central-1")
|
|
|
|
name = "test-rule"
|
|
|
|
stack_name = "test-stack"
|
|
|
|
template = rule_template.substitute({"rule_name": name})
|
|
|
|
|
|
|
|
# when
|
|
|
|
cfn_client.create_stack(StackName=stack_name, TemplateBody=template)
|
|
|
|
|
|
|
|
# then
|
2022-11-17 22:41:08 +00:00
|
|
|
rule_arn = f"arn:aws:events:eu-central-1:{ACCOUNT_ID}:rule/{name}"
|
2021-08-18 19:31:29 +00:00
|
|
|
stack = cfn_client.describe_stacks(StackName=stack_name)["Stacks"][0]
|
2023-07-27 09:39:24 +00:00
|
|
|
assert stack["Outputs"][0]["OutputValue"] == rule_arn
|
2021-08-18 19:31:29 +00:00
|
|
|
|
|
|
|
events_client = boto3.client("events", region_name="eu-central-1")
|
|
|
|
response = events_client.describe_rule(Name=name)
|
|
|
|
|
2023-07-27 09:39:24 +00:00
|
|
|
assert response["Arn"] == rule_arn
|
|
|
|
assert response["EventPattern"] == '{"detail-type": ["SomeDetailType"]}'
|
2021-10-09 10:11:46 +00:00
|
|
|
|
|
|
|
|
2024-01-07 12:03:33 +00:00
|
|
|
@mock_aws
|
2021-10-09 10:11:46 +00:00
|
|
|
def test_delete_rule():
|
|
|
|
# given
|
|
|
|
cfn_client = boto3.client("cloudformation", region_name="eu-central-1")
|
|
|
|
name = "test-rule"
|
|
|
|
stack_name = "test-stack"
|
|
|
|
template = rule_template.substitute({"rule_name": name})
|
|
|
|
cfn_client.create_stack(StackName=stack_name, TemplateBody=template)
|
|
|
|
|
|
|
|
# when
|
|
|
|
cfn_client.update_stack(StackName=stack_name, TemplateBody=empty)
|
|
|
|
|
|
|
|
# then
|
|
|
|
events_client = boto3.client("events", region_name="eu-central-1")
|
|
|
|
|
2021-10-18 19:44:29 +00:00
|
|
|
with pytest.raises(ClientError) as exc:
|
2021-10-09 10:11:46 +00:00
|
|
|
events_client.describe_rule(Name=name)
|
2021-10-18 19:44:29 +00:00
|
|
|
err = exc.value.response["Error"]
|
2023-07-27 09:39:24 +00:00
|
|
|
assert err["Code"] == "ResourceNotFoundException"
|
|
|
|
assert err["Message"] == "Rule test-rule does not exist."
|