Skip to content

Commit 5f2c9f4

Browse files
brbzull0zwoop
authored andcommitted
Autest - Proxy Verifier Extension, add context template $-base string substitution in the replay file. (#7866)
(cherry picked from commit 84f3177)
1 parent 7b9c826 commit 5f2c9f4

3 files changed

Lines changed: 70 additions & 11 deletions

File tree

tests/gold_tests/autest-site/verifier_client.test.ext

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ Implement the Proxy Verifier client extensions.
1919

2020

2121
import os
22-
from verifier_common import create_address_argument
22+
from verifier_common import create_address_argument, substitute_context_in_replay_file
2323

2424

2525
def _configure_client(obj, process, name, replay_path, http_ports=None,
2626
https_ports=None, http3_ports=None, keys=None,
27-
ssl_cert='', ca_cert='', verbose=True, other_args=''):
27+
ssl_cert='', ca_cert='', verbose=True, other_args='',
28+
context=None):
2829
"""
2930
Configure the process for running the verifier-client.
3031

@@ -53,6 +54,10 @@ def _configure_client(obj, process, name, replay_path, http_ports=None,
5354
# Configure the verifier-client command line arguments.
5455
command = "verifier-client run "
5556
if replay_path:
57+
if context:
58+
# replace the passed replay file with the new one generated using the passed
59+
# context
60+
replay_path = substitute_context_in_replay_file(process, replay_path, context)
5661
# Create a copy of the replay directory in the run directory.
5762
run_replay_path = os.path.join(client_dir, os.path.basename(replay_path))
5863
process.Setup.Copy(replay_path, run_replay_path, CopyLogic.SoftFiles)
@@ -133,7 +138,8 @@ def _configure_client(obj, process, name, replay_path, http_ports=None,
133138

134139
def AddVerifierClientProcess(run, name, replay_path, http_ports=None,
135140
https_ports=None, http3_ports=None, keys=None,
136-
ssl_cert='', ca_cert='', verbose=True, other_args=''):
141+
ssl_cert='', ca_cert='', verbose=True, other_args='',
142+
context=None):
137143
"""
138144
Set the Default process of the test run to a verifier-client Process.
139145

@@ -163,15 +169,27 @@ def AddVerifierClientProcess(run, name, replay_path, http_ports=None,
163169

164170
other_args: (str) Any other arbitrary options to pass to verifier-client.
165171

172+
context: Any dictionary-like object with keys that match the placeholders
173+
in the replay file.
174+
Template strings support $-based substitutions in the replay file.
175+
You can refer to https://docs.python.org/3/library/string.html#template-strings
176+
for more information how to add template strings to the replay file.
166177
Returns:
167178
The newly constructed verifier-client for the test run, which is also the
168179
Default Process of the test run.
180+
Throws:
181+
ValueError:
182+
If context substitution is expected to be done but a directory is passed as a
183+
replay_path.
184+
OSError in case of any issues related to I/O error, ie: File Not found for the replay
185+
file when a context substitution is expected.
186+
KeyError if placeholders are missing from the mapping between context and the replay file.
169187
"""
170188

171189
p = run.Processes.Default
172190
_configure_client(run, p, name, replay_path, http_ports, https_ports,
173191
http3_ports, keys, ssl_cert, ca_cert, verbose,
174-
other_args)
192+
other_args, context)
175193
return p
176194

177195

tests/gold_tests/autest-site/verifier_common.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1717
# See the License for the specific language governing permissions and
1818
# limitations under the License.
19+
import os
20+
import tempfile
21+
from string import Template
1922

2023

2124
def create_address_argument(ports):
@@ -33,3 +36,24 @@ def create_address_argument(ports):
3336
argument += "127.0.0.1:{}".format(port)
3437
argument += '"'
3538
return argument
39+
40+
41+
def substitute_context_in_replay_file(process, replay_path, context):
42+
'''
43+
Perform substitution base on the passed context dict.
44+
This function will return the new replay_path file
45+
'''
46+
# Only files for now
47+
if os.path.isdir(replay_path):
48+
raise ValueError(f"Mapping substitution not supported for directories.")
49+
50+
with open(os.path.join(process.TestDirectory, replay_path), 'r') as replay_file:
51+
replay_template = Template(replay_file.read())
52+
replay_content = replay_template.substitute(context)
53+
tf = tempfile.NamedTemporaryFile(delete=False, dir=process.RunDirectory, suffix=f"_{os.path.basename(replay_path)}")
54+
replay_path = tf.name
55+
with open(replay_path, "w") as new_replay_file:
56+
new_replay_file.write(replay_content)
57+
58+
# use this as replay_path
59+
return replay_path

tests/gold_tests/autest-site/verifier_server.test.ext

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ Implement the Proxy Verifier test server extension.
1818
# limitations under the License.
1919

2020
import os
21+
2122
from ports import get_port
22-
from verifier_common import create_address_argument
23+
from verifier_common import create_address_argument, substitute_context_in_replay_file
2324

2425

2526
def _configure_server(obj, process, name, replay_path, http_ports=None, https_ports=None,
2627
http3_ports=None, ssl_cert='', ca_cert='', verbose=True,
27-
other_args=''):
28+
other_args='', context=None):
2829
"""
2930
Configure the provided process to run a verifier-server command.
3031

@@ -119,8 +120,13 @@ def _configure_server(obj, process, name, replay_path, http_ports=None, https_po
119120
command += ' --ca-certs "{}" '.format(run_ca_cert)
120121

121122
if replay_path:
122-
# Create a copy of the replay directory in the run directory.
123+
if context:
124+
# replace the passed replay file with the new one generated using the passed
125+
# context
126+
replay_path = substitute_context_in_replay_file(process, replay_path, context)
127+
123128
run_replay_path = os.path.join(server_dir, os.path.basename(replay_path))
129+
# Create a copy of the replay directory in the run directory.
124130
process.Setup.Copy(replay_path, run_replay_path, CopyLogic.SoftFiles)
125131
command += "{} ".format(run_replay_path)
126132

@@ -141,7 +147,7 @@ def _configure_server(obj, process, name, replay_path, http_ports=None, https_po
141147

142148
def MakeVerifierServerProcess(test, name, replay_path, http_ports=None,
143149
https_ports=None, http3_ports=None, ssl_cert='',
144-
ca_cert='', verbose=True, other_args=''):
150+
ca_cert='', verbose=True, other_args='', context=None):
145151
"""
146152
Create a verifier-server process for the Test.
147153

@@ -174,19 +180,30 @@ def MakeVerifierServerProcess(test, name, replay_path, http_ports=None,
174180

175181
other_args: (str) Any other arbitrary options to pass to verifier-server.
176182

183+
context: Any dictionary-like object with keys that match the placeholders
184+
in the replay file.
185+
Template strings support $-based substitutions in the
186+
replay file.
187+
You can refer to https://docs.python.org/3/library/string.html#template-strings
188+
for more information how to add template strings to the replay file.
177189
Raises:
178190
ValueError if https_ports is non-empty and a valid ssl_cert could not
179191
be derived.
192+
If context substitution is expected to be done but a directory is passed as a
193+
replay_path.
194+
OSError in case of any issues related to I/O error, ie: File Not found for the replay
195+
file when a context substitution is expected.
196+
KeyError if placeholders are missing from the mapping between context and the replay file.
180197
"""
181198
server = test.Processes.Process(name)
182199
_configure_server(test, server, name, replay_path, http_ports, https_ports,
183-
http3_ports, ssl_cert, ca_cert, verbose, other_args)
200+
http3_ports, ssl_cert, ca_cert, verbose, other_args, context)
184201
return server
185202

186203

187204
def AddVerifierServerProcess(run, name, replay_path, http_ports=None,
188205
https_ports=None, http3_ports=None, ssl_cert='',
189-
ca_cert='', verbose=True, other_args=''):
206+
ca_cert='', verbose=True, other_args='', context=None):
190207
"""
191208
Create a verifier-server process and configure it for the given TestRun.
192209

@@ -201,7 +218,7 @@ def AddVerifierServerProcess(run, name, replay_path, http_ports=None,
201218

202219
server = run.Processes.Process(name)
203220
_configure_server(run, server, name, replay_path, http_ports, https_ports,
204-
http3_ports, ssl_cert, ca_cert, verbose, other_args)
221+
http3_ports, ssl_cert, ca_cert, verbose, other_args, context)
205222

206223
client = run.Processes.Default
207224
client.StartBefore(server)

0 commit comments

Comments
 (0)