From 0f1dfb1691baa3bed64ac4dee07c130744a0685a Mon Sep 17 00:00:00 2001 From: Masahiro Sakamoto Date: Wed, 29 Oct 2025 13:07:01 +0900 Subject: [PATCH] Fix issue where options for sanity test command are ignored --- .../apache/bookkeeper/bookie/BookieShell.java | 4 +- .../commands/bookie/SanityTestCommand.java | 8 +++- .../bookkeeper/bookie/BookieShellTest.java | 41 +++++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java index d7d856a7de3..39b86948857 100644 --- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java +++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieShell.java @@ -1084,8 +1084,10 @@ String getUsage() { @Override int runCmd(CommandLine cmdLine) throws Exception { - SanityTestCommand command = new SanityTestCommand(); SanityTestCommand.SanityFlags flags = new SanityTestCommand.SanityFlags(); + flags.entries(getOptionIntValue(cmdLine, "e", 10)); + flags.timeout(getOptionIntValue(cmdLine, "t", 1)); + SanityTestCommand command = SanityTestCommand.newSanityTestCommand(flags); boolean result = command.apply(bkConf, flags); return (result) ? 0 : -1; } diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookie/SanityTestCommand.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookie/SanityTestCommand.java index d8a1908fb90..3165ce53b6f 100644 --- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookie/SanityTestCommand.java +++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/tools/cli/commands/bookie/SanityTestCommand.java @@ -61,12 +61,16 @@ public SanityTestCommand(SanityFlags flags) { super(CliSpec.newBuilder().withFlags(flags).withName(NAME).withDescription(DESC).build()); } + public static SanityTestCommand newSanityTestCommand(SanityFlags flags) { + return new SanityTestCommand(flags); + } + /** * Flags for sanity command. */ @Accessors(fluent = true) @Setter - public static class SanityFlags extends CliFlags{ + public static class SanityFlags extends CliFlags { @Parameter(names = {"-e", "--entries"}, description = "Total entries to be added for the test (default 10)") private int entries = 10; @@ -86,7 +90,7 @@ public boolean apply(ServerConfiguration conf, SanityFlags cmdFlags) { } } - private static boolean handle(ServerConfiguration conf, SanityFlags cmdFlags) throws Exception { + public static boolean handle(ServerConfiguration conf, SanityFlags cmdFlags) throws Exception { try { return handleAsync(conf, cmdFlags).get(); } catch (Exception e) { diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieShellTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieShellTest.java index a802fc4f6ce..9df3b51c6f9 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieShellTest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/BookieShellTest.java @@ -38,6 +38,8 @@ import com.google.common.collect.Maps; import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.lang.reflect.Field; +import java.util.List; import java.util.Set; import java.util.SortedMap; import java.util.function.Function; @@ -52,6 +54,8 @@ import org.apache.bookkeeper.meta.MetadataDrivers; import org.apache.bookkeeper.net.BookieId; import org.apache.bookkeeper.tools.cli.commands.bookie.LastMarkCommand; +import org.apache.bookkeeper.tools.cli.commands.bookie.SanityTestCommand; +import org.apache.bookkeeper.tools.cli.commands.bookie.SanityTestCommand.SanityFlags; import org.apache.bookkeeper.tools.cli.commands.bookies.ClusterInfoCommand; import org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand; import org.apache.bookkeeper.tools.cli.commands.client.SimpleTestCommand; @@ -70,6 +74,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; import org.mockito.MockedStatic; import org.mockito.junit.MockitoJUnitRunner; @@ -376,6 +381,42 @@ public void testLastMarkCmd() throws Exception { .apply(same(shell.bkConf), any(CliFlags.class)); } + @Test + public void testBookieSanityTestCmd() throws Exception { + SanityTestCommand mockCommand = spy(new SanityTestCommand()); + + @Cleanup + MockedStatic commandMockedStatic = mockStatic(SanityTestCommand.class); + commandMockedStatic.when(() -> SanityTestCommand.newSanityTestCommand(any(SanityFlags.class))) + .thenReturn(mockCommand); + commandMockedStatic.when(() -> SanityTestCommand.handle(any(ServerConfiguration.class), any(SanityFlags.class))) + .thenReturn(true); + + shell.run(new String[] { + "bookiesanity" + }); + + shell.run(new String[] { + "bookiesanity", + "-e", "20", + "-t", "5" + }); + + ArgumentCaptor flagsCaptor = ArgumentCaptor.forClass(SanityFlags.class); + commandMockedStatic.verify(() -> SanityTestCommand.handle(same(shell.bkConf), flagsCaptor.capture()), times(2)); + List flagsList = flagsCaptor.getAllValues(); + + Field entriesField = SanityFlags.class.getDeclaredField("entries"); + entriesField.setAccessible(true); + assertEquals(10, (int) entriesField.get(flagsList.get(0))); // default value + assertEquals(20, (int) entriesField.get(flagsList.get(1))); + + Field timeoutField = SanityFlags.class.getDeclaredField("timeout"); + timeoutField.setAccessible(true); + assertEquals(1, (int) timeoutField.get(flagsList.get(0))); // default value + assertEquals(5, (int) timeoutField.get(flagsList.get(1))); + } + @Test public void testSimpleTestCmd() throws Exception { SimpleTestCommand.Flags mockSimpleTestFlags = spy(new SimpleTestCommand.Flags());