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());
}
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);
}
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());
}
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);
}
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"));
}
Aggregations