1
1
import pytest
2
2
3
3
from localstack import config
4
- from localstack .config import HostAndPort
4
+ from localstack .config import HostAndPort , external_service_url , internal_service_url
5
5
6
6
7
7
class TestProviderConfig :
@@ -67,15 +67,7 @@ def ip() -> str:
67
67
68
68
69
69
class TestEdgeVariablesDerivedCorrectly :
70
- """
71
- Post-v2 we are deriving
72
-
73
- * EDGE_PORT
74
-
75
- from GATEWAY_LISTEN. We are also ensuring the configuration behaves
76
- well with LOCALSTACK_HOST, i.e. if LOCALSTACK_HOST is supplied and
77
- GATEWAY_LISTEN is not, then we should propagate LOCALSTACK_HOST configuration
78
- into GATEWAY_LISTEN.
70
+ """We are deriving GATEWAY_LISTEN and LOCALSTACK_HOST from provided environment variables.
79
71
80
72
Implementation note: monkeypatching the config module is hard, and causes
81
73
tests run after these ones to import the wrong config. Instead, we test the
@@ -96,29 +88,28 @@ class TestEdgeVariablesDerivedCorrectly:
96
88
"localstack_host" ,
97
89
"expected_gateway_listen" ,
98
90
"expected_localstack_host" ,
99
- "expected_edge_port" ,
100
91
],
101
92
[
102
93
###
103
- (None , None , [f"{ ip ()} :4566" ], "localhost.localstack.cloud:4566" , 4566 ),
104
- ("1.1.1.1" , None , ["1.1.1.1:4566" ], "localhost.localstack.cloud:4566" , 4566 ),
105
- (":5555" , None , [f"{ ip ()} :5555" ], "localhost.localstack.cloud:5555" , 5555 ),
106
- ("1.1.1.1:5555" , None , ["1.1.1.1:5555" ], "localhost.localstack.cloud:5555" , 5555 ),
94
+ (None , None , [f"{ ip ()} :4566" ], "localhost.localstack.cloud:4566" ),
95
+ ("1.1.1.1" , None , ["1.1.1.1:4566" ], "localhost.localstack.cloud:4566" ),
96
+ (":5555" , None , [f"{ ip ()} :5555" ], "localhost.localstack.cloud:5555" ),
97
+ ("1.1.1.1:5555" , None , ["1.1.1.1:5555" ], "localhost.localstack.cloud:5555" ),
107
98
###
108
- (None , "foo.bar" , [f"{ ip ()} :4566" ], "foo.bar:4566" , 4566 ),
109
- ("1.1.1.1" , "foo.bar" , ["1.1.1.1:4566" ], "foo.bar:4566" , 4566 ),
110
- (":5555" , "foo.bar" , [f"{ ip ()} :5555" ], "foo.bar:5555" , 5555 ),
111
- ("1.1.1.1:5555" , "foo.bar" , ["1.1.1.1:5555" ], "foo.bar:5555" , 5555 ),
99
+ (None , "foo.bar" , [f"{ ip ()} :4566" ], "foo.bar:4566" ),
100
+ ("1.1.1.1" , "foo.bar" , ["1.1.1.1:4566" ], "foo.bar:4566" ),
101
+ (":5555" , "foo.bar" , [f"{ ip ()} :5555" ], "foo.bar:5555" ),
102
+ ("1.1.1.1:5555" , "foo.bar" , ["1.1.1.1:5555" ], "foo.bar:5555" ),
112
103
###
113
- (None , ":7777" , [f"{ ip ()} :4566" ], "localhost.localstack.cloud:7777" , 4566 ),
114
- ("1.1.1.1" , ":7777" , ["1.1.1.1:4566" ], "localhost.localstack.cloud:7777" , 4566 ),
115
- (":5555" , ":7777" , [f"{ ip ()} :5555" ], "localhost.localstack.cloud:7777" , 5555 ),
116
- ("1.1.1.1:5555" , ":7777" , ["1.1.1.1:5555" ], "localhost.localstack.cloud:7777" , 5555 ),
104
+ (None , ":7777" , [f"{ ip ()} :4566" ], "localhost.localstack.cloud:7777" ),
105
+ ("1.1.1.1" , ":7777" , ["1.1.1.1:4566" ], "localhost.localstack.cloud:7777" ),
106
+ (":5555" , ":7777" , [f"{ ip ()} :5555" ], "localhost.localstack.cloud:7777" ),
107
+ ("1.1.1.1:5555" , ":7777" , ["1.1.1.1:5555" ], "localhost.localstack.cloud:7777" ),
117
108
###
118
- (None , "foo.bar:7777" , [f"{ ip ()} :4566" ], "foo.bar:7777" , 4566 ),
119
- ("1.1.1.1" , "foo.bar:7777" , ["1.1.1.1:4566" ], "foo.bar:7777" , 4566 ),
120
- (":5555" , "foo.bar:7777" , [f"{ ip ()} :5555" ], "foo.bar:7777" , 5555 ),
121
- ("1.1.1.1:5555" , "foo.bar:7777" , ["1.1.1.1:5555" ], "foo.bar:7777" , 5555 ),
109
+ (None , "foo.bar:7777" , [f"{ ip ()} :4566" ], "foo.bar:7777" ),
110
+ ("1.1.1.1" , "foo.bar:7777" , ["1.1.1.1:4566" ], "foo.bar:7777" ),
111
+ (":5555" , "foo.bar:7777" , [f"{ ip ()} :5555" ], "foo.bar:7777" ),
112
+ ("1.1.1.1:5555" , "foo.bar:7777" , ["1.1.1.1:5555" ], "foo.bar:7777" ),
122
113
],
123
114
)
124
115
def test_edge_configuration (
@@ -127,7 +118,6 @@ def test_edge_configuration(
127
118
localstack_host : str | None ,
128
119
expected_gateway_listen : list [str ],
129
120
expected_localstack_host : str ,
130
- expected_edge_port : int ,
131
121
):
132
122
environment = {}
133
123
if gateway_listen is not None :
@@ -276,3 +266,53 @@ def test_port_out_of_range(self, port):
276
266
)
277
267
278
268
assert "port out of range" in str (exc_info )
269
+
270
+
271
+ class TestServiceUrlHelpers :
272
+ @pytest .mark .parametrize (
273
+ ["protocol" , "subdomains" , "host" , "port" , "expected_service_url" ],
274
+ [
275
+ # Default
276
+ (None , None , None , None , "http://localhost.localstack.cloud:4566" ),
277
+ # Customize each part with defaults
278
+ ("https" , None , None , None , "https://localhost.localstack.cloud:4566" ),
279
+ (None , "s3" , None , None , "http://s3.localhost.localstack.cloud:4566" ),
280
+ (None , None , "localstack-container" , None , "http://localstack-container:4566" ),
281
+ (None , None , None , 5555 , "http://localhost.localstack.cloud:5555" ),
282
+ # Multiple subdomains
283
+ (
284
+ None ,
285
+ "abc123.execute-api.lambda" ,
286
+ None ,
287
+ None ,
288
+ "http://abc123.execute-api.lambda.localhost.localstack.cloud:4566" ,
289
+ ),
290
+ # Customize everything
291
+ (
292
+ "https" ,
293
+ "abc.execute-api" ,
294
+ "localstack-container" ,
295
+ 5555 ,
296
+ "https://abc.execute-api.localstack-container:5555" ,
297
+ ),
298
+ ],
299
+ )
300
+ def test_external_service_url (
301
+ self ,
302
+ protocol : str | None ,
303
+ subdomains : str | None ,
304
+ host : str | None ,
305
+ port : int | None ,
306
+ expected_service_url : str ,
307
+ ):
308
+ url = external_service_url (host = host , port = port , protocol = protocol , subdomains = subdomains )
309
+ assert url == expected_service_url
310
+
311
+ def test_internal_service_url (self ):
312
+ # defaults
313
+ assert internal_service_url () == "http://localhost:4566"
314
+ # subdomains
315
+ assert (
316
+ internal_service_url (subdomains = "abc.execute-api" )
317
+ == "http://abc.execute-api.localhost:4566"
318
+ )
0 commit comments