embedded-database-spring-test icon indicating copy to clipboard operation
embedded-database-spring-test copied to clipboard

postgres database initializationError

Open webcane opened this issue 1 year ago • 3 comments

with libraries

testImplementation 'io.zonky.test:embedded-database-spring-test:2.5.1'
testImplementation 'io.zonky.test:embedded-postgres:2.0.7'
testImplementation enforcedPlatform('io.zonky.test.postgres:embedded-postgres-binaries-bom:15.7.0')

and test classes

@DataJpaTest
@AutoConfigureEmbeddedDatabase(type = POSTGRES, provider = ZONKY)
public abstract class EmbeddedPostgresIntegrationTest {

    @Autowired
    protected TestSampleRepository repo;

    @AfterEach
    public void tearDown() {
        // Release test data after each test method
        repo.deleteAll();
    }
}
@Repository
public interface TestSampleRepository extends JpaRepository<TestSample, Long>, JpaSpecificationExecutor<TestSample> {
}

@Data
@Builder
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class TestSample {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "test_sample_generator")
    @SequenceGenerator(name = "test_sample_generator", sequenceName = "test_sample_SEQ", allocationSize = 1)
    private Long id;

    @Column(name = "vendor_number")
    private String vendorNumber;

    @Column(name = "json_data", columnDefinition = "jsonb default '{}'::jsonb")
    @ColumnTransformer(write = "?::jsonb")
    @Convert(converter = JsonPropertiesConverter.class)
    private Map<String, Object> jsonData;
}

I have following issue

java.lang.IllegalStateException: Failed to load ApplicationContext for [MergedContextConfiguration@6f4be694 testClass = com.brothers.data.jpa.domain.AccsnmEqualTest.ToPredicateTest, locations = [], classes = [com.brothers.SidraApplication, com.brothers.config.EmbeddedPostgresConfiguration, com.brothers.config.TestJsonConvConfig], contextInitializerClasses = [], activeProfiles = [], propertySourceLocations = [], propertySourceProperties = ["org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true"],
...

Caused by: io.zonky.test.db.shaded.com.google.common.util.concurrent.UncheckedExecutionException: io.zonky.test.db.provider.ProviderException: Unexpected error when preparing a database cluster
	... 121 more

Caused by: io.zonky.test.db.provider.ProviderException: Unexpected error when preparing a database cluster
	at app//io.zonky.test.db.provider.postgres.ZonkyPostgresDatabaseProvider.createDatabase(ZonkyPostgresDatabaseProvider.java:110)...

Caused by: java.lang.IllegalStateException: Process [/tmp/embedded-pg/PG-9792fffe99d0c596fbafe7369527c255/bin/initdb, -A, trust, -U, postgres, -D, /tmp/epg18110462070077864717, -E, UTF-8] failed
	at io.zonky.test.db.postgres.embedded.EmbeddedPostgres.system(EmbeddedPostgres.java:633)
	at io.zonky.test.db.postgres.embedded.EmbeddedPostgres.initdb(EmbeddedPostgres.java:250)
	at io.zonky.test.db.postgres.embedded.EmbeddedPostgres.<init>(EmbeddedPostgres.java:157)
	at io.zonky.test.db.postgres.embedded.EmbeddedPostgres$Builder.start(EmbeddedPostgres.java:584)
	at io.zonky.test.db.provider.postgres.ZonkyPostgresDatabaseProvider$DatabaseInstance.<init>(ZonkyPostgresDatabaseProvider.java:139)
	at io.zonky.test.db.provider.postgres.ZonkyPostgresDatabaseProvider$DatabaseInstance.<init>(ZonkyPostgresDatabaseProvider.java:130)
	at ...

io.zonky.test.db.provider.postgres.ZonkyPostgresDatabaseProvider.createDatabase(ZonkyPostgresDatabaseProvider.java:106)

issue not reproducible locally, but only in gitlab. command to run is

./gradlew sonar -Dsonar.token=$SONAR_TOKEN --stacktrace --warning-mode all

equivalent to

./gradlew test

webcane avatar Sep 19 '24 07:09 webcane

every run the process uses the same tmp folder /tmp/embedded-pg/PG-9792fffe99d0c596fbafe7369527c255/bin/initdb. is it make sense to move embedded db out of tmp folder?

webcane avatar Sep 19 '24 07:09 webcane

This error typically occurs when user permissions are not set correctly. PostgreSQL database does not allow running under the root user. Try enabling debug or trace logging to get more information.

You can find more information for identifying the problem here. And this is a similar issue we resolved recently: https://github.com/zonkyio/embedded-database-spring-test/issues/278

tomix26 avatar Sep 27 '24 09:09 tomix26

every run the process uses the same tmp folder /tmp/embedded-pg/PG-9792fffe99d0c596fbafe7369527c255/bin/initdb. is it make sense to move embedded db out of tmp folder?

Yes, that's correct. This is how the library is currently designed. If unpacking into the tmp directory isn't causing any problems, I don't see a reason to change this behavior.

tomix26 avatar Sep 27 '24 09:09 tomix26

I'm closing this issue for now. Feel free to reopen it if the problem persists.

tomix26 avatar Nov 25 '24 16:11 tomix26

I ran into the same issue on a GitLab instance using the Docker executor for CI. As the docs mention, this executor uses the root user by default if the image doesn't specify otherwise.

This is slightly annoying if you don't want to maintain your own base images. I ended up working around this by building a "builder" image (just a JDK image with an added non-root user) in the pipeline itself, which is then used for the job that runs the tests.

Relevant for GitLab specifically: https://gitlab.com/gitlab-org/gitlab/-/issues/23046

JohnnyJayJay avatar Oct 15 '25 11:10 JohnnyJayJay