Skip to content

Commit

Permalink
tidy up env
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyifan committed Oct 19, 2023
1 parent 1e0de86 commit 6431f6a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 78 deletions.
34 changes: 0 additions & 34 deletions testplan/common/utils/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,40 +97,6 @@ def expand(value, contextobj, constructor=None):
return value


def expand_env(
orig: Dict[str, str],
overrides: Dict[str, Union[str, ContextValue]],
contextobj,
):
"""
Copies the `orig` dict of environment variables.
Applies specified overrides.
Removes keys that have value of None as override.
Expands context values as strings.
Returns as a copy.
:param orig: The initial environment variables. Usually `os.environ` is to
be passed in. This will not be modified.
:type orig: ``dict`` of ``str`` to ``str``
:param overrides: Keys and values to be overriden. Values can be strings or
context objects.
:type overrides: ``dict`` of ``str`` to either ``str`` or ``ContextValue``
:param contextobj: The context object that can be used to expand context
values.
:type contextobj: ``object``
:return: Copied, overridden and expanded environment variables
:rtype: ``dict``
"""
env = orig.copy()
env.update(overrides)
return {
key: expand(val, contextobj, str)
for key, val in env.items()
if val is not None
}


def render(template: Union[Template, TempitaTemplate, str], context) -> str:
"""
Renders the template with the given context, that used for expression resolution.
Expand Down
2 changes: 1 addition & 1 deletion testplan/runnable/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ def add(
self.cfg.test_lister.log_test_info(task_info.materialized_test)
return None

if resource is None or self.cfg.interactive_port:
if resource is None or self._is_interactive_run():
# use local runner for interactive
resource = self.resources.first()
# just enqueue the materialized test
Expand Down
46 changes: 27 additions & 19 deletions testplan/testing/multitest/driver/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,17 @@ def cmd(self) -> str:
]
return cmd

@emphasized
@property
# @emphasized
# @property
def env(self) -> Dict[str, str]:
"""Environment variables."""

if isinstance(self.cfg.env, dict):
ctx = self.context_input()
return {
key: expand(val, self.context, str)
if is_context(val)
else render(val, self.context_input())
key: expand(val, self.context, str) if is_context(val)
# retain None val for child class use case
else (render(val, ctx) if val is not None else None)
for key, val in self.cfg.env.items()
}
else:
Expand Down Expand Up @@ -237,25 +239,31 @@ def binpath(self) -> str:
@property
def binary(self) -> str:
"""The actual binary to execute"""
if not self._binary:

if self._binary and os.path.isfile(self._binary):
return self._binary

if os.path.isfile(self.resolved_bin):

if self.cfg.path_cleanup is True:
name = os.path.basename(self.cfg.binary)
else:
name = "{}-{}".format(
os.path.basename(self.cfg.binary), uuid.uuid4()
)

if os.path.isfile(self.resolved_bin):
target = os.path.join(self.binpath, name)
if self.cfg.binary_strategy == "copy":
shutil.copyfile(self.resolved_bin, target)
self._binary = target
elif self.cfg.binary_strategy == "link" and not IS_WIN:
os.symlink(os.path.abspath(self.resolved_bin), target)
self._binary = target
# else binary_strategy is noop then we don't do anything
else:
self._binary = self.resolved_bin
target = os.path.join(self.binpath, name)

if self.cfg.binary_strategy == "copy":
shutil.copyfile(self.resolved_bin, target)
self._binary = target
elif self.cfg.binary_strategy == "link" and not IS_WIN:
os.symlink(os.path.abspath(self.resolved_bin), target)
self._binary = target
# else binary_strategy is noop then we don't do anything
else:
self._binary = self.resolved_bin
else:
self._binary = self.resolved_bin

return self._binary

Expand Down Expand Up @@ -337,7 +345,7 @@ def starting(self) -> None:
stdout=self.std.out,
stderr=self.std.err,
cwd=cwd,
env=self.env,
env=self.env(),
)
except Exception:
self.logger.error(
Expand Down
6 changes: 2 additions & 4 deletions testplan/testing/multitest/driver/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,7 @@ def install_files(self) -> None:
install_file = render(install_file, context)
if not os.path.isfile(install_file):
raise ValueError("{} is not a file".format(install_file))
instantiate(
install_file, self.context_input(), self._install_target()
)
instantiate(install_file, context, self._install_target())
elif isinstance(install_file, tuple):
if len(install_file) != 2:
raise ValueError(
Expand All @@ -419,7 +417,7 @@ def install_files(self) -> None:
src, dst = install_file
# may have jinja2/tempita template in file path
src = render(src, context)
dst = render(src, context)
dst = render(dst, context)
if not os.path.isabs(dst):
dst = os.path.join(self._install_target(), dst)
instantiate(src, self.context_input(), dst)
Expand Down
7 changes: 4 additions & 3 deletions tests/functional/testplan/runners/pools/test_auto_part.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_auto_parts_discover():
assert len(pool.added_items) == 5
for task in pool.added_items.values():
assert task.weight == 45

mockplan.run()
assert pool.size == 2


Expand Down Expand Up @@ -64,7 +64,8 @@ def test_auto_parts_discover_interactive(runpath):
)

local_pool = mockplan.resources.get(mockplan.resources.first())
# validate that only on etask added to the local pool without split
# validate that only one task added to the local pool without split

assert len(pool.added_items) == 0
assert len(local_pool.added_items) == 1

Expand Down Expand Up @@ -94,5 +95,5 @@ def test_auto_weight_discover():
assert len(pool.added_items) == 2
for task in pool.added_items.values():
assert task.weight == 140

mockplan.run()
assert pool.size == 1
32 changes: 16 additions & 16 deletions tests/unit/testplan/common/utils/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from testplan.common.utils.context import (
ContextValue,
expand,
expand_env,
render,
)

Expand Down Expand Up @@ -65,21 +64,22 @@ def test_expand(driver_context):
assert expand(cv, driver_context, int) == 123


def test_expand_env(driver_context):
env = dict(a="1", b="2")
overrides = dict(
c="str",
d="{{notcontext}}",
e=ContextValue("driver", "{{host}}"),
b=ContextValue("driver", "{{port}}"),
)
result = expand_env(env, overrides, driver_context)

assert result["a"] == "1"
assert result["b"] == "123"
assert result["c"] == "str"
assert result["d"] == "{{notcontext}}"
assert result["e"] == "host.ms.com"
# def test_expand_env(driver_context):
#
# env = dict(a="1", b="2")
# overrides = dict(
# c="str",
# d="{{notcontext}}",
# e=ContextValue("driver", "{{host}}"),
# b=ContextValue("driver", "{{port}}"),
# )
# result = expand_env(env, overrides, driver_context)
#
# assert result["a"] == "1"
# assert result["b"] == "123"
# assert result["c"] == "str"
# assert result["d"] == "{{notcontext}}"
# assert result["e"] == "host.ms.com"


def test_render():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
binary: {{binary}}
command: {{cmd}}
app_path: {{app_path}}
app_path: {{app_path}}

0 comments on commit 6431f6a

Please sign in to comment.