1313 "imagename,called_sns_publish, publish_call_count" ,
1414 [
1515 ("test-image-10" , True , 1 ),
16- ("test-image-11" , True , 4 ),
16+ ("test-image-11" , True , 2 ),
17+ ("test-image-12" , True , 2 ),
1718 ],
1819)
1920def test_sns_publish (imagename , called_sns_publish , publish_call_count ):
@@ -23,11 +24,12 @@ def test_sns_publish(imagename, called_sns_publish, publish_call_count):
2324 with patch ("boto3.client" ) as bclient_mock :
2425 instance = bclient_mock .return_value
2526 ctx = context .Context (curdir / "fixtures/config1.yaml" , None )
26- image_conf = ctx . conf [ "images" ][ imagename ]
27-
28- for region in image_conf [ "regions" ]:
29- sns . SNSNotification ( ctx , imagename , region ). publish ()
27+ instance . describe_regions . return_value = {
28+ "Regions" : [{ "RegionName" : "eu-central-1" }, { "RegionName" : "us-east-1" }]
29+ }
30+ instance . list_buckets . return_value = { "Buckets" : [{ "Name" : "bucket1" }]}
3031
32+ sns .SNSNotification (ctx , imagename ).publish ()
3133 assert instance .publish .called == called_sns_publish
3234 assert instance .publish .call_count == publish_call_count
3335
@@ -37,6 +39,7 @@ def test_sns_publish(imagename, called_sns_publish, publish_call_count):
3739 [
3840 ("test-image-10" ),
3941 ("test-image-11" ),
42+ ("test-image-12" ),
4043 ],
4144)
4245def test_sns_publish_fail_with_invalid_topic (imagename ):
@@ -46,12 +49,15 @@ def test_sns_publish_fail_with_invalid_topic(imagename):
4649 with patch ("boto3.client" ) as bclient_mock :
4750 instance = bclient_mock .return_value
4851 ctx = context .Context (curdir / "fixtures/config1.yaml" , None )
49- image_conf = ctx .conf ["images" ][imagename ]
52+ instance .describe_regions .return_value = {
53+ "Regions" : [{"RegionName" : "eu-central-1" }, {"RegionName" : "us-east-1" }]
54+ }
55+ instance .list_buckets .return_value = {"Buckets" : [{"Name" : "bucket1" }]}
5056
5157 # topic1 is invalid topic
5258 def side_effect (* args , ** kwargs ):
5359 topic_arn = kwargs .get ("TopicArn" )
54- if "topic1" in topic_arn :
60+ if "topic1" in topic_arn and "us-east-1" in topic_arn :
5561 error_reponse = {
5662 "Error" : {
5763 "Code" : "NotFoundException" ,
@@ -63,16 +69,16 @@ def side_effect(*args, **kwargs):
6369
6470 instance .publish .side_effect = side_effect
6571
66- for region in image_conf ["regions" ]:
67- with pytest .raises (exceptions .AWSNotificationException ):
68- sns .SNSNotification (ctx , imagename , region ).publish ()
72+ with pytest .raises (exceptions .AWSNotificationException ):
73+ sns .SNSNotification (ctx , imagename ).publish ()
6974
7075
7176@pytest .mark .parametrize (
7277 "imagename" ,
7378 [
7479 ("test-image-10" ),
7580 ("test-image-11" ),
81+ ("test-image-12" ),
7682 ],
7783)
7884def test_sns_publish_fail_with_unauthorized_user (imagename ):
@@ -82,7 +88,10 @@ def test_sns_publish_fail_with_unauthorized_user(imagename):
8288 with patch ("boto3.client" ) as bclient_mock :
8389 instance = bclient_mock .return_value
8490 ctx = context .Context (curdir / "fixtures/config1.yaml" , None )
85- image_conf = ctx .conf ["images" ][imagename ]
91+ instance .describe_regions .return_value = {
92+ "Regions" : [{"RegionName" : "eu-central-1" }, {"RegionName" : "us-east-1" }]
93+ }
94+ instance .list_buckets .return_value = {"Buckets" : [{"Name" : "bucket1" }]}
8695
8796 error_reponse = {
8897 "Error" : {
@@ -92,49 +101,89 @@ def test_sns_publish_fail_with_unauthorized_user(imagename):
92101 }
93102 instance .publish .side_effect = botocore .exceptions .ClientError (error_reponse , "" )
94103
95- for region in image_conf ["regions" ]:
96- with pytest .raises (exceptions .AWSAuthorizationException ):
97- sns .SNSNotification (ctx , imagename , region ).publish ()
104+ with pytest .raises (exceptions .AWSAuthorizationException ):
105+ sns .SNSNotification (ctx , imagename ).publish ()
98106
99107
100108@pytest .mark .parametrize (
101- "imagename, partition, expected" ,
109+ "imagename, partition, regions_in_partition, expected" ,
102110 [
103111 (
104112 "test-image-10" ,
105113 "aws-cn" ,
114+ ["cn-north1" , "cn-northwest-1" ],
115+ [],
116+ ),
117+ (
118+ "test-image-11" ,
119+ "aws" ,
120+ ["us-east-1" , "eu-central-1" ],
106121 [
107- "arn:aws-cn:sns:us-east-1:1234:topic1" ,
122+ "arn:aws:sns:us-east-1:1234:topic1" ,
123+ "arn:aws:sns:eu-central-1:1234:topic2" ,
108124 ],
109125 ),
110126 (
111- "test-image-11 " ,
127+ "test-image-12 " ,
112128 "aws" ,
129+ ["us-east-1" , "eu-central-1" ],
113130 [
114131 "arn:aws:sns:us-east-1:1234:topic1" ,
115- "arn:aws:sns:us-east-1:1234:topic2" ,
116132 "arn:aws:sns:eu-central-1:1234:topic1" ,
117- "arn:aws:sns:eu-central-1:1234:topic2" ,
118133 ],
119134 ),
120135 ],
121136)
122- def test_sns__get_topic_arn (imagename , partition , expected ):
137+ def test_sns__get_topic_arn (imagename , partition , regions_in_partition , expected ):
123138 """
124139 Test the send_notification logic
125140 """
126141 with patch ("boto3.client" ) as bclient_mock :
127142 instance = bclient_mock .return_value
128143 ctx = context .Context (curdir / "fixtures/config1.yaml" , None )
129- image_conf = ctx .conf ["images" ][imagename ]
144+ sns_conf = ctx .conf ["images" ][imagename ]["sns" ]
145+ instance .describe_regions .return_value = {"Regions" : [{"RegionName" : r } for r in regions_in_partition ]}
146+ instance .list_buckets .return_value = {"Buckets" : [{"Name" : "bucket1" }]}
130147
131148 instance .get_caller_identity .return_value = {"Account" : "1234" , "Arn" : f"arn:{ partition } :iam::1234:user/test" }
132149
133150 topic_arns = []
134- for region in image_conf ["regions" ]:
135- for topic in image_conf ["sns" ]:
136- topic_name = next (iter (topic ))
137- res_arn = sns .SNSNotification (ctx , imagename , region )._get_topic_arn (topic_name )
138- topic_arns .append (res_arn )
151+ for topic in sns_conf :
152+ for topic_name , topic_conf in topic .items ():
153+ sns_regions = sns .SNSNotification (ctx , imagename )._sns_regions (topic_conf )
154+ for region in sns_regions :
155+ res_arn = sns .SNSNotification (ctx , imagename )._get_topic_arn (topic_name , region )
156+ topic_arns .append (res_arn )
139157
140158 assert topic_arns == expected
159+
160+
161+ @pytest .mark .parametrize (
162+ "imagename,regions_in_partition,regions_expected" ,
163+ [
164+ ("test-image-10" , ["us-east-1" , "eu-west-1" ], {"topic1" : ["us-east-1" ]}),
165+ (
166+ "test-image-11" ,
167+ ["us-east-1" , "eu-west-1" ],
168+ {"topic1" : ["us-east-1" ], "topic2" : []},
169+ ),
170+ ("test-image-12" , ["eu-northwest-1" , "ap-southeast-1" ], {"topic1" : ["eu-northwest-1" , "ap-southeast-1" ]}),
171+ ],
172+ )
173+ def test_sns_regions (imagename , regions_in_partition , regions_expected ):
174+ """
175+ Test the regions for a given image
176+ """
177+ with patch ("boto3.client" ) as bclient_mock :
178+ instance = bclient_mock .return_value
179+ instance .describe_regions .return_value = {"Regions" : [{"RegionName" : r } for r in regions_in_partition ]}
180+ ctx = context .Context (curdir / "fixtures/config1.yaml" , None )
181+ sns_conf = ctx .conf ["images" ][imagename ]["sns" ]
182+ instance .list_buckets .return_value = {"Buckets" : [{"Name" : "bucket1" }]}
183+
184+ sns_regions = {}
185+ for topic in sns_conf :
186+ for topic_name , topic_conf in topic .items ():
187+ sns_regions [topic_name ] = sns .SNSNotification (ctx , imagename )._sns_regions (topic_conf )
188+
189+ assert sns_regions == regions_expected
0 commit comments