1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3set -eux
4set -o pipefail
5
6export SYSTEMD_PAGER=cat
7
8dd if=/dev/urandom of=/var/tmp/testimage.raw bs=$((1024*1024+7)) count=5
9
10# Test import
11machinectl import-raw /var/tmp/testimage.raw
12machinectl image-status testimage
13test -f /var/lib/machines/testimage.raw
14cmp /var/tmp/testimage.raw /var/lib/machines/testimage.raw
15
16# Test export
17machinectl export-raw testimage /var/tmp/testimage2.raw
18cmp /var/tmp/testimage.raw /var/tmp/testimage2.raw
19rm /var/tmp/testimage2.raw
20
21# Test compressed export (gzip)
22machinectl export-raw testimage /var/tmp/testimage2.raw.gz
23gunzip /var/tmp/testimage2.raw.gz
24cmp /var/tmp/testimage.raw /var/tmp/testimage2.raw
25rm /var/tmp/testimage2.raw
26
27# Test clone
28machinectl clone testimage testimage3
29test -f /var/lib/machines/testimage3.raw
30machinectl image-status testimage3
31test -f /var/lib/machines/testimage.raw
32machinectl image-status testimage
33cmp /var/tmp/testimage.raw /var/lib/machines/testimage.raw
34cmp /var/tmp/testimage.raw /var/lib/machines/testimage3.raw
35
36# Test removal
37machinectl remove testimage
38test ! -f /var/lib/machines/testimage.raw
39machinectl image-status testimage && { echo 'unexpected success'; exit 1; }
40
41# Test export of clone
42machinectl export-raw testimage3 /var/tmp/testimage3.raw
43cmp /var/tmp/testimage.raw /var/tmp/testimage3.raw
44rm /var/tmp/testimage3.raw
45
46# Test rename
47machinectl rename testimage3 testimage4
48test -f /var/lib/machines/testimage4.raw
49machinectl image-status testimage4
50test ! -f /var/lib/machines/testimage3.raw
51machinectl image-status testimage3 && { echo 'unexpected success'; exit 1; }
52cmp /var/tmp/testimage.raw /var/lib/machines/testimage4.raw
53
54# Test export of rename
55machinectl export-raw testimage4 /var/tmp/testimage4.raw
56cmp /var/tmp/testimage.raw /var/tmp/testimage4.raw
57rm /var/tmp/testimage4.raw
58
59# Test removal
60machinectl remove testimage4
61test ! -f /var/lib/machines/testimage4.raw
62machinectl image-status testimage4 && { echo 'unexpected success'; exit 1; }
63
64# → And now, let's test directory trees ← #
65
66# Set up a directory we can import
67mkdir /var/tmp/scratch
68mv /var/tmp/testimage.raw /var/tmp/scratch/
69touch /var/tmp/scratch/anotherfile
70mkdir /var/tmp/scratch/adirectory
71echo "piep" >/var/tmp/scratch/adirectory/athirdfile
72
73# Test import-fs
74machinectl import-fs /var/tmp/scratch/
75test -d /var/lib/machines/scratch
76machinectl image-status scratch
77
78# Test export-tar
79machinectl export-tar scratch /var/tmp/scratch.tar.gz
80test -f /var/tmp/scratch.tar.gz
81mkdir /var/tmp/extract
82(cd /var/tmp/extract ; tar xzf /var/tmp/scratch.tar.gz)
83diff -r /var/tmp/scratch/ /var/tmp/extract/
84rm -rf /var/tmp/extract
85
86# Test import-tar
87machinectl import-tar /var/tmp/scratch.tar.gz scratch2
88test -d /var/lib/machines/scratch2
89machinectl image-status scratch2
90diff -r /var/tmp/scratch/ /var/lib/machines/scratch2
91
92# Test removal
93machinectl remove scratch
94test ! -f /var/lib/machines/scratch
95machinectl image-status scratchi && { echo 'unexpected success'; exit 1; }
96
97# Test clone
98machinectl clone scratch2 scratch3
99test -d /var/lib/machines/scratch2
100machinectl image-status scratch2
101test -d /var/lib/machines/scratch3
102machinectl image-status scratch3
103diff -r /var/tmp/scratch/ /var/lib/machines/scratch3
104
105# Test removal
106machinectl remove scratch2
107test ! -f /var/lib/machines/scratch2
108machinectl image-status scratch2 && { echo 'unexpected success'; exit 1; }
109
110# Test rename
111machinectl rename scratch3 scratch4
112test -d /var/lib/machines/scratch4
113machinectl image-status scratch4
114test ! -f /var/lib/machines/scratch3
115machinectl image-status scratch3 && { echo 'unexpected success'; exit 1; }
116diff -r /var/tmp/scratch/ /var/lib/machines/scratch4
117
118# Test removal
119machinectl remove scratch4
120test ! -f /var/lib/machines/scratch4
121machinectl image-status scratch4 && { echo 'unexpected success'; exit 1; }
122
123# Test import-tar hyphen/stdin pipe behavior
124# shellcheck disable=SC2002
125cat /var/tmp/scratch.tar.gz | machinectl import-tar - scratch5
126test -d /var/lib/machines/scratch5
127machinectl image-status scratch5
128diff -r /var/tmp/scratch/ /var/lib/machines/scratch5
129
130# Test export-tar hyphen/stdout pipe behavior
131mkdir -p /var/tmp/extract
132machinectl export-tar scratch5 - | tar xvf - -C /var/tmp/extract/
133diff -r /var/tmp/scratch/ /var/tmp/extract/
134rm -rf /var/tmp/extract
135
136rm -rf /var/tmp/scratch
137
138# Test removal
139machinectl remove scratch5
140test ! -f /var/lib/machines/scratch5
141machinectl image-status scratch5 && { echo 'unexpected success'; exit 1; }
142
143echo OK >/testok
144
145exit 0
146