From 30ab5793ffd5549320cb2226fc3030347682d88d Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Mon, 6 Jan 2020 17:52:20 +0100 Subject: [PATCH] Fix GCS Mock Broken Handling of some Blobs (#50666) * Fix GCS Mock Broken Handling of some Blobs We were incorrectly handling blobs starting in `\r\n` which broke tests randomly when blobs started on these. Relates #49429 --- .../java/fixture/gcs/GoogleCloudStorageHttpHandler.java | 4 +++- .../blobstore/ESBlobStoreRepositoryIntegTestCase.java | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/test/fixtures/gcs-fixture/src/main/java/fixture/gcs/GoogleCloudStorageHttpHandler.java b/test/fixtures/gcs-fixture/src/main/java/fixture/gcs/GoogleCloudStorageHttpHandler.java index aba410d945ba7..2aadf7a1c886d 100644 --- a/test/fixtures/gcs-fixture/src/main/java/fixture/gcs/GoogleCloudStorageHttpHandler.java +++ b/test/fixtures/gcs-fixture/src/main/java/fixture/gcs/GoogleCloudStorageHttpHandler.java @@ -271,6 +271,7 @@ public byte[] toByteArray() { return buf; } }; + boolean skippedEmptyLine = false; while ((read = in.read()) != -1) { out.reset(); boolean markAndContinue = false; @@ -290,7 +291,7 @@ public byte[] toByteArray() { } while ((read = in.read()) != -1); final String bucketPrefix = "{\"bucket\":"; final String start = new String(out.toByteArray(), 0, Math.min(out.size(), bucketPrefix.length()), UTF_8); - if (start.length() == 0 || start.equals("\r\n") || start.startsWith("--") + if ((skippedEmptyLine == false && start.length() == 0) || start.startsWith("--") || start.toLowerCase(Locale.ROOT).startsWith("content")) { markAndContinue = true; } else if (start.startsWith(bucketPrefix)) { @@ -302,6 +303,7 @@ public byte[] toByteArray() { } } if (markAndContinue) { + skippedEmptyLine = start.length() == 0; in.mark(Integer.MAX_VALUE); continue; } diff --git a/test/framework/src/main/java/org/elasticsearch/repositories/blobstore/ESBlobStoreRepositoryIntegTestCase.java b/test/framework/src/main/java/org/elasticsearch/repositories/blobstore/ESBlobStoreRepositoryIntegTestCase.java index c41785afe5042..db8100f9897a4 100644 --- a/test/framework/src/main/java/org/elasticsearch/repositories/blobstore/ESBlobStoreRepositoryIntegTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/repositories/blobstore/ESBlobStoreRepositoryIntegTestCase.java @@ -132,7 +132,11 @@ public void testWriteRead() throws IOException { byte[] buffer = new byte[scaledRandomIntBetween(1, data.length - target.length())]; int offset = scaledRandomIntBetween(0, buffer.length - 1); int read = stream.read(buffer, offset, buffer.length - offset); - target.append(new BytesRef(buffer, offset, read)); + if (read >= 0) { + target.append(new BytesRef(buffer, offset, read)); + } else { + fail("Expected [" + (data.length - target.length()) + "] more bytes to be readable but reached EOF"); + } } assertEquals(data.length, target.length()); assertArrayEquals(data, Arrays.copyOfRange(target.bytes(), 0, target.length()));