8
8
9
9
package org .elasticsearch .repositories .blobstore ;
10
10
11
- import org .apache .lucene .tests .mockfile .ExtrasFS ;
12
11
import org .elasticsearch .ElasticsearchException ;
13
12
import org .elasticsearch .action .ActionListener ;
14
13
import org .elasticsearch .action .admin .cluster .snapshots .restore .RestoreSnapshotResponse ;
23
22
import org .elasticsearch .repositories .fs .FsRepository ;
24
23
import org .elasticsearch .snapshots .AbstractSnapshotIntegTestCase ;
25
24
import org .elasticsearch .snapshots .SnapshotState ;
26
- import org .elasticsearch .test .ESTestCase ;
27
25
import org .elasticsearch .test .hamcrest .ElasticsearchAssertions ;
28
26
import org .junit .Before ;
29
27
30
- import java .io .IOException ;
31
- import java .nio .file .FileVisitResult ;
32
- import java .nio .file .Files ;
33
- import java .nio .file .Path ;
34
- import java .nio .file .SimpleFileVisitor ;
35
- import java .nio .file .attribute .BasicFileAttributes ;
36
28
import java .util .ArrayList ;
37
- import java .util .Base64 ;
38
29
import java .util .List ;
39
30
40
31
public class BlobStoreCorruptionIT extends AbstractSnapshotIntegTestCase {
@@ -57,7 +48,7 @@ public void testCorruptionDetection() throws Exception {
57
48
flushAndRefresh (indexName );
58
49
createSnapshot (repositoryName , snapshotName , List .of (indexName ));
59
50
60
- final var corruptedFile = corruptRandomFile (repositoryRootPath );
51
+ final var corruptedFile = BlobStoreCorruptionUtils . corruptRandomFile (repositoryRootPath );
61
52
final var corruptedFileType = RepositoryFileType .getRepositoryFileType (repositoryRootPath , corruptedFile );
62
53
final var corruptionDetectors = new ArrayList <CheckedConsumer <ActionListener <Exception >, ?>>();
63
54
@@ -126,61 +117,4 @@ public void testCorruptionDetection() throws Exception {
126
117
logger .info (Strings .format ("--> corrupted [%s] and caught exception" , corruptedFile ), exception );
127
118
}
128
119
}
129
-
130
- private static Path corruptRandomFile (Path repositoryRootPath ) throws IOException {
131
- final var corruptedFileType = getRandomCorruptibleFileType ();
132
- final var corruptedFile = getRandomFileToCorrupt (repositoryRootPath , corruptedFileType );
133
- if (randomBoolean ()) {
134
- logger .info ("--> deleting [{}]" , corruptedFile );
135
- Files .delete (corruptedFile );
136
- } else {
137
- corruptFileContents (corruptedFile );
138
- }
139
- return corruptedFile ;
140
- }
141
-
142
- private static void corruptFileContents (Path fileToCorrupt ) throws IOException {
143
- final var oldFileContents = Files .readAllBytes (fileToCorrupt );
144
- logger .info ("--> contents of [{}] before corruption: [{}]" , fileToCorrupt , Base64 .getEncoder ().encodeToString (oldFileContents ));
145
- final byte [] newFileContents = new byte [randomBoolean () ? oldFileContents .length : between (0 , oldFileContents .length )];
146
- System .arraycopy (oldFileContents , 0 , newFileContents , 0 , newFileContents .length );
147
- if (newFileContents .length == oldFileContents .length ) {
148
- final var corruptionPosition = between (0 , newFileContents .length - 1 );
149
- newFileContents [corruptionPosition ] = randomValueOtherThan (oldFileContents [corruptionPosition ], ESTestCase ::randomByte );
150
- logger .info (
151
- "--> updating byte at position [{}] from [{}] to [{}]" ,
152
- corruptionPosition ,
153
- oldFileContents [corruptionPosition ],
154
- newFileContents [corruptionPosition ]
155
- );
156
- } else {
157
- logger .info ("--> truncating file from length [{}] to length [{}]" , oldFileContents .length , newFileContents .length );
158
- }
159
- Files .write (fileToCorrupt , newFileContents );
160
- logger .info ("--> contents of [{}] after corruption: [{}]" , fileToCorrupt , Base64 .getEncoder ().encodeToString (newFileContents ));
161
- }
162
-
163
- private static RepositoryFileType getRandomCorruptibleFileType () {
164
- return randomValueOtherThanMany (
165
- // these blob types do not have reliable corruption detection, so we must skip them
166
- t -> t == RepositoryFileType .ROOT_INDEX_N || t == RepositoryFileType .ROOT_INDEX_LATEST ,
167
- () -> randomFrom (RepositoryFileType .values ())
168
- );
169
- }
170
-
171
- private static Path getRandomFileToCorrupt (Path repositoryRootPath , RepositoryFileType corruptedFileType ) throws IOException {
172
- final var corruptibleFiles = new ArrayList <Path >();
173
- Files .walkFileTree (repositoryRootPath , new SimpleFileVisitor <>() {
174
- @ Override
175
- public FileVisitResult visitFile (Path filePath , BasicFileAttributes attrs ) throws IOException {
176
- if (ExtrasFS .isExtra (filePath .getFileName ().toString ()) == false
177
- && RepositoryFileType .getRepositoryFileType (repositoryRootPath , filePath ) == corruptedFileType ) {
178
- corruptibleFiles .add (filePath );
179
- }
180
- return super .visitFile (filePath , attrs );
181
- }
182
- });
183
- return randomFrom (corruptibleFiles );
184
- }
185
-
186
120
}
0 commit comments