使用GenericContainer#execInContainer我只能得到stdout或stderr。
有什么方法可以获取已执行命令的退出代码吗?
我不能依靠stderr中文本的存在。我执行的应用程序将一些信息打印到stderr,但是退出并显示代码0。
我在 IDE 中遇到以下错误:
参数化类“GenericContainer”的原始使用检查信息:报告省略类型参数的参数化类的任何使用。参数化类型的这种原始使用在 Java 中是有效的,但违背了使用类型参数的目的,并且可能掩盖错误。
我已经检查过文档,并且创作者也到处使用原始类型: https: //www.testcontainers.org/quickstart/junit_4_quickstart/fe:
@Rule
public GenericContainer redis = new GenericContainer<>("redis:5.0.3-alpine")
.withExposedPorts(6379);
Run Code Online (Sandbox Code Playgroud)
我不明白这种方法。任何人都可以解释我应该如何参数化 GenericContainer<>?
我已经使用 testcontainers 创建了一个 Postgres 实例。容器启动但我无法访问它。
我尝试使用 DBeaver 在容器化数据库上进行连接。在 Eclipse 控制台中,一切似乎都很好:
01:29:34.662 [main] DEBUG com.github.dockerjava.core.command.AbstrDockerCmd - Cmd:com.github.dockerjava.core.command.CreateContainerCmdImpl@73386d72[name=,hostName=,domainName=,user=,attachStdin =,attachStdout=,attachStderr=,portSpecs=,tty=,stdinOpen=,stdInOnce=,env= {POSTGRES_USER=test,POSTGRES_PASSWORD=test,POSTGRES_DB=ASIGDB_TEST}
这是我的代码:
public class CustomPostgresContainer extends PostgreSQLContainer<CustomPostgresContainer>{
private static final String IMAGE_VERSION = "postgres:9.6";
private static CustomPostgresContainer customPostgresContainer;
private static final int EXPOSED_PORT = 5555;
private static final String DB_NAME = "ASIGDB_TEST";
private static final String DB_USER= "test";
private static final String DB_PASSWORD= "test";
public CustomPostgresContainer() {
super(IMAGE_VERSION);
}
public static CustomPostgresContainer getCustomPostgresContainerInstance() {
if(customPostgresContainer == null) {
return extracted().withExposedPorts(EXPOSED_PORT)
.withDatabaseName(DB_NAME)
.withUsername(DB_USER)
.withPassword(DB_PASSWORD); …Run Code Online (Sandbox Code Playgroud) 我想启动 kafka 测试容器并获取它的引导服务器:
@SpringBootTest
@ContextConfiguration(classes = {TestConfig.class, MyApplication.class}, initializers = MyIntegrationTest.Initializer.class)
@Testcontainers
public class MyIntegrationTest {
@Container
private static final KafkaContainer KAFKA = new KafkaContainer();
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(@NotNull ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues values = TestPropertyValues.of(
"spring.kafka.consumer.bootstrap-servers=" + KAFKA.getBootstrapServers(),
"spring.producer.bootstrap-servers=" + KAFKA.getBootstrapServers()
);
values.applyTo(configurableApplicationContext);
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是我得到:
java.lang.IllegalStateException: You should start Kafka container first
at org.testcontainers.containers.KafkaContainer.getBootstrapServers(KafkaContainer.java:65) ~[kafka-1.12.2.jar:na]
Run Code Online (Sandbox Code Playgroud) 我配置了 bitbucket-pipelines.yml 并使用了image: gradle:6.3.0-jdk11. 我的项目基于 Java11 和 Gradle 6.3 构建。一切都很好,直到开始测试用例。因为我使用 Testontainers 来测试应用程序。Bitbucket 无法启动测试容器。错误是:
org.testcontainers.containers.ContainerLaunchException: Container startup failed
如何解决这个问题?
我的代码库很古老并且被锁定在 JUnit4 中。我想将 testcontainers 与项目集成,以将 docker 容器合并到自动化测试中。
我的开发箱(我控制)运行 docker,但是,我的 CI 系统(我不控制)没有。
如果我可以使用 JUnit5,我只需添加@Testcontainers(disabledWithoutDocker = true)注释,基于docker的测试将在我的开发箱上愉快地运行,而在 CI 机器上被禁用。
JUnit4 相当于@Testcontainers(disabledWithoutDocker = true)什么?
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-3.11.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:128) ~[mongodb-driver-core-3.11.2.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.11.2.jar:na]
at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Run Code Online (Sandbox Code Playgroud)
我的代码:
gradle.build
testImplementation "org.testcontainers:spock:1.14.3"
testImplementation "org.testcontainers:mongodb:1.14.3"
Run Code Online (Sandbox Code Playgroud)
应用程序属性
spring.data.mongodb.uri=mongodb://127.0.0.1:27017/test
Run Code Online (Sandbox Code Playgroud)
测试
@Testcontainers
class TestContainersExample extends IntegrationSpec {
@Shared
MongoDBContainer mongoDb = new MongoDBContainer()
def "setup"() {
mongoDb.start()
mongoDb.waitingFor(Wait.forListeningPort()
.withStartupTimeout(Duration.ofSeconds(180L)));
}
//test case
}
Run Code Online (Sandbox Code Playgroud) integration-testing mongodb spock spring-boot testcontainers
我正在使用 PostgreSQL TestContainer 在 Spring Boot 中测试 Liquibase 模式迁移。我没有任何存储库。我想知道是否可以查看/访问 TestContainer 的内容,并测试架构迁移。
I'm trying to use TestContainers to run JUnit tests.
However, I'm getting a InternalServerErrorException: Status 500: {"message":"Get https://registry-1.docker.io/v2/: Forbidden"} error.
Please note, that I am on a secure network.
I can replicate this by doing docker pull testcontainers/ryuk on the command line.
$ docker pull testcontainers/ryuk
Using default tag: latest
Error response from daemon: Get https://registry-1.docker.io/v2/: Forbidden
Run Code Online (Sandbox Code Playgroud)
However, I need it to pull from our nexus service: https://nexus.company.com/18443.
Inside the docker-compose file, I'm already using the correct nexus image …
我正在使用 Testcontainers 通过 docker 启动 CockroachDB 实例。我需要通过将该属性设置sql.defaults.experimental_temporary_tables.enabled为 true 来启用对临时表的支持。
当机器运行时,我可以这样做:
docker exec -it CockroachDBHibernate ./cockroach sql --insecure -e "SET CLUSTER SETTING sql.defaults.experimental_temporary_tables.enabled = 'true';"
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用 Testcontainers 运行相同的命令时:
container.execInContainer(
"./cockroach sql --insecure -e \"SET CLUSTER SETTING sql.defaults.experimental_temporary_tables.enabled = 'true';\"" );
Run Code Online (Sandbox Code Playgroud)
我有以下错误:
OCI runtime exec failed: exec failed: container_linux.go:370: starting container process caused: exec: "./cockroach sql --insecure -e \"SET CLUSTER SETTING sql.defaults.experimental_temporary_tables.enabled = 'true';\"": stat ./cockroach sql --insecure -e "SET CLUSTER SETTING sql.defaults.experimental_temporary_tables.enabled = 'true';": no such file or directory: …Run Code Online (Sandbox Code Playgroud) testcontainers ×10
docker ×4
java ×3
spring-boot ×3
junit5 ×2
postgresql ×2
apache-kafka ×1
bitbucket ×1
cockroachdb ×1
docker-java ×1
junit4 ×1
liquibase ×1
mongodb ×1
spock ×1
spring ×1