@@ -149,42 +149,78 @@ public void testRemoveBlockWritePerms() throws IOException {
149
149
// Change the permissions on the block node root directory
150
150
removeBlockWritePerms (testConfig );
151
151
BlockWriter <BlockItem > blockWriter = new BlockAsDirWriter (JUNIT , testConfig );
152
+
153
+ // The first BlockItem contains a header which will create a new block directory.
154
+ // This will fail here due to the lack of write permissions. Detect the exception
155
+ // thrown.
152
156
assertThrows (AccessDeniedException .class , () -> blockWriter .write (blockItems .get (0 )));
153
157
}
154
158
159
+ @ Test
160
+ public void testRemoveBlockItemWritePerms () throws IOException {
161
+
162
+ final List <BlockItem > blockItems = PersistTestUtils .generateBlockItems (1 );
163
+ final BlockWriter <BlockItem > blockWriter = new BlockAsDirWriter (JUNIT , testConfig );
164
+
165
+ // Writing the header BlockItem will create the block directory.
166
+ blockWriter .write (blockItems .get (0 ));
167
+
168
+ // Change the permissions on the block node root directory
169
+ removeBlockWritePerms (1 , testConfig );
170
+
171
+ // Here, BlockItem writes won't throw an exception.
172
+ // We will rely on a different process to detect the invalid
173
+ // block and replace it.
174
+ for (int i = 2 ; i < blockItems .size (); i ++) {
175
+ blockWriter .write (blockItems .get (1 ));
176
+ }
177
+
178
+ // Verify only the header block is on the file system
179
+ final BlockReader <Block > blockReader = new BlockAsDirReader (JUNIT , testConfig );
180
+ Optional <Block > blockOpt = blockReader .read (1 );
181
+ assertFalse (blockOpt .isEmpty ());
182
+
183
+ for (int i = 2 ; i < blockItems .size (); i ++) {
184
+ blockOpt = blockReader .read (i );
185
+ assertTrue (blockOpt .isEmpty ());
186
+ }
187
+ }
188
+
155
189
@ Test
156
190
public void testConstructorWithInvalidPath () {
157
- Map <String , String > testProperties = Map .of (JUNIT , "invalid-path" );
158
- ConfigSource testConfigSource = MapConfigSource .builder ().map (testProperties ).build ();
159
- Config testConfig = Config .builder (testConfigSource ).build ();
191
+ final Map <String , String > testProperties = Map .of (JUNIT , "invalid-path" );
192
+ final ConfigSource testConfigSource = MapConfigSource .builder ().map (testProperties ).build ();
193
+ final Config testConfig = Config .builder (testConfigSource ).build ();
160
194
assertThrows (IllegalArgumentException .class , () -> new BlockAsDirWriter (JUNIT , testConfig ));
161
195
}
162
196
163
- private void removeBlockReadPerms (int blockNumber , Config config ) throws IOException {
164
-
197
+ private void removeBlockReadPerms (int blockNumber , final Config config ) throws IOException {
165
198
final Path blockNodeRootPath = Path .of (config .get (JUNIT ).asString ().get ());
166
199
final Path blockPath = blockNodeRootPath .resolve (String .valueOf (blockNumber ));
167
-
168
- Set <PosixFilePermission > perms = PosixFilePermissions .fromString (NO_READ );
200
+ final Set <PosixFilePermission > perms = PosixFilePermissions .fromString (NO_READ );
169
201
Files .setPosixFilePermissions (blockPath , perms );
170
202
}
171
203
172
- private void removeBlockWritePerms (Config config ) throws IOException {
173
-
204
+ private void removeBlockWritePerms (final Config config ) throws IOException {
174
205
final Path blockNodeRootPath = Path .of (config .get (JUNIT ).asString ().get ());
175
-
176
- Set <PosixFilePermission > perms = PosixFilePermissions .fromString (NO_WRITE );
206
+ final Set <PosixFilePermission > perms = PosixFilePermissions .fromString (NO_WRITE );
177
207
Files .setPosixFilePermissions (blockNodeRootPath , perms );
178
208
}
179
209
180
- private void removeBlockItemReadPerms ( int blockNumber , int blockItem , Config config )
210
+ private void removeBlockWritePerms ( final int blockNumber , final Config config )
181
211
throws IOException {
212
+ final Path blockNodeRootPath = Path .of (config .get (JUNIT ).asString ().get ());
213
+ final Path blockPath = blockNodeRootPath .resolve (String .valueOf (blockNumber ));
214
+ final Set <PosixFilePermission > perms = PosixFilePermissions .fromString (NO_WRITE );
215
+ Files .setPosixFilePermissions (blockPath , perms );
216
+ }
182
217
218
+ private void removeBlockItemReadPerms (int blockNumber , int blockItem , Config config )
219
+ throws IOException {
183
220
final Path blockNodeRootPath = Path .of (config .get (JUNIT ).asString ().get ());
184
221
final Path blockPath = blockNodeRootPath .resolve (String .valueOf (blockNumber ));
185
222
final Path blockItemPath = blockPath .resolve (blockItem + BLOCK_FILE_EXTENSION );
186
-
187
- Set <PosixFilePermission > perms = PosixFilePermissions .fromString (NO_READ );
223
+ final Set <PosixFilePermission > perms = PosixFilePermissions .fromString (NO_READ );
188
224
Files .setPosixFilePermissions (blockItemPath , perms );
189
225
}
190
226
}
0 commit comments