Use random purpose in blob store repository tests (#102789)

Today many blob store repository tests specify that the operations they
perform have purpose `OperationPurpose#SNAPSHOT`, but most of these
tests do not care about the purpose of these operations. This commit
switches them to using a random purpose to highlight that the purpose is
unimportant to the test.
This commit is contained in:
David Turner 2023-11-29 20:21:07 -08:00 committed by GitHub
parent 7cf32030e5
commit 824d06c8cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 196 additions and 258 deletions

View file

@ -10,7 +10,6 @@ package org.elasticsearch.common.blobstore.url;
import org.elasticsearch.common.UUIDs;
import org.elasticsearch.common.blobstore.BlobContainer;
import org.elasticsearch.common.blobstore.OperationPurpose;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.Streams;
@ -21,6 +20,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.file.NoSuchFileException;
import static org.elasticsearch.repositories.blobstore.BlobStoreTestUtil.randomPurpose;
import static org.hamcrest.core.IsEqual.equalTo;
public abstract class AbstractURLBlobStoreTests extends ESTestCase {
@ -34,7 +34,7 @@ public abstract class AbstractURLBlobStoreTests extends ESTestCase {
BytesArray data = getOriginalData();
String blobName = getBlobName();
BlobContainer container = getBlobContainer();
try (InputStream stream = container.readBlob(OperationPurpose.SNAPSHOT, blobName)) {
try (InputStream stream = container.readBlob(randomPurpose(), blobName)) {
BytesReference bytesRead = Streams.readFully(stream);
assertThat(data, equalTo(bytesRead));
}
@ -46,7 +46,7 @@ public abstract class AbstractURLBlobStoreTests extends ESTestCase {
BlobContainer container = getBlobContainer();
int position = randomIntBetween(0, data.length() - 1);
int length = randomIntBetween(1, data.length() - position);
try (InputStream stream = container.readBlob(OperationPurpose.SNAPSHOT, blobName, position, length)) {
try (InputStream stream = container.readBlob(randomPurpose(), blobName, position, length)) {
BytesReference bytesRead = Streams.readFully(stream);
assertThat(data.slice(position, length), equalTo(bytesRead));
}
@ -55,7 +55,7 @@ public abstract class AbstractURLBlobStoreTests extends ESTestCase {
public void testNoBlobFound() throws IOException {
BlobContainer container = getBlobContainer();
String incorrectBlobName = UUIDs.base64UUID();
try (InputStream ignored = container.readBlob(OperationPurpose.SNAPSHOT, incorrectBlobName)) {
try (InputStream ignored = container.readBlob(randomPurpose(), incorrectBlobName)) {
ignored.read();
fail("Should have thrown NoSuchFileException exception");
} catch (NoSuchFileException e) {

View file

@ -10,7 +10,6 @@ package org.elasticsearch.common.blobstore.url;
import org.elasticsearch.common.blobstore.BlobContainer;
import org.elasticsearch.common.blobstore.BlobPath;
import org.elasticsearch.common.blobstore.OperationPurpose;
import org.elasticsearch.common.blobstore.url.http.URLHttpClient;
import org.elasticsearch.common.blobstore.url.http.URLHttpClientSettings;
import org.elasticsearch.common.bytes.BytesArray;
@ -21,6 +20,7 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.elasticsearch.repositories.blobstore.BlobStoreTestUtil.randomPurpose;
import static org.mockito.Mockito.mock;
public class FileURLBlobStoreTests extends AbstractURLBlobStoreTests {
@ -60,6 +60,6 @@ public class FileURLBlobStoreTests extends AbstractURLBlobStoreTests {
@Override
public void testURLBlobStoreCanReadBlobRange() throws IOException {
expectThrows(UnsupportedOperationException.class, () -> getBlobContainer().readBlob(OperationPurpose.SNAPSHOT, "test", 0, 12));
expectThrows(UnsupportedOperationException.class, () -> getBlobContainer().readBlob(randomPurpose(), "test", 0, 12));
}
}

View file

@ -13,7 +13,6 @@ import com.sun.net.httpserver.HttpServer;
import org.elasticsearch.common.blobstore.BlobContainer;
import org.elasticsearch.common.blobstore.BlobPath;
import org.elasticsearch.common.blobstore.OperationPurpose;
import org.elasticsearch.common.blobstore.url.http.URLHttpClient;
import org.elasticsearch.common.blobstore.url.http.URLHttpClientSettings;
import org.elasticsearch.common.bytes.BytesArray;
@ -36,6 +35,8 @@ import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.elasticsearch.repositories.blobstore.BlobStoreTestUtil.randomPurpose;
@SuppressForbidden(reason = "use http server")
public class HttpURLBlobStoreTests extends AbstractURLBlobStoreTests {
private static final Pattern RANGE_PATTERN = Pattern.compile("bytes=(\\d+)-(\\d+)$");
@ -127,14 +128,8 @@ public class HttpURLBlobStoreTests extends AbstractURLBlobStoreTests {
public void testRangeReadOutsideOfLegalRange() {
BlobContainer container = getBlobContainer();
expectThrows(
IllegalArgumentException.class,
() -> container.readBlob(OperationPurpose.SNAPSHOT, blobName, -1, content.length).read()
);
expectThrows(
IOException.class,
() -> container.readBlob(OperationPurpose.SNAPSHOT, blobName, content.length + 1, content.length).read()
);
expectThrows(IllegalArgumentException.class, () -> container.readBlob(randomPurpose(), blobName, -1, content.length).read());
expectThrows(IOException.class, () -> container.readBlob(randomPurpose(), blobName, content.length + 1, content.length).read());
}
private String getEndpointForServer() {