mirror of https://github.com/hyperledger/besu
Added validate-config subcommand (#2994)
* Remove benchmark from workflow (#2827) Remove benchmark from workflow Signed-off-by: Antony Denyer <git@antonydenyer.co.uk> Co-authored-by: Sally MacFarlane <sally.macfarlane@consensys.net> Signed-off-by: Min Song <minsong@splunk.com> * Added subcommand for validate-config Signed-off-by: Min Song <ms2597@cornell.edu> Signed-off-by: Min Song <minsong@splunk.com> * formatting Signed-off-by: Sally MacFarlane <sally.macfarlane@consensys.net> * use the main TomlConfigFile provider * inject commandLine Signed-off-by: Sally MacFarlane <sally.macfarlane@consensys.net> Co-authored-by: Antony Denyer <git@antonydenyer.co.uk> Co-authored-by: Min Song <minsong@splunk.com> Co-authored-by: Min Song <ms2597@cornell.edu>pull/3034/head
parent
3658ef034e
commit
4f44863694
@ -0,0 +1,70 @@ |
||||
/* |
||||
* Copyright contributors to Hyperledger Besu. |
||||
* |
||||
* Licensed 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. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
package org.hyperledger.besu.cli.subcommands; |
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull; |
||||
import static org.hyperledger.besu.cli.subcommands.ValidateConfigSubCommand.COMMAND_NAME; |
||||
|
||||
import org.hyperledger.besu.cli.BesuCommand; |
||||
import org.hyperledger.besu.cli.DefaultCommandValues; |
||||
import org.hyperledger.besu.cli.util.TomlConfigFileDefaultProvider; |
||||
|
||||
import java.io.PrintStream; |
||||
import java.nio.file.Path; |
||||
|
||||
import picocli.CommandLine; |
||||
import picocli.CommandLine.Command; |
||||
import picocli.CommandLine.Option; |
||||
import picocli.CommandLine.ParentCommand; |
||||
|
||||
@Command( |
||||
name = COMMAND_NAME, |
||||
description = "This command provides basic Besu config validation (syntax only).", |
||||
mixinStandardHelpOptions = true) |
||||
public class ValidateConfigSubCommand implements Runnable { |
||||
|
||||
public static final String COMMAND_NAME = "validate-config"; |
||||
|
||||
@Option( |
||||
names = "--config-file", |
||||
paramLabel = DefaultCommandValues.MANDATORY_PATH_FORMAT_HELP, |
||||
description = "Path to Besu config file") |
||||
private final Path dataPath = DefaultCommandValues.getDefaultBesuDataPath(this); |
||||
|
||||
@SuppressWarnings("unused") |
||||
@ParentCommand |
||||
private BesuCommand parentCommand; |
||||
|
||||
final PrintStream out; |
||||
final CommandLine commandLine; |
||||
|
||||
public ValidateConfigSubCommand(final CommandLine commandLine, final PrintStream out) { |
||||
this.out = out; |
||||
this.commandLine = commandLine; |
||||
} |
||||
|
||||
@Override |
||||
public void run() { |
||||
checkNotNull(parentCommand); |
||||
try { |
||||
new TomlConfigFileDefaultProvider(commandLine, dataPath.toFile()).loadConfigurationFromFile(); |
||||
} catch (Exception e) { |
||||
this.out.print(e); |
||||
return; |
||||
} |
||||
this.out.print( |
||||
"TOML config file is valid on basic inspection. Further dependencies between related options are checked when Besu starts."); |
||||
} |
||||
} |
@ -0,0 +1,90 @@ |
||||
/* |
||||
* Copyright contributors to Hyperledger Besu. |
||||
* |
||||
* Licensed 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. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
package org.hyperledger.besu.cli; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
import java.io.IOException; |
||||
import java.net.URL; |
||||
import java.nio.file.Files; |
||||
import java.nio.file.Path; |
||||
|
||||
import com.google.common.io.Resources; |
||||
import org.junit.Test; |
||||
import picocli.CommandLine.Model.CommandSpec; |
||||
|
||||
public class ValidateConfigSubCommandTest extends CommandTestAbstract { |
||||
|
||||
private static final String EXPECTED_PUBLIC_KEY_USAGE = |
||||
"Usage: besu validate-config [-hV] [--config-file=<PATH>]" |
||||
+ System.lineSeparator() |
||||
+ "This command provides basic Besu config validation (syntax only)." |
||||
+ System.lineSeparator() |
||||
+ " --config-file=<PATH> Path to Besu config file" |
||||
+ System.lineSeparator() |
||||
+ " -h, --help Show this help message and exit." |
||||
+ System.lineSeparator() |
||||
+ " -V, --version Print version information and exit." |
||||
+ System.lineSeparator(); |
||||
|
||||
private static final String VALIDATE_CONFIG_SUBCOMMAND_NAME = "validate-config"; |
||||
|
||||
@Test |
||||
public void validateConfigSubCommandExists() { |
||||
CommandSpec spec = parseCommand().getSpec(); |
||||
assertThat(spec.subcommands()).containsKeys(VALIDATE_CONFIG_SUBCOMMAND_NAME); |
||||
assertThat(commandOutput.toString()).isEmpty(); |
||||
assertThat(commandErrorOutput.toString()).isEmpty(); |
||||
} |
||||
|
||||
@Test |
||||
public void callingValidateConfigSubCommandHelpMustDisplayUsage() { |
||||
parseCommand(VALIDATE_CONFIG_SUBCOMMAND_NAME, "--help"); |
||||
assertThat(commandOutput.toString()).startsWith(EXPECTED_PUBLIC_KEY_USAGE); |
||||
assertThat(commandErrorOutput.toString()).isEmpty(); |
||||
} |
||||
|
||||
@Test |
||||
public void callingValidateConfigSubCommandWithNonExistentMustDisplayError() { |
||||
parseCommand(VALIDATE_CONFIG_SUBCOMMAND_NAME, "--config-file", "/non/existent/file"); |
||||
assertThat(commandOutput.toString()) |
||||
.contains("Unable to read TOML configuration, file not found."); |
||||
assertThat(commandErrorOutput.toString()).isEmpty(); |
||||
} |
||||
|
||||
@Test |
||||
public void callingValidateConfigSubCommandWithInvalidFileMustDisplayError() throws IOException { |
||||
final Path invalidToml = Files.createTempFile("invalid", "toml"); |
||||
Files.writeString(invalidToml, "xyz="); |
||||
invalidToml.toFile().deleteOnExit(); |
||||
|
||||
parseCommand(VALIDATE_CONFIG_SUBCOMMAND_NAME, "--config-file", invalidToml.toString()); |
||||
assertThat(commandOutput.toString()).contains("Invalid TOML configuration"); |
||||
assertThat(commandErrorOutput.toString()).isEmpty(); |
||||
} |
||||
|
||||
@Test |
||||
public void callingValidateConfigSubCommandWithValidTomlFileSucceeds() throws IOException { |
||||
|
||||
final URL configFile = this.getClass().getResource("/everything_config.toml"); |
||||
final Path validToml = Files.createTempFile("valid", "toml"); |
||||
Files.write(validToml, Resources.toByteArray(configFile)); |
||||
validToml.toFile().deleteOnExit(); |
||||
|
||||
parseCommand(VALIDATE_CONFIG_SUBCOMMAND_NAME, "--config-file", validToml.toString()); |
||||
assertThat(commandOutput.toString()).startsWith("TOML config file is valid"); |
||||
assertThat(commandErrorOutput.toString()).isEmpty(); |
||||
} |
||||
} |
Loading…
Reference in new issue