2016-12-02 01:23:51 +00:00
|
|
|
import random
|
|
|
|
import boto3
|
2018-05-15 16:04:59 +00:00
|
|
|
import json
|
2016-12-02 01:23:51 +00:00
|
|
|
|
|
|
|
from moto.events import mock_events
|
2017-10-28 19:17:34 +00:00
|
|
|
from botocore.exceptions import ClientError
|
|
|
|
from nose.tools import assert_raises
|
2016-12-02 01:23:51 +00:00
|
|
|
|
|
|
|
RULES = [
|
2019-10-31 15:44:26 +00:00
|
|
|
{"Name": "test1", "ScheduleExpression": "rate(5 minutes)"},
|
|
|
|
{"Name": "test2", "ScheduleExpression": "rate(1 minute)"},
|
|
|
|
{"Name": "test3", "EventPattern": '{"source": ["test-source"]}'},
|
2016-12-02 01:23:51 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
TARGETS = {
|
2019-10-31 15:44:26 +00:00
|
|
|
"test-target-1": {
|
|
|
|
"Id": "test-target-1",
|
|
|
|
"Arn": "arn:aws:lambda:us-west-2:111111111111:function:test-function-1",
|
|
|
|
"Rules": ["test1", "test2"],
|
2016-12-02 01:23:51 +00:00
|
|
|
},
|
2019-10-31 15:44:26 +00:00
|
|
|
"test-target-2": {
|
|
|
|
"Id": "test-target-2",
|
|
|
|
"Arn": "arn:aws:lambda:us-west-2:111111111111:function:test-function-2",
|
|
|
|
"Rules": ["test1", "test3"],
|
2016-12-02 01:23:51 +00:00
|
|
|
},
|
2019-10-31 15:44:26 +00:00
|
|
|
"test-target-3": {
|
|
|
|
"Id": "test-target-3",
|
|
|
|
"Arn": "arn:aws:lambda:us-west-2:111111111111:function:test-function-3",
|
|
|
|
"Rules": ["test1", "test2"],
|
2016-12-02 01:23:51 +00:00
|
|
|
},
|
2019-10-31 15:44:26 +00:00
|
|
|
"test-target-4": {
|
|
|
|
"Id": "test-target-4",
|
|
|
|
"Arn": "arn:aws:lambda:us-west-2:111111111111:function:test-function-4",
|
|
|
|
"Rules": ["test1", "test3"],
|
2016-12-02 01:23:51 +00:00
|
|
|
},
|
2019-10-31 15:44:26 +00:00
|
|
|
"test-target-5": {
|
|
|
|
"Id": "test-target-5",
|
|
|
|
"Arn": "arn:aws:lambda:us-west-2:111111111111:function:test-function-5",
|
|
|
|
"Rules": ["test1", "test2"],
|
|
|
|
},
|
|
|
|
"test-target-6": {
|
|
|
|
"Id": "test-target-6",
|
|
|
|
"Arn": "arn:aws:lambda:us-west-2:111111111111:function:test-function-6",
|
|
|
|
"Rules": ["test1", "test3"],
|
2016-12-02 01:23:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_random_rule():
|
|
|
|
return RULES[random.randint(0, len(RULES) - 1)]
|
|
|
|
|
|
|
|
|
|
|
|
def generate_environment():
|
2019-10-31 15:44:26 +00:00
|
|
|
client = boto3.client("events", "us-west-2")
|
2016-12-02 01:23:51 +00:00
|
|
|
|
|
|
|
for rule in RULES:
|
|
|
|
client.put_rule(
|
2019-10-31 15:44:26 +00:00
|
|
|
Name=rule["Name"],
|
|
|
|
ScheduleExpression=rule.get("ScheduleExpression", ""),
|
|
|
|
EventPattern=rule.get("EventPattern", ""),
|
2016-12-02 01:23:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
targets = []
|
2016-12-02 03:10:59 +00:00
|
|
|
for target in TARGETS:
|
2019-10-31 15:44:26 +00:00
|
|
|
if rule["Name"] in TARGETS[target].get("Rules"):
|
|
|
|
targets.append({"Id": target, "Arn": TARGETS[target]["Arn"]})
|
2016-12-02 01:23:51 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
client.put_targets(Rule=rule["Name"], Targets=targets)
|
2016-12-02 01:23:51 +00:00
|
|
|
|
|
|
|
return client
|
|
|
|
|
|
|
|
|
|
|
|
@mock_events
|
|
|
|
def test_list_rules():
|
|
|
|
client = generate_environment()
|
|
|
|
response = client.list_rules()
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
assert response is not None
|
|
|
|
assert len(response["Rules"]) > 0
|
2016-12-02 01:23:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_events
|
|
|
|
def test_describe_rule():
|
2019-10-31 15:44:26 +00:00
|
|
|
rule_name = get_random_rule()["Name"]
|
2016-12-02 01:23:51 +00:00
|
|
|
client = generate_environment()
|
|
|
|
response = client.describe_rule(Name=rule_name)
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
assert response is not None
|
|
|
|
assert response.get("Name") == rule_name
|
|
|
|
assert response.get(
|
|
|
|
"Arn"
|
|
|
|
) == "arn:aws:events:us-west-2:111111111111:rule/{0}".format(rule_name)
|
2016-12-02 01:23:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_events
|
|
|
|
def test_enable_disable_rule():
|
2019-10-31 15:44:26 +00:00
|
|
|
rule_name = get_random_rule()["Name"]
|
2016-12-02 01:23:51 +00:00
|
|
|
client = generate_environment()
|
|
|
|
|
|
|
|
# Rules should start out enabled in these tests.
|
|
|
|
rule = client.describe_rule(Name=rule_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
assert rule["State"] == "ENABLED"
|
2016-12-02 01:23:51 +00:00
|
|
|
|
|
|
|
client.disable_rule(Name=rule_name)
|
|
|
|
rule = client.describe_rule(Name=rule_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
assert rule["State"] == "DISABLED"
|
2016-12-02 01:23:51 +00:00
|
|
|
|
|
|
|
client.enable_rule(Name=rule_name)
|
|
|
|
rule = client.describe_rule(Name=rule_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
assert rule["State"] == "ENABLED"
|
2016-12-02 01:23:51 +00:00
|
|
|
|
2019-02-18 14:52:37 +00:00
|
|
|
# Test invalid name
|
|
|
|
try:
|
2019-10-31 15:44:26 +00:00
|
|
|
client.enable_rule(Name="junk")
|
2019-02-18 14:52:37 +00:00
|
|
|
|
|
|
|
except ClientError as ce:
|
2019-10-31 15:44:26 +00:00
|
|
|
assert ce.response["Error"]["Code"] == "ResourceNotFoundException"
|
2019-02-18 14:52:37 +00:00
|
|
|
|
2016-12-02 01:23:51 +00:00
|
|
|
|
|
|
|
@mock_events
|
|
|
|
def test_list_rule_names_by_target():
|
2019-10-31 15:44:26 +00:00
|
|
|
test_1_target = TARGETS["test-target-1"]
|
|
|
|
test_2_target = TARGETS["test-target-2"]
|
2016-12-02 01:23:51 +00:00
|
|
|
client = generate_environment()
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
rules = client.list_rule_names_by_target(TargetArn=test_1_target["Arn"])
|
|
|
|
assert len(rules["RuleNames"]) == len(test_1_target["Rules"])
|
|
|
|
for rule in rules["RuleNames"]:
|
|
|
|
assert rule in test_1_target["Rules"]
|
2016-12-02 01:23:51 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
rules = client.list_rule_names_by_target(TargetArn=test_2_target["Arn"])
|
|
|
|
assert len(rules["RuleNames"]) == len(test_2_target["Rules"])
|
|
|
|
for rule in rules["RuleNames"]:
|
|
|
|
assert rule in test_2_target["Rules"]
|
2016-12-02 01:23:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_events
|
|
|
|
def test_list_rules():
|
|
|
|
client = generate_environment()
|
|
|
|
|
|
|
|
rules = client.list_rules()
|
2019-10-31 15:44:26 +00:00
|
|
|
assert len(rules["Rules"]) == len(RULES)
|
2016-12-02 01:23:51 +00:00
|
|
|
|
|
|
|
|
2017-04-13 02:15:18 +00:00
|
|
|
@mock_events
|
|
|
|
def test_delete_rule():
|
|
|
|
client = generate_environment()
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
client.delete_rule(Name=RULES[0]["Name"])
|
2017-04-13 02:15:18 +00:00
|
|
|
rules = client.list_rules()
|
2019-10-31 15:44:26 +00:00
|
|
|
assert len(rules["Rules"]) == len(RULES) - 1
|
2017-04-13 02:15:18 +00:00
|
|
|
|
|
|
|
|
2016-12-02 01:23:51 +00:00
|
|
|
@mock_events
|
|
|
|
def test_list_targets_by_rule():
|
2019-10-31 15:44:26 +00:00
|
|
|
rule_name = get_random_rule()["Name"]
|
2016-12-02 01:23:51 +00:00
|
|
|
client = generate_environment()
|
|
|
|
targets = client.list_targets_by_rule(Rule=rule_name)
|
|
|
|
|
|
|
|
expected_targets = []
|
2016-12-02 03:10:59 +00:00
|
|
|
for target in TARGETS:
|
2019-10-31 15:44:26 +00:00
|
|
|
if rule_name in TARGETS[target].get("Rules"):
|
2016-12-02 01:23:51 +00:00
|
|
|
expected_targets.append(target)
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
assert len(targets["Targets"]) == len(expected_targets)
|
2016-12-02 01:23:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_events
|
|
|
|
def test_remove_targets():
|
2019-10-31 15:44:26 +00:00
|
|
|
rule_name = get_random_rule()["Name"]
|
2016-12-02 01:23:51 +00:00
|
|
|
client = generate_environment()
|
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
targets = client.list_targets_by_rule(Rule=rule_name)["Targets"]
|
2016-12-02 01:23:51 +00:00
|
|
|
targets_before = len(targets)
|
2019-10-31 15:44:26 +00:00
|
|
|
assert targets_before > 0
|
2016-12-02 01:23:51 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
client.remove_targets(Rule=rule_name, Ids=[targets[0]["Id"]])
|
2016-12-02 01:23:51 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
targets = client.list_targets_by_rule(Rule=rule_name)["Targets"]
|
2016-12-02 01:23:51 +00:00
|
|
|
targets_after = len(targets)
|
2019-10-31 15:44:26 +00:00
|
|
|
assert targets_before - 1 == targets_after
|
2016-12-02 01:23:51 +00:00
|
|
|
|
|
|
|
|
2017-10-28 19:17:34 +00:00
|
|
|
@mock_events
|
|
|
|
def test_permissions():
|
2019-10-31 15:44:26 +00:00
|
|
|
client = boto3.client("events", "eu-central-1")
|
2017-10-28 19:17:34 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
client.put_permission(
|
|
|
|
Action="events:PutEvents", Principal="111111111111", StatementId="Account1"
|
|
|
|
)
|
|
|
|
client.put_permission(
|
|
|
|
Action="events:PutEvents", Principal="222222222222", StatementId="Account2"
|
|
|
|
)
|
2017-10-28 19:17:34 +00:00
|
|
|
|
|
|
|
resp = client.describe_event_bus()
|
2019-10-31 15:44:26 +00:00
|
|
|
resp_policy = json.loads(resp["Policy"])
|
|
|
|
assert len(resp_policy["Statement"]) == 2
|
2017-10-28 19:17:34 +00:00
|
|
|
|
2019-10-31 15:44:26 +00:00
|
|
|
client.remove_permission(StatementId="Account2")
|
2017-10-28 19:17:34 +00:00
|
|
|
|
|
|
|
resp = client.describe_event_bus()
|
2019-10-31 15:44:26 +00:00
|
|
|
resp_policy = json.loads(resp["Policy"])
|
|
|
|
assert len(resp_policy["Statement"]) == 1
|
|
|
|
assert resp_policy["Statement"][0]["Sid"] == "Account1"
|
2017-10-28 19:17:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_events
|
|
|
|
def test_put_events():
|
2019-10-31 15:44:26 +00:00
|
|
|
client = boto3.client("events", "eu-central-1")
|
2017-10-28 19:17:34 +00:00
|
|
|
|
|
|
|
event = {
|
|
|
|
"Source": "com.mycompany.myapp",
|
|
|
|
"Detail": '{"key1": "value3", "key2": "value4"}',
|
|
|
|
"Resources": ["resource1", "resource2"],
|
2019-10-31 15:44:26 +00:00
|
|
|
"DetailType": "myDetailType",
|
2017-10-28 19:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client.put_events(Entries=[event])
|
|
|
|
# Boto3 would error if it didn't return 200 OK
|
|
|
|
|
|
|
|
with assert_raises(ClientError):
|
2019-10-31 15:44:26 +00:00
|
|
|
client.put_events(Entries=[event] * 20)
|