使用嵌入式 postgres 的测试失败,并显示非法状态异常
2022-09-01 17:29:38
我正在使用otj-pg-embedded对嵌入式postgres数据库运行一些测试。虽然测试在本地运行良好,但当 Gitlab-CI 运行并出现非法状态异常时,它们会失败。Gitlab CI构建它并运行不包含otj-pg-embedded的测试。
我已经注释掉了大部分测试类,并将问题查明为:
public static SingleInstancePostgresRule pg = EmbeddedPostgresRules.singleInstance();
import com.goldfinger.models.AuditLog;
import com.opentable.db.postgres.embedded.FlywayPreparer;
import com.opentable.db.postgres.junit.EmbeddedPostgresRules;
import com.opentable.db.postgres.junit.PreparedDbRule;
import org.junit.*;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
public class SQLAuditRepositoryTest {
private static SQLAuditRepository sqlAuditRepository;
private static AuditLog auditLog_1;
private static AuditLog auditLog_2;
private static AuditLog auditLog_3;
private static List<AuditLog> auditLogList;
@ClassRule
public static SingleInstancePostgresRule pg = EmbeddedPostgresRules.singleInstance();
@Test
public void simpleTest() {
assert (2 == 2);
}
}
这是堆栈跟踪:
java.lang.IllegalStateException: Process [/tmp/embedded-pg/PG-06e3a92a2edb6ddd6dbdf5602d0252ca/bin/initdb, -A, trust, -U, postgres, -D, /tmp/epg6584640257265165384, -E, UTF-8] failed
at com.opentable.db.postgres.embedded.EmbeddedPostgres.system(EmbeddedPostgres.java:626)
at com.opentable.db.postgres.embedded.EmbeddedPostgres.initdb(EmbeddedPostgres.java:240)
...
... many lines here
...
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
at java.lang.Thread.run(Thread.java:745)
这是gitlab-ci.yml
image: java:latest
services:
- postgres:latest
before_script:
- export GRADLE_USER_HOME=`pwd`/.gradle
package:
stage: build
script:
- ./gradlew assemble
test:
stage: test
script:
- ./gradlew check
artifacts:
reports:
junit: build/test-results/test/*.xml
任何帮助将不胜感激。