|
29 | 29 | import com.google.common.collect.Lists; |
30 | 30 | import com.google.inject.Guice; |
31 | 31 | import com.google.inject.Injector; |
| 32 | +import com.netflix.priam.aws.DataPart; |
32 | 33 | import com.netflix.priam.aws.RemoteBackupPath; |
33 | 34 | import com.netflix.priam.aws.S3FileSystem; |
| 35 | +import com.netflix.priam.aws.S3PartUploader; |
34 | 36 | import com.netflix.priam.backup.AbstractBackupPath.BackupFileType; |
35 | 37 | import com.netflix.priam.compress.CompressionType; |
36 | 38 | import com.netflix.priam.config.IConfiguration; |
@@ -80,6 +82,7 @@ public TestS3FileSystem() { |
80 | 82 |
|
81 | 83 | @BeforeClass |
82 | 84 | public static void setUp() { |
| 85 | + new MockS3PartUploader(); |
83 | 86 | new MockAmazonS3Client(); |
84 | 87 | if (!DIR.exists()) DIR.mkdirs(); |
85 | 88 | } |
@@ -115,6 +118,41 @@ public void testFileUploadDeleteExists() throws Exception { |
115 | 118 | Assert.assertFalse(fs.checkObjectExists(Paths.get(backupfile.getRemotePath()))); |
116 | 119 | } |
117 | 120 |
|
| 121 | + @Test |
| 122 | + public void testFileUploadFailures() throws Exception { |
| 123 | + MockS3PartUploader.setup(); |
| 124 | + MockS3PartUploader.partFailure = true; |
| 125 | + long noOfFailures = backupMetrics.getInvalidUploads().count(); |
| 126 | + S3FileSystem fs = injector.getInstance(S3FileSystem.class); |
| 127 | + RemoteBackupPath backupfile = injector.getInstance(RemoteBackupPath.class); |
| 128 | + backupfile.parseLocal(localFile(), BackupFileType.META_V2); |
| 129 | + try { |
| 130 | + // temporary hack to allow tests to complete in a timely fashion |
| 131 | + // This will be removed once we stop inheriting from AbstractFileSystem |
| 132 | + fs.uploadAndDeleteInternal(backupfile, Instant.EPOCH, 0 /* retries */); |
| 133 | + } catch (BackupRestoreException e) { |
| 134 | + // ignore |
| 135 | + } |
| 136 | + Assert.assertEquals(0, MockS3PartUploader.compattempts); |
| 137 | + Assert.assertEquals(1, backupMetrics.getInvalidUploads().count() - noOfFailures); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testFileUploadCompleteFailure() throws Exception { |
| 142 | + MockS3PartUploader.setup(); |
| 143 | + MockS3PartUploader.completionFailure = true; |
| 144 | + S3FileSystem fs = injector.getInstance(S3FileSystem.class); |
| 145 | + fs.setS3Client(new MockAmazonS3Client().getMockInstance()); |
| 146 | + RemoteBackupPath backupfile = injector.getInstance(RemoteBackupPath.class); |
| 147 | + backupfile.parseLocal(localFile(), BackupFileType.META_V2); |
| 148 | + try { |
| 149 | + // temporary hack to allow tests to complete in a timely fashion |
| 150 | + // This will be removed once we stop inheriting from AbstractFileSystem |
| 151 | + fs.uploadAndDeleteInternal(backupfile, Instant.EPOCH, 0 /* retries */); |
| 152 | + } catch (BackupRestoreException e) { |
| 153 | + // ignore |
| 154 | + } |
| 155 | + } |
118 | 156 |
|
119 | 157 | @Test |
120 | 158 | public void testCleanupAdd() throws Exception { |
@@ -251,6 +289,48 @@ private File localFile() throws IOException { |
251 | 289 | return file; |
252 | 290 | } |
253 | 291 |
|
| 292 | + // Mock S3PartUploader class |
| 293 | + static class MockS3PartUploader extends MockUp<S3PartUploader> { |
| 294 | + static int compattempts = 0; |
| 295 | + static int partAttempts = 0; |
| 296 | + static boolean partFailure = false; |
| 297 | + static boolean completionFailure = false; |
| 298 | + private static List<PartETag> partETags; |
| 299 | + |
| 300 | + @Mock |
| 301 | + public void $init(AmazonS3 client, DataPart dp, List<PartETag> partETags) { |
| 302 | + MockS3PartUploader.partETags = partETags; |
| 303 | + } |
| 304 | + |
| 305 | + @Mock |
| 306 | + private Void uploadPart() throws AmazonClientException, BackupRestoreException { |
| 307 | + ++partAttempts; |
| 308 | + if (partFailure) throw new BackupRestoreException("Test exception"); |
| 309 | + partETags.add(new PartETag(0, null)); |
| 310 | + return null; |
| 311 | + } |
| 312 | + |
| 313 | + @Mock |
| 314 | + public CompleteMultipartUploadResult completeUpload() throws BackupRestoreException { |
| 315 | + ++compattempts; |
| 316 | + if (completionFailure) throw new BackupRestoreException("Test exception"); |
| 317 | + |
| 318 | + return null; |
| 319 | + } |
| 320 | + |
| 321 | + @Mock |
| 322 | + public Void retriableCall() throws AmazonClientException, BackupRestoreException { |
| 323 | + logger.info("MOCK UPLOADING..."); |
| 324 | + return uploadPart(); |
| 325 | + } |
| 326 | + |
| 327 | + public static void setup() { |
| 328 | + compattempts = 0; |
| 329 | + partAttempts = 0; |
| 330 | + partFailure = false; |
| 331 | + completionFailure = false; |
| 332 | + } |
| 333 | + } |
254 | 334 |
|
255 | 335 | static class MockAmazonS3Client extends MockUp<AmazonS3Client> { |
256 | 336 | private boolean ruleAvailable = false; |
|
0 commit comments