-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathtest_scheduled_lambda.py
More file actions
155 lines (135 loc) · 5.14 KB
/
test_scheduled_lambda.py
File metadata and controls
155 lines (135 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Unit tests for scheduled_lambda.py functions.
"""
import boto3
from botocore.exceptions import ClientError
import pytest
import scheduled_lambda
@pytest.mark.parametrize(
"failed_target_count,error_code,stop_on_method",
[
(0, None, None),
(1, None, None),
(0, "TestException", "stub_put_rule"),
(0, "TestException", "stub_add_permission"),
(0, "TestException", "stub_put_targets"),
],
)
def test_schedule_lambda_function(
make_stubber, stub_runner, failed_target_count, error_code, stop_on_method
):
eventbridge_client = boto3.client("events")
eventbridge_stubber = make_stubber(eventbridge_client)
lambda_client = boto3.client("lambda")
lambda_stubber = make_stubber(lambda_client)
event_rule_name = "test-rule"
event_schedule = "test-schedule"
event_rule_arn = f"arn:aws:events:::rules/{event_rule_name}"
lambda_func_name = "test-func"
lambda_func_arn = f"arn:aws:lambda:::functions/{lambda_func_name}"
with stub_runner(error_code, stop_on_method) as runner:
runner.add(
eventbridge_stubber.stub_put_rule,
event_rule_name,
event_schedule,
event_rule_arn,
)
runner.add(
lambda_stubber.stub_add_permission,
lambda_func_name,
"lambda:InvokeFunction",
"events.amazonaws.com",
event_rule_arn,
)
runner.add(
eventbridge_stubber.stub_put_targets,
event_rule_name,
[{"Id": lambda_func_name, "Arn": lambda_func_arn}],
failed_count=failed_target_count,
)
if error_code is None:
got_arn = scheduled_lambda.schedule_lambda_function(
eventbridge_client,
event_rule_name,
event_schedule,
lambda_client,
lambda_func_name,
lambda_func_arn,
)
assert got_arn == event_rule_arn
else:
with pytest.raises(ClientError) as exc_info:
scheduled_lambda.schedule_lambda_function(
eventbridge_client,
event_rule_name,
event_schedule,
lambda_client,
lambda_func_name,
lambda_func_arn,
)
assert exc_info.value.response["Error"]["Code"] == error_code
@pytest.mark.parametrize(
"enable,error_code", [(True, None), (False, None), (True, "TestException")]
)
def test_update_event(make_stubber, enable, error_code):
eventbridge_client = boto3.client("events")
eventbridge_stubber = make_stubber(eventbridge_client)
event_rule_name = "test-rule"
if enable:
eventbridge_stubber.stub_enable_rule(event_rule_name, error_code=error_code)
else:
eventbridge_stubber.stub_disable_rule(event_rule_name, error_code=error_code)
if error_code is None:
scheduled_lambda.update_event_rule(eventbridge_client, event_rule_name, enable)
else:
with pytest.raises(ClientError) as exc_info:
scheduled_lambda.update_event_rule(
eventbridge_client, event_rule_name, enable
)
assert exc_info.value.response["Error"]["Code"] == error_code
@pytest.mark.parametrize(
"state,error_code",
[("ENABLED", None), ("DISABLED", None), ("ENABLED", "TestException")],
)
def test_get_event_enabled(make_stubber, state, error_code):
eventbridge_client = boto3.client("events")
eventbridge_stubber = make_stubber(eventbridge_client)
event_rule_name = "test-rule"
eventbridge_stubber.stub_describe_rule(
event_rule_name, state, error_code=error_code
)
if error_code is None:
got_enabled = scheduled_lambda.get_event_rule_enabled(
eventbridge_client, event_rule_name
)
assert got_enabled == (state == "ENABLED")
else:
with pytest.raises(ClientError) as exc_info:
scheduled_lambda.get_event_rule_enabled(eventbridge_client, event_rule_name)
assert exc_info.value.response["Error"]["Code"] == error_code
@pytest.mark.parametrize(
"error_code,stop_on_method",
[(None, None), ("TestException", "stub_remove_targets")],
)
def test_delete_event(make_stubber, stub_runner, error_code, stop_on_method):
eventbridge_client = boto3.client("events")
eventbridge_stubber = make_stubber(eventbridge_client)
event_rule_name = "test-rule"
lambda_func_name = "test-func"
with stub_runner(error_code, stop_on_method) as runner:
runner.add(
eventbridge_stubber.stub_remove_targets, event_rule_name, [lambda_func_name]
)
runner.add(eventbridge_stubber.stub_delete_rule, event_rule_name)
if error_code is None:
scheduled_lambda.delete_event_rule(
eventbridge_client, event_rule_name, lambda_func_name
)
else:
with pytest.raises(ClientError) as exc_info:
scheduled_lambda.delete_event_rule(
eventbridge_client, event_rule_name, lambda_func_name
)
assert exc_info.value.response["Error"]["Code"] == error_code