The bg_setenv/bg_printenv command-line interface is currently untested,
which makes it error-prone to extend the current code.

This commit introduces basic tests for the bg_setenv/bg_printenv
command-line interface.

The following new test dependencies are introduced (included as git
submodules, as suggested in the docs [1]):

- bats-core [2]: MIT
- bats-support [3]: CC0-1.0
- bats-assert [4]: CC0-1.0

Strictly speaking, only bats-core is a must-have, but [3] and [4] make
it much more friendly to use (and they are being used in this commit as
well).

[1] https://bats-core.readthedocs.io/en/stable/tutorial.html#quick-installation
[2] https://github.com/bats-core/bats-core
[3] https://github.com/bats-core/bats-support
[4] https://github.com/bats-core/bats-assert

Signed-off-by: Michael Adler <[email protected]>
---
 .github/workflows/main.yaml    |   5 +-
 .gitmodules                    |   9 +++
 docs/COMPILE.md                |   3 +-
 tests/bats                     |   1 +
 tests/bg_setenv.bats           | 108 +++++++++++++++++++++++++++++++++
 tests/files/BGENV.DAT          | Bin 0 -> 132104 bytes
 tests/test_helper/bats-assert  |   1 +
 tests/test_helper/bats-support |   1 +
 8 files changed, 126 insertions(+), 2 deletions(-)
 create mode 160000 tests/bats
 create mode 100755 tests/bg_setenv.bats
 create mode 100644 tests/files/BGENV.DAT
 create mode 160000 tests/test_helper/bats-assert
 create mode 160000 tests/test_helper/bats-support

diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml
index 6bb9f66..e283cb2 100644
--- a/.github/workflows/main.yaml
+++ b/.github/workflows/main.yaml
@@ -86,9 +86,12 @@ jobs:
       - name: Build amd64
         if: ${{ matrix.target == 'amd64' }}
         run: |
-          cd build
+          pushd build >/dev/null
           ../configure
           make check -j $(nproc)
+          sudo make install
+          popd >/dev/null
+          time ./tests/bats/bin/bats --tap --print-output-on-failure tests
       - name: Build i386
         if: ${{ matrix.target == 'i386' }}
         run: |
diff --git a/.gitmodules b/.gitmodules
index 80ad11c..941b2fd 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,3 +1,12 @@
 [submodule "tests/fff"]
        path = tests/fff
        url = https://github.com/meekrosoft/fff
+[submodule "tests/bats"]
+       path = tests/bats
+       url = https://github.com/bats-core/bats-core.git
+[submodule "tests/test_helper/bats-support"]
+       path = tests/test_helper/bats-support
+       url = https://github.com/bats-core/bats-support.git
+[submodule "tests/test_helper/bats-assert"]
+       path = tests/test_helper/bats-assert
+       url = https://github.com/bats-core/bats-assert.git
diff --git a/docs/COMPILE.md b/docs/COMPILE.md
index bda19d4..61650a2 100644
--- a/docs/COMPILE.md
+++ b/docs/COMPILE.md
@@ -50,4 +50,5 @@ where `<sys-root-dir>` points to the wanted sysroot for 
cross-compilation.
 
 ## Testing ##
 
-`make check` will run all unit tests.
+* `make check` will run all unit tests.
+* `./tests/bats/bin/bats tests` will run all integration tests (`bats` is a 
git submodule)
diff --git a/tests/bats b/tests/bats
new file mode 160000
index 0000000..01636e4
--- /dev/null
+++ b/tests/bats
@@ -0,0 +1 @@
+Subproject commit 01636e489bb9b9d6c430465e6f8409422e63d3fa
diff --git a/tests/bg_setenv.bats b/tests/bg_setenv.bats
new file mode 100755
index 0000000..a329f0e
--- /dev/null
+++ b/tests/bg_setenv.bats
@@ -0,0 +1,108 @@
+#!/usr/bin/env bats
+# Copyright (c) Siemens AG, 2021
+#
+# Authors:
+#  Michael Adler <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPL, version 2.  See
+# the COPYING file in the top-level directory.
+#
+# SPDX-License-Identifier: GPL-2.0
+#
+
+setup() {
+    load 'test_helper/bats-support/load'
+    load 'test_helper/bats-assert/load'
+
+    # get the containing directory of this file
+    # use $BATS_TEST_FILENAME instead of ${BASH_SOURCE[0]} or $0,
+    # as those will point to the bats executable's location or the 
preprocessed file respectively
+    DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )"
+    PATH="$DIR/..:$PATH"
+}
+
+@test "print existing BGENV" {
+    envfile="$DIR/files/BGENV.DAT"
+
+    run bg_printenv -f "$envfile"
+    assert_output "Values:
+in_progress:      no
+revision:         1
+kernel:           C:BOOT:kernel.efi
+kernelargs:       root=/dev/sda
+watchdog timeout: 0 seconds
+ustate:           0 (OK)
+
+user variables:
+foo = bar"
+
+    run md5sum "$envfile"
+    assert_output --regexp "6613ca4da6bcaad37362f4096a60f0e3\s*$envfile"
+}
+
+@test "create an empty BGENV" {
+    envfile="$BATS_TEST_TMPDIR/BGENV.DAT"
+
+    run bg_setenv -f "$envfile"
+
+    assert_output "Output written to $envfile."
+
+    run stat --format=%s "$envfile"
+    assert_output 132104
+
+    run bg_printenv -f "$envfile"
+    assert_output "Values:
+in_progress:      no
+revision:         0
+kernel:           
+kernelargs:       
+watchdog timeout: 0 seconds
+ustate:           0 (OK)
+
+user variables:"
+    run md5sum "$envfile"
+    assert_output --regexp "441b49e907a117d2fe1dc1d69d8ea1b0\s*$envfile"
+}
+
+@test "modify BGENV, discard existing values" {
+    envfile="$BATS_TEST_TMPDIR/BGENV.DAT"
+
+    cp "$DIR/files/BGENV.DAT" "$envfile"
+    run bg_setenv -f "$envfile" -k C:BOOTNEW:kernel.efi
+
+    run bg_printenv -f "$envfile"
+    assert_output "Values:
+in_progress:      no
+revision:         0
+kernel:           C:BOOTNEW:kernel.efi
+kernelargs:       
+watchdog timeout: 0 seconds
+ustate:           0 (OK)
+
+user variables:"
+
+    run md5sum "$envfile"
+    assert_output --regexp "15bc40c9feae99cc879cfc55e0132caa\s*$envfile"
+}
+
+@test "modify BGENV, preserve existing values" {
+    envfile="$BATS_TEST_TMPDIR/BGENV.DAT"
+
+    cp "$DIR/files/BGENV.DAT" "$envfile"
+    run bg_setenv -f "$envfile" -k C:BOOTNEW:kernel.efi -P
+
+    run bg_printenv -f "$envfile"
+    assert_output "Values:
+in_progress:      no
+revision:         1
+kernel:           C:BOOTNEW:kernel.efi
+kernelargs:       root=/dev/sda
+watchdog timeout: 0 seconds
+ustate:           0 (OK)
+
+user variables:
+foo = bar"
+
+    run md5sum "$envfile"
+    assert_output --regexp "56f5733996e25fa1ea9b053d8f197658\s*$envfile"
+}
diff --git a/tests/files/BGENV.DAT b/tests/files/BGENV.DAT
new file mode 100644
index 
0000000000000000000000000000000000000000..1dbbfe51dbbeddd43c2ed228d713a47d43e5dda5
GIT binary patch
literal 132104
zcmeIw!3{xC5CzaH8c>1?h{Q@_Ljg7#_<}@;FP~^ZgKDuh3srpQO)_)0bLOqH$SRwv
zZKZP#>2j@p$)x%*r+EDp=+ag_vdlF5y7#l@y;4&vU+Oo^a?8BG>)R}YGB~zvMm5Jj
zk6qLKl0kp~0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+
z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly
zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF
z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk
z1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs
z0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZ
zfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&U
zAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7
z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N
z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+
z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly
NK!Cu%34C6|_ZORb5l;XB

literal 0
HcmV?d00001

diff --git a/tests/test_helper/bats-assert b/tests/test_helper/bats-assert
new file mode 160000
index 0000000..672ad18
--- /dev/null
+++ b/tests/test_helper/bats-assert
@@ -0,0 +1 @@
+Subproject commit 672ad1823a4d2f0c475fdbec0c4497498eec5f41
diff --git a/tests/test_helper/bats-support b/tests/test_helper/bats-support
new file mode 160000
index 0000000..d140a65
--- /dev/null
+++ b/tests/test_helper/bats-support
@@ -0,0 +1 @@
+Subproject commit d140a65044b2d6810381935ae7f0c94c7023c8c3
-- 
2.33.0

-- 
You received this message because you are subscribed to the Google Groups "EFI 
Boot Guard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/efibootguard-dev/20211020070554.411028-2-michael.adler%40siemens.com.

Reply via email to