From c12e615e260955ca4687a364c3ff5e3697921e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0pl=C3=ADchal?= Date: Thu, 5 Mar 2020 18:12:37 +0100 Subject: [PATCH] Add environment support to the discover step Combine L1 and L2 environment in Test.export(). Include a simple test with an environment variable. Extend utils.format() to support dictionaries as well. --- tests/env/main.fmf | 4 ++++ tests/env/test.sh | 13 +++++++++++++ tmt/base.py | 16 ++++++++++++++-- tmt/steps/discover/__init__.py | 4 +++- tmt/utils.py | 5 +++++ 5 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 tests/env/main.fmf create mode 100755 tests/env/test.sh diff --git a/tests/env/main.fmf b/tests/env/main.fmf new file mode 100644 index 0000000000..83cb99d980 --- /dev/null +++ b/tests/env/main.fmf @@ -0,0 +1,4 @@ +summary: Check environment variables handling +tier: 2 +environment: + TESTING: WORKS diff --git a/tests/env/test.sh b/tests/env/test.sh new file mode 100755 index 0000000000..6e67306003 --- /dev/null +++ b/tests/env/test.sh @@ -0,0 +1,13 @@ +#!/bin/bash +# vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k + +# Include Beaker environment +. /usr/share/beakerlib/beakerlib.sh || exit 1 + +PACKAGE="tmt" + +rlJournalStart + rlPhaseStartTest + rlRun "echo $TESTING | grep WORKS" + rlPhaseEnd +rlJournalEnd diff --git a/tmt/base.py b/tmt/base.py index 958b3a6d94..d8743e457b 100644 --- a/tmt/base.py +++ b/tmt/base.py @@ -192,7 +192,7 @@ def lint(self): elif len(self.summary) > 50: echo(verdict(2, 'summary should not exceed 50 characters')) - def export(self, format_='yaml', keys=None): + def export(self, format_='yaml', keys=None, environment=None): """ Export test data into requested format @@ -210,7 +210,13 @@ def export(self, format_='yaml', keys=None): data['path'] = self.path if self.duration is not None: data['duration'] = self.duration - if self.environment is not None: + # Combine environment variables (plan overrides test) + if self.environment is not None or environment is not None: + combined_environment = dict() + if self.environment: + combined_environment.update(self.environment) + if environment: + combined_environment.update(environment) data['environment'] = ' '.join( tmt.utils.shell_variables(self.environment)) return name, data @@ -249,6 +255,9 @@ def __init__(self, node, run=None): if not isinstance(gates, list): gates = [gates] + # Environment variables + self.environment = node.get('environment') + @staticmethod def overview(tree): """ Show overview of available plans """ @@ -300,6 +309,9 @@ def show(self): self.ls(summary=True) for step in self.steps(disabled=True): step.show() + if self.environment is not None: + echo(tmt.utils.format( + 'environment', self.environment, key_color='blue')) if self.opt('verbose'): self._sources() diff --git a/tmt/steps/discover/__init__.py b/tmt/steps/discover/__init__.py index a3f82cd740..5fbcd4125a 100644 --- a/tmt/steps/discover/__init__.py +++ b/tmt/steps/discover/__init__.py @@ -20,7 +20,9 @@ def save(self): """ Save step data to the workdir """ super(Discover, self).save() # Create 'tests.yaml' with the list of tests for the executor - tests = dict([test.export(format_='execute') for test in self.tests()]) + tests = dict([ + test.export(format_='execute', environment=self.plan.environment) + for test in self.tests()]) self.write('tests.yaml', tmt.utils.dictionary_to_yaml(tests)) def wake(self): diff --git a/tmt/utils.py b/tmt/utils.py index 8d224760f4..cb2614a77b 100644 --- a/tmt/utils.py +++ b/tmt/utils.py @@ -438,6 +438,11 @@ def format( # Otherwise just place each item on a new line else: output += ('\n' + indent_string).join(value) + # Dictionary + elif isinstance(value, dict): + # Place each key value pair on a separate line + output += ('\n' + indent_string).join( + f'{item[0]}: {item[1]}' for item in value.items()) # Text elif isinstance(value, str): # In 'auto' mode enable wrapping when long lines present