Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/releases/pending/4427.fmf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
description: |
Beakerlib libraries can now be referenced without a ``url`` or ``path`` key.
In that case the ``name`` is relative to the discovered test's tmt tree.
The ``nick`` key **must** be set in this situation, matching what the
consuming scripts are using in ``rlImport``, or the name of the repo or
equivalent.
25 changes: 23 additions & 2 deletions spec/tests/require.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@ description: |

By default, fetched repositories are stored in the discover
step workdir under the ``libs`` directory. Use optional key
``destination`` to choose a different location. The ``nick``
key can be used to override the default git repository name.
``destination`` to choose a different location.

.. note::
:name: nick-usage

The name of the beakerlib library (the value used with
``rlImport`` is taken as the last directory pointed by
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
``rlImport`` is taken as the last directory pointed by
``rlImport``) is taken as the last directory pointed by

``url`` or ``path``. Use the ``nick`` key if you need
to override this.

For debugging beakerlib libraries it is useful to reference
the development version directly from the local filesystem.
Expand All @@ -49,6 +56,13 @@ description: |
__ https://github.com/beakerlib/
__ https://fmf.readthedocs.io/en/latest/concept.html#identifiers

.. versionadded:: 1.65
As a special handling when using ``type: library``, if both
``url`` and ``path`` is omitted, the current test's tree is
used as the repo of the library. The ``nick`` key **must**
be set in this case (see :ref:`previous note<nick-usage>`
for what values to set for this)

example:
- |
# Require a single package
Expand All @@ -75,6 +89,13 @@ example:
url: https://github.com/beakerlib/openssl
name: /certgen

- |
# Library from the current tmt tree
require:
- type: library
name: /certgen
nick: openssl

- |
# Library from the local filesystem
require:
Expand Down
5 changes: 3 additions & 2 deletions tests/libraries/local/data/main.fmf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/plan:
discover:
how: fmf
test: ^/test
provision:
how: container
execute:
Expand All @@ -10,6 +11,6 @@
test: ./test.sh
framework: beakerlib
require:
- path: PATH
name: /file
- name: /file
type: library
path: PATH
34 changes: 26 additions & 8 deletions tests/libraries/local/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,40 @@ rlJournalStart
rlRun "popd"
rlPhaseEnd

rlPhaseStartSetup "Prepare test"
rlRun "testdir=\$(mktemp -d)" 0 "Create testdir directory"
rlRun "cp -a data $testdir"
rlRun "pushd $testdir/data"
rlRun "sed -i 's|PATH|${libdir}/example|' main.fmf"
rlPhaseStartSetup "Prepare test (local filesystem)"
rlRun "testdir_local=\$(mktemp -d)" 0 "Create testdir directory"
rlRun "cp -a data $testdir_local"
rlRun "sed -i 's|PATH|${libdir}/example|' $testdir_local/data/main.fmf"
rlPhaseEnd

rlPhaseStartTest
rlPhaseStartTest "Test library in local filesystem"
rlRun "pushd $testdir_local/data"
rlRun "set -o pipefail"
rlRun "tmt run -ar discover -vvvddd report -vvv 2>&1 >/dev/null | tee output"
rlAssertGrep "Copy local library.*example" "output"
rlAssertGrep "Create fyle 'fooo'" "output"
rlRun "popd"
rlPhaseEnd

rlPhaseStartCleanup
rlPhaseStartSetup "Prepare test (relative to tmt tree)"
rlRun "testdir_tree=\$(mktemp -d)" 0 "Create testdir directory"
rlRun "cp -a data $testdir_tree"
# Assuming libdir/example does not have a top-level main.fmf or test.sh that would override ours
rlRun "cp -a $libdir/example/* $testdir_tree/data" 0 "Copy library to tmt tree"
rlRun "sed -i 's|path: PATH|nick: example|' $testdir_tree/data/main.fmf" 0 "Use nick instead of path"
rlRun "mv $testdir_tree/data $testdir_tree/example" 0 "Rename tree base to match the library's"
rlPhaseEnd

rlPhaseStartTest "Test library relative to tmt tree"
rlRun "pushd $testdir_tree/example"
rlRun "set -o pipefail"
rlRun "tmt run -ar discover -vvvddd report -vvv 2>&1 >/dev/null | tee output"
rlAssertGrep "Copy local library.*example" "output"
rlAssertGrep "Create fyle 'fooo'" "output"
rlRun "popd"
rlRun "rm -rf $libdir $testdir" 0 "Remove temporary directories"
rlPhaseEnd

rlPhaseStartCleanup
rlRun "rm -rf $libdir $testdir_local $testdir_tree" 0 "Remove temporary directories"
rlPhaseEnd
rlJournalEnd
2 changes: 1 addition & 1 deletion tests/lint/test/data/library-missing-url-path.fmf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
summary: Library requirement missing both url and path
summary: Library requirement missing all of url, path and nick
test: /bin/true
require:
- type: library
Expand Down
30 changes: 27 additions & 3 deletions tmt/libraries/beakerlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ def __init__(
parent: Optional[tmt.utils.Common] = None,
logger: tmt.log.Logger,
) -> None:
from tmt.steps.discover import (
DiscoverPlugin,
DiscoverStepData,
)
from tmt.steps.discover.fmf import DiscoverFmfStepData

super().__init__(parent=parent, logger=logger)

# Default branch is detected from the origin after cloning
Expand Down Expand Up @@ -111,9 +117,27 @@ def __init__(
self.url = identifier.url
self.path = identifier.path
if not self.url and not self.path:
raise tmt.utils.SpecificationError(
"Need 'url' or 'path' to fetch a beakerlib library."
)
# If not explicit url or path is given we take the test's tree as the
# repo. We also require a nick to be specified to figure out the intended
# name of the repo used in `rlImport`.
if not identifier.nick:
raise tmt.utils.SpecificationError(
"Need either 'url' to fetch a remote beakerlib library, "
"or 'path' to use a local filesystem library, "
"or 'nick' to use the discovered tmt tree as the library."
)
assert isinstance(self.parent, DiscoverPlugin) # narrow type
assert isinstance(self.parent.data, DiscoverStepData) # narrow type
fmf_path = "."
# When using `path` with `url`, it behaves differently from local `path`
# TODO: Remove this special handling when DiscoverFmf is more consistent
if (
isinstance(self.parent.data, DiscoverFmfStepData)
and self.parent.data.url
and self.parent.data.path
):
fmf_path = self.parent.data.path
self.path = self.parent.test_dir / fmf_path
# Strip the '.git' suffix from url for known forges
if self.url:
for forge in STRIP_SUFFIX_FORGES:
Expand Down
5 changes: 5 additions & 0 deletions tmt/schemas/common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,15 @@ definitions:
- type

anyOf:
# Remote repo case
- required:
- url
# Local filesystem case
- required:
- path
# Test's tmt tree case
- required:
- nick

# This would have been the easy way, understandable by newer jsonschema
# packages.
Expand Down