Skip to content

Commit c8e6ed1

Browse files
committed
Add foundational BATS tests for limactl yq and limactl list
They are the basis for more complex tests later, so have filenames starting with a digit, so they are processed first. Signed-off-by: Jan Dubois <[email protected]>
1 parent 69538a7 commit c8e6ed1

File tree

7 files changed

+409
-23
lines changed

7 files changed

+409
-23
lines changed

hack/bats/helpers/limactl.bash

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SPDX-FileCopyrightText: Copyright The Lima Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Create a dummy Lima instance for testing purposes. It cannot be started because it doesn't have an actual image.
5+
# This function intentionally doesn't use create/editflags, but modifies the template with yq instead.
6+
create_dummy_instance() {
7+
local name=$1
8+
local expr=$2
9+
10+
# Template does not validate without an image, and the image must point to a file that exists (for clonefile).
11+
local template="{images: [location: /etc/profile]}"
12+
if [[ -n $expr ]]; then
13+
template="$(limactl yq "$expr" <<<"$template")"
14+
fi
15+
limactl create --name "$name" - <<<"$template"
16+
}

hack/bats/helpers/load.bash

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# SPDX-FileCopyrightText: Copyright The Lima Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
14
set -o errexit -o nounset -o pipefail
25

36
# Don't run the tests in ~/.lima because they may destroy _config, _templates etc.
@@ -17,8 +20,15 @@ source "$PATH_BATS_ROOT/lib/bats-support/load.bash"
1720
source "$PATH_BATS_ROOT/lib/bats-assert/load.bash"
1821
source "$PATH_BATS_ROOT/lib/bats-file/load.bash"
1922

23+
source "$PATH_BATS_HELPERS/limactl.bash"
24+
source "$PATH_BATS_HELPERS/logs.bash"
25+
2026
bats_require_minimum_version 1.5.0
2127

28+
run_e() {
29+
run --separate-stderr "$@"
30+
}
31+
2232
# If called from foo() this function will call local_foo() if it exist.
2333
call_local_function() {
2434
local func
@@ -45,4 +55,10 @@ setup() {
4555
}
4656
teardown() {
4757
call_local_function
48-
}
58+
}
59+
60+
assert_output_lines_count() {
61+
assert_equal "${#lines[@]}" "$1"
62+
}
63+
64+

hack/bats/helpers/logs.bash

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# SPDX-FileCopyrightText: Copyright The Lima Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Format the string the way strconv.Quote() would do.
5+
# If the input ends with an ellipsis then no closing quote will be added (and the … will be removed).
6+
quote_msg() {
7+
local quoted
8+
quoted=$(sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/^/"/' <<<"$1")
9+
if [[ $quoted == *… ]]; then
10+
echo "${quoted%…}"
11+
else
12+
echo "${quoted}\""
13+
fi
14+
}
15+
16+
assert_fatal() {
17+
assert_stderr_line --partial "level=fatal msg=$(quote_msg "$1")"
18+
}
19+
assert_error() {
20+
assert_stderr_line --partial "level=error msg=$(quote_msg "$1")"
21+
}
22+
assert_warning() {
23+
assert_stderr_line --partial "level=warning msg=$(quote_msg "$1")"
24+
}
25+
assert_info() {
26+
assert_stderr_line --partial "level=info msg=$(quote_msg "$1")"
27+
}
28+
assert_debug() {
29+
assert_stderr_line --partial "level=debug msg=$(quote_msg "$1")"
30+
}

hack/bats/tests/00-yq.bats

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# SPDX-FileCopyrightText: Copyright The Lima Authors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
load "../helpers/load"
5+
6+
@test 'make sure the yq subcommand exists' {
7+
run -0 limactl yq --version
8+
assert_output --regexp '^yq .*mikefarah.* version v'
9+
}
10+
11+
@test 'yq can evaluate yq expressions' {
12+
run -0 limactl yq .foo=42 <<<""
13+
assert_output 'foo: 42'
14+
}
15+
16+
@test 'yq command understand yq options' {
17+
run -0 limactl yq -n -o json -I 0 .foo=42
18+
assert_output '{"foo":42}'
19+
}
20+
21+
@test 'yq errors set non-zero exit code' {
22+
run -1 limactl yq -n foo
23+
assert_output --partial "invalid input"
24+
}
25+
26+
@test 'yq works as a multi-call binary' {
27+
# multi-call command detection strips all extensions
28+
YQ="yq.lima.exe"
29+
ln -sf "$(which limactl)" "${BATS_TEST_TMPDIR}/${YQ}"
30+
export PATH="$BATS_TEST_TMPDIR:$PATH"
31+
32+
run -0 "$YQ" --version
33+
assert_output --regexp '^yq .*mikefarah.* version v'
34+
35+
run -0 "$YQ" -n -o json -I 0 .foo=42
36+
assert_output '{"foo":42}'
37+
}

0 commit comments

Comments
 (0)