Skip to content
Merged
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 @@ -110,6 +110,7 @@
import org.apache.bookkeeper.replication.ReplicationException.UnavailableException;
import org.apache.bookkeeper.stats.NullStatsLogger;
import org.apache.bookkeeper.tools.cli.commands.bookie.FormatCommand;
import org.apache.bookkeeper.tools.cli.commands.bookie.InitCommand;
import org.apache.bookkeeper.tools.cli.commands.bookie.LastMarkCommand;
import org.apache.bookkeeper.tools.cli.commands.bookies.InfoCommand;
import org.apache.bookkeeper.tools.cli.commands.bookies.ListBookiesCommand;
Expand Down Expand Up @@ -498,7 +499,8 @@ String getUsage() {
@Override
int runCmd(CommandLine cmdLine) throws Exception {
ServerConfiguration conf = new ServerConfiguration(bkConf);
boolean result = BookKeeperAdmin.initBookie(conf);
InitCommand initCommand = new InitCommand();
boolean result = initCommand.apply(conf, new CliFlags());
return (result) ? 0 : 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.bookkeeper.tools.cli.commands.bookie;

import com.google.common.util.concurrent.UncheckedExecutionException;
import org.apache.bookkeeper.client.BookKeeperAdmin;
import org.apache.bookkeeper.conf.ServerConfiguration;
import org.apache.bookkeeper.tools.cli.helpers.BookieCommand;
import org.apache.bookkeeper.tools.framework.CliFlags;
import org.apache.bookkeeper.tools.framework.CliSpec;

/**
* A command to initialize new bookie.
*/
public class InitCommand extends BookieCommand<CliFlags> {

private static final String NAME = "init";
private static final String DESC = "Initialize new bookie.";

public InitCommand() {
super(CliSpec.newBuilder()
.withName(NAME)
.withDescription(DESC)
.withFlags(new CliFlags())
.build());
}

@Override
public boolean apply(ServerConfiguration conf, CliFlags cmdFlags) {

boolean result = false;
try {
result = BookKeeperAdmin.initBookie(conf);
} catch (Exception e) {
throw new UncheckedExecutionException(e.getMessage(), e);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.apache.bookkeeper.tools.cli.BKCtl;
import org.apache.bookkeeper.tools.cli.commands.bookie.FormatCommand;
import org.apache.bookkeeper.tools.cli.commands.bookie.InitCommand;
import org.apache.bookkeeper.tools.cli.commands.bookie.LastMarkCommand;
import org.apache.bookkeeper.tools.common.BKFlags;
import org.apache.bookkeeper.tools.framework.CliCommandGroup;
Expand All @@ -41,6 +42,7 @@ public class BookieCommandGroup extends CliCommandGroup<BKFlags> {
.withParent(BKCtl.NAME)
.withCategory(CATEGORY_INFRA_SERVICE)
.addCommand(new LastMarkCommand())
.addCommand(new InitCommand())
.addCommand(new FormatCommand())
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.bookkeeper.tools.cli.commands.bookie;

import static org.junit.Assert.fail;

import org.apache.bookkeeper.client.BookKeeperAdmin;
import org.apache.bookkeeper.conf.ServerConfiguration;
import org.apache.bookkeeper.tools.cli.helpers.BookieCommandTestBase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

/**
* Unit test for {@link InitCommand}.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({BookKeeperAdmin.class})
public class InitCommandTest extends BookieCommandTestBase {

public InitCommandTest() {
super(3, 0);
}

public void setup() throws Exception {
PowerMockito.whenNew(ServerConfiguration.class).withNoArguments().thenReturn(conf);
PowerMockito.mockStatic(BookKeeperAdmin.class);
PowerMockito.when(BookKeeperAdmin.initBookie(conf)).thenReturn(true);
}

@Test
public void testInitCommand() {
InitCommand initCommand = new InitCommand();
try {
initCommand.apply(bkFlags, new String[] { "" });
} catch (Exception e) {
fail("Should not throw any exception here.");
}
}
}