| 1 | # SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | load "../helpers/load" |
| 5 | CUSTOM_INSTANCE="test-passwordlessSudo" |
| 6 | local_setup_file() { |
| 7 | limactl delete -f "${CUSTOM_INSTANCE}" 2>/dev/null || true |
| 8 | limactl start --name="${CUSTOM_INSTANCE}" --tty=false \ |
| 9 | --set '.user.passwordlessSudo = false' \ |
| 10 | --plain \ |
| 11 | template:default |
| 12 | } |
| 13 | |
| 14 | local_teardown_file() { |
| 15 | limactl delete -f "${CUSTOM_INSTANCE}" |
| 16 | } |
| 17 | |
| 18 | @test "passwordlessSudo: false - sudo without password fails" { |
| 19 | run -1 limactl shell "${CUSTOM_INSTANCE}" -- sudo -n true |
| 20 | } |
| 21 | |
| 22 | @test "passwordlessSudo: false - password file exists with correct permissions" { |
| 23 | limactl shell "${CUSTOM_INSTANCE}" -- bash -c 'stat ~/password' |
| 24 | run -0 limactl shell "${CUSTOM_INSTANCE}" -- bash -c 'stat -c "%a" ~/password' |
| 25 | assert_output "600" |
| 26 | } |
| 27 | |
| 28 | @test "passwordlessSudo: false - generated password authenticates sudo" { |
| 29 | limactl shell "${CUSTOM_INSTANCE}" -- bash -c 'echo "$(cat ~/password)" | sudo -S true' |
| 30 | } |
| 31 | |
| 32 | @test "passwordlessSudo: false - password file is not regenerated on reboot" { |
| 33 | original=$(limactl shell "${CUSTOM_INSTANCE}" -- bash -c 'cat ~/password') |
| 34 | |
| 35 | limactl stop "${CUSTOM_INSTANCE}" |
| 36 | limactl start --tty=false "${CUSTOM_INSTANCE}" |
| 37 | |
| 38 | run -0 limactl shell "${CUSTOM_INSTANCE}" -- bash -c 'cat ~/password' |
| 39 | assert_output "${original}" |
| 40 | } |
| 41 | |
| 42 | @test "passwordlessSudo: false - deleted password file is not regenerated" { |
| 43 | limactl shell "${CUSTOM_INSTANCE}" -- bash -c 'rm -f ~/password' |
| 44 | limactl stop "${CUSTOM_INSTANCE}" |
| 45 | limactl start --tty=false "${CUSTOM_INSTANCE}" |
| 46 | run -1 limactl shell "${CUSTOM_INSTANCE}" -- bash -c 'test -f ~/password' |
| 47 | } |