17
17
import random
18
18
import string
19
19
20
+ from google .api_core .exceptions import Aborted
20
21
from google .cloud import monitoring_v3
21
22
import google .protobuf .json_format
22
23
import pytest
24
+ from retrying import retry
23
25
24
26
import snippets
25
27
@@ -29,6 +31,10 @@ def random_name(length):
29
31
[random .choice (string .ascii_lowercase ) for i in range (length )])
30
32
31
33
34
+ def retry_if_aborted (exception ):
35
+ return isinstance (exception , Aborted )
36
+
37
+
32
38
class PochanFixture :
33
39
"""A test fixture that creates an alert POlicy and a notification CHANnel,
34
40
hence the name, pochan.
@@ -42,30 +48,40 @@ def __init__(self):
42
48
monitoring_v3 .NotificationChannelServiceClient ())
43
49
44
50
def __enter__ (self ):
45
- # Create a policy.
46
- policy = monitoring_v3 .types .alert_pb2 .AlertPolicy ()
47
- json = open ('test_alert_policy.json' ).read ()
48
- google .protobuf .json_format .Parse (json , policy )
49
- policy .display_name = 'snippets-test-' + random_name (10 )
50
- self .alert_policy = self .alert_policy_client .create_alert_policy (
51
- self .project_name , policy )
52
- # Create a notification channel.
53
- notification_channel = (
54
- monitoring_v3 .types .notification_pb2 .NotificationChannel ())
55
- json = open ('test_notification_channel.json' ).read ()
56
- google .protobuf .json_format .Parse (json , notification_channel )
57
- notification_channel .display_name = 'snippets-test-' + random_name (10 )
58
- self .notification_channel = (
59
- self .notification_channel_client .create_notification_channel (
60
- self .project_name , notification_channel ))
51
+ @retry (wait_exponential_multiplier = 1000 , wait_exponential_max = 10000 ,
52
+ stop_max_attempt_number = 5 , retry_on_exception = retry_if_aborted )
53
+ def setup ():
54
+ # Create a policy.
55
+ policy = monitoring_v3 .types .alert_pb2 .AlertPolicy ()
56
+ json = open ('test_alert_policy.json' ).read ()
57
+ google .protobuf .json_format .Parse (json , policy )
58
+ policy .display_name = 'snippets-test-' + random_name (10 )
59
+ self .alert_policy = self .alert_policy_client .create_alert_policy (
60
+ self .project_name , policy )
61
+ # Create a notification channel.
62
+ notification_channel = (
63
+ monitoring_v3 .types .notification_pb2 .NotificationChannel ())
64
+ json = open ('test_notification_channel.json' ).read ()
65
+ google .protobuf .json_format .Parse (json , notification_channel )
66
+ notification_channel .display_name = (
67
+ 'snippets-test-' + random_name (10 ))
68
+ self .notification_channel = (
69
+ self .notification_channel_client .create_notification_channel (
70
+ self .project_name , notification_channel ))
71
+ setup ()
61
72
return self
62
73
63
74
def __exit__ (self , type , value , traceback ):
64
75
# Delete the policy and channel we created.
65
- self .alert_policy_client .delete_alert_policy (self .alert_policy .name )
66
- if self .notification_channel .name :
67
- self .notification_channel_client .delete_notification_channel (
68
- self .notification_channel .name )
76
+ @retry (wait_exponential_multiplier = 1000 , wait_exponential_max = 10000 ,
77
+ stop_max_attempt_number = 5 , retry_on_exception = retry_if_aborted )
78
+ def teardown ():
79
+ self .alert_policy_client .delete_alert_policy (
80
+ self .alert_policy .name )
81
+ if self .notification_channel .name :
82
+ self .notification_channel_client .delete_notification_channel (
83
+ self .notification_channel .name )
84
+ teardown ()
69
85
70
86
71
87
@pytest .fixture (scope = 'session' )
@@ -81,36 +97,55 @@ def test_list_alert_policies(capsys, pochan):
81
97
82
98
83
99
def test_enable_alert_policies (capsys , pochan ):
84
- snippets .enable_alert_policies (pochan .project_name , False )
85
- out , _ = capsys .readouterr ()
100
+ @retry (wait_exponential_multiplier = 1000 , wait_exponential_max = 10000 ,
101
+ stop_max_attempt_number = 5 , retry_on_exception = retry_if_aborted )
102
+ def invoke_sample (val ):
103
+ snippets .enable_alert_policies (pochan .project_name , val )
86
104
87
- snippets .enable_alert_policies (pochan .project_name , False )
105
+ invoke_sample (False )
106
+ invoke_sample (False )
88
107
out , _ = capsys .readouterr ()
89
108
assert "already disabled" in out
90
109
91
- snippets . enable_alert_policies ( pochan . project_name , True )
110
+ invoke_sample ( True )
92
111
out , _ = capsys .readouterr ()
93
112
assert "Enabled {0}" .format (pochan .project_name ) in out
94
113
95
- snippets . enable_alert_policies ( pochan . project_name , True )
114
+ invoke_sample ( True )
96
115
out , _ = capsys .readouterr ()
97
116
assert "already enabled" in out
98
117
99
118
100
119
def test_replace_channels (capsys , pochan ):
101
- alert_policy_id = pochan .alert_policy .name .split ('/' )[- 1 ]
102
- notification_channel_id = pochan .notification_channel .name .split ('/' )[- 1 ]
103
- snippets .replace_notification_channels (
104
- pochan .project_name , alert_policy_id , [notification_channel_id ])
120
+ @retry (wait_exponential_multiplier = 1000 , wait_exponential_max = 10000 ,
121
+ stop_max_attempt_number = 5 , retry_on_exception = retry_if_aborted )
122
+ def invoke_sample ():
123
+ alert_policy_id = pochan .alert_policy .name .split ('/' )[- 1 ]
124
+ notification_channel_id = pochan .notification_channel .name .split (
125
+ '/' )[- 1 ]
126
+ snippets .replace_notification_channels (
127
+ pochan .project_name , alert_policy_id , [notification_channel_id ])
128
+
129
+ invoke_sample ()
105
130
out , _ = capsys .readouterr ()
106
131
assert "Updated {0}" .format (pochan .alert_policy .name ) in out
107
132
108
133
109
134
def test_backup_and_restore (capsys , pochan ):
110
- snippets .backup (pochan .project_name , 'backup.json' )
135
+ @retry (wait_exponential_multiplier = 1000 , wait_exponential_max = 10000 ,
136
+ stop_max_attempt_number = 5 , retry_on_exception = retry_if_aborted )
137
+ def invoke_backup ():
138
+ snippets .backup (pochan .project_name , 'backup.json' )
139
+
140
+ invoke_backup ()
111
141
out , _ = capsys .readouterr ()
112
142
113
- snippets .restore (pochan .project_name , 'backup.json' )
143
+ @retry (wait_exponential_multiplier = 1000 , wait_exponential_max = 10000 ,
144
+ stop_max_attempt_number = 5 , retry_on_exception = retry_if_aborted )
145
+ def invoke_restore ():
146
+ snippets .restore (pochan .project_name , 'backup.json' )
147
+
148
+ invoke_restore ()
114
149
out , _ = capsys .readouterr ()
115
150
assert "Updated {0}" .format (pochan .alert_policy .name ) in out
116
151
assert "Updating channel {0}" .format (
@@ -119,8 +154,14 @@ def test_backup_and_restore(capsys, pochan):
119
154
120
155
def test_delete_channels (capsys , pochan ):
121
156
notification_channel_id = pochan .notification_channel .name .split ('/' )[- 1 ]
122
- snippets .delete_notification_channels (
123
- pochan .project_name , [notification_channel_id ], force = True )
157
+
158
+ @retry (wait_exponential_multiplier = 1000 , wait_exponential_max = 10000 ,
159
+ stop_max_attempt_number = 5 , retry_on_exception = retry_if_aborted )
160
+ def invoke_delete ():
161
+ snippets .delete_notification_channels (
162
+ pochan .project_name , [notification_channel_id ], force = True )
163
+
164
+ invoke_delete ()
124
165
out , _ = capsys .readouterr ()
125
166
assert "{0} deleted" .format (notification_channel_id ) in out
126
167
pochan .notification_channel .name = '' # So teardown is not tried
0 commit comments