use of io.pravega.cli.admin.CommandArgs in project pravega by pravega.
the class TestUtils method executeCommand.
/**
* Invoke any command and get the result by using a mock PrintStream object (instead of System.out). The returned
* String is the output written by the Command that can be check in any test.
*
* @param inputCommand Command to execute.
* @param state Configuration to execute the command.
* @return Output of the command.
* @throws Exception If a problem occurs.
*/
public static String executeCommand(String inputCommand, AdminCommandState state) throws Exception {
Parser.Command pc = Parser.parse(inputCommand);
CommandArgs args = new CommandArgs(pc.getArgs(), state);
AdminCommand cmd = AdminCommand.Factory.get(pc.getComponent(), pc.getName(), args);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (PrintStream ps = new PrintStream(baos, true, StandardCharsets.UTF_8)) {
cmd.setOut(ps);
cmd.execute();
}
return baos.toString(StandardCharsets.UTF_8);
}
use of io.pravega.cli.admin.CommandArgs in project pravega by pravega.
the class DataRecoveryTest method testRepairLogEditOperationCreateSegmentProperties.
@Test
public void testRepairLogEditOperationCreateSegmentProperties() throws IOException {
// Setup command object.
STATE.set(new AdminCommandState());
Properties pravegaProperties = new Properties();
pravegaProperties.setProperty("pravegaservice.container.count", "1");
pravegaProperties.setProperty("pravegaservice.clusterName", "pravega0");
STATE.get().getConfigBuilder().include(pravegaProperties);
CommandArgs args = new CommandArgs(List.of("0"), STATE.get());
DurableDataLogRepairCommand command = Mockito.spy(new DurableDataLogRepairCommand(args));
// Create a SegmentProperties object via the command logic and verify that it is equal to the expected one.
long timestamp = System.currentTimeMillis();
Map<AttributeId, Long> attributes = new HashMap<>();
UUID uuid = UUID.randomUUID();
attributes.put(AttributeId.fromUUID(uuid), 10L);
Mockito.doReturn(true).doReturn(false).when(command).confirmContinue();
Mockito.doReturn(2L).doReturn(3L).doReturn(1L).doReturn(10L).doReturn(timestamp).when(command).getLongUserInput(Mockito.any());
Mockito.doReturn("test").doReturn(uuid.toString()).when(command).getStringUserInput(Mockito.any());
Mockito.doReturn(true).doReturn(true).doReturn(false).doReturn(false).when(command).getBooleanUserInput(Mockito.any());
SegmentProperties segmentProperties = StreamSegmentInformation.builder().name("test").startOffset(2).length(3).storageLength(1).sealed(true).deleted(false).sealedInStorage(true).deletedInStorage(false).attributes(attributes).lastModified(new ImmutableDate(timestamp)).build();
Assert.assertEquals(segmentProperties, command.createSegmentProperties());
// Induce exceptions during the process of creating attributes to check error handling.
segmentProperties = StreamSegmentInformation.builder().name("test").startOffset(2).length(3).storageLength(1).sealed(true).deleted(false).sealedInStorage(true).deletedInStorage(false).attributes(new HashMap<>()).lastModified(new ImmutableDate(timestamp)).build();
Mockito.doReturn(true).doReturn(false).when(command).confirmContinue();
Mockito.doReturn(true).doReturn(false).when(command).confirmContinue();
Mockito.doReturn(2L).doReturn(3L).doReturn(1L).doReturn(timestamp).when(command).getLongUserInput(Mockito.any());
Mockito.doReturn("test").doThrow(NumberFormatException.class).when(command).getStringUserInput(Mockito.any());
Mockito.doReturn(true).doReturn(true).doReturn(false).doReturn(false).when(command).getBooleanUserInput(Mockito.any());
Assert.assertEquals(segmentProperties, command.createSegmentProperties());
Mockito.doReturn(true).doReturn(false).when(command).confirmContinue();
Mockito.doReturn(true).doReturn(false).when(command).confirmContinue();
Mockito.doReturn(2L).doReturn(3L).doReturn(1L).doReturn(timestamp).when(command).getLongUserInput(Mockito.any());
Mockito.doReturn("test").doThrow(NullPointerException.class).when(command).getStringUserInput(Mockito.any());
Mockito.doReturn(true).doReturn(true).doReturn(false).doReturn(false).when(command).getBooleanUserInput(Mockito.any());
Assert.assertEquals(segmentProperties, command.createSegmentProperties());
}
use of io.pravega.cli.admin.CommandArgs in project pravega by pravega.
the class DataRecoveryTest method testCheckBackupLogAssertions.
@Test
public void testCheckBackupLogAssertions() throws IOException {
// Setup command object.
STATE.set(new AdminCommandState());
Properties pravegaProperties = new Properties();
pravegaProperties.setProperty("pravegaservice.container.count", "1");
pravegaProperties.setProperty("pravegaservice.clusterName", "pravega0");
STATE.get().getConfigBuilder().include(pravegaProperties);
CommandArgs args = new CommandArgs(List.of("0"), STATE.get());
DurableDataLogRepairCommand command = new DurableDataLogRepairCommand(args);
AssertExtensions.assertThrows("Different beforeCommitCalls and commitSuccessCalls should have thrown an assertion error.", () -> command.checkBackupLogAssertions(1, 0, 1, false), t -> t instanceof IllegalStateException);
AssertExtensions.assertThrows("Different beforeCommitCalls and commitSuccessCalls should have thrown an assertion error.", () -> command.checkBackupLogAssertions(0, 1, 1, false), t -> t instanceof IllegalStateException);
AssertExtensions.assertThrows("Different commitSuccessCalls and originalReads from Original Log should have thrown an assertion error.", () -> command.checkBackupLogAssertions(1, 1, 2, false), t -> t instanceof IllegalStateException);
AssertExtensions.assertThrows("Not successful BackupLogProcessor execution should have thrown an assertion error..", () -> command.checkBackupLogAssertions(1, 1, 1, true), t -> t instanceof IllegalStateException);
}
use of io.pravega.cli.admin.CommandArgs in project pravega by pravega.
the class DataRecoveryTest method testRepairLogEditOperationsWithContent.
@Test
public void testRepairLogEditOperationsWithContent() throws IOException {
// Setup command object.
STATE.set(new AdminCommandState());
Properties pravegaProperties = new Properties();
pravegaProperties.setProperty("pravegaservice.container.count", "1");
pravegaProperties.setProperty("pravegaservice.clusterName", "pravega0");
STATE.get().getConfigBuilder().include(pravegaProperties);
CommandArgs args = new CommandArgs(List.of("0"), STATE.get());
DurableDataLogRepairCommand command = Mockito.spy(new DurableDataLogRepairCommand(args));
List<DurableDataLogRepairCommand.LogEditOperation> editOps = new ArrayList<>();
// Case 1: Input Add Edit Operations for a MetadataCheckpointOperation and StorageMetadataCheckpointOperation operations
// with payload operation with zeros as content.
Mockito.doReturn(true).doReturn(true).doReturn(false).when(command).confirmContinue();
Mockito.doReturn(1L).when(command).getLongUserInput(Mockito.any());
Mockito.doReturn(100).when(command).getIntUserInput(Mockito.any());
Mockito.doReturn("add").doReturn("MetadataCheckpointOperation").doReturn("zero").doReturn("StorageMetadataCheckpointOperation").doReturn("zero").when(command).getStringUserInput(Mockito.any());
MetadataCheckpointOperation metadataCheckpointOperation = new MetadataCheckpointOperation();
metadataCheckpointOperation.setContents(new ByteArraySegment(new byte[100]));
StorageMetadataCheckpointOperation storageMetadataCheckpointOperation = new StorageMetadataCheckpointOperation();
storageMetadataCheckpointOperation.setContents(new ByteArraySegment(new byte[100]));
editOps.add(new DurableDataLogRepairCommand.LogEditOperation(DurableDataLogRepairCommand.LogEditType.ADD_OPERATION, 1, 1, metadataCheckpointOperation));
editOps.add(new DurableDataLogRepairCommand.LogEditOperation(DurableDataLogRepairCommand.LogEditType.ADD_OPERATION, 1, 1, storageMetadataCheckpointOperation));
Assert.assertEquals(editOps, command.getDurableLogEditsFromUser());
// Case 2: Input an Add Edit Operation for a StreamSegmentAppendOperation with content loaded from a file.
editOps.clear();
byte[] content = new byte[] { 1, 2, 3, 4, 5 };
File tmpFile = File.createTempFile("operationContent", "bin");
Files.write(tmpFile.toPath(), content);
Mockito.doReturn(true).doReturn(false).when(command).confirmContinue();
Mockito.doReturn(1L).when(command).getLongUserInput(Mockito.any());
Mockito.doReturn(1).doReturn(10).when(command).getIntUserInput(Mockito.any());
Mockito.doReturn("wrong").doReturn("add").doReturn("StreamSegmentAppendOperation").doReturn("file").doReturn(tmpFile.toString()).when(command).getStringUserInput(Mockito.any());
StreamSegmentAppendOperation appendOperation = new StreamSegmentAppendOperation(1, 20, new ByteArraySegment(content), new AttributeUpdateCollection());
editOps.add(new DurableDataLogRepairCommand.LogEditOperation(DurableDataLogRepairCommand.LogEditType.ADD_OPERATION, 1, 1, appendOperation));
Assert.assertEquals(editOps, command.getDurableLogEditsFromUser());
Files.delete(tmpFile.toPath());
// Case 3: Abort content generation.
Mockito.doReturn("quit").when(command).getStringUserInput(Mockito.any());
AssertExtensions.assertThrows("", command::createOperationContents, ex -> ex instanceof RuntimeException);
}
use of io.pravega.cli.admin.CommandArgs in project pravega by pravega.
the class DataRecoveryTest method testUserInputMethods.
@Test
public void testUserInputMethods() throws Exception {
// Setup command object.
STATE.set(new AdminCommandState());
Properties pravegaProperties = new Properties();
pravegaProperties.setProperty("pravegaservice.container.count", "1");
pravegaProperties.setProperty("pravegaservice.clusterName", "pravega0");
STATE.get().getConfigBuilder().include(pravegaProperties);
CommandArgs args = new CommandArgs(List.of("0"), STATE.get());
DurableDataLogRepairCommand command = new DurableDataLogRepairCommand(args);
System.setIn(new ByteArrayInputStream("true".getBytes()));
Assert.assertTrue(command.getBooleanUserInput("Test message"));
System.setIn(new ByteArrayInputStream("yes".getBytes()));
Assert.assertEquals("yes", command.getStringUserInput("Test message"));
System.setIn(new ByteArrayInputStream("1".getBytes()));
Assert.assertEquals(1, command.getIntUserInput("Test message"));
System.setIn(new ByteArrayInputStream("2".getBytes()));
Assert.assertEquals(2L, command.getLongUserInput("Test message"));
}
Aggregations