common/config.sh currently tries to find the top directory by looking
for .git. There are also many places under tests/ where we use
hard-coded ../../.. to find things like ${TOP_DIR}/lib.
We don't actually ship the tests with 'make dist'. So we will
be exporting tests/ as a separate tarball. In particular, I want
to then import this in the debian package. However, there it will
be under shadow.git/debian/tests, not shadow.git/tests.
To support this, accept the environment variable BUILD_BASE_DIR,
which should point to shadow.git.
An alternative would be to move the tests to their own git
tree. However, keeping tests in separate git tree tends to
lead to repos getting out of sync. And we'd still need to accept
something like BUILD_BASE_DIR.
Note there are a lot of tests under run-all, which I'm not converting
as they currently are not being run in CI, so I'm more likely to
break something.
Changelog:
2024 05 26: Incorporate feedback from alejandro-colomar
Link: <https://salsa.debian.org/debian/shadow/-/merge_requests/21>
Link: <https://salsa.debian.org/debian/shadow/-/merge_requests/22>
Cc: Chris Hofstaedtler <zeha@debian.org>
Signed-off-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
135 lines
2.9 KiB
Bash
135 lines
2.9 KiB
Bash
# Generic functions to save, change, and restore configuration files
|
|
|
|
set -e
|
|
|
|
if [ -n "${BUILD_BASE_DIR}" ]; then
|
|
build_path="${BUILD_BASE_DIR}"
|
|
else
|
|
build_path=$(git rev-parse --show-toplevel)
|
|
fi
|
|
if [ -z "${build_path}" ]; then
|
|
echo "Failed to find build base path"
|
|
exit 1
|
|
fi
|
|
export build_path
|
|
|
|
# Save the configuration files in tmp.
|
|
save_config ()
|
|
{
|
|
[ ! -d tmp ] && mkdir tmp
|
|
find config -depth -type f -print | sed -e 's/config\///' |
|
|
while read file
|
|
do
|
|
mkdir -p "tmp/$(dirname "$file")"
|
|
[ -f "/$file" ] && cp -dp "/$file" "tmp/$file" || true
|
|
done
|
|
# remove some things which can mess up PATH for some of our tests
|
|
# on github runners
|
|
mkdir -p "tmp/root"
|
|
cp -dp /root/.bashrc tmp/root/.bashrc
|
|
cp -dp /root/.profile tmp/root/.profile
|
|
sed -i '/pipx/d' /root/.bashrc /root/.profile
|
|
sed -i '/etc\/skel\/.cargo\/env/d' /root/.bashrc /root/.profile
|
|
}
|
|
|
|
# Copy the config files from config to the system
|
|
change_config ()
|
|
{
|
|
find config -depth -type f -print | sed -e 's/config\///' |
|
|
while read file
|
|
do
|
|
cp -f "config/$file" "/$file"
|
|
done
|
|
}
|
|
|
|
# Restored the config files in the system.
|
|
# The config files must be saved before with save_config ().
|
|
restore_config ()
|
|
{
|
|
find config -depth -type f -print | sed -e 's/config\///' |
|
|
while read file
|
|
do
|
|
if [ -f "tmp/$file" ]; then
|
|
cp -dp "tmp/$file" "/$file"
|
|
rm "tmp/$file"
|
|
else
|
|
rm -f "/$file"
|
|
fi
|
|
d="$(dirname "tmp/$file")"
|
|
while [ -n "$d" ] && [ "$d" != "." ]
|
|
do
|
|
rmdir "$d" 2>/dev/null || true
|
|
d="$(dirname "$d")"
|
|
done
|
|
done
|
|
|
|
rmdir tmp 2>/dev/null || true
|
|
}
|
|
|
|
prepare_chroot ()
|
|
{
|
|
mkdir tmp/root
|
|
cp -rfdp config_chroot/* tmp/root/
|
|
|
|
lists=/root/tests/common/config_chroot.list
|
|
[ -f config_chroot.list ] && lists="$lists config_chroot.list"
|
|
cat $lists | grep -v "#" | while read f
|
|
do
|
|
# Create parent directory if needed
|
|
d=$(dirname tmp/root/$f)
|
|
[ -d $d ] || mkdir -p $d
|
|
# Create hard link
|
|
ln $f tmp/root/$f
|
|
done
|
|
|
|
# Copy existing gcda
|
|
mkdir -p tmp/root$build_path/lib
|
|
mkdir -p tmp/root$build_path/src
|
|
find "$build_path" -name "*.gcda" | while read f
|
|
do
|
|
ln $f tmp/root/$f
|
|
done
|
|
}
|
|
|
|
clean_chroot ()
|
|
{
|
|
# Remove copied files
|
|
lists=/root/tests/common/config_chroot.list
|
|
[ -f config_chroot.list ] && lists="$lists config_chroot.list"
|
|
cat $lists | grep -v "#" | while read f
|
|
do
|
|
rm -f tmp/root/$f
|
|
# Remove parent directory if empty
|
|
d=$(dirname tmp/root/$f)
|
|
rmdir -p --ignore-fail-on-non-empty $d
|
|
done
|
|
|
|
find "$build_path" -name "*.gcda" | while read f
|
|
do
|
|
rm -f tmp/root/$f
|
|
done
|
|
find tmp/root -name "*.gcda" | while read f
|
|
do
|
|
g=${f#tmp/root}
|
|
mv "$f" "$g"
|
|
done
|
|
rmdir tmp/root$build_path/lib
|
|
rmdir tmp/root$build_path/src
|
|
rmdir tmp/root$build_path
|
|
rmdir tmp/root/root/build
|
|
rmdir tmp/root/root
|
|
|
|
find config_chroot -type f | while read f
|
|
do
|
|
f=${f#config_chroot/}
|
|
rm -f tmp/root/$f
|
|
done
|
|
|
|
find config_chroot -depth -type d | while read d
|
|
do
|
|
d=${d#config_chroot}
|
|
[ -d "tmp/root$d" ] && rmdir tmp/root$d
|
|
done
|
|
}
|
|
|