Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 75c97e8

Browse files
committed
Inject env variables inside SUT via option
Added --env option that permits to inject environment variables inside SUT before any execution. Signed-off-by: Andrea Cervesato <[email protected]>
1 parent 4ad85fb commit 75c97e8

File tree

3 files changed

+85
-13
lines changed

3 files changed

+85
-13
lines changed

ltp/main.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ def _sut_config(value: str) -> dict:
9090
return config
9191

9292

93+
def _env_config(value: str) -> dict:
94+
"""
95+
Return an environment configuration dictionary, parsing strings such as
96+
"key=value:key=value:key=value".
97+
"""
98+
if not value:
99+
return None
100+
101+
params = value.split(':')
102+
config = _from_params_to_config(params)
103+
104+
return config
105+
106+
93107
def _discover_sut(folder: str) -> list:
94108
"""
95109
Discover new SUT implementations inside a specific folder.
@@ -185,7 +199,8 @@ def _ltp_run(parser: ArgumentParser, args: Namespace) -> None:
185199
args.run_cmd,
186200
args.ltp_dir,
187201
tmpdir,
188-
skip_tests=skip_tests)
202+
skip_tests=skip_tests,
203+
env=args.env)
189204

190205
ltp.events.stop_event_loop()
191206

@@ -243,6 +258,11 @@ def run() -> None:
243258
type=int,
244259
default=3600,
245260
help="Timeout before stopping a single execution")
261+
parser.add_argument(
262+
"--env",
263+
"-e",
264+
type=_env_config,
265+
help="List of key=value environment values separated by ':'")
246266
parser.add_argument(
247267
"--run-suite",
248268
"-r",

ltp/session.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,27 +147,35 @@ def _start_sut(
147147
self,
148148
ltpdir: str,
149149
tmpdir: TempDir,
150-
sut_config: dict) -> SUT:
150+
sut_config: dict,
151+
env: dict) -> SUT:
151152
"""
152153
Start a new SUT and return it initialized.
153154
"""
154155
sut_name = sut_config.get("name", None)
155156
testcases = os.path.join(ltpdir, "testcases", "bin")
156157

157-
env = {}
158-
env["PATH"] = "/sbin:/usr/sbin:/usr/local/sbin:" + \
158+
sut_env = {}
159+
sut_env["PATH"] = "/sbin:/usr/sbin:/usr/local/sbin:" + \
159160
f"/root/bin:/usr/local/bin:/usr/bin:/bin:{testcases}"
160-
env["LTPROOT"] = ltpdir
161-
env["TMPDIR"] = tmpdir.root if tmpdir.root else "/tmp"
162-
env["LTP_TIMEOUT_MUL"] = str((self._exec_timeout * 0.9) / 300.0)
161+
sut_env["LTPROOT"] = ltpdir
162+
sut_env["TMPDIR"] = tmpdir.root if tmpdir.root else "/tmp"
163+
sut_env["LTP_TIMEOUT_MUL"] = str((self._exec_timeout * 0.9) / 300.0)
163164

164165
if self._no_colors:
165-
env["LTP_COLORIZE_OUTPUT"] = "0"
166+
sut_env["LTP_COLORIZE_OUTPUT"] = "0"
166167
else:
167-
env["LTP_COLORIZE_OUTPUT"] = "1"
168+
sut_env["LTP_COLORIZE_OUTPUT"] = "1"
169+
170+
if env:
171+
for key, value in env.items():
172+
if key not in sut_env:
173+
self._logger.info(
174+
"Add %s=%s environment variable into SUT")
175+
sut_env[key] = value
168176

169177
config = {}
170-
config['env'] = env
178+
config['env'] = sut_env
171179
config['cwd'] = testcases
172180
config['tmpdir'] = tmpdir.abspath
173181
config.update(sut_config)
@@ -257,7 +265,8 @@ def run_single(
257265
command: str,
258266
ltpdir: str,
259267
tmpdir: TempDir,
260-
skip_tests: str = None) -> int:
268+
skip_tests: str = None,
269+
env: dict = None) -> int:
261270
"""
262271
Run some testing suites with a specific SUT configurations.
263272
:param sut_config: system under test configuration.
@@ -274,6 +283,8 @@ def run_single(
274283
:type tmpdir: TempDir
275284
:param skip_tests: tests to ignore in a regex form
276285
:type skip_tests: str
286+
:param env: environment variables to inject inside SUT
287+
:type env: dict
277288
:returns: exit code for the session
278289
"""
279290
if not sut_config:
@@ -296,7 +307,8 @@ def run_single(
296307
self._sut = self._start_sut(
297308
ltpdir,
298309
tmpdir,
299-
sut_config)
310+
sut_config,
311+
env)
300312

301313
self._logger.info("Created SUT: %s", self._sut.name)
302314

ltp/tests/test_session.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
22
Unittests for session module.
33
"""
4-
import json
54
import os
65
import stat
6+
import json
77
import queue
88
import pytest
99
import ltp
@@ -353,3 +353,43 @@ def test_suite_timeout_report(self, suts, tmpdir, sut_config, ltpdir):
353353
assert tracer.next_event() == "suite_timeout"
354354
assert tracer.next_event() == "sut_stop"
355355
assert tracer.next_event() == "session_completed"
356+
357+
def test_env(self, suts, tmpdir, sut_config, ltpdir):
358+
"""
359+
Run a session without suites but only one command run.
360+
"""
361+
report_path = tmpdir / "report.json"
362+
363+
ltpdir = tmpdir.mkdir("ltp")
364+
script_sh = ltpdir.mkdir("testcases").mkdir("bin") / "script.sh"
365+
script_sh.write("#!/bin/sh\necho -n $VAR0:$VAR1")
366+
367+
st = os.stat(str(script_sh))
368+
os.chmod(str(script_sh), st.st_mode | stat.S_IEXEC)
369+
370+
suite = ltpdir.mkdir("runtest") / "suite"
371+
suite.write("test script.sh")
372+
373+
tmpdir_obj = TempDir(root=tmpdir)
374+
try:
375+
session = Session(suts)
376+
retcode = session.run_single(
377+
sut_config,
378+
report_path,
379+
["suite"],
380+
None,
381+
ltpdir,
382+
tmpdir_obj,
383+
env=dict(VAR0="0", VAR1="1"))
384+
385+
assert retcode == Session.RC_OK
386+
assert os.path.isfile(report_path)
387+
388+
report_d = None
389+
with open(report_path, 'r') as report_f:
390+
report_d = json.loads(report_f.read())
391+
392+
assert len(report_d["results"]) > 0
393+
assert report_d["results"][0]["test"]["log"] == "0:1"
394+
finally:
395+
session.stop()

0 commit comments

Comments
 (0)