Skip to content

Commit

Permalink
Keep patched sources directly in TMT_SOURCE_DIR (#2999)
Browse files Browse the repository at this point in the history
Moves content out *-build subdirectory which is now produced by rpm
4.20, so older `cd $TMT_SOURCE_DIR/*/tests` use case continues to work.

Fix: #2987
  • Loading branch information
lukaszachy authored Jun 12, 2024
1 parent 6eade22 commit 3f19bef
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 25 deletions.
3 changes: 2 additions & 1 deletion spec/plans/discover.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ description: |

Patches are applied by ``rpm-build -bp`` command which
runs in ``prepare`` step on the provisioned guest, with
order ``60``.
order ``60``. All created files and directories by this command
are directly in ``TMT_SOURCE_DIR``.

.. versionadded:: 1.32

Expand Down
2 changes: 2 additions & 0 deletions tests/discover/data/demo.spec
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Some tests are being added by patches, lets discover them correctly
%prep
%autosetup -n package-src

%build

%install

%changelog
Expand Down
50 changes: 29 additions & 21 deletions tests/discover/distgit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -454,19 +454,27 @@ EOF

### discover -h shell ###

rlPhaseStartTest "Shell always merges the plan's git"
rlRun "tmp=\$(mktemp -d)" 0 "Create tmp directory"
rlRun 'pushd $tmp'
for build_phase in 'no' 'has'; do
rlPhaseStartTest "Shell always merges the plan's git ($build_phase %build phase in spec)"
rlRun "tmp=\$(mktemp -d)" 0 "Create tmp directory"
rlRun 'pushd $tmp'

rlRun "git init"
echo no-tmt-2.tgz > $MOCK_SOURCES_FILENAME
sed -e '/^BuildArch:/aSource0: no-tmt-2.tgz' $TEST_DIR/data/demo.spec > demo.spec
sed -e 's/package-src/no-tmt-2/' -i demo.spec
rlRun "git init"
echo no-tmt-2.tgz > $MOCK_SOURCES_FILENAME
sed -e '/^BuildArch:/aSource0: no-tmt-2.tgz' $TEST_DIR/data/demo.spec > demo.spec
sed -e 's/package-src/no-tmt-2/' -i demo.spec

rlRun "tmt init"
# TODO try again with cd \$TMT_SOURCE_DIR/no-tmt-*
# Fails after the rename
cat <<EOF > plans.fmf
if [ "$build_phase" == "no" ]; then
sed -e '/%build/d' -i demo.spec
rlAssertNotGrep '%build' demo.spec
else
rlAssertGrep '%build' demo.spec
fi

rlRun "tmt init"
# TODO try again with cd \$TMT_SOURCE_DIR/no-tmt-*
# Fails after the rename
cat <<EOF > plans.fmf
discover:
how: shell
tests:
Expand All @@ -488,19 +496,19 @@ execute:
how: tmt
EOF

WORKDIR=/var/tmp/tmt/XXX
WORKDIR_SOURCE=$WORKDIR/plans/discover/default-0/source

rlRun -s "tmt run --keep --id $WORKDIR --scratch -vvv"
WORKDIR=/var/tmp/tmt/XXX
WORKDIR_SOURCE=$WORKDIR/plans/discover/default-0/source

# Source dir has everything available
rlAssertExists $WORKDIR_SOURCE/no-tmt-2/all_in_one
rlAssertExists $WORKDIR_SOURCE/no-tmt-2.tgz
rlRun -s "tmt run --keep --id $WORKDIR --scratch -vvv"

rlRun "popd"
rlRun "rm -rf $tmp"
rlPhaseEnd
# Source dir has everything available
rlAssertExists $WORKDIR_SOURCE/no-tmt-2/all_in_one
rlAssertExists $WORKDIR_SOURCE/no-tmt-2.tgz

rlRun "popd"
rlRun "rm -rf $tmp"
rlPhaseEnd
done
rlPhaseStartTest "shell with download-only"
rlRun "tmp=\$(mktemp -d)" 0 "Create tmp directory"
rlRun 'pushd $tmp'
Expand Down
32 changes: 29 additions & 3 deletions tmt/steps/prepare/distgit.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from tmt.package_managers import Package
from tmt.steps.prepare import PreparePlugin, _RawPrepareStepData
from tmt.steps.provision import Guest
from tmt.utils import Command, Path, field, uniq
from tmt.utils import Command, Path, ShellScript, field, uniq

if TYPE_CHECKING:
import tmt.base
Expand Down Expand Up @@ -148,8 +148,9 @@ class PrepareDistGit(tmt.steps.prepare.PreparePlugin[DistGitData]):
Step is responsible:
1. Install required packages for the rpmbuild itself
2. Detect and install build requires
3. Patch sources
3. Call function of discover plugin to discover tests from patched sources
3. Patch sources (rpmbuild -bp)
4. Move patched sources from buildroot into TMT_SOURCE_DIR
5. Call function of discover plugin to discover tests from TMT_SOURCE_DIR
"""

_data_class = DistGitData
Expand Down Expand Up @@ -246,6 +247,31 @@ def go(
except tmt.utils.RunError as error:
raise tmt.utils.PrepareError("Unable to 'rpmbuild -bp'.", causes=[error])

# Workaround around new rpm behavior, https://github.com/teemtee/tmt/issues/2987
# No hardcoded name, should keep working in the future
cmd = Command(
"rpmbuild",
"-bc",
"--short-circuit",
"--nodeps",
"--define",
'__spec_build_pre echo tmt-get-builddir=%{_builddir}; exit 0',
spec_name,
*dir_defines)
outcome = guest.execute(command=cmd, cwd=source_dir).stdout or ''
match = re.search(r'tmt-get-builddir=(.+)', outcome)
builddir = Path(match.group(1)) if match else None

# But if the %build is missing in spec (e.g. in our test) the previous output was empty
if builddir is None:
guest.execute(command=ShellScript(
"shopt -s dotglob; if test -e */SPECPARTS; then mv ./*-build/* .; else true; fi"),
cwd=source_dir)
elif builddir.resolve() != source_dir.resolve():
guest.execute(command=ShellScript(f"shopt -s dotglob; mv {builddir}/* {source_dir}"))
else:
self.debug("Builddir matches source_dir, no need to copy anything.")

# Make sure to pull back sources ...
# FIXME -- Do we need to? Can be lot of data...
guest.pull(source_dir)
Expand Down

0 comments on commit 3f19bef

Please sign in to comment.