Skip to content

Commit

Permalink
Implement tests.yaml creation in discover
Browse files Browse the repository at this point in the history
Prepared custom format for the simple shell executor.
Environment variables converted to shell-like list.
Updated default discover implementation to 'fmf'.
  • Loading branch information
psss committed Oct 25, 2019
1 parent 250a8fa commit 984f45a
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 8 deletions.
1 change: 1 addition & 0 deletions examples/environment/.fmf/version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
15 changes: 15 additions & 0 deletions examples/environment/main.fmf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/plan:
summary:
Environment variable.
description:
Tests can be provided with a list of environment variables.
execute:
how: shell

/test:
summary: Just a simple test
test: ./test.sh
environment:
x: That's nice!
y: a b c
z: 3
2 changes: 1 addition & 1 deletion spec/steps/main.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ description: |
which implementation should be used. Default implementations
are as follows:

* discover: shell
* discover: fmf
* provision: virtual
* prepare: shell
* execute: shell
Expand Down
34 changes: 32 additions & 2 deletions tmt/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,18 @@ def ls(self, summary=False):
echo(tmt.utils.format('summary', self.summary))

def export(self, format_='yaml', keys=None):
""" Export data into requested format """
""" Export data into requested format (yaml or dict) """
if keys is None:
keys = self._keys
data = dict([(key, getattr(self, key)) for key in keys])
return tmt.utils.dictionary_to_yaml(data)
# Choose proper format
if format_ == 'dict':
return data
elif format_ == 'yaml':
return tmt.utils.dictionary_to_yaml(data)
else:
raise tmt.utils.GeneralError(
f"Invalid test export format '{format_}'.")


class Test(Node):
Expand Down Expand Up @@ -165,6 +172,29 @@ def lint(self):
elif len(self.summary) > 50:
echo(verdict(2, 'summary should not exceed 50 characters'))

def export(self, format_='yaml', keys=None):
"""
Export test data into requested format
In addition to 'yaml' and 'dict' it supports also a special
format 'execute' used by the execute step which returns
(test-name, test-data) tuples.
"""
if format_ != 'execute':
return super(Test, self).export(format_, keys)

# Prepare special format for the executor
name = f'/{self._repository}{self.name}'
data = dict()
data['test'] = self.test
data['path'] = f'/{self._repository}{self.path}'
if self.duration is not None:
data['duration'] = self.duration
if self.environment is not None:
data['environment'] = ' '.join(
tmt.utils.dict_to_shell(self.environment))
return name, data


class Plan(Node):
""" Plan object (L2 Metadata) """
Expand Down
2 changes: 1 addition & 1 deletion tmt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def export(context, names, format_):
""" Export test data into the desired format. """
tmt.Test._context = context
for test in context.obj.tree.tests(names=names):
echo(test.export(format_='yaml'))
echo(test.export(format_=format_))


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
4 changes: 4 additions & 0 deletions tmt/steps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def load(self):
""" Load step data from the workdir """
pass

def save(self):
""" Save step data to the workdir """
pass

def wake(self):
""" Wake up the step (process workdir and command line) """
# Check workdir for possible stored data
Expand Down
20 changes: 17 additions & 3 deletions tmt/steps/discover/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,24 @@
class Discover(tmt.steps.Step):
""" Gather and show information about test cases to be executed """

# Default implementation for discover is fmf
how = 'fmf'

def __init__(self, data, plan):
""" Store supported attributes, check for sanity """
super(Discover, self).__init__(data, plan)
self.steps = []

def load(self):
""" Load step data from the workdir """
pass

def save(self):
""" Save step data to the workdir """
# Create 'tests.yaml' with the list of tests for the executor
tests = dict([test.export(format_='execute') for test in self.tests()])
self.write('tests.yaml', tmt.utils.dictionary_to_yaml(tests))

def wake(self):
""" Wake up the step (process workdir and command line) """
super(Discover, self).wake()
Expand Down Expand Up @@ -42,14 +55,15 @@ def go(self):
super(Discover, self).go()
for step in self.steps:
step.go()
self.save()
self.status('done')

def tests(self):
""" Return a list of all tests """
tests = []
for step in self.steps:
self.tests.extend(step.tests())
return tests
for test in step.tests():
test._repository = step
yield test


class DiscoverPlugin(tmt.steps.Plugin):
Expand Down
2 changes: 1 addition & 1 deletion tmt/steps/discover/fmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def tests(self):
return [
tmt.Test(test) for test in self.tests_tree.prune(
keys=['test'],
filters=self.filter or [],
filters=self.filters or [],
names=tmt.base.Test._opt('names', []))]

def dump(self):
Expand Down
7 changes: 7 additions & 0 deletions tmt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def opt(self, option, default=None):
if self._context is None:
if self.parent is not None:
return self.parent.opt(option, default)
else:
return None
return self._context.params.get(option, default)

def run(self, command, message=None):
Expand Down Expand Up @@ -225,6 +227,11 @@ def dictionary_to_yaml(data):
return output.getvalue()


def dict_to_shell(data):
""" Convert dictionary to list of key=value pairs """
return [f"{key}={shlex.quote(str(value))}" for key, value in data.items()]


def verdict(decision, comment=None, good='pass', bad='fail', problem='warn'):
"""
Return verdict in green or red based on the decision
Expand Down

0 comments on commit 984f45a

Please sign in to comment.