From 6c946118cf7440f09485a90181796959a21dec54 Mon Sep 17 00:00:00 2001
From: Rob Norris <rob.norris@klarasystems.com>
Date: Tue, 12 Nov 2024 14:54:39 +1100
Subject: [PATCH] zts: add test for log spacemap flushall + zpool condense

Sponsored-by: Klara, Inc.
Sponsored-by: Wasabi Technology, Inc.
Signed-off-by: Rob Norris <rob.norris@klarasystems.com>
---
 tests/runfiles/common.run                     |  2 +-
 tests/zfs-tests/tests/Makefile.am             |  1 +
 .../log_spacemap/log_spacemap_flushall.ksh    | 80 +++++++++++++++++++
 3 files changed, 82 insertions(+), 1 deletion(-)
 create mode 100755 tests/zfs-tests/tests/functional/log_spacemap/log_spacemap_flushall.ksh

diff --git a/tests/runfiles/common.run b/tests/runfiles/common.run
index fc4adc42d00a..f22d6f261c46 100644
--- a/tests/runfiles/common.run
+++ b/tests/runfiles/common.run
@@ -1054,7 +1054,7 @@ tests = ['many_fds', 'libzfs_input']
 tags = ['functional', 'libzfs']
 
 [tests/functional/log_spacemap]
-tests = ['log_spacemap_import_logs']
+tests = ['log_spacemap_import_logs', 'log_spacemap_flushall']
 pre =
 post =
 tags = ['functional', 'log_spacemap']
diff --git a/tests/zfs-tests/tests/Makefile.am b/tests/zfs-tests/tests/Makefile.am
index 7d1551a63f0d..d3b4aa20e94c 100644
--- a/tests/zfs-tests/tests/Makefile.am
+++ b/tests/zfs-tests/tests/Makefile.am
@@ -1612,6 +1612,7 @@ nobase_dist_datadir_zfs_tests_tests_SCRIPTS += \
 	functional/longname/longname_002_pos.ksh \
 	functional/longname/longname_003_pos.ksh \
 	functional/longname/setup.ksh \
+	functional/log_spacemap/log_spacemap_flushall.ksh \
 	functional/log_spacemap/log_spacemap_import_logs.ksh \
 	functional/migration/cleanup.ksh \
 	functional/migration/migration_001_pos.ksh \
diff --git a/tests/zfs-tests/tests/functional/log_spacemap/log_spacemap_flushall.ksh b/tests/zfs-tests/tests/functional/log_spacemap/log_spacemap_flushall.ksh
new file mode 100755
index 000000000000..10e4f80c03d3
--- /dev/null
+++ b/tests/zfs-tests/tests/functional/log_spacemap/log_spacemap_flushall.ksh
@@ -0,0 +1,80 @@
+#! /bin/ksh -p
+#
+# CDDL HEADER START
+#
+# This file and its contents are supplied under the terms of the
+# Common Development and Distribution License ("CDDL"), version 1.0.
+# You may only use this file in accordance with the terms of version
+# 1.0 of the CDDL.
+#
+# A full copy of the text of the CDDL should have accompanied this
+# source.  A copy of the CDDL is also available via the Internet at
+# http://www.illumos.org/license/CDDL.
+#
+# CDDL HEADER END
+#
+
+#
+# Copyright (c) 2019 by Delphix. All rights reserved.
+# Copyright (c) 2024, Klara, Inc.
+#
+
+. $STF_SUITE/include/libtest.shlib
+
+#
+# DESCRIPTION:
+
+# This tests the on-demand "flush all spacemap logs" feature. This is the same
+# process is that triggered at pool export, but instead we trigger it ahead of
+# time via `zpool condense`.
+#
+# This test uses the `log_spacemaps` kstat and `zdb -m` to know how much is
+# waiting to be flushed. All we're looking for is that the flushall function
+# works, not how much it's doing.
+
+#
+# STRATEGY:
+#	1. Create pool.
+#	2. Write things, which will add to the spacemap logs.
+#	3. Save the counters.
+#	4. Request the spacemap logs be flushed.
+#	5. Compare counters against previous values.
+#
+
+verify_runnable "global"
+
+function cleanup
+{
+	if poolexists $LOGSM_POOL; then
+		log_must zpool destroy -f $LOGSM_POOL
+	fi
+}
+log_onexit cleanup
+
+function get_smp_length {
+	zdb -m $LOGSM_POOL | grep smp_length | \
+	    awk '{ sum += $3 } END { print sum }'
+}
+
+LOGSM_POOL="logsm_flushall"
+read -r TESTDISK _ <<<"$DISKS"
+
+log_must zpool create -o cachefile=none -f -O compression=off \
+    $LOGSM_POOL $TESTDISK
+
+log_must file_write -o create -f /$LOGSM_POOL/f1 -b 131072 -c 32 -d R
+log_must file_write -o create -f /$LOGSM_POOL/f2 -b 131072 -c 32 -d R
+log_must file_write -o create -f /$LOGSM_POOL/f3 -b 131072 -c 32 -d R
+log_must file_write -o create -f /$LOGSM_POOL/f4 -b 131072 -c 32 -d R
+
+sync_all_pools
+
+typeset length_1=$(get_smp_length)
+
+log_must zpool condense -t log-spacemap -w $LOGSM_POOL
+
+typeset length_2=$(get_smp_length)
+
+log_must test $length_1 -gt $length_2
+
+log_pass "Log spacemaps on-demand flushall works"