1111
1212from __future__ import annotations
1313
14+ import fcntl
1415import time
1516from contextlib import contextmanager
1617from typing import TYPE_CHECKING
@@ -99,32 +100,72 @@ def _delete_provider(stub: object, name: str) -> None:
99100 raise
100101
101102
102- @contextmanager
103- def _providers_v2_enabled (stub : object ) -> Iterator [None ]:
104- """Enable the gateway-global ``providers_v2_enabled`` opt-in for the block.
103+ @pytest .fixture
104+ def providers_v2_enabled (
105+ sandbox_client : SandboxClient ,
106+ tmp_path_factory : pytest .TempPathFactory ,
107+ ) -> Iterator [None ]:
108+ """Enable the gateway-global ``providers_v2_enabled`` opt-in for one test.
105109
106110 Composing a provider's network policy onto a sandbox is gated behind this
107- setting, which defaults off. The built-in github profile's git-transport
108- rules only reach the sandbox with it enabled. ``global`` is a Python keyword,
109- so it is passed positionally through a dict expansion.
111+ setting, which defaults off; the built-in github profile's git-transport
112+ rules only reach the sandbox with it enabled.
113+
114+ The setting is gateway-global, so this mutates shared state. To keep it safe
115+ on an existing or shared gateway and under parallel workers:
116+
117+ - The mutation is serialized across xdist workers with an exclusive file lock
118+ on the run's shared base temp dir (``getbasetemp().parent``), held for the
119+ whole test so the read-modify-restore cannot interleave.
120+ - The exact prior value (or absence) is captured via ``GetGatewayConfig`` and
121+ restored in ``finally``, leaving the gateway as it was found.
122+
123+ ``global`` is a Python keyword, so it is passed through a dict expansion.
110124 """
111- stub .UpdateConfig (
112- openshell_pb2 .UpdateConfigRequest (
113- setting_key = "providers_v2_enabled" ,
114- setting_value = sandbox_pb2 .SettingValue (bool_value = True ),
115- ** {"global" : True },
125+ stub = sandbox_client ._stub
126+ key = "providers_v2_enabled"
127+ lock_path = tmp_path_factory .getbasetemp ().parent / "providers-v2-setting.lock"
128+ with lock_path .open ("w" ) as lock_file :
129+ fcntl .flock (lock_file , fcntl .LOCK_EX )
130+
131+ # GetGatewayConfig returns known keys even when unset, with an empty
132+ # SettingValue (no populated oneof). Only treat the setting as explicitly
133+ # present when its value oneof is actually set; otherwise restore = delete.
134+ config = stub .GetGatewayConfig (sandbox_pb2 .GetGatewayConfigRequest ())
135+ prior_value = sandbox_pb2 .SettingValue ()
136+ had_prior = (
137+ key in config .settings
138+ and config .settings [key ].WhichOneof ("value" ) is not None
116139 )
117- )
118- try :
119- yield
120- finally :
140+ if had_prior :
141+ prior_value .CopyFrom (config .settings [key ])
142+
121143 stub .UpdateConfig (
122144 openshell_pb2 .UpdateConfigRequest (
123- setting_key = "providers_v2_enabled" ,
124- delete_setting = True ,
145+ setting_key = key ,
146+ setting_value = sandbox_pb2 . SettingValue ( bool_value = True ) ,
125147 ** {"global" : True },
126148 )
127149 )
150+ try :
151+ yield
152+ finally :
153+ if had_prior :
154+ stub .UpdateConfig (
155+ openshell_pb2 .UpdateConfigRequest (
156+ setting_key = key ,
157+ setting_value = prior_value ,
158+ ** {"global" : True },
159+ )
160+ )
161+ else :
162+ stub .UpdateConfig (
163+ openshell_pb2 .UpdateConfigRequest (
164+ setting_key = key ,
165+ delete_setting = True ,
166+ ** {"global" : True },
167+ )
168+ )
128169
129170
130171# ===========================================================================
@@ -519,6 +560,7 @@ def test_update_provider_rejects_type_change(
519560# ===========================================================================
520561
521562
563+ @pytest .mark .usefixtures ("providers_v2_enabled" )
522564def test_github_provider_allows_https_git_clone (
523565 sandbox : Callable [..., Sandbox ],
524566 sandbox_client : SandboxClient ,
@@ -531,9 +573,11 @@ def test_github_provider_allows_https_git_clone(
531573 the sandbox, exercising provider attachment, effective-policy composition,
532574 TLS interception, and real git behavior end to end. git delegates HTTPS to a
533575 ``git-remote-https`` helper whose ancestor is ``/usr/bin/git``, so the
534- profile's git binary covers it via ancestor matching.
576+ profile's git binary covers it via ancestor matching. The
577+ ``providers_v2_enabled`` fixture turns on the gateway-global gate that
578+ composes the provider's network policy.
535579 """
536- with _providers_v2_enabled ( sandbox_client . _stub ), provider (
580+ with provider (
537581 sandbox_client ._stub ,
538582 name = "e2e-test-github-clone" ,
539583 provider_type = "github" ,
0 commit comments