1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
3set -eux
4set -o pipefail
5
6# Check if homectl is installed, and if it isn't bail out early instead of failing
7if ! test -x /usr/bin/homectl ; then
8        echo "no homed" >/skipped
9        exit 0
10fi
11
12inspect() {
13    # As updating disk-size-related attributes can take some time on some
14    # filesystems, let's drop these fields before comparing the outputs to
15    # avoid unexpected fails. To see the full outputs of both homectl &
16    # userdbctl (for debugging purposes) drop the fields just before the
17    # comparison.
18    local USERNAME="${1:?}"
19    homectl inspect "$USERNAME" | tee /tmp/a
20    userdbctl user "$USERNAME" | tee /tmp/b
21
22    # diff uses the grep BREs for pattern matching
23    diff -I '^\s*Disk \(Size\|Free\|Floor\|Ceiling\):' /tmp/{a,b}
24    rm /tmp/{a,b}
25
26    homectl inspect --json=pretty "$USERNAME"
27}
28
29wait_for_state() {
30    for ((i=0;i<10;i++)) ; do
31        homectl inspect "$1" | grep -qF "State: $2" && break
32        sleep .5
33    done
34}
35
36systemd-analyze log-level debug
37systemd-analyze log-target console
38systemctl service-log-level systemd-homed debug
39
40# Create a tmpfs to use as backing store for the home dir. That way we can enforce a size limit nicely.
41mkdir -p /home
42mount -t tmpfs tmpfs /home -o size=290M
43
44# we enable --luks-discard= since we run our tests in a tight VM, hence don't
45# needlessly pressure for storage. We also set the cheapest KDF, since we don't
46# want to waste CI CPU cycles on it.
47NEWPASSWORD=xEhErW0ndafV4s homectl create test-user \
48           --disk-size=min \
49           --luks-discard=yes \
50           --image-path=/home/test-user.home \
51           --luks-pbkdf-type=pbkdf2 \
52           --luks-pbkdf-time-cost=1ms
53inspect test-user
54
55PASSWORD=xEhErW0ndafV4s homectl authenticate test-user
56
57PASSWORD=xEhErW0ndafV4s homectl activate test-user
58inspect test-user
59
60PASSWORD=xEhErW0ndafV4s homectl update test-user --real-name="Inline test"
61inspect test-user
62
63homectl deactivate test-user
64inspect test-user
65
66PASSWORD=xEhErW0ndafV4s NEWPASSWORD=yPN4N0fYNKUkOq homectl passwd test-user
67inspect test-user
68
69PASSWORD=yPN4N0fYNKUkOq homectl activate test-user
70inspect test-user
71
72SYSTEMD_LOG_LEVEL=debug PASSWORD=yPN4N0fYNKUkOq NEWPASSWORD=xEhErW0ndafV4s homectl passwd test-user
73inspect test-user
74
75homectl deactivate test-user
76inspect test-user
77
78PASSWORD=xEhErW0ndafV4s homectl activate test-user
79inspect test-user
80
81homectl deactivate test-user
82inspect test-user
83
84PASSWORD=xEhErW0ndafV4s homectl update test-user --real-name="Offline test"
85inspect test-user
86
87PASSWORD=xEhErW0ndafV4s homectl activate test-user
88inspect test-user
89
90homectl deactivate test-user
91inspect test-user
92
93# Do some resize tests, but only if we run on real kernels, as quota inside of containers will fail
94if ! systemd-detect-virt -cq ; then
95    # grow while inactive
96    PASSWORD=xEhErW0ndafV4s homectl resize test-user 300M
97    inspect test-user
98
99    # minimize while inactive
100    PASSWORD=xEhErW0ndafV4s homectl resize test-user min
101    inspect test-user
102
103    PASSWORD=xEhErW0ndafV4s homectl activate test-user
104    inspect test-user
105
106    # grow while active
107    PASSWORD=xEhErW0ndafV4s homectl resize test-user max
108    inspect test-user
109
110    # minimize while active
111    PASSWORD=xEhErW0ndafV4s homectl resize test-user 0
112    inspect test-user
113
114    # grow while active
115    PASSWORD=xEhErW0ndafV4s homectl resize test-user 300M
116    inspect test-user
117
118    # shrink to original size while active
119    PASSWORD=xEhErW0ndafV4s homectl resize test-user 256M
120    inspect test-user
121
122    # minimize again
123    PASSWORD=xEhErW0ndafV4s homectl resize test-user min
124    inspect test-user
125
126    # Increase space, so that we can reasonably rebalance free space between to home dirs
127    mount /home -o remount,size=800M
128
129    # create second user
130    NEWPASSWORD=uuXoo8ei homectl create test-user2 \
131           --disk-size=min \
132           --luks-discard=yes \
133           --image-path=/home/test-user2.home \
134           --luks-pbkdf-type=pbkdf2 \
135           --luks-pbkdf-time-cost=1ms
136    inspect test-user2
137
138    # activate second user
139    PASSWORD=uuXoo8ei homectl activate test-user2
140    inspect test-user2
141
142    # set second user's rebalance weight to 100
143    PASSWORD=uuXoo8ei homectl update test-user2 --rebalance-weight=100
144    inspect test-user2
145
146    # set first user's rebalance weight to quarter of that of the second
147    PASSWORD=xEhErW0ndafV4s homectl update test-user --rebalance-weight=25
148    inspect test-user
149
150    # synchronously rebalance
151    homectl rebalance
152    inspect test-user
153    inspect test-user2
154fi
155
156PASSWORD=xEhErW0ndafV4s homectl with test-user -- test ! -f /home/test-user/xyz
157PASSWORD=xEhErW0ndafV4s homectl with test-user -- test -f /home/test-user/xyz \
158    && { echo 'unexpected success'; exit 1; }
159PASSWORD=xEhErW0ndafV4s homectl with test-user -- touch /home/test-user/xyz
160PASSWORD=xEhErW0ndafV4s homectl with test-user -- test -f /home/test-user/xyz
161PASSWORD=xEhErW0ndafV4s homectl with test-user -- rm /home/test-user/xyz
162PASSWORD=xEhErW0ndafV4s homectl with test-user -- test ! -f /home/test-user/xyz
163PASSWORD=xEhErW0ndafV4s homectl with test-user -- test -f /home/test-user/xyz \
164    && { echo 'unexpected success'; exit 1; }
165
166wait_for_state test-user inactive
167homectl remove test-user
168
169if ! systemd-detect-virt -cq ; then
170    wait_for_state test-user2 active
171    homectl deactivate test-user2
172    wait_for_state test-user2 inactive
173    homectl remove test-user2
174fi
175
176systemd-analyze log-level info
177
178echo OK >/testok
179
180exit 0
181