2020-03-04 12:35:06 +03:00
#!/usr/bin/env bash
2021-10-17 19:13:06 +03:00
# SPDX-License-Identifier: LGPL-2.1-or-later
2021-04-09 20:39:41 +03:00
set -eux
2018-10-26 19:18:36 +03:00
set -o pipefail
export SYSTEMD_PAGER = cat
dd if = /dev/urandom of = /var/tmp/testimage.raw bs = $(( 1024 * 1024 + 7 )) count = 5
# Test import
machinectl import-raw /var/tmp/testimage.raw
machinectl image-status testimage
test -f /var/lib/machines/testimage.raw
cmp /var/tmp/testimage.raw /var/lib/machines/testimage.raw
# Test export
machinectl export-raw testimage /var/tmp/testimage2.raw
cmp /var/tmp/testimage.raw /var/tmp/testimage2.raw
rm /var/tmp/testimage2.raw
# Test compressed export (gzip)
machinectl export-raw testimage /var/tmp/testimage2.raw.gz
gunzip /var/tmp/testimage2.raw.gz
cmp /var/tmp/testimage.raw /var/tmp/testimage2.raw
rm /var/tmp/testimage2.raw
# Test clone
machinectl clone testimage testimage3
test -f /var/lib/machines/testimage3.raw
machinectl image-status testimage3
test -f /var/lib/machines/testimage.raw
machinectl image-status testimage
cmp /var/tmp/testimage.raw /var/lib/machines/testimage.raw
cmp /var/tmp/testimage.raw /var/lib/machines/testimage3.raw
# Test removal
machinectl remove testimage
2021-04-08 00:24:25 +03:00
test ! -f /var/lib/machines/testimage.raw
2021-04-08 02:27:33 +03:00
machinectl image-status testimage && { echo 'unexpected success' ; exit 1; }
2018-10-26 19:18:36 +03:00
# Test export of clone
machinectl export-raw testimage3 /var/tmp/testimage3.raw
cmp /var/tmp/testimage.raw /var/tmp/testimage3.raw
rm /var/tmp/testimage3.raw
# Test rename
machinectl rename testimage3 testimage4
test -f /var/lib/machines/testimage4.raw
machinectl image-status testimage4
2021-04-08 00:24:25 +03:00
test ! -f /var/lib/machines/testimage3.raw
2021-04-08 02:27:33 +03:00
machinectl image-status testimage3 && { echo 'unexpected success' ; exit 1; }
2018-10-26 19:18:36 +03:00
cmp /var/tmp/testimage.raw /var/lib/machines/testimage4.raw
# Test export of rename
machinectl export-raw testimage4 /var/tmp/testimage4.raw
cmp /var/tmp/testimage.raw /var/tmp/testimage4.raw
rm /var/tmp/testimage4.raw
# Test removal
machinectl remove testimage4
2021-04-08 00:24:25 +03:00
test ! -f /var/lib/machines/testimage4.raw
2021-04-08 02:27:33 +03:00
machinectl image-status testimage4 && { echo 'unexpected success' ; exit 1; }
2018-10-26 19:18:36 +03:00
# → And now, let's test directory trees ← #
# Set up a directory we can import
mkdir /var/tmp/scratch
mv /var/tmp/testimage.raw /var/tmp/scratch/
touch /var/tmp/scratch/anotherfile
mkdir /var/tmp/scratch/adirectory
2021-04-08 01:09:55 +03:00
echo "piep" >/var/tmp/scratch/adirectory/athirdfile
2018-10-26 19:18:36 +03:00
# Test import-fs
machinectl import-fs /var/tmp/scratch/
test -d /var/lib/machines/scratch
machinectl image-status scratch
# Test export-tar
machinectl export-tar scratch /var/tmp/scratch.tar.gz
test -f /var/tmp/scratch.tar.gz
mkdir /var/tmp/extract
( cd /var/tmp/extract ; tar xzf /var/tmp/scratch.tar.gz)
diff -r /var/tmp/scratch/ /var/tmp/extract/
rm -rf /var/tmp/extract
# Test import-tar
machinectl import-tar /var/tmp/scratch.tar.gz scratch2
test -d /var/lib/machines/scratch2
machinectl image-status scratch2
diff -r /var/tmp/scratch/ /var/lib/machines/scratch2
# Test removal
machinectl remove scratch
2021-04-08 00:24:25 +03:00
test ! -f /var/lib/machines/scratch
2021-04-08 02:27:33 +03:00
machinectl image-status scratchi && { echo 'unexpected success' ; exit 1; }
2018-10-26 19:18:36 +03:00
# Test clone
machinectl clone scratch2 scratch3
test -d /var/lib/machines/scratch2
machinectl image-status scratch2
test -d /var/lib/machines/scratch3
machinectl image-status scratch3
diff -r /var/tmp/scratch/ /var/lib/machines/scratch3
# Test removal
machinectl remove scratch2
2021-04-08 00:24:25 +03:00
test ! -f /var/lib/machines/scratch2
2021-04-08 02:27:33 +03:00
machinectl image-status scratch2 && { echo 'unexpected success' ; exit 1; }
2018-10-26 19:18:36 +03:00
# Test rename
machinectl rename scratch3 scratch4
test -d /var/lib/machines/scratch4
machinectl image-status scratch4
2021-04-08 00:24:25 +03:00
test ! -f /var/lib/machines/scratch3
2021-04-08 02:27:33 +03:00
machinectl image-status scratch3 && { echo 'unexpected success' ; exit 1; }
2018-10-26 19:18:36 +03:00
diff -r /var/tmp/scratch/ /var/lib/machines/scratch4
# Test removal
machinectl remove scratch4
2021-04-08 00:24:25 +03:00
test ! -f /var/lib/machines/scratch4
2021-04-08 02:27:33 +03:00
machinectl image-status scratch4 && { echo 'unexpected success' ; exit 1; }
2018-10-26 19:18:36 +03:00
2020-04-21 21:46:53 +03:00
# Test import-tar hyphen/stdin pipe behavior
2021-04-09 20:56:12 +03:00
# shellcheck disable=SC2002
2019-12-17 12:08:04 +03:00
cat /var/tmp/scratch.tar.gz | machinectl import-tar - scratch5
test -d /var/lib/machines/scratch5
machinectl image-status scratch5
diff -r /var/tmp/scratch/ /var/lib/machines/scratch5
2020-04-21 21:46:53 +03:00
# Test export-tar hyphen/stdout pipe behavior
2019-12-17 12:08:04 +03:00
mkdir -p /var/tmp/extract
machinectl export-tar scratch5 - | tar xvf - -C /var/tmp/extract/
diff -r /var/tmp/scratch/ /var/tmp/extract/
rm -rf /var/tmp/extract
2018-10-26 19:18:36 +03:00
rm -rf /var/tmp/scratch
2020-03-21 17:29:43 +03:00
# Test removal
machinectl remove scratch5
2021-04-08 00:24:25 +03:00
test ! -f /var/lib/machines/scratch5
2021-04-08 02:27:33 +03:00
machinectl image-status scratch5 && { echo 'unexpected success' ; exit 1; }
2020-03-21 17:29:43 +03:00
2021-04-08 01:09:55 +03:00
echo OK >/testok
2018-10-26 19:18:36 +03:00
exit 0