Cutover more tests to CommandTestCase

Original commit: elastic/x-pack-elasticsearch@19c168a712
This commit is contained in:
Ryan Ernst 2016-03-08 17:28:11 -08:00
parent 64419c0856
commit d880803c2d
5 changed files with 65 additions and 59 deletions

View File

@ -18,22 +18,23 @@ import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.UserError;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.common.io.PathUtils;
import static org.elasticsearch.license.core.CryptUtils.writeEncryptedPrivateKey;
import static org.elasticsearch.license.core.CryptUtils.writeEncryptedPublicKey;
public class KeyPairGeneratorTool extends Command {
private final OptionSpec<File> publicKeyPathOption;
private final OptionSpec<File> privateKeyPathOption;
private final OptionSpec<String> publicKeyPathOption;
private final OptionSpec<String> privateKeyPathOption;
public KeyPairGeneratorTool() {
super("Generates a key pair with RSA 2048-bit security");
// TODO: in jopt-simple 5.0 we can use a PathConverter to take Path instead of File
this.publicKeyPathOption = parser.accepts("publicKeyPath", "public key path")
.withRequiredArg().ofType(File.class).required();
.withRequiredArg().required();
this.privateKeyPathOption = parser.accepts("privateKeyPath", "private key path")
.withRequiredArg().ofType(File.class).required();
.withRequiredArg().required();
}
public static void main(String[] args) throws Exception {
@ -50,13 +51,8 @@ public class KeyPairGeneratorTool extends Command {
@Override
protected void execute(Terminal terminal, OptionSet options) throws Exception {
File publicKeyPath = publicKeyPathOption.value(options);
File privateKeyPath = privateKeyPathOption.value(options);
execute(terminal, publicKeyPath.toPath(), privateKeyPath.toPath());
}
// pkg private for tests
void execute(Terminal terminal, Path publicKeyPath, Path privateKeyPath) throws Exception {
Path publicKeyPath = PathUtils.get(publicKeyPathOption.value(options));
Path privateKeyPath = PathUtils.get(privateKeyPathOption.value(options));
if (Files.exists(privateKeyPath)) {
throw new UserError(ExitCodes.USAGE, privateKeyPath + " already exists");
} else if (Files.exists(publicKeyPath)) {

View File

@ -8,6 +8,8 @@ package org.elasticsearch.license.licensor.tools;
import java.nio.file.Files;
import java.nio.file.Path;
import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.CommandTestCase;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.UserError;
import org.elasticsearch.cli.Terminal;
@ -15,31 +17,34 @@ import org.elasticsearch.test.ESTestCase;
import static org.hamcrest.CoreMatchers.containsString;
public class KeyPairGenerationToolTests extends ESTestCase {
public class KeyPairGenerationToolTests extends CommandTestCase {
@Override
protected Command newCommand() {
return new KeyPairGeneratorTool();
}
public void testMissingKeyPaths() throws Exception {
KeyPairGeneratorTool keyPairGeneratorTool = new KeyPairGeneratorTool();
Path exists = createTempFile();
Path exists = createTempFile("", "existing");
Path dne = createTempDir().resolve("dne");
UserError e = expectThrows(UserError.class, () -> {
keyPairGeneratorTool.execute(Terminal.DEFAULT, exists, dne);
execute("--publicKeyPath", exists.toString(), "--privateKeyPath", dne.toString());
});
assertThat(e.getMessage(), containsString("pub"));
assertThat(e.getMessage(), containsString("existing"));
assertEquals(ExitCodes.USAGE, e.exitCode);
e = expectThrows(UserError.class, () -> {
keyPairGeneratorTool.execute(Terminal.DEFAULT, dne, exists);
execute("--publicKeyPath", dne.toString(), "--privateKeyPath", exists.toString());
});
assertThat(e.getMessage(), containsString("pri"));
assertEquals(ExitCodes.USAGE, e.exitCode);
}
public void testTool() throws Exception {
KeyPairGeneratorTool keyPairGeneratorTool = new KeyPairGeneratorTool();
Path keysDir = createTempDir();
Path publicKeyFilePath = keysDir.resolve("public");
Path privateKeyFilePath = keysDir.resolve("private");
keyPairGeneratorTool.execute(Terminal.DEFAULT, publicKeyFilePath, privateKeyFilePath);
execute("--publicKeyPath", publicKeyFilePath.toString(), "--privateKeyPath", privateKeyFilePath.toString());
assertTrue(publicKeyFilePath.toString(), Files.exists(publicKeyFilePath));
assertTrue(privateKeyFilePath.toString(), Files.exists(privateKeyFilePath));
}

View File

@ -9,6 +9,8 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.CommandTestCase;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.UserError;
import org.elasticsearch.cli.MockTerminal;
@ -18,7 +20,7 @@ import org.elasticsearch.license.licensor.TestUtils;
import org.elasticsearch.test.ESTestCase;
import org.junit.Before;
public class LicenseGenerationToolTests extends ESTestCase {
public class LicenseGenerationToolTests extends CommandTestCase {
protected Path pubKeyPath = null;
protected Path priKeyPath = null;
@ -28,28 +30,31 @@ public class LicenseGenerationToolTests extends ESTestCase {
priKeyPath = getDataPath(TestUtils.PRIVATE_KEY_RESOURCE);
}
@Override
protected Command newCommand() {
return new LicenseGeneratorTool();
}
public void testMissingKeyPaths() throws Exception {
LicenseGeneratorTool licenseGeneratorTool = new LicenseGeneratorTool();
Path pub = createTempDir().resolve("pub");
Path pri = createTempDir().resolve("pri");
UserError e = expectThrows(UserError.class, () -> {
licenseGeneratorTool.execute(Terminal.DEFAULT, pub, pri, null, null);
execute("--publicKeyPath", pub.toString(), "--privateKeyPath", pri.toString());
});
assertTrue(e.getMessage(), e.getMessage().contains("pri does not exist"));
assertEquals(ExitCodes.USAGE, e.exitCode);
Files.createFile(pri);
e = expectThrows(UserError.class, () -> {
licenseGeneratorTool.execute(Terminal.DEFAULT, pub, pri, null, null);
execute("--publicKeyPath", pub.toString(), "--privateKeyPath", pri.toString());
});
assertTrue(e.getMessage(), e.getMessage().contains("pub does not exist"));
assertEquals(ExitCodes.USAGE, e.exitCode);
}
public void testMissingLicenseSpec() throws Exception {
LicenseGeneratorTool licenseGeneratorTool = new LicenseGeneratorTool();
UserError e = expectThrows(UserError.class, () -> {
licenseGeneratorTool.execute(Terminal.DEFAULT, pubKeyPath, priKeyPath, null, null);
execute("--publicKeyPath", pubKeyPath.toString(), "--privateKeyPath", priKeyPath.toString());
});
assertTrue(e.getMessage(), e.getMessage().contains("Must specify either --license or --licenseFile"));
assertEquals(ExitCodes.USAGE, e.exitCode);
@ -58,7 +63,9 @@ public class LicenseGenerationToolTests extends ESTestCase {
public void testLicenseSpecString() throws Exception {
TestUtils.LicenseSpec inputLicenseSpec = TestUtils.generateRandomLicenseSpec(License.VERSION_CURRENT);
String licenseSpecString = TestUtils.generateLicenseSpecString(inputLicenseSpec);
String output = runTool(licenseSpecString, null);
String output = execute("--publicKeyPath", pubKeyPath.toString(),
"--privateKeyPath", priKeyPath.toString(),
"--license", licenseSpecString);
License outputLicense = License.fromSource(output.getBytes(StandardCharsets.UTF_8));
TestUtils.assertLicenseSpec(inputLicenseSpec, outputLicense);
}
@ -68,18 +75,10 @@ public class LicenseGenerationToolTests extends ESTestCase {
String licenseSpecString = TestUtils.generateLicenseSpecString(inputLicenseSpec);
Path licenseSpecFile = createTempFile();
Files.write(licenseSpecFile, licenseSpecString.getBytes(StandardCharsets.UTF_8));
String output = runTool(null, licenseSpecFile);
String output = execute("--publicKeyPath", pubKeyPath.toString(),
"--privateKeyPath", priKeyPath.toString(),
"--licenseFile", licenseSpecFile.toString());
License outputLicense = License.fromSource(output.getBytes(StandardCharsets.UTF_8));
TestUtils.assertLicenseSpec(inputLicenseSpec, outputLicense);
}
private String runTool(String licenseSpecString, Path licenseSpecPath) throws Exception {
MockTerminal outputTerminal = new MockTerminal();
LicenseGeneratorTool licenseGeneratorTool = new LicenseGeneratorTool();
licenseGeneratorTool.execute(outputTerminal, pubKeyPath, priKeyPath, licenseSpecString, licenseSpecPath);
String output = outputTerminal.getOutput();
assertFalse(output, output.isEmpty());
return output;
}
}

View File

@ -9,16 +9,16 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.CommandTestCase;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.UserError;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.license.core.License;
import org.elasticsearch.license.licensor.TestUtils;
import org.elasticsearch.test.ESTestCase;
import org.junit.Before;
public class LicenseVerificationToolTests extends ESTestCase {
public class LicenseVerificationToolTests extends CommandTestCase {
protected Path pubKeyPath = null;
protected Path priKeyPath = null;
@ -29,20 +29,23 @@ public class LicenseVerificationToolTests extends ESTestCase {
priKeyPath = getDataPath(TestUtils.PRIVATE_KEY_RESOURCE);
}
@Override
protected Command newCommand() {
return new LicenseVerificationTool();
}
public void testMissingKeyPath() throws Exception {
LicenseVerificationTool tool = new LicenseVerificationTool();
Path pub = createTempDir().resolve("pub");
UserError e = expectThrows(UserError.class, () -> {
tool.execute(Terminal.DEFAULT, pub, null, null);
execute("--publicKeyPath", pub.toString());
});
assertTrue(e.getMessage(), e.getMessage().contains("pub does not exist"));
assertEquals(ExitCodes.USAGE, e.exitCode);
}
public void testMissingLicenseSpec() throws Exception {
LicenseVerificationTool tool = new LicenseVerificationTool();
UserError e = expectThrows(UserError.class, () -> {
tool.execute(Terminal.DEFAULT, pubKeyPath, null, null);
execute("--publicKeyPath", pubKeyPath.toString());
});
assertTrue(e.getMessage(), e.getMessage().contains("Must specify either --license or --licenseFile"));
assertEquals(ExitCodes.USAGE, e.exitCode);
@ -53,9 +56,9 @@ public class LicenseVerificationToolTests extends ESTestCase {
License tamperedLicense = License.builder()
.fromLicenseSpec(signedLicense, signedLicense.signature())
.expiryDate(signedLicense.expiryDate() + randomIntBetween(1, 1000)).build();
LicenseVerificationTool tool = new LicenseVerificationTool();
UserError e = expectThrows(UserError.class, () -> {
tool.execute(Terminal.DEFAULT, pubKeyPath, TestUtils.dumpLicense(tamperedLicense), null);
execute("--publicKeyPath", pubKeyPath.toString(),
"--license", TestUtils.dumpLicense(tamperedLicense));
});
assertEquals("Invalid License!", e.getMessage());
assertEquals(ExitCodes.DATA_ERROR, e.exitCode);
@ -63,15 +66,17 @@ public class LicenseVerificationToolTests extends ESTestCase {
public void testLicenseSpecString() throws Exception {
License signedLicense = TestUtils.generateSignedLicense(TimeValue.timeValueHours(1), pubKeyPath, priKeyPath);
LicenseVerificationTool tool = new LicenseVerificationTool();
tool.execute(Terminal.DEFAULT, pubKeyPath, TestUtils.dumpLicense(signedLicense), null);
String output = execute("--publicKeyPath", pubKeyPath.toString(),
"--license", TestUtils.dumpLicense(signedLicense));
assertFalse(output, output.isEmpty());
}
public void testLicenseSpecFile() throws Exception {
License signedLicense = TestUtils.generateSignedLicense(TimeValue.timeValueHours(1), pubKeyPath, priKeyPath);
Path licenseSpecFile = createTempFile();
Files.write(licenseSpecFile, TestUtils.dumpLicense(signedLicense).getBytes(StandardCharsets.UTF_8));
LicenseVerificationTool tool = new LicenseVerificationTool();
tool.execute(Terminal.DEFAULT, pubKeyPath, null, licenseSpecFile);
String output = execute("--publicKeyPath", pubKeyPath.toString(),
"--licenseFile", licenseSpecFile.toString());
assertFalse(output, output.isEmpty());
}
}

View File

@ -5,18 +5,19 @@
*/
package org.elasticsearch.watcher.trigger.schedule.tool;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.CommandTestCase;
public class CronEvalToolTests extends CommandTestCase {
@Override
protected Command newCommand() {
return new CronEvalTool();
}
public class CronEvalToolTests extends ESTestCase {
public void testParse() throws Exception {
String countOption = randomBoolean() ? "-c" : "--count";
int count = randomIntBetween(1, 100);
/*
CliTool.Command command = new CronEvalTool().parse("eval", new String[] { "0 0 0 1-6 * ?", countOption, String.valueOf(count) });
assertThat(command, instanceOf(CronEvalTool.Eval.class));
CronEvalTool.Eval eval = (CronEvalTool.Eval) command;
assertThat(eval.expression, is("0 0 0 1-6 * ?"));
assertThat(eval.count, is(count));
*/
String output = execute(countOption, Integer.toString(count), "0 0 0 1-6 * ?");
assertTrue(output, output.contains("Here are the next 60 times this cron expression will trigger"));
}
}