-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: moved BlockWriter tests into their own file
Signed-off-by: Matt Peterson <[email protected]>
- Loading branch information
1 parent
d78eebe
commit 23106d3
Showing
3 changed files
with
231 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
server/src/test/java/com/hedera/block/server/persistence/storage/BlockAsDirReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.