Skip to content

Commit

Permalink
[Tests] Use mock storage in repository-gcs unit tests (#29397)
Browse files Browse the repository at this point in the history
The repository-gcs unit tests rely on the GoogleCloudStorageTestServer
but it would be better if they rely on a mocked Storage client instead.

That would also help to extract the GoogleCloudStorageFixture and the
GoogleCloudStorageTestServer classes in a QA third party project.

Closes #28960
  • Loading branch information
tlrx authored Apr 6, 2018
1 parent 451a328 commit 7d29087
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
import org.elasticsearch.repositories.ESBlobStoreContainerTestCase;

import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;

public class GoogleCloudStorageBlobStoreContainerTests extends ESBlobStoreContainerTestCase {

@Override
protected BlobStore newBlobStore() {
String bucket = randomAlphaOfLength(randomIntBetween(1, 10)).toLowerCase(Locale.ROOT);
return new GoogleCloudStorageBlobStore(Settings.EMPTY, bucket, MockStorage.newStorageClient(bucket, getTestName()));
return new GoogleCloudStorageBlobStore(Settings.EMPTY, bucket, new MockStorage(bucket, new ConcurrentHashMap<>()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,23 @@
import org.elasticsearch.env.Environment;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.repositories.blobstore.ESBlobStoreRepositoryIntegTestCase;
import org.junit.BeforeClass;
import org.junit.AfterClass;

import java.net.SocketPermission;
import java.security.AccessController;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;

public class GoogleCloudStorageBlobStoreRepositoryTests extends ESBlobStoreRepositoryIntegTestCase {

private static final String BUCKET = "gcs-repository-test";

// Static storage client shared among all nodes in order to act like a remote repository service:
// Static list of blobs shared among all nodes in order to act like a remote repository service:
// all nodes must see the same content
private static final AtomicReference<Storage> storage = new AtomicReference<>();
private static final ConcurrentMap<String, byte[]> blobs = new ConcurrentHashMap<>();

@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
Expand All @@ -62,15 +61,17 @@ protected void createTestRepository(String name) {
.put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
}

@BeforeClass
public static void setUpStorage() {
storage.set(MockStorage.newStorageClient(BUCKET, GoogleCloudStorageBlobStoreRepositoryTests.class.getName()));
@AfterClass
public static void wipeRepository() {
blobs.clear();
}

public static class MockGoogleCloudStoragePlugin extends GoogleCloudStoragePlugin {

public MockGoogleCloudStoragePlugin(final Settings settings) {
super(settings);
}

@Override
protected GoogleCloudStorageService createStorageService(Environment environment) {
return new MockGoogleCloudStorageService(environment, getClientsSettings());
Expand All @@ -85,9 +86,7 @@ public static class MockGoogleCloudStorageService extends GoogleCloudStorageServ

@Override
public Storage createClient(String clientName) {
// The actual impl might open a connection. So check we have permission when this call is made.
AccessController.checkPermission(new SocketPermission("*", "connect"));
return storage.get();
return new MockStorage(BUCKET, blobs);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
import org.elasticsearch.repositories.ESBlobStoreTestCase;

import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;

public class GoogleCloudStorageBlobStoreTests extends ESBlobStoreTestCase {

@Override
protected BlobStore newBlobStore() {
String bucket = randomAlphaOfLength(randomIntBetween(1, 10)).toLowerCase(Locale.ROOT);
return new GoogleCloudStorageBlobStore(Settings.EMPTY, bucket, MockStorage.newStorageClient(bucket, getTestName()));
return new GoogleCloudStorageBlobStore(Settings.EMPTY, bucket, new MockStorage(bucket, new ConcurrentHashMap<>()));
}
}
Loading

0 comments on commit 7d29087

Please sign in to comment.