-
Notifications
You must be signed in to change notification settings - Fork 40.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d815cad
commit da05e9e
Showing
11 changed files
with
320 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
.../service/connection/db2/Db2JdbcDockerComposeConnectionDetailsFactoryIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...resources/org/springframework/boot/docker/compose/service/connection/db2/db2-compose.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
- '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 |
57 changes: 57 additions & 0 deletions
57
...n/java/org/springframework/boot/docker/compose/service/connection/db2/Db2Environment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
...t/docker/compose/service/connection/db2/Db2JdbcDockerComposeConnectionDetailsFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...ain/java/org/springframework/boot/docker/compose/service/connection/db2/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...a/org/springframework/boot/docker/compose/service/connection/db2/Db2EnvironmentTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters