test: console logs to file

Test for the logging feature of obmc-console-server.

Change-Id: I9836727a944a0351962133baa2fd2ff5eb60e67f
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b8cc49a..947a5e6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -25,6 +25,14 @@
 
    More details can be found [in the documentation](docs/mux-support.md).
 
+4. Integration tests
+
+   Note that it's now advised to run `meson test ...` under [dbus-run-session][]
+   as the integration tests connect to the session bus.
+
+[dbus-run-session]:
+  https://manpages.debian.org/bookworm/dbus-daemon/dbus-run-session.1.en.html
+
 ### Changed:
 
 1. The bespoke config parser was replaced with iniparser
diff --git a/README.md b/README.md
index b2e2af3..9873e9d 100644
--- a/README.md
+++ b/README.md
@@ -9,7 +9,7 @@
 
 To test:
 
-    meson test -C build
+    dbus-run-session meson test -C build
 
 ## To Run Server
 
diff --git a/meson.build b/meson.build
index 27ac968..46f7e71 100644
--- a/meson.build
+++ b/meson.build
@@ -54,7 +54,7 @@
 
 iniparser_dep = dependency('iniparser')
 
-executable('obmc-console-server',
+server = executable('obmc-console-server',
            'config.c',
            'console-dbus.c',
            'console-server.c',
@@ -78,7 +78,7 @@
            install_dir: get_option('sbindir'),
            install: true)
 
-executable('obmc-console-client',
+client = executable('obmc-console-client',
            'config.c',
            'console-client.c',
            'console-socket.c',
diff --git a/test/meson.build b/test/meson.build
index a204b12..4aa1829 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -32,3 +32,13 @@
     )
   )
 endforeach
+
+socat = find_program('socat', native: true)
+
+meson.override_find_program('test-console-logs-to-file', files('test-console-logs-to-file'))
+test('test-console-logs-to-file',
+  find_program('test-console-logs-to-file'),
+  args: [ socat.full_path(), server.full_path() ],
+  depends: [ server ],
+  suite: 'itests'
+)
diff --git a/test/test-console-logs-to-file b/test/test-console-logs-to-file
new file mode 100755
index 0000000..ffedbd9
--- /dev/null
+++ b/test/test-console-logs-to-file
@@ -0,0 +1,47 @@
+#!/usr/bin/sh
+
+set -eux
+
+SOCAT="$1"
+SERVER="$2"
+
+# Meet DBus bus and path name constraints, append own PID for parallel runs
+TEST_NAME="$(basename "$0" | tr '-' '_')"_${$}
+TEST_DIR="$(mktemp --tmpdir --directory "${TEST_NAME}.XXXXXX")"
+PTYS_PID=""
+SERVER_PID=""
+
+cd "$TEST_DIR"
+
+cleanup()
+{
+  [ -z "$SERVER_PID" ] || kill -s INT "$SERVER_PID"
+  [ -z "$PTYS_PID" ] || kill "$PTYS_PID"
+  wait
+  cd -
+  rm -rf "$TEST_DIR"
+}
+
+trap cleanup EXIT
+
+TEST_CONF="${TEST_NAME}.conf"
+TEST_LOG="${TEST_NAME}.log"
+
+cat <<EOF > "$TEST_CONF"
+active-console = $TEST_NAME
+[$TEST_NAME]
+logfile = $TEST_LOG
+console-id = $TEST_NAME
+EOF
+
+"$SOCAT" -u PTY,raw,echo=0,link=remote PTY,raw,echo=0,wait-slave,link=local &
+PTYS_PID="$!"
+while ! [ -e remote ] || ! [ -e local ]; do sleep 1; done
+
+"$SERVER" --config "$TEST_CONF" "$(realpath local)" &
+SERVER_PID="$!"
+while ! busctl status --user xyz.openbmc_project.Console."${TEST_NAME}"; do sleep 1; done
+
+echo console-should-log-to-file > remote
+sleep 1
+grep -LF console-should-log-to-file "$TEST_LOG"