From be24d0bf763675886635da73f2b6ce961ca8fba9 Mon Sep 17 00:00:00 2001 From: Pyifan Date: Wed, 18 Oct 2023 20:50:06 +0800 Subject: [PATCH] tidy up env --- testplan/common/utils/context.py | 34 ------------------- testplan/testing/multitest/driver/app.py | 31 ++++++++++------- testplan/testing/multitest/driver/base.py | 6 ++-- .../testplan/common/utils/test_context.py | 32 ++++++++--------- .../multitest/driver/myapp/config.yaml | 2 +- 5 files changed, 37 insertions(+), 68 deletions(-) diff --git a/testplan/common/utils/context.py b/testplan/common/utils/context.py index d981798b2..54df98f03 100644 --- a/testplan/common/utils/context.py +++ b/testplan/common/utils/context.py @@ -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. diff --git a/testplan/testing/multitest/driver/app.py b/testplan/testing/multitest/driver/app.py index 274221f16..ceedf0471 100644 --- a/testplan/testing/multitest/driver/app.py +++ b/testplan/testing/multitest/driver/app.py @@ -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: @@ -238,15 +240,16 @@ def binpath(self) -> str: def binary(self) -> str: """The actual binary to execute""" if not self._binary: - 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): + + 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() + ) target = os.path.join(self.binpath, name) + if self.cfg.binary_strategy == "copy": shutil.copyfile(self.resolved_bin, target) self._binary = target @@ -256,6 +259,8 @@ def binary(self) -> str: # 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 @@ -337,7 +342,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( diff --git a/testplan/testing/multitest/driver/base.py b/testplan/testing/multitest/driver/base.py index 3a06e5daa..56675e5bb 100644 --- a/testplan/testing/multitest/driver/base.py +++ b/testplan/testing/multitest/driver/base.py @@ -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( @@ -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) diff --git a/tests/unit/testplan/common/utils/test_context.py b/tests/unit/testplan/common/utils/test_context.py index 925ca5811..4bbef95d2 100644 --- a/tests/unit/testplan/common/utils/test_context.py +++ b/tests/unit/testplan/common/utils/test_context.py @@ -8,7 +8,6 @@ from testplan.common.utils.context import ( ContextValue, expand, - expand_env, render, ) @@ -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(): diff --git a/tests/unit/testplan/testing/multitest/driver/myapp/config.yaml b/tests/unit/testplan/testing/multitest/driver/myapp/config.yaml index 6299427b0..a2e19b53b 100644 --- a/tests/unit/testplan/testing/multitest/driver/myapp/config.yaml +++ b/tests/unit/testplan/testing/multitest/driver/myapp/config.yaml @@ -1,3 +1,3 @@ binary: {{binary}} command: {{cmd}} -app_path: {{app_path}} +app_path: {{app_path}} \ No newline at end of file