Search in sources :

Example 11 with CommandArgs

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);
}
Also used : CommandArgs(io.pravega.cli.admin.CommandArgs) PrintStream(java.io.PrintStream) AdminCommand(io.pravega.cli.admin.AdminCommand) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Parser(io.pravega.cli.admin.Parser)

Example 12 with CommandArgs

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());
}
Also used : CommandArgs(io.pravega.cli.admin.CommandArgs) ImmutableDate(io.pravega.common.util.ImmutableDate) HashMap(java.util.HashMap) AttributeId(io.pravega.segmentstore.contracts.AttributeId) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) Properties(java.util.Properties) UUID(java.util.UUID) AdminCommandState(io.pravega.cli.admin.AdminCommandState) Test(org.junit.Test)

Example 13 with CommandArgs

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);
}
Also used : CommandArgs(io.pravega.cli.admin.CommandArgs) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) Properties(java.util.Properties) AdminCommandState(io.pravega.cli.admin.AdminCommandState) Test(org.junit.Test)

Example 14 with CommandArgs

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);
}
Also used : CommandArgs(io.pravega.cli.admin.CommandArgs) AttributeUpdateCollection(io.pravega.segmentstore.contracts.AttributeUpdateCollection) StorageMetadataCheckpointOperation(io.pravega.segmentstore.server.logs.operations.StorageMetadataCheckpointOperation) MetadataCheckpointOperation(io.pravega.segmentstore.server.logs.operations.MetadataCheckpointOperation) ByteArraySegment(io.pravega.common.util.ByteArraySegment) CompositeByteArraySegment(io.pravega.common.util.CompositeByteArraySegment) ArrayList(java.util.ArrayList) StorageMetadataCheckpointOperation(io.pravega.segmentstore.server.logs.operations.StorageMetadataCheckpointOperation) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) Properties(java.util.Properties) StreamSegmentAppendOperation(io.pravega.segmentstore.server.logs.operations.StreamSegmentAppendOperation) AdminCommandState(io.pravega.cli.admin.AdminCommandState) File(java.io.File) Test(org.junit.Test)

Example 15 with CommandArgs

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"));
}
Also used : CommandArgs(io.pravega.cli.admin.CommandArgs) ByteArrayInputStream(java.io.ByteArrayInputStream) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) Properties(java.util.Properties) AdminCommandState(io.pravega.cli.admin.AdminCommandState) Test(org.junit.Test)

Aggregations

CommandArgs (io.pravega.cli.admin.CommandArgs)18 Test (org.junit.Test)16 Properties (java.util.Properties)12 AdminCommandState (io.pravega.cli.admin.AdminCommandState)11 SegmentProperties (io.pravega.segmentstore.contracts.SegmentProperties)10 Cleanup (lombok.Cleanup)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ArrayList (java.util.ArrayList)4 lombok.val (lombok.val)4 TestUtils (io.pravega.cli.admin.utils.TestUtils)3 AttributeUpdateCollection (io.pravega.segmentstore.contracts.AttributeUpdateCollection)3 DeleteSegmentOperation (io.pravega.segmentstore.server.logs.operations.DeleteSegmentOperation)3 BookKeeperLogFactory (io.pravega.segmentstore.storage.impl.bookkeeper.BookKeeperLogFactory)3 UUID (java.util.UUID)3 Parser (io.pravega.cli.admin.Parser)2 CompositeByteArraySegment (io.pravega.common.util.CompositeByteArraySegment)2 ImmutableDate (io.pravega.common.util.ImmutableDate)2 AttributeId (io.pravega.segmentstore.contracts.AttributeId)2 AttributeUpdate (io.pravega.segmentstore.contracts.AttributeUpdate)2 DataCorruptionException (io.pravega.segmentstore.server.DataCorruptionException)2