1818import re
1919import typing as _typing
2020
21+ import pytest
2122from pytest import mark , raises
2223
2324import firebase_functions .private .manifest as _manifest
@@ -69,6 +70,15 @@ def asamplefunctionpreserved(_):
6970 return "hello world"
7071
7172
73+ @pytest .fixture (autouse = True )
74+ def _cleanup_global_options ():
75+ """Reset global options so each test runs with a clean state."""
76+ original_options = options ._GLOBAL_OPTIONS
77+ options ._GLOBAL_OPTIONS = options .RuntimeOptions ()
78+ yield
79+ options ._GLOBAL_OPTIONS = original_options
80+
81+
7282def test_set_global_options ():
7383 """
7484 Testing if setting a global option internally change the values.
@@ -92,6 +102,30 @@ def test_global_options_merged_with_provider_options():
92102 )
93103
94104
105+ @pytest .mark .parametrize (
106+ "vpc_connector_expr_factory" ,
107+ [
108+ lambda : params .StringParam ("VPC_CONNECTOR" ),
109+ lambda : params .BoolParam ("USE_VPC" ).equals (True ).then ("my-vpc" , "" ),
110+ ],
111+ )
112+ def test_set_global_options_accepts_vpc_connector_expression (vpc_connector_expr_factory ):
113+ vpc_connector_expr = vpc_connector_expr_factory ()
114+ options .set_global_options (vpc_connector = vpc_connector_expr )
115+
116+ assert options ._GLOBAL_OPTIONS .vpc_connector == vpc_connector_expr , (
117+ "global vpc_connector expression was not stored"
118+ )
119+
120+ https_options = options .HttpsOptions ()
121+ endpoint = https_options ._endpoint (func_name = "test_vpc_global" )
122+
123+ assert endpoint .vpc is not None , "vpc block was not set on endpoint"
124+ assert endpoint .vpc ["connector" ] == str (vpc_connector_expr ), (
125+ "global vpc_connector Expression[str] was not applied to the endpoint"
126+ )
127+
128+
95129def test_https_options_removes_cors ():
96130 """
97131 Testing _HttpsOptions strips out the 'cors' property when converted to a dict.
@@ -246,6 +280,48 @@ def test_invoker_with_no_element_throws():
246280 options .HttpsOptions (invoker = [])._endpoint (func_name = "test" )
247281
248282
283+ @pytest .mark .parametrize (
284+ "vpc_connector_expr_factory" ,
285+ [
286+ lambda : params .StringParam ("VPC_CONNECTOR" ),
287+ lambda : params .BoolParam ("USE_VPC" ).equals (True ).then ("my-vpc" , "" ),
288+ ],
289+ )
290+ def test_vpc_connector_accepts_expression (vpc_connector_expr_factory ):
291+ vpc_connector_expr = vpc_connector_expr_factory ()
292+ https_options = options .HttpsOptions (vpc_connector = vpc_connector_expr )
293+ https_options_dict = https_options ._asdict_with_global_options ()
294+
295+ # The options dict should contain the CEL string representation for the expression.
296+ assert https_options_dict ["vpc_connector" ] == str (vpc_connector_expr ), (
297+ "vpc_connector expression was not converted to CEL string"
298+ )
299+
300+ # The generated endpoint should map the resolved vpc_connector into the vpc block.
301+ endpoint = https_options ._endpoint (func_name = "test_vpc" )
302+ assert endpoint .vpc is not None , "vpc block was not set on endpoint"
303+ assert endpoint .vpc ["connector" ] == str (vpc_connector_expr ), (
304+ "vpc connector was not set from vpc_connector Expression[str]"
305+ )
306+
307+
308+ def test_vpc_connector_expression_with_egress_settings ():
309+ vpc_connector_expr = params .StringParam ("VPC_CONNECTOR" )
310+ https_options = options .HttpsOptions (
311+ vpc_connector = vpc_connector_expr ,
312+ vpc_connector_egress_settings = options .VpcEgressSetting .ALL_TRAFFIC ,
313+ )
314+
315+ endpoint = https_options ._endpoint (func_name = "test_vpc_egress" )
316+ assert endpoint .vpc is not None , "vpc block was not set on endpoint"
317+ assert endpoint .vpc ["connector" ] == str (vpc_connector_expr ), (
318+ "vpc connector was not set from vpc_connector Expression[str]"
319+ )
320+ assert endpoint .vpc .get ("egressSettings" ) == options .VpcEgressSetting .ALL_TRAFFIC .value , (
321+ "egressSettings was not set alongside an Expression[str] vpc_connector"
322+ )
323+
324+
249325def _assert_alert_endpoint_options (endpoint , expected_alert_type , expect_app_id : str | None = None ):
250326 assert endpoint .region == ["europe-west1" ]
251327 assert endpoint .maxInstances == 1
0 commit comments