Skip to content

Commit

Permalink
fix: moved BlockWriter tests into their own file
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Peterson <[email protected]>
  • Loading branch information
mattp-swirldslabs committed Aug 1, 2024
1 parent d78eebe commit 23106d3
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,12 @@ private void resetState(final BlockItem blockItem) throws IOException {
}

private void repairPermissions(final Path path) throws IOException {
final boolean isReadable = Files.isReadable(path);
final boolean isWritable = Files.isWritable(path);
if (!isWritable || !isReadable) {
if (!isWritable) {
LOGGER.log(
System.Logger.Level.ERROR,
"Block node root directory is not writable. Attempting to change the"
+ " permissions.");
}

if (!isReadable) {
LOGGER.log(
System.Logger.Level.ERROR,
"Block node root directory is not readable. Attempting to change the"
+ " permissions.");
}
if (!isWritable) {
LOGGER.log(
System.Logger.Level.ERROR,
"Block node root directory is not writable. Attempting to change the"
+ " permissions.");

try {
// Attempt to restore the permissions on the block node root directory
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hedera.block.server.persistence.storage;

import static com.hedera.block.protos.BlockStreamService.Block;
import static com.hedera.block.protos.BlockStreamService.BlockItem;
import static com.hedera.block.server.Constants.BLOCK_FILE_EXTENSION;
import static org.junit.jupiter.api.Assertions.*;

import com.hedera.block.server.util.PersistTestUtils;
import com.hedera.block.server.util.TestUtils;
import io.helidon.config.Config;
import io.helidon.config.MapConfigSource;
import io.helidon.config.spi.ConfigSource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class BlockAsDirReaderTest {

private final System.Logger LOGGER = System.getLogger(getClass().getName());

private static final String TEMP_DIR = "block-node-unit-test-dir";
private static final String JUNIT = "my-junit-test";

private Path testPath;
private Config testConfig;

@BeforeEach
public void setUp() throws IOException {
testPath = Files.createTempDirectory(TEMP_DIR);
LOGGER.log(System.Logger.Level.INFO, "Created temp directory: " + testPath.toString());

Map<String, String> testProperties = Map.of(JUNIT, testPath.toString());
ConfigSource testConfigSource = MapConfigSource.builder().map(testProperties).build();
testConfig = Config.builder(testConfigSource).build();
}

@AfterEach
public void tearDown() {
if (!TestUtils.deleteDirectory(testPath.toFile())) {
LOGGER.log(
System.Logger.Level.ERROR,
"Failed to delete temp directory: " + testPath.toString());
}
}

@Test
public void testReadBlockDoesNotExist() throws IOException {
BlockReader<Block> blockReader = new BlockAsDirReader(JUNIT, testConfig);
Optional<Block> blockOpt = blockReader.read(10000);
assertTrue(blockOpt.isEmpty());
}

@Test
public void testReadPermsRepairSucceeded() throws IOException {
List<BlockItem> blockItems = PersistTestUtils.generateBlockItems(1);
BlockWriter<BlockItem> blockWriter = new BlockAsDirWriter(JUNIT, testConfig);
for (BlockItem blockItem : blockItems) {
blockWriter.write(blockItem);
}

// Make the block unreadable
removeBlockReadPerms(1, testConfig);

// The default BlockReader will attempt to repair the permissions and should succeed
BlockReader<Block> blockReader = new BlockAsDirReader(JUNIT, testConfig);
Optional<Block> blockOpt = blockReader.read(1);
assertFalse(blockOpt.isEmpty());
assertEquals(10, blockOpt.get().getBlockItemsList().size());
}

@Test
public void testRemoveBlockReadPermsRepairFailed() throws IOException {
List<BlockItem> blockItems = PersistTestUtils.generateBlockItems(1);
BlockWriter<BlockItem> blockWriter = new BlockAsDirWriter(JUNIT, testConfig);
for (BlockItem blockItem : blockItems) {
blockWriter.write(blockItem);
}

// Make the block unreadable
removeBlockReadPerms(1, testConfig);

// For this test, build the Reader with ineffective repair permissions to
// simulate a failed repair (root changed the perms, etc.)
BlockRemover blockRemover =
new BlockAsDirRemover(
Path.of(testConfig.get(JUNIT).asString().get()), Util.defaultPerms);
BlockReader<Block> blockReader =
new BlockAsDirReader(JUNIT, testConfig, blockRemover, TestUtils.getNoPerms());
Optional<Block> blockOpt = blockReader.read(1);
assertTrue(blockOpt.isEmpty());
}

@Test
public void testRemoveBlockItemReadPerms() throws IOException {
List<BlockItem> blockItems = PersistTestUtils.generateBlockItems(1);
BlockWriter<BlockItem> blockWriter = new BlockAsDirWriter(JUNIT, testConfig);
for (BlockItem blockItem : blockItems) {
blockWriter.write(blockItem);
}

removeBlockItemReadPerms(1, 1, testConfig);

BlockReader<Block> blockReader = new BlockAsDirReader(JUNIT, testConfig);
assertThrows(IOException.class, () -> blockReader.read(1));
}

static void removeBlockReadPerms(int blockNumber, final Config config) throws IOException {
final Path blockNodeRootPath = Path.of(config.get(JUNIT).asString().get());
final Path blockPath = blockNodeRootPath.resolve(String.valueOf(blockNumber));
Files.setPosixFilePermissions(blockPath, TestUtils.getNoRead().value());
}

private void removeBlockItemReadPerms(int blockNumber, int blockItem, Config config)
throws IOException {
final Path blockNodeRootPath = Path.of(config.get(JUNIT).asString().get());
final Path blockPath = blockNodeRootPath.resolve(String.valueOf(blockNumber));
final Path blockItemPath = blockPath.resolve(blockItem + BLOCK_FILE_EXTENSION);
Files.setPosixFilePermissions(blockItemPath, TestUtils.getNoRead().value());
}
}
Loading

0 comments on commit 23106d3

Please sign in to comment.