File: test-settings.sh

package info (click to toggle)
guestfs-tools 1.48.2-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 60,976 kB
  • sloc: ansic: 14,910; ml: 14,596; sh: 7,403; makefile: 3,639; perl: 1,496; xml: 1,472; lex: 135; yacc: 128; python: 80
file content (168 lines) | stat: -rwxr-xr-x 5,129 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash -
# Test various virt-customize settings.
# Copyright (C) 2016 Red Hat Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# This slow test checks that settings such as hostname, timezone work.
#
# NB. 'test-settings.sh' runs the tests, but the various tests are
# run via the 'test-settings-GUESTNAME.sh' wrappers.

set -e
set -x

$TEST_FUNCTIONS
slow_test
skip_if_skipped "$script"

guestname="$1"
if [ -z "$guestname" ]; then
    echo "$script: guestname parameter not set, don't run this test directly."
    exit 1
fi

# Undefine this which is set by ./run script, since we want to use the
# public virt-builder templates.
unset VIRT_BUILDER_DIRS

disk="settings-$guestname.img"
rm -f "$disk" "$disk.firstboot.sh" "$disk.firstboot.out"

# If the guest doesn't exist in virt-builder, skip.  This is because
# we test some RHEL guests which most users won't have access to.
skip_unless_virt_builder_guest "$guestname"

# We can only run the tests on x86_64.
skip_unless_arch x86_64

# Check qemu is installed.
qemu=qemu-system-x86_64
skip_unless $qemu -help

# Some guests need special virt-builder parameters.
# See virt-builder --notes "$guestname"
declare -a extra
case "$guestname" in
    debian-6|debian-7)
        extra[${#extra[*]}]='--edit'
        extra[${#extra[*]}]='/etc/inittab:
                                s,^#([1-9].*respawn.*/sbin/getty.*),$1,'
        ;;
    fedora*|rhel*|centos*)
        extra[${#extra[*]}]='--selinux-relabel'
        ;;
    *)
        ;;
esac

# Create a firstboot script.  Broadly the same across all distros, but
# could be different in future if we support non-Linux (XXX).
fb="$disk.firstboot.sh"
case "$guestname" in
    centos*|debian*|fedora*|rhel*|ubuntu*)
        echo '#!/bin/sh'                                > "$fb"
        echo 'exec > /firstboot.out 2> /firstboot.err' >> "$fb"

        # Only some guests can read the short hostname properly.
        case "$guestname" in
            debian*|fedora*|ubuntu*)
                echo 'echo -n HOSTNAME='               >> "$fb"
                echo 'hostname -s'                     >> "$fb"
                ;;
        esac

        # Only some guests can read the FQDN properly.
        case "$guestname" in
            ubuntu-10.04) ;;
            fedora*|debian*|ubuntu*)
                echo 'echo -n FQDN='                   >> "$fb"
                echo 'hostname -f'                     >> "$fb"
                ;;
        esac

        # RHEL 4.9 did not have "%:z".
        case "$guestname" in
            rhel-4.9) ;;
            *)
                echo 'echo -n TIMEZONE='               >> "$fb"
                echo 'date +%:z'                       >> "$fb"
        esac

        echo 'sync'                                    >> "$fb"
        echo 'poweroff'                                >> "$fb"
        ;;
    *)
        echo "$script: don't know how to write a firstboot script for $guestname"
        exit 1
        ;;
esac

# Build a guest (using virt-builder) with some virt-customize setting
# parameters.
#
# Note we choose a timezone that doesn't have daylight savings, so
# that the output of the date command should always be the same.
virt-builder "$guestname" \
             --quiet \
             -o "$disk" \
             --hostname test-set.example.com \
             --timezone Japan \
             --firstboot "$fb" \
             "${extra[@]}"

# Boot the guest in qemu and wait for the firstboot scripts to run.
#
# Use IDE because some ancient guests don't support anything else.
$qemu \
    -no-user-config \
    -display none \
    -machine accel=kvm:tcg \
    -m 2048 \
    -boot c \
    -drive file="$disk",format=raw,if=ide \
    -serial stdio ||:

# Get the output of the firstboot script.
fbout="$disk.firstboot.out"
guestfish --ro -a "$disk" -i <<EOF
download /firstboot.out "$fbout"

# For information/debugging only.
echo === /firstboot.out ===
-cat /firstboot.out
echo === /firstboot.err ===
-cat /firstboot.err
echo === /etc/hosts ===
-cat /etc/hosts
echo === /etc/hostname ===
-cat /etc/hostname
echo === /etc/sysconfig/network ===
-cat /etc/sysconfig/network

EOF

# Check the output of the firstboot script.
if grep "^HOSTNAME=" "$fbout"; then
    grep "^HOSTNAME=test-set" "$fbout"
fi
if grep "^FQDN=" "$fbout"; then
    grep "^FQDN=test-set.example.com" "$fbout"
fi
if grep "^TIMEZONE=" "$fbout"; then
    grep "^TIMEZONE=+09:00" "$fbout"
fi

rm "$disk" "$disk.firstboot.sh" "$disk.firstboot.out"