parent
3955c284f5
commit
e28915bd4b
@ -0,0 +1,87 @@
|
||||
sanitize_cgroups() {
|
||||
mkdir -p /sys/fs/cgroup
|
||||
mountpoint -q /sys/fs/cgroup || \
|
||||
mount -t tmpfs -o uid=0,gid=0,mode=0755 cgroup /sys/fs/cgroup
|
||||
|
||||
mount -o remount,rw /sys/fs/cgroup
|
||||
|
||||
sed -e 1d /proc/cgroups | while read sys hierarchy num enabled; do
|
||||
if [ "$enabled" != "1" ]; then
|
||||
# subsystem disabled; skip
|
||||
continue
|
||||
fi
|
||||
|
||||
grouping="$(cat /proc/self/cgroup | cut -d: -f2 | grep "\\<$sys\\>")"
|
||||
if [ -z "$grouping" ]; then
|
||||
# subsystem not mounted anywhere; mount it on its own
|
||||
grouping="$sys"
|
||||
fi
|
||||
|
||||
mountpoint="/sys/fs/cgroup/$grouping"
|
||||
|
||||
mkdir -p "$mountpoint"
|
||||
|
||||
# clear out existing mount to make sure new one is read-write
|
||||
if mountpoint -q "$mountpoint"; then
|
||||
umount "$mountpoint"
|
||||
fi
|
||||
|
||||
mount -n -t cgroup -o "$grouping" cgroup "$mountpoint"
|
||||
|
||||
if [ "$grouping" != "$sys" ]; then
|
||||
if [ -L "/sys/fs/cgroup/$sys" ]; then
|
||||
rm "/sys/fs/cgroup/$sys"
|
||||
fi
|
||||
|
||||
ln -s "$mountpoint" "/sys/fs/cgroup/$sys"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
start_docker() {
|
||||
mkdir -p /var/log
|
||||
mkdir -p /var/run
|
||||
|
||||
sanitize_cgroups
|
||||
|
||||
# check for /proc/sys being mounted readonly, as systemd does
|
||||
if grep '/proc/sys\s\+\w\+\s\+ro,' /proc/mounts >/dev/null; then
|
||||
mount -o remount,rw /proc/sys
|
||||
fi
|
||||
|
||||
local server_args=""
|
||||
|
||||
for registry in $1; do
|
||||
server_args="${server_args} --insecure-registry ${registry}"
|
||||
done
|
||||
|
||||
if [ -n "$2" ]; then
|
||||
server_args="${server_args} --registry-mirror=$2"
|
||||
fi
|
||||
|
||||
if [ -n "$3" ]; then
|
||||
server_args="${server_args} -g=$3"
|
||||
fi
|
||||
|
||||
docker daemon --data-root /scratch/docker ${server_args} >/tmp/docker.log 2>&1 &
|
||||
echo $! > /tmp/docker.pid
|
||||
|
||||
trap stop_docker EXIT
|
||||
|
||||
sleep 1
|
||||
|
||||
until docker info >/dev/null 2>&1; do
|
||||
echo waiting for docker to come up...
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
|
||||
stop_docker() {
|
||||
local pid=$(cat /tmp/docker.pid)
|
||||
if [ -z "$pid" ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
kill -TERM $pid
|
||||
wait $pid
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.junit.AssumptionViolatedException;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
import org.testcontainers.DockerClientFactory;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
|
||||
/**
|
||||
* {@link TestRule} for working with an optional Docker environment. Spins up a {@link GenericContainer}
|
||||
* if a valid docker environment is found.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
public class DockerTestContainer<T extends GenericContainer> implements TestRule {
|
||||
|
||||
private Supplier<T> containerSupplier;
|
||||
|
||||
public DockerTestContainer(Supplier<T> containerSupplier) {
|
||||
this.containerSupplier = containerSupplier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
try {
|
||||
DockerClientFactory.instance().client();
|
||||
return this.containerSupplier.get().apply(base, description);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
return new SkipStatement();
|
||||
}
|
||||
}
|
||||
|
||||
private static class SkipStatement extends Statement {
|
||||
|
||||
@Override
|
||||
public void evaluate() {
|
||||
throw new AssumptionViolatedException(
|
||||
"Could not find a valid Docker environment.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,100 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.data.cassandra;
|
||||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Assume;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
/**
|
||||
* {@link TestRule} for working with an optional Cassandra server.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class CassandraTestServer implements TestRule {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(CassandraTestServer.class);
|
||||
|
||||
private Cluster cluster;
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
try {
|
||||
this.cluster = newCluster();
|
||||
return new CassandraStatement(base, this.cluster);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
logger.error("No Cassandra server available", ex);
|
||||
return new SkipStatement();
|
||||
}
|
||||
}
|
||||
|
||||
private Cluster newCluster() {
|
||||
Cluster cluster = Cluster.builder().addContactPoint("localhost").build();
|
||||
testCluster(cluster);
|
||||
return cluster;
|
||||
}
|
||||
|
||||
private void testCluster(Cluster cluster) {
|
||||
cluster.connect().close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the cluster if any
|
||||
*/
|
||||
public Cluster getCluster() {
|
||||
return this.cluster;
|
||||
}
|
||||
|
||||
private static class CassandraStatement extends Statement {
|
||||
|
||||
private final Statement base;
|
||||
|
||||
private final Cluster cluster;
|
||||
|
||||
CassandraStatement(Statement base, Cluster cluster) {
|
||||
this.base = base;
|
||||
this.cluster = cluster;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
try {
|
||||
this.base.evaluate();
|
||||
}
|
||||
finally {
|
||||
this.cluster.closeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class SkipStatement extends Statement {
|
||||
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
Assume.assumeTrue("Skipping test due to Cassandra not being available",
|
||||
false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.autoconfigure;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.junit.AssumptionViolatedException;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
import org.testcontainers.DockerClientFactory;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
|
||||
/**
|
||||
* {@link TestRule} for working with an optional Docker environment. Spins up a {@link GenericContainer}
|
||||
* if a valid docker environment is found.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
public class DockerTestContainer<T extends GenericContainer> implements TestRule {
|
||||
|
||||
private Supplier<T> containerSupplier;
|
||||
|
||||
public DockerTestContainer(Supplier<T> containerSupplier) {
|
||||
this.containerSupplier = containerSupplier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
try {
|
||||
DockerClientFactory.instance().client();
|
||||
return this.containerSupplier.get().apply(base, description);
|
||||
}
|
||||
catch (Throwable thrown) {
|
||||
return new SkipStatement();
|
||||
}
|
||||
}
|
||||
|
||||
private static class SkipStatement extends Statement {
|
||||
|
||||
@Override
|
||||
public void evaluate() {
|
||||
throw new AssumptionViolatedException(
|
||||
"Could not find a valid Docker environment.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,114 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.autoconfigure.data.neo4j;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Assume;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
import org.neo4j.ogm.config.Configuration;
|
||||
import org.neo4j.ogm.session.SessionFactory;
|
||||
|
||||
/**
|
||||
* {@link TestRule} for working with an optional Neo4j server running on localhost. Make
|
||||
* sure to disable authentication if you haven't done so already.
|
||||
*
|
||||
* @author Eddú Meléndez
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class Neo4jTestServer implements TestRule {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(Neo4jTestServer.class);
|
||||
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
private String[] packages;
|
||||
|
||||
public Neo4jTestServer(String[] packages) {
|
||||
this.packages = packages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
try {
|
||||
this.sessionFactory = createSessionFactory();
|
||||
return new Neo4jStatement(base, this.sessionFactory);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
logger.error("No Neo4j server available", ex);
|
||||
return new SkipStatement();
|
||||
}
|
||||
}
|
||||
|
||||
public SessionFactory getSessionFactory() {
|
||||
return this.sessionFactory;
|
||||
}
|
||||
|
||||
private SessionFactory createSessionFactory() {
|
||||
Configuration configuration = new Configuration.Builder()
|
||||
.uri("bolt://localhost:7687").build();
|
||||
SessionFactory sessionFactory = new SessionFactory(configuration, this.packages);
|
||||
testConnection(sessionFactory);
|
||||
return sessionFactory;
|
||||
}
|
||||
|
||||
private void testConnection(SessionFactory sessionFactory) {
|
||||
sessionFactory.openSession().beginTransaction().close();
|
||||
}
|
||||
|
||||
private static class Neo4jStatement extends Statement {
|
||||
|
||||
private final Statement base;
|
||||
|
||||
private final SessionFactory sessionFactory;
|
||||
|
||||
Neo4jStatement(Statement base, SessionFactory sessionFactory) {
|
||||
this.base = base;
|
||||
this.sessionFactory = sessionFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
try {
|
||||
this.base.evaluate();
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
this.sessionFactory.close();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
logger.warn("Exception while trying to cleanup neo4j resource", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class SkipStatement extends Statement {
|
||||
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
Assume.assumeTrue(
|
||||
"Skipping test due to Neo4j SessionFactory" + " not being available",
|
||||
false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,152 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.testsupport.rule;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Assume;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* {@link TestRule} for working with an optional Redis server.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
* @author Gary Russell
|
||||
* @author Dave Syer
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class RedisTestServer implements TestRule {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(RedisTestServer.class);
|
||||
|
||||
private RedisConnectionFactory connectionFactory;
|
||||
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
try {
|
||||
this.connectionFactory = createConnectionFactory();
|
||||
return new RedisStatement(base, this.connectionFactory);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
logger.error("No Redis server available", ex);
|
||||
return new SkipStatement();
|
||||
}
|
||||
}
|
||||
|
||||
private RedisConnectionFactory createConnectionFactory() {
|
||||
ClassLoader classLoader = RedisTestServer.class.getClassLoader();
|
||||
RedisConnectionFactory cf;
|
||||
if (ClassUtils.isPresent("redis.clients.jedis.Jedis", classLoader)) {
|
||||
cf = new JedisConnectionFactoryConfiguration().createConnectionFactory();
|
||||
}
|
||||
else {
|
||||
cf = new LettuceConnectionFactoryConfiguration().createConnectionFactory();
|
||||
}
|
||||
|
||||
testConnection(cf);
|
||||
return cf;
|
||||
}
|
||||
|
||||
private void testConnection(RedisConnectionFactory connectionFactory) {
|
||||
connectionFactory.getConnection().close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Redis connection factory or {@code null} if the factory is not
|
||||
* available.
|
||||
* @return the connection factory or {@code null}
|
||||
*/
|
||||
public RedisConnectionFactory getConnectionFactory() {
|
||||
return this.connectionFactory;
|
||||
}
|
||||
|
||||
private static class RedisStatement extends Statement {
|
||||
|
||||
private final Statement base;
|
||||
|
||||
private final RedisConnectionFactory connectionFactory;
|
||||
|
||||
RedisStatement(Statement base, RedisConnectionFactory connectionFactory) {
|
||||
this.base = base;
|
||||
this.connectionFactory = connectionFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
try {
|
||||
this.base.evaluate();
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
if (this.connectionFactory instanceof DisposableBean) {
|
||||
((DisposableBean) this.connectionFactory).destroy();
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
logger.warn("Exception while trying to cleanup redis resource", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class SkipStatement extends Statement {
|
||||
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
Assume.assumeTrue("Skipping test due to " + "Redis ConnectionFactory"
|
||||
+ " not being available", false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class JedisConnectionFactoryConfiguration {
|
||||
|
||||
RedisConnectionFactory createConnectionFactory() {
|
||||
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
|
||||
connectionFactory.afterPropertiesSet();
|
||||
return connectionFactory;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class LettuceConnectionFactoryConfiguration {
|
||||
|
||||
RedisConnectionFactory createConnectionFactory() {
|
||||
LettuceClientConfiguration config = LettuceClientConfiguration.builder()
|
||||
.shutdownTimeout(Duration.ofMillis(0)).build();
|
||||
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(
|
||||
new RedisStandaloneConfiguration(), config);
|
||||
connectionFactory.afterPropertiesSet();
|
||||
return connectionFactory;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue