使用嵌入式postgres的测试因非法状态异常而失败

时间:2019-05-02 18:18:16

标签: java postgresql gitlab-ci

我正在使用otj-pg-embedded对嵌入式postgres数据库进行一些测试。尽管这些测试在本地运行良好,但是当由Gitlab-CI运行并带有非法状态异常时失败。 Gitlab CI可以构建它并运行不包含otj-pg嵌入式的测试。

我已经注释掉了大多数测试类,并指出了问题所在:

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


任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

我在 MacOS M1 上使用嵌入式 postgres 运行测试时遇到了类似的问题:

java.lang.IllegalStateException: Process [/var/folders/g_/jxzv9glj0p13njlzpr9xtcfm0000gn/T/embedded-pg/PG-4f38c8a8b2500189835f8ec1b75de81b/bin/initdb, -A, trust, -U, postgres, -D, /var/folders/g_/jxzv9glj0p13njlzpr9xtcfm0000gn/T/pg-embedded-009358be-3e95-4dbe-aa7a-01a9008511cb-3277843270072134584/epg524445573999525527, -E, UTF-8] failed

我复制粘贴的命令并在 shell 上运行它:

/var/folders/g_/jxzv9glj0p13njlzpr9xtcfm0000gn/T/embedded-pg/PG-4f38c8a8b2500189835f8ec1b75de81b/bin/initdb -A trust -U postgres -D /var/folders/g_/jxzv9glj0p13njlzpr9xtcfm0000gn/T/pg-embedded-009358be-3e95-4dbe-aa7a-01a9008511cb-3277843270072134584/epg524445573999525527 -E UTF-8

并且输出显示了一个提示:

Running bootstrap script ... 2021-02-17 08:18:21.008 WET [28692] FATAL:  could not create shared memory segment: Cannot allocate memory
2021-02-17 08:18:21.008 WET [28692] DETAIL:  Failed system call was shmget(key=5432001, size=56, 03600).
2021-02-17 08:18:21.008 WET [28692] HINT:  This error usually means that PostgreSQL's request for a shared memory segment exceeded your kernel's SHMALL parameter.  You might need to reconfigure the kernel with larger SHMALL.
    The PostgreSQL documentation contains more information about shared memory configuration.
child process exited with exit code 1
initdb: removing contents of data directory "/var/folders/g_/jxzv9glj0p13njlzpr9xtcfm0000gn/T/pg-embedded-cbe6f5f8-24b2-4431-afec-6e7aeb1dbb5d-8656684715588054403/epg6904898061245880853"

然后我在 shell 上运行:

sudo sysctl kern.sysv.shmmax=104857600
sudo sysctl kern.sysv.shmall=25600

它奏效了。

答案 1 :(得分:1)

对于Mac用户(尤其是新的BigSur),已经有一个issue可以解决此问题。

还没有一个确定的“修复程序”,但是我可以通过以下方式安装postgresql

brew install postgresql

不确定是否同样适用于其他操作系统。

答案 2 :(得分:0)

我在flyway / embedded-pg中遇到了同样的问题。似乎临时文件在某个时刻已损坏。重新启动将修复它(直到再次发生),或者您可以删除PG-*内的/tmp/embedded-pg/目录