-
Notifications
You must be signed in to change notification settings - Fork 41.4k
Add DB2 support for Docker Compose #42395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * Copyright 2012-2024 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 | ||
| * | ||
| * https://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.docker.compose.service.connection.db2; | ||
|
|
||
| import java.sql.Driver; | ||
|
|
||
| import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails; | ||
| import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest; | ||
| import org.springframework.boot.jdbc.DatabaseDriver; | ||
| import org.springframework.boot.testsupport.container.TestImage; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
| import org.springframework.jdbc.datasource.SimpleDriverDataSource; | ||
| import org.springframework.util.ClassUtils; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** | ||
| * Integration tests for {@link Db2JdbcDockerComposeConnectionDetailsFactory}. | ||
| * | ||
| * @author Yanming Zhou | ||
| */ | ||
| class Db2JdbcDockerComposeConnectionDetailsFactoryIntegrationTests { | ||
|
|
||
| @DockerComposeTest(composeFile = "db2-compose.yaml", image = TestImage.DB2) | ||
| void runCreatesConnectionDetails(JdbcConnectionDetails connectionDetails) throws Exception { | ||
| assertConnectionDetails(connectionDetails); | ||
| checkDatabaseAccess(connectionDetails); | ||
| } | ||
|
|
||
| private void assertConnectionDetails(JdbcConnectionDetails connectionDetails) { | ||
| assertThat(connectionDetails.getUsername()).isEqualTo("db2inst1"); | ||
| assertThat(connectionDetails.getPassword()).isEqualTo("secret"); | ||
| assertThat(connectionDetails.getJdbcUrl()).startsWith("jdbc:db2://").endsWith("/testdb"); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private void checkDatabaseAccess(JdbcConnectionDetails connectionDetails) throws ClassNotFoundException { | ||
| SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); | ||
| dataSource.setUrl(connectionDetails.getJdbcUrl()); | ||
| dataSource.setUsername(connectionDetails.getUsername()); | ||
| dataSource.setPassword(connectionDetails.getPassword()); | ||
| dataSource.setDriverClass((Class<? extends Driver>) ClassUtils.forName(connectionDetails.getDriverClassName(), | ||
| getClass().getClassLoader())); | ||
| JdbcTemplate template = new JdbcTemplate(dataSource); | ||
| assertThat(template.queryForObject(DatabaseDriver.DB2.getValidationQuery(), Integer.class)).isEqualTo(1); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| services: | ||
| database: | ||
| image: '{imageName}' | ||
| ports: | ||
| - '50000' | ||
| privileged: true | ||
| environment: | ||
| - 'LICENSE=accept' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this mean in practice? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which part? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's required by starting DB2 instance, testcontainers' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand that it's required, I am asking what it means in practice that our project starts accepting that license. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not for production use, I guess. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is resolved as we haven't determined if we can legally accept the license for testing purposes. Knowing that Testcontainers has support for accepting the license is useful but it's opt-in and doesn't tell us if we can do the same automatically on CI and dev machines for testing purposes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not opt-in, Testcontainers accept the license by default. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. License is not accepted by default in Testcontainers. User must call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My mistake, I forgot I added |
||
| - 'DB2INSTANCE=db2inst1' | ||
| - 'DB2INST1_PASSWORD=secret' | ||
| - 'DBNAME=testdb' | ||
| - 'AUTOCONFIG=false' | ||
| - 'ARCHIVE_LOGS=false' | ||
| healthcheck: | ||
| test: 'su - db2inst1 -c "db2 connect to testdb user db2inst1 using secret"' | ||
| start_period: 90s | ||
| labels: | ||
| org.springframework.boot.readiness-check.tcp.disable: true | ||
quaff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright 2012-2024 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 | ||
| * | ||
| * https://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.docker.compose.service.connection.db2; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.springframework.util.Assert; | ||
| import org.springframework.util.StringUtils; | ||
|
|
||
| /** | ||
| * DB2 environment details. | ||
| * | ||
| * @author Yanming Zhou | ||
| */ | ||
| class Db2Environment { | ||
|
|
||
| private final String username; | ||
|
|
||
| private final String password; | ||
|
|
||
| private final String database; | ||
|
|
||
| Db2Environment(Map<String, String> env) { | ||
| this.username = env.getOrDefault("DB2INSTANCE", "db2inst1"); | ||
| this.password = env.get("DB2INST1_PASSWORD"); | ||
| Assert.state(StringUtils.hasLength(this.password), "DB2 password must be provided"); | ||
| this.database = env.get("DBNAME"); | ||
| Assert.state(StringUtils.hasLength(this.database), "DB2 database must be provided"); | ||
| } | ||
|
|
||
| String getUsername() { | ||
| return this.username; | ||
| } | ||
|
|
||
| String getPassword() { | ||
| return this.password; | ||
| } | ||
|
|
||
| String getDatabase() { | ||
| return this.database; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Copyright 2012-2024 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 | ||
| * | ||
| * https://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.docker.compose.service.connection.db2; | ||
|
|
||
| import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails; | ||
| import org.springframework.boot.docker.compose.core.RunningService; | ||
| import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory; | ||
| import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource; | ||
| import org.springframework.boot.docker.compose.service.connection.jdbc.JdbcUrlBuilder; | ||
|
|
||
| /** | ||
| * {@link DockerComposeConnectionDetailsFactory} to create {@link JdbcConnectionDetails} | ||
| * for a {@code db2} service. | ||
| * | ||
| * @author Yanming Zhou | ||
| */ | ||
| class Db2JdbcDockerComposeConnectionDetailsFactory | ||
| extends DockerComposeConnectionDetailsFactory<JdbcConnectionDetails> { | ||
|
|
||
| private static final String[] DB2_CONTAINER_NAMES = { "ibmcom/db2", "db2_community/db2" }; | ||
|
|
||
| protected Db2JdbcDockerComposeConnectionDetailsFactory() { | ||
| super(DB2_CONTAINER_NAMES); | ||
| } | ||
|
|
||
| @Override | ||
| protected JdbcConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) { | ||
| return new Db2JdbcDockerComposeConnectionDetails(source.getRunningService()); | ||
| } | ||
|
|
||
| /** | ||
| * {@link JdbcConnectionDetails} backed by a {@code db2} {@link RunningService}. | ||
| */ | ||
| static class Db2JdbcDockerComposeConnectionDetails extends DockerComposeConnectionDetails | ||
| implements JdbcConnectionDetails { | ||
|
|
||
| private static final JdbcUrlBuilder jdbcUrlBuilder = new JdbcUrlBuilder("db2", 50000); | ||
|
|
||
| private final Db2Environment environment; | ||
|
|
||
| private final String jdbcUrl; | ||
|
|
||
| Db2JdbcDockerComposeConnectionDetails(RunningService service) { | ||
| super(service); | ||
| this.environment = new Db2Environment(service.env()); | ||
| this.jdbcUrl = jdbcUrlBuilder.build(service, this.environment.getDatabase()); | ||
| } | ||
|
|
||
| @Override | ||
| public String getUsername() { | ||
| return this.environment.getUsername(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getPassword() { | ||
| return this.environment.getPassword(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getJdbcUrl() { | ||
| return this.jdbcUrl; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| * Copyright 2012-2024 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 | ||
| * | ||
| * https://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. | ||
| */ | ||
|
|
||
| /** | ||
| * Auto-configuration for Docker Compose DB2 service connections. | ||
| */ | ||
| package org.springframework.boot.docker.compose.service.connection.db2; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Copyright 2012-2024 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 | ||
| * | ||
| * https://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.docker.compose.service.connection.db2; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Map; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatIllegalStateException; | ||
|
|
||
| /** | ||
| * Tests for {@link Db2Environment}. | ||
| * | ||
| * @author Yanming Zhou | ||
| */ | ||
| class Db2EnvironmentTests { | ||
|
|
||
| @Test | ||
| void createWhenNoDb2InstancePasswordThrowsException() { | ||
| assertThatIllegalStateException().isThrownBy(() -> new Db2Environment(Collections.emptyMap())) | ||
| .withMessage("DB2 password must be provided"); | ||
| } | ||
|
|
||
| @Test | ||
| void createWhenNoDb2InstanceDatabaseThrowsException() { | ||
| assertThatIllegalStateException().isThrownBy(() -> new Db2Environment(Map.of("DB2INST1_PASSWORD", "secret"))) | ||
| .withMessage("DB2 database must be provided"); | ||
| } | ||
|
|
||
| @Test | ||
| void getUsernameWhenNoDb2Instance() { | ||
| Db2Environment environment = new Db2Environment(Map.of("DB2INST1_PASSWORD", "secret", "DBNAME", "testdb")); | ||
| assertThat(environment.getUsername()).isEqualTo("db2inst1"); | ||
| } | ||
|
|
||
| @Test | ||
| void getUsernameWhenHasDb2Instance() { | ||
| Db2Environment environment = new Db2Environment( | ||
| Map.of("DB2INSTANCE", "db2inst2", "DB2INST1_PASSWORD", "secret", "DBNAME", "testdb")); | ||
| assertThat(environment.getUsername()).isEqualTo("db2inst2"); | ||
| } | ||
|
|
||
| @Test | ||
| void getPasswordWhenHasDb2InstancePassword() { | ||
| Db2Environment environment = new Db2Environment(Map.of("DB2INST1_PASSWORD", "secret", "DBNAME", "testdb")); | ||
| assertThat(environment.getPassword()).isEqualTo("secret"); | ||
| } | ||
|
|
||
| @Test | ||
| void getDatabaseWhenHasDbName() { | ||
| Db2Environment environment = new Db2Environment(Map.of("DB2INST1_PASSWORD", "secret", "DBNAME", "testdb")); | ||
| assertThat(environment.getDatabase()).isEqualTo("testdb"); | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you can run
privilegedcontainers on the CI. Why is that needed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise
Connection resetexception always be thrown, that's why testcontainersDb2Containerneed that also.https://github.com/testcontainers/testcontainers-java/blob/04206d9dff440e875906f01151f09ad04da3c68f/modules/db2/src/main/java/org/testcontainers/containers/Db2Container.java#L60