Enable gcloud testing in docker (#5651)

* Enable "gcloud" profile for gcloud session testing.

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>

* use docker image as google datastore

Signed-off-by: olivier lamy <oliver.lamy@gmail.com>

* fix project id

Signed-off-by: olivier lamy <oliver.lamy@gmail.com>

* all tests pass here

Signed-off-by: olivier lamy <oliver.lamy@gmail.com>

* do not trim stack trace

Signed-off-by: olivier lamy <oliver.lamy@gmail.com>

* Try distinguishing between localhost and non-localhost

Signed-off-by: Jan Bartel <janb@webtide.com>

* add some debug

Signed-off-by: olivier lamy <oliver.lamy@gmail.com>

* remove non needed pom content

Signed-off-by: olivier lamy <oliver.lamy@gmail.com>

Co-authored-by: Jan Bartel <janb@webtide.com>
This commit is contained in:
Olivier Lamy 2020-11-17 11:13:22 +10:00 committed by GitHub
parent 7f86f5e9ad
commit 213ac63d2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 95 additions and 25 deletions

6
Jenkinsfile vendored
View File

@ -12,7 +12,7 @@ pipeline {
steps {
container('jetty-build') {
timeout( time: 120, unit: 'MINUTES' ) {
mavenBuild( "jdk8", "clean install -T3 -Premote-session-tests", "maven3",
mavenBuild( "jdk8", "clean install -T3 -Premote-session-tests -Pgcloud", "maven3",
[[parserName: 'Maven'], [parserName: 'Java']])
// Collect up the jacoco execution results (only on main build)
jacoco inclusionPattern: '**/org/eclipse/jetty/**/*.class',
@ -42,7 +42,7 @@ pipeline {
steps {
container( 'jetty-build' ) {
timeout( time: 120, unit: 'MINUTES' ) {
mavenBuild( "jdk11", "clean install -T3 -Djacoco.skip=true -Premote-session-tests", "maven3",
mavenBuild( "jdk11", "clean install -T3 -Djacoco.skip=true -Premote-session-tests -Pgcloud", "maven3",
[[parserName: 'Maven'], [parserName: 'Java']])
}
}
@ -54,7 +54,7 @@ pipeline {
steps {
container( 'jetty-build' ) {
timeout( time: 120, unit: 'MINUTES' ) {
mavenBuild( "jdk15", "clean install -T3 -Djacoco.skip=true -Premote-session-tests", "maven3",
mavenBuild( "jdk15", "clean install -T3 -Djacoco.skip=true -Premote-session-tests -Pgcloud", "maven3",
[[parserName: 'Maven'], [parserName: 'Java']])
}
}

View File

@ -61,6 +61,22 @@
<artifactId>jetty-test-helper</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>gcloud</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
@ -78,15 +94,10 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>false</skipTests>
<systemPropertyVariables>
<DATASTORE_DATASET>jetty9-work</DATASTORE_DATASET>
<DATASTORE_HOST>http://localhost:8088</DATASTORE_HOST>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@ -20,12 +20,17 @@ package org.eclipse.jetty.gcloud.session;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.cloud.NoCredentials;
import com.google.cloud.ServiceOptions;
import com.google.cloud.datastore.Batch;
import com.google.cloud.datastore.Blob;
import com.google.cloud.datastore.BlobValue;
import com.google.cloud.datastore.Datastore;
@ -38,13 +43,17 @@ import com.google.cloud.datastore.Query;
import com.google.cloud.datastore.Query.ResultType;
import com.google.cloud.datastore.QueryResults;
import com.google.cloud.datastore.StructuredQuery.PropertyFilter;
import com.google.cloud.datastore.testing.LocalDatastoreHelper;
import org.eclipse.jetty.gcloud.session.GCloudSessionDataStore.EntityDataModel;
import org.eclipse.jetty.server.session.SessionData;
import org.eclipse.jetty.server.session.SessionDataStore;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.util.ClassLoadingObjectInputStream;
import org.threeten.bp.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.DatastoreEmulatorContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@ -55,10 +64,35 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
*/
public class GCloudSessionTestSupport
{
LocalDatastoreHelper _helper = LocalDatastoreHelper.create(1.0);
Datastore _ds;
KeyFactory _keyFactory;
private static final Logger LOGGER = LoggerFactory.getLogger(GCloudSessionTestSupport.class);
private static final Logger GCLOUD_LOG = LoggerFactory.getLogger("org.eclipse.jetty.gcloud.session.gcloudLogs");
public DatastoreEmulatorContainer emulator = new CustomDatastoreEmulatorContainer(
DockerImageName.parse("gcr.io/google.com/cloudsdktool/cloud-sdk:316.0.0-emulators")
).withLogConsumer(new Slf4jLogConsumer(GCLOUD_LOG));
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("gcr.io/google.com/cloudsdktool/cloud-sdk");
private static final String CMD = "gcloud beta emulators datastore start --project test-project --host-port 0.0.0.0:8081 --consistency=1.0";
private static final int HTTP_PORT = 8081;
public static class CustomDatastoreEmulatorContainer extends DatastoreEmulatorContainer
{
public CustomDatastoreEmulatorContainer(DockerImageName dockerImageName)
{
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
withExposedPorts(HTTP_PORT);
setWaitStrategy(Wait.forHttp("/").forStatusCode(200));
withCommand("/bin/sh", "-c", CMD);
}
}
public static class TestGCloudSessionDataStoreFactory extends GCloudSessionDataStoreFactory
{
Datastore _d;
@ -88,15 +122,35 @@ public class GCloudSessionTestSupport
public GCloudSessionTestSupport()
{
DatastoreOptions options = _helper.getOptions();
_ds = options.getService();
_keyFactory = _ds.newKeyFactory().setKind(EntityDataModel.KIND);
// no op
}
public void setUp()
throws Exception
{
_helper.start();
emulator.start();
String host;
//work out if we're running locally or not: if not local, then the host passed to
//DatastoreOptions must be prefixed with a scheme
String endPoint = emulator.getEmulatorEndpoint();
InetAddress hostAddr = InetAddress.getByName(new URL("http://" + endPoint).getHost());
LOGGER.info("endPoint: {} ,hostAddr.isAnyLocalAddress(): {},hostAddr.isLoopbackAddress(): {}",
endPoint,
hostAddr.isAnyLocalAddress(),
hostAddr.isLoopbackAddress());
if (hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress())
host = endPoint;
else
host = "http://" + endPoint;
DatastoreOptions options = DatastoreOptions.newBuilder()
.setHost(host)
.setCredentials(NoCredentials.getInstance())
.setRetrySettings(ServiceOptions.getNoRetrySettings())
.setProjectId("test-project")
.build();
_ds = options.getService();
_keyFactory = _ds.newKeyFactory().setKind(EntityDataModel.KIND);
}
public Datastore getDatastore()
@ -107,12 +161,13 @@ public class GCloudSessionTestSupport
public void tearDown()
throws Exception
{
_helper.stop(Duration.ofMinutes(1)); //wait up to 1min for shutdown
emulator.stop();
}
public void reset() throws Exception
{
_helper.reset();
emulator.stop();
this.setUp();
}
public void createSession(String id, String contextPath, String vhost,
@ -260,12 +315,14 @@ public class GCloudSessionTestSupport
QueryResults<Key> results = _ds.run(query);
assertNotNull(results);
int actual = 0;
List<Key> keys = new ArrayList<>();
while (results.hasNext())
{
results.next();
Key key = results.next();
keys.add(key);
++actual;
}
assertEquals(count, actual);
assertEquals(count, actual, "keys found: " + keys);
}
public void deleteSessions() throws Exception
@ -273,18 +330,21 @@ public class GCloudSessionTestSupport
Query<Key> query = Query.newKeyQueryBuilder().setKind(GCloudSessionDataStore.EntityDataModel.KIND).build();
QueryResults<Key> results = _ds.run(query);
Batch batch = _ds.newBatch();
if (results != null)
{
List<Key> keys = new ArrayList<Key>();
List<Key> keys = new ArrayList<>();
while (results.hasNext())
{
keys.add(results.next());
}
_ds.delete(keys.toArray(new Key[keys.size()]));
batch.delete(keys.toArray(new Key[keys.size()]));
}
batch.submit();
assertSessions(0);
}
}

View File

@ -0,0 +1,3 @@
org.slf4j.simpleLogger.defaultLogLevel=info
org.slf4j.simpleLogger.log.org.eclipse.jetty.gcloud.session=info
org.slf4j.simpleLogger.log.org.eclipse.jetty.gcloud.session.gcloudLogs=info

View File

@ -25,8 +25,6 @@ import java.nio.file.Path;
import java.util.Properties;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.eclipse.jetty.util.IO;
import org.hibernate.search.cfg.Environment;
import org.hibernate.search.cfg.SearchMapping;
@ -37,7 +35,6 @@ import org.infinispan.configuration.cache.Index;
import org.infinispan.configuration.global.GlobalConfigurationBuilder;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

View File

@ -42,7 +42,6 @@ import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
import org.testcontainers.utility.MountableFile;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;