Current Dir: /snap/canonical-livepatch/418/resources/tests/
[FILE] 99-livepatch-kernel-upgrade-required.bats [ edit | delete | rename ]
[FILE] livepatch-kernel-installed.bats [ edit | delete | rename ]
[FILE] reboot-required-check.bats [ edit | delete | rename ]
Viewing: /snap/canonical-livepatch/418/resources/tests/99-livepatch-kernel-upgrade-required.bats
#!/usr/bin/env bats
#
# Tests for resources/99-livepatch-kernel-upgrade-required.
#
# Each test copies the script to a temp file and rewrites just
# those variable assignments with sed before executing the copy.
setup() {
ORIG_SCRIPT="${BATS_TEST_DIRNAME}/../99-livepatch-kernel-upgrade-required"
WORKDIR="$(mktemp -d)"
SCRIPT="${WORKDIR}/99-livepatch-kernel-upgrade-required"
LIVEPATCH_STUB="${WORKDIR}/canonical-livepatch"
REBOOT_REQUIRED="${WORKDIR}/reboot-required"
sed \
-e "s#^LIVEPATCH=.*#LIVEPATCH=\"${LIVEPATCH_STUB}\"#" \
-e "s#^REBOOT_REQUIRED=.*#REBOOT_REQUIRED=\"${REBOOT_REQUIRED}\"#" \
-e "s#^TIMEOUT_SECONDS=.*#TIMEOUT_SECONDS=1#" \
"$ORIG_SCRIPT" > "$SCRIPT"
chmod +x "$SCRIPT"
}
teardown() {
rm -rf "$WORKDIR"
}
livepatch_aware_policy='{"reboot-notification-policy":"livepatch-aware"}'
livepatch_agnostic_policy='{"reboot-notification-policy":"livepatch-agnostic"}'
@test "no output when livepatch binary is missing/non-executable" {
run "$SCRIPT"
[ "$status" -eq 0 ]
[ -z "$output" ]
}
@test "exit 0 prints the message regardless of policy" {
cat > "$LIVEPATCH_STUB" <<'EOF'
#!/bin/sh
case "$1" in
kernel-upgrade-required)
printf 'reboot message X'
exit 0
;;
esac
EOF
chmod +x "$LIVEPATCH_STUB"
run "$SCRIPT"
[ "$status" -eq 0 ]
[[ "$output" == *"reboot message X"* ]]
}
@test "exit 1, no reboot-required file, aware policy prints the message" {
cat > "$LIVEPATCH_STUB" <<EOF
#!/bin/sh
case "\$1" in
kernel-upgrade-required)
printf 'covered message Y'
exit 1
;;
config)
printf '%s' '$livepatch_aware_policy'
exit 0
;;
esac
EOF
chmod +x "$LIVEPATCH_STUB"
run "$SCRIPT"
[ "$status" -eq 0 ]
[[ "$output" == *"covered message Y"* ]]
}
@test "exit 1, no reboot-required file, agnostic policy prints nothing" {
cat > "$LIVEPATCH_STUB" <<EOF
#!/bin/sh
case "\$1" in
kernel-upgrade-required)
printf 'covered message Y'
exit 1
;;
config)
printf '%s' '$livepatch_agnostic_policy'
exit 0
;;
esac
EOF
chmod +x "$LIVEPATCH_STUB"
run "$SCRIPT"
[ "$status" -eq 0 ]
[ -z "$output" ]
}
@test "exit 1, reboot-required present suppresses message under aware policy" {
touch "$REBOOT_REQUIRED"
cat > "$LIVEPATCH_STUB" <<EOF
#!/bin/sh
case "\$1" in
kernel-upgrade-required)
printf 'covered message Y'
exit 1
;;
config)
printf '%s' '$livepatch_aware_policy'
exit 0
;;
esac
EOF
chmod +x "$LIVEPATCH_STUB"
run "$SCRIPT"
[ "$status" -eq 0 ]
[ -z "$output" ]
}
@test "exit 1, reboot-required present suppresses message under agnostic policy" {
touch "$REBOOT_REQUIRED"
cat > "$LIVEPATCH_STUB" <<EOF
#!/bin/sh
case "\$1" in
kernel-upgrade-required)
printf 'covered message Y'
exit 1
;;
config)
printf '%s' '$livepatch_agnostic_policy'
exit 0
;;
esac
EOF
chmod +x "$LIVEPATCH_STUB"
run "$SCRIPT"
[ "$status" -eq 0 ]
[ -z "$output" ]
}
@test "exit 2 with message, no marker, aware policy prints the message" {
cat > "$LIVEPATCH_STUB" <<EOF
#!/bin/sh
case "\$1" in
kernel-upgrade-required)
printf 'covered message Z'
exit 2
;;
config)
printf '%s' '$livepatch_aware_policy'
exit 0
;;
esac
EOF
chmod +x "$LIVEPATCH_STUB"
run "$SCRIPT"
[ "$status" -eq 0 ]
[[ "$output" == *"covered message Z"* ]]
}
@test "exit 2 with message, agnostic policy prints nothing" {
cat > "$LIVEPATCH_STUB" <<EOF
#!/bin/sh
case "\$1" in
kernel-upgrade-required)
printf 'covered message Z'
exit 2
;;
config)
printf '%s' '$livepatch_agnostic_policy'
exit 0
;;
esac
EOF
chmod +x "$LIVEPATCH_STUB"
run "$SCRIPT"
[ "$status" -eq 0 ]
[ -z "$output" ]
}
@test "exit 2 with message, marker present suppresses message" {
touch "$REBOOT_REQUIRED"
cat > "$LIVEPATCH_STUB" <<EOF
#!/bin/sh
case "\$1" in
kernel-upgrade-required)
printf 'covered message Z'
exit 2
;;
config)
printf '%s' '$livepatch_aware_policy'
exit 0
;;
esac
EOF
chmod +x "$LIVEPATCH_STUB"
run "$SCRIPT"
[ "$status" -eq 0 ]
[ -z "$output" ]
}
@test "exit 2 with empty output prints nothing" {
cat > "$LIVEPATCH_STUB" <<'EOF'
#!/bin/sh
case "$1" in
kernel-upgrade-required)
exit 2
;;
esac
EOF
chmod +x "$LIVEPATCH_STUB"
run "$SCRIPT"
[ "$status" -eq 0 ]
[ -z "$output" ]
}
@test "exit 1 with empty output prints nothing" {
cat > "$LIVEPATCH_STUB" <<'EOF'
#!/bin/sh
case "$1" in
kernel-upgrade-required)
exit 1
;;
config)
printf '%s' '$livepatch_aware_policy'
exit 0
;;
esac
EOF
chmod +x "$LIVEPATCH_STUB"
run "$SCRIPT"
[ "$status" -eq 0 ]
[ -z "$output" ]
}
@test "exit 3 produces no output" {
cat > "$LIVEPATCH_STUB" <<'EOF'
#!/bin/sh
case "$1" in
kernel-upgrade-required)
exit 3
;;
esac
EOF
chmod +x "$LIVEPATCH_STUB"
run "$SCRIPT"
[ "$status" -eq 0 ]
[ -z "$output" ]
}
@test "command timeout produces no output and does not hang" {
cat > "$LIVEPATCH_STUB" <<'EOF'
#!/bin/sh
case "$1" in
kernel-upgrade-required)
sleep 5
;;
esac
EOF
chmod +x "$LIVEPATCH_STUB"
start=$(date +%s)
run "$SCRIPT"
elapsed=$(( $(date +%s) - start ))
[ "$status" -eq 0 ]
[ -z "$output" ]
[ "$elapsed" -lt 4 ]
}