4
4
from uuid import uuid4
5
5
6
6
import pytest
7
- from boto3 . session import Session
8
- from cloudformation_cli_python_lib . boto3_proxy import SessionProxy
7
+ import botocore . errorfactory
8
+ import botocore . session
9
9
from cloudformation_cli_python_lib .log_delivery import (
10
10
HookProviderLogHandler ,
11
11
ProviderFilter ,
19
19
)
20
20
21
21
22
+ logs_model = botocore .session .get_session ().get_service_model ("logs" )
23
+ factory = botocore .errorfactory .ClientExceptionsFactory ()
24
+ logs_exceptions = factory .create_client_exceptions (logs_model )
25
+
26
+
22
27
@pytest .fixture
23
28
def mock_logger ():
24
29
return create_autospec (logging .getLogger ())
@@ -100,38 +105,36 @@ def mock_handler_set_formatter():
100
105
101
106
102
107
@pytest .fixture
103
- def mock_provider_handler ():
108
+ def mock_provider_handler (mock_session ):
104
109
plh = ProviderLogHandler (
105
110
group = "test-group" ,
106
111
stream = "test-stream" ,
107
- session = SessionProxy (
108
- Session (
109
- aws_access_key_id = "" , aws_secret_access_key = "" , aws_session_token = ""
110
- )
111
- ),
112
+ session = mock_session ,
112
113
)
113
114
# not mocking the whole client because that replaces generated exception classes to
114
115
# be replaced with mocks
115
116
for method in ["create_log_group" , "create_log_stream" , "put_log_events" ]:
116
117
setattr (plh .client , method , Mock (auto_spec = True ))
118
+
119
+ # set exceptions instead of using Mock
120
+ plh .client .exceptions = logs_exceptions
117
121
return plh
118
122
119
123
120
124
@pytest .fixture
121
- def mock_hook_provider_handler ():
125
+ def mock_hook_provider_handler (mock_session ):
122
126
plh = HookProviderLogHandler (
123
127
group = "test-hook-group" ,
124
128
stream = "test-hook-stream" ,
125
- session = SessionProxy (
126
- Session (
127
- aws_access_key_id = "" , aws_secret_access_key = "" , aws_session_token = ""
128
- )
129
- ),
129
+ session = mock_session ,
130
130
)
131
131
# not mocking the whole client because that replaces generated exception classes to
132
132
# be replaced with mocks
133
133
for method in ["create_log_group" , "create_log_stream" , "put_log_events" ]:
134
134
setattr (plh .client , method , Mock (auto_spec = True ))
135
+
136
+ # set exceptions instead of using Mock
137
+ plh .client .exceptions = logs_exceptions
135
138
return plh
136
139
137
140
@@ -286,12 +289,11 @@ def test__put_log_event_success(mock_provider_handler, sequence_token):
286
289
287
290
288
291
def test__put_log_event_invalid_token (mock_provider_handler ):
289
- exc = mock_provider_handler .client .exceptions
290
292
mock_put = mock_provider_handler .client .put_log_events
291
293
mock_put .return_value = {"nextSequenceToken" : "some-other-seq" }
292
294
mock_put .side_effect = [
293
- exc .InvalidSequenceTokenException ({}, operation_name = "Test" ),
294
- exc .DataAlreadyAcceptedException ({}, operation_name = "Test" ),
295
+ logs_exceptions .InvalidSequenceTokenException ({}, operation_name = "Test" ),
296
+ logs_exceptions .DataAlreadyAcceptedException ({}, operation_name = "Test" ),
295
297
DEFAULT ,
296
298
]
297
299
mock_provider_handler ._put_log_event (
@@ -309,8 +311,7 @@ def test_emit_existing_cwl_group_stream(mock_provider_handler):
309
311
310
312
311
313
def test_emit_no_group_stream (mock_provider_handler ):
312
- exc = mock_provider_handler .client .exceptions .ResourceNotFoundException
313
- group_exc = exc (
314
+ group_exc = logs_exceptions .ResourceNotFoundException (
314
315
{"Error" : {"Message" : "log group does not exist" }},
315
316
operation_name = "PutLogRecords" ,
316
317
)
@@ -326,7 +327,7 @@ def test_emit_no_group_stream(mock_provider_handler):
326
327
mock_provider_handler ._create_log_stream .assert_called_once ()
327
328
328
329
# create_group should not be called again if the group already exists
329
- stream_exc = exc (
330
+ stream_exc = logs_exceptions . ResourceNotFoundException (
330
331
{"Error" : {"Message" : "log stream does not exist" }},
331
332
operation_name = "PutLogRecords" ,
332
333
)
@@ -474,8 +475,9 @@ def test_hook_log_stream_create_success(mock_hook_provider_handler):
474
475
@pytest .mark .parametrize ("create_method" , ["_create_log_group" , "_create_log_stream" ])
475
476
def test__hook_create_already_exists (mock_hook_provider_handler , create_method ):
476
477
mock_logs_method = getattr (mock_hook_provider_handler .client , create_method [1 :])
477
- exc = mock_hook_provider_handler .client .exceptions .ResourceAlreadyExistsException
478
- mock_logs_method .side_effect = exc ({}, operation_name = "Test" )
478
+ mock_logs_method .side_effect = logs_exceptions .ResourceAlreadyExistsException (
479
+ {}, operation_name = "Test"
480
+ )
479
481
# should not raise an exception if the log group already exists
480
482
getattr (mock_hook_provider_handler , create_method )()
481
483
mock_logs_method .assert_called_once ()
@@ -493,12 +495,11 @@ def test__hook_put_log_event_success(mock_hook_provider_handler, sequence_token)
493
495
494
496
495
497
def test__hook_put_log_event_invalid_token (mock_hook_provider_handler ):
496
- exc = mock_hook_provider_handler .client .exceptions
497
498
mock_put = mock_hook_provider_handler .client .put_log_events
498
499
mock_put .return_value = {"nextSequenceToken" : "some-other-seq" }
499
500
mock_put .side_effect = [
500
- exc .InvalidSequenceTokenException ({}, operation_name = "Test" ),
501
- exc .DataAlreadyAcceptedException ({}, operation_name = "Test" ),
501
+ logs_exceptions .InvalidSequenceTokenException ({}, operation_name = "Test" ),
502
+ logs_exceptions .DataAlreadyAcceptedException ({}, operation_name = "Test" ),
502
503
DEFAULT ,
503
504
]
504
505
mock_hook_provider_handler ._put_log_event (
@@ -516,8 +517,7 @@ def test_hook_emit_existing_cwl_group_stream(mock_hook_provider_handler):
516
517
517
518
518
519
def test_hook_emit_no_group_stream (mock_hook_provider_handler ):
519
- exc = mock_hook_provider_handler .client .exceptions .ResourceNotFoundException
520
- group_exc = exc (
520
+ group_exc = logs_exceptions .ResourceNotFoundException (
521
521
{"Error" : {"Message" : "log group does not exist" }},
522
522
operation_name = "PutLogRecords" ,
523
523
)
@@ -533,7 +533,7 @@ def test_hook_emit_no_group_stream(mock_hook_provider_handler):
533
533
mock_hook_provider_handler ._create_log_stream .assert_called_once ()
534
534
535
535
# create_group should not be called again if the group already exists
536
- stream_exc = exc (
536
+ stream_exc = logs_exceptions . ResourceNotFoundException (
537
537
{"Error" : {"Message" : "log stream does not exist" }},
538
538
operation_name = "PutLogRecords" ,
539
539
)
0 commit comments