Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ public SanityTestCommand(SanityFlags flags) {
super(CliSpec.<SanityFlags>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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<SanityTestCommand> 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<SanityFlags> flagsCaptor = ArgumentCaptor.forClass(SanityFlags.class);
commandMockedStatic.verify(() -> SanityTestCommand.handle(same(shell.bkConf), flagsCaptor.capture()), times(2));
List<SanityFlags> 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());
Expand Down
Loading