Search in sources :

Example 16 with AdminCommandState

use of io.pravega.cli.admin.AdminCommandState 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 17 with AdminCommandState

use of io.pravega.cli.admin.AdminCommandState 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 18 with AdminCommandState

use of io.pravega.cli.admin.AdminCommandState in project pravega by pravega.

the class DataRecoveryTest method testListSegmentsCommand.

/**
 * Tests list segments command.
 * @throws Exception    In case of any exception thrown while execution.
 */
@Test
public void testListSegmentsCommand() throws Exception {
    int instanceId = 0;
    int bookieCount = 3;
    int containerCount = 1;
    @Cleanup TestUtils.PravegaRunner pravegaRunner = new TestUtils.PravegaRunner(bookieCount, containerCount);
    pravegaRunner.startBookKeeperRunner(instanceId);
    pravegaRunner.startControllerAndSegmentStore(this.storageFactory, null);
    String streamName = "testListSegmentsCommand";
    TestUtils.createScopeStream(pravegaRunner.getControllerRunner().getController(), SCOPE, streamName, config);
    try (val clientRunner = new TestUtils.ClientRunner(pravegaRunner.getControllerRunner(), SCOPE)) {
        // Write events to the streams.
        TestUtils.writeEvents(streamName, clientRunner.getClientFactory());
    }
    // Shut down the controller
    pravegaRunner.shutDownControllerRunner();
    // Flush all Tier 1 to LTS
    ServiceBuilder.ComponentSetup componentSetup = new ServiceBuilder.ComponentSetup(pravegaRunner.getSegmentStoreRunner().getServiceBuilder());
    for (int containerId = 0; containerId < containerCount; containerId++) {
        componentSetup.getContainerRegistry().getContainer(containerId).flushToStorage(TIMEOUT).join();
    }
    // Shutdown SegmentStore
    pravegaRunner.shutDownSegmentStoreRunner();
    // Shutdown BookKeeper & ZooKeeper
    pravegaRunner.shutDownBookKeeperRunner();
    // set pravega properties for the test
    STATE.set(new AdminCommandState());
    Properties pravegaProperties = new Properties();
    pravegaProperties.setProperty("pravegaservice.container.count", "1");
    pravegaProperties.setProperty("pravegaservice.storage.impl.name", "FILESYSTEM");
    pravegaProperties.setProperty("pravegaservice.storage.layout", "ROLLING_STORAGE");
    pravegaProperties.setProperty("filesystem.root", this.baseDir.getAbsolutePath());
    STATE.get().getConfigBuilder().include(pravegaProperties);
    // Execute the command for list segments
    TestUtils.executeCommand("data-recovery list-segments " + this.logsDir.getAbsolutePath(), STATE.get());
    // There should be a csv file created for storing segments in Container 0
    Assert.assertTrue(new File(this.logsDir.getAbsolutePath(), "Container_0.csv").exists());
    // Check if the file has segments listed in it
    Path path = Paths.get(this.logsDir.getAbsolutePath() + "/Container_0.csv");
    long lines = Files.lines(path).count();
    AssertExtensions.assertGreaterThan("There should be at least one segment.", 1, lines);
    Assert.assertNotNull(StorageListSegmentsCommand.descriptor());
}
Also used : lombok.val(lombok.val) Path(java.nio.file.Path) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) Properties(java.util.Properties) Cleanup(lombok.Cleanup) ServiceBuilder(io.pravega.segmentstore.server.store.ServiceBuilder) TestUtils(io.pravega.cli.admin.utils.TestUtils) AdminCommandState(io.pravega.cli.admin.AdminCommandState) File(java.io.File) Test(org.junit.Test)

Example 19 with AdminCommandState

use of io.pravega.cli.admin.AdminCommandState 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 20 with AdminCommandState

use of io.pravega.cli.admin.AdminCommandState 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

AdminCommandState (io.pravega.cli.admin.AdminCommandState)27 Properties (java.util.Properties)23 Test (org.junit.Test)22 Cleanup (lombok.Cleanup)14 SegmentProperties (io.pravega.segmentstore.contracts.SegmentProperties)13 CommandArgs (io.pravega.cli.admin.CommandArgs)11 lombok.val (lombok.val)9 TestUtils (io.pravega.cli.admin.utils.TestUtils)6 BookKeeperLogFactory (io.pravega.segmentstore.storage.impl.bookkeeper.BookKeeperLogFactory)5 DeleteSegmentOperation (io.pravega.segmentstore.server.logs.operations.DeleteSegmentOperation)4 ClientConfig (io.pravega.client.ClientConfig)3 AttributeUpdateCollection (io.pravega.segmentstore.contracts.AttributeUpdateCollection)3 ServiceBuilder (io.pravega.segmentstore.server.store.ServiceBuilder)3 DefaultCredentials (io.pravega.shared.security.auth.DefaultCredentials)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ArrayList (java.util.ArrayList)3 UUID (java.util.UUID)3 CompositeByteArraySegment (io.pravega.common.util.CompositeByteArraySegment)2 ImmutableDate (io.pravega.common.util.ImmutableDate)2 AttributeId (io.pravega.segmentstore.contracts.AttributeId)2