|
| 1 | +load "../helpers/load" |
| 2 | + |
| 3 | +NAME=bats |
| 4 | + |
| 5 | +local_setup_file() { |
| 6 | + unset LIMA_SHELLENV_ALLOW |
| 7 | + unset LIMA_SHELLENV_BLOCK |
| 8 | + |
| 9 | + if [[ -n "${LIMA_BATS_REUSE_INSTANCE:-}" ]]; then |
| 10 | + run limactl list --format '{{.Status}}' "$NAME" |
| 11 | + [[ $status == 0 ]] && [[ $output == "Running" ]] && return |
| 12 | + fi |
| 13 | + limactl unprotect "$NAME" || : |
| 14 | + limactl delete --force "$NAME" || : |
| 15 | + # Make sure that the host agent doesn't inherit file handles 3 or 4. |
| 16 | + # Otherwise bats will not finish until the host agent exits. |
| 17 | + limactl start --yes --name "$NAME" template://default 3>&- 4>&- |
| 18 | +} |
| 19 | + |
| 20 | +local_teardown_file() { |
| 21 | + if [[ -z "${LIMA_BATS_REUSE_INSTANCE:-}" ]]; then |
| 22 | + limactl delete --force "$NAME" |
| 23 | + fi |
| 24 | +} |
| 25 | + |
| 26 | +local_setup() { |
| 27 | + # make sure changes from previous tests are removed |
| 28 | + limactl shell "$NAME" sh -c '[ ! -f ~/.bash_profile ] || sed -i -E "/^export (FOO|BAR|SSH_)/d" ~/.bash_profile' |
| 29 | +} |
| 30 | + |
| 31 | +@test 'there are no FOO*, BAR*, or SSH_FOO* variables defined in the VM' { |
| 32 | + # just to confirm because the other tests depend on these being unused |
| 33 | + run -0 limactl shell "$NAME" printenv |
| 34 | + refute_line --regexp '^FOO' |
| 35 | + refute_line --regexp '^BAR' |
| 36 | + refute_line --regexp '^SSH_FOO' |
| 37 | +} |
| 38 | + |
| 39 | +@test 'environment is not preserved by default' { |
| 40 | + export FOO=foo |
| 41 | + run -0 limactl shell "$NAME" printenv |
| 42 | + refute_line --regexp '^FOO=' |
| 43 | +} |
| 44 | + |
| 45 | +@test 'environment is preserved with --preserve-env' { |
| 46 | + export FOO=foo |
| 47 | + run -0 limactl shell --preserve-env "$NAME" printenv |
| 48 | + assert_line FOO=foo |
| 49 | +} |
| 50 | + |
| 51 | +@test 'profile settings inside the VM take precedence over preserved variables' { |
| 52 | + limactl shell "$NAME" sh -c 'echo "export FOO=bar" >>~/.bash_profile' |
| 53 | + export FOO=foo |
| 54 | + run -0 limactl shell --preserve-env "$NAME" printenv |
| 55 | + assert_line FOO=bar |
| 56 | +} |
| 57 | + |
| 58 | +@test 'builtin block list is used when LIMA_SHELLENV_BLOCK is not set' { |
| 59 | + # default block list includes SSH_* |
| 60 | + export SSH_FOO=ssh_foo |
| 61 | + run -0 limactl shell --preserve-env "$NAME" printenv |
| 62 | + refute_line --regexp '^SSH_FOO=' |
| 63 | +} |
| 64 | + |
| 65 | +@test 'custom block list replaces builtin block list' { |
| 66 | + export LIMA_SHELLENV_BLOCK=FOO |
| 67 | + export FOO=foo |
| 68 | + export SSH_FOO=foo |
| 69 | + run -0 limactl shell --preserve-env "$NAME" printenv |
| 70 | + refute_line --regexp '^FOO=' |
| 71 | + assert_line SSH_FOO=foo |
| 72 | +} |
| 73 | + |
| 74 | +@test 'custom block list starting with + appends to builtin block list' { |
| 75 | + export LIMA_SHELLENV_BLOCK=+FOO |
| 76 | + export FOO=foo |
| 77 | + export SSH_FOO=foo |
| 78 | + run -0 limactl shell --preserve-env "$NAME" printenv |
| 79 | + refute_line --regexp '^FOO=' |
| 80 | + refute_line --regexp '^SSH_FOO=' |
| 81 | +} |
| 82 | + |
| 83 | +@test 'block list entries can use * wildcard at the end' { |
| 84 | + export LIMA_SHELLENV_BLOCK="FOO*" |
| 85 | + export FOO=foo |
| 86 | + export FOOBAR=foobar |
| 87 | + export BAR=bar |
| 88 | + run -0 limactl shell --preserve-env "$NAME" printenv |
| 89 | + refute_line --regexp '^FOO' |
| 90 | + assert_line BAR=bar |
| 91 | +} |
| 92 | + |
| 93 | +@test 'wildcard does only work at the end of the pattern' { |
| 94 | + export LIMA_SHELLENV_BLOCK="*FOO" |
| 95 | + export FOO=foo |
| 96 | + export BARFOO=barfoo |
| 97 | + run -0 limactl shell --preserve-env "$NAME" printenv |
| 98 | + assert_line FOO=foo |
| 99 | + assert_line BARFOO=barfoo |
| 100 | +} |
| 101 | + |
| 102 | +@test 'block list can use a , separated list with whitespace ignored' { |
| 103 | + export LIMA_SHELLENV_BLOCK="FOO*, , BAR" |
| 104 | + export FOO=foo |
| 105 | + export FOOBAR=foobar |
| 106 | + export BAR=bar |
| 107 | + export BARBAZ=barbaz |
| 108 | + run -0 limactl shell --preserve-env "$NAME" printenv |
| 109 | + refute_line --regexp '^FOO' |
| 110 | + refute_line --regexp '^BAR=' |
| 111 | + assert_line BARBAZ=barbaz |
| 112 | +} |
| 113 | + |
| 114 | +@test 'allow list overrides block list but blocks everything else' { |
| 115 | + export LIMA_SHELLENV_ALLOW=SSH_FOO |
| 116 | + export SSH_FOO=ssh_foo |
| 117 | + export SSH_BAR=ssh_bar |
| 118 | + export BAR=bar |
| 119 | + run -0 limactl shell --preserve-env "$NAME" printenv |
| 120 | + assert_line SSH_FOO=ssh_foo |
| 121 | + refute_line --regexp '^SSH_BAR=' |
| 122 | + refute_line --regexp '^BAR=' |
| 123 | +} |
| 124 | + |
| 125 | +@test 'allow list can use a , separated list with whitespace ignored' { |
| 126 | + export LIMA_SHELLENV_ALLOW="FOO*, , BAR" |
| 127 | + export FOO=foo |
| 128 | + export FOOBAR=foobar |
| 129 | + export BAR=bar |
| 130 | + export BARBAZ=barbaz |
| 131 | + run -0 limactl shell --preserve-env "$NAME" printenv |
| 132 | + assert_line FOO=foo |
| 133 | + assert_line FOOBAR=foobar |
| 134 | + assert_line BAR=bar |
| 135 | + refute_line --regexp '^BARBAZ=' |
| 136 | +} |
| 137 | + |
| 138 | +@test 'setting both allow list and block list generates a warning' { |
| 139 | + export LIMA_SHELLENV_ALLOW=FOO |
| 140 | + export LIMA_SHELLENV_BLOCK=BAR |
| 141 | + export FOO=foo |
| 142 | + run -0 --separate-stderr limactl shell --preserve-env "$NAME" printenv FOO |
| 143 | + assert_output foo |
| 144 | + assert_stderr --regexp 'level=warning msg="Both LIMA_SHELLENV_BLOCK and LIMA_SHELLENV_ALLOW are set' |
| 145 | +} |
| 146 | + |
| 147 | +@test 'limactl info includes the default block list' { |
| 148 | + run -0 limactl info |
| 149 | + run -0 limactl yq '.shellEnvBlock[]' <<<"$output" |
| 150 | + assert_line PATH |
| 151 | + assert_line "SSH_*" |
| 152 | + assert_line USER |
| 153 | +} |
0 commit comments