|
| 1 | +package org.testcontainers.containers; |
| 2 | + |
| 3 | +import org.apache.commons.lang3.StringUtils; |
| 4 | +import org.testcontainers.utility.DockerImageName; |
| 5 | + |
| 6 | +/** |
| 7 | + * Testcontainers implementation for OceanBase. |
| 8 | + * <p> |
| 9 | + * Supported image: {@code oceanbase/oceanbase-ce} |
| 10 | + * <p> |
| 11 | + * Exposed ports: |
| 12 | + * <ul> |
| 13 | + * <li>SQL: 2881</li> |
| 14 | + * <li>RPC: 2882</li> |
| 15 | + * </ul> |
| 16 | + */ |
| 17 | +public class OceanBaseContainer extends JdbcDatabaseContainer<OceanBaseContainer> { |
| 18 | + |
| 19 | + static final String NAME = "oceanbase"; |
| 20 | + |
| 21 | + static final String DOCKER_IMAGE_NAME = "oceanbase/oceanbase-ce"; |
| 22 | + |
| 23 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse(DOCKER_IMAGE_NAME); |
| 24 | + |
| 25 | + private static final Integer SQL_PORT = 2881; |
| 26 | + private static final Integer RPC_PORT = 2882; |
| 27 | + |
| 28 | + private static final String SYSTEM_TENANT_NAME = "sys"; |
| 29 | + private static final String DEFAULT_TEST_TENANT_NAME = "test"; |
| 30 | + private static final String DEFAULT_USERNAME = "root"; |
| 31 | + private static final String DEFAULT_PASSWORD = ""; |
| 32 | + private static final String DEFAULT_DATABASE_NAME = "test"; |
| 33 | + |
| 34 | + private boolean enableFastboot; |
| 35 | + private String mode; |
| 36 | + private String tenantName = DEFAULT_TEST_TENANT_NAME; |
| 37 | + |
| 38 | + public OceanBaseContainer(String dockerImageName) { |
| 39 | + this(DockerImageName.parse(dockerImageName)); |
| 40 | + } |
| 41 | + |
| 42 | + public OceanBaseContainer(DockerImageName dockerImageName) { |
| 43 | + super(dockerImageName); |
| 44 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); |
| 45 | + |
| 46 | + addExposedPorts(SQL_PORT, RPC_PORT); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Integer getMappedPort(int originalPort) { |
| 51 | + return "host".equals(getNetworkMode()) ? originalPort : super.getMappedPort(originalPort); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String getDriverClassName() { |
| 56 | + return "com.mysql.cj.jdbc.Driver"; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public String getJdbcUrl() { |
| 61 | + return getJdbcUrl(DEFAULT_DATABASE_NAME); |
| 62 | + } |
| 63 | + |
| 64 | + public String getJdbcUrl(String databaseName) { |
| 65 | + String additionalUrlParams = constructUrlParameters("?", "&"); |
| 66 | + return "jdbc:mysql://" + getHost() + ":" + getMappedPort(SQL_PORT) + "/" + databaseName + additionalUrlParams; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public String getDatabaseName() { |
| 71 | + return DEFAULT_DATABASE_NAME; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public String getUsername() { |
| 76 | + return DEFAULT_USERNAME + "@" + tenantName; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public String getPassword() { |
| 81 | + return DEFAULT_PASSWORD; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected String getTestQueryString() { |
| 86 | + return "SELECT 1"; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Enable fastboot. |
| 91 | + * |
| 92 | + * @return this |
| 93 | + */ |
| 94 | + public OceanBaseContainer enableFastboot() { |
| 95 | + this.enableFastboot = true; |
| 96 | + return self(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Set the deployment mode, see <a href="https://hub.docker.com/r/oceanbase/oceanbase-ce">Docker Hub</a> for more details. |
| 101 | + * |
| 102 | + * @param mode the deployment mode |
| 103 | + * @return this |
| 104 | + */ |
| 105 | + public OceanBaseContainer withMode(String mode) { |
| 106 | + this.mode = mode; |
| 107 | + return self(); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Set the non-system tenant to be created for testing. |
| 112 | + * |
| 113 | + * @param tenantName the name of tenant to be created |
| 114 | + * @return this |
| 115 | + */ |
| 116 | + public OceanBaseContainer withTenant(String tenantName) { |
| 117 | + if (StringUtils.isEmpty(tenantName)) { |
| 118 | + throw new IllegalArgumentException("Tenant name cannot be null or empty"); |
| 119 | + } |
| 120 | + if (SYSTEM_TENANT_NAME.equals(tenantName)) { |
| 121 | + throw new IllegalArgumentException("Tenant name cannot be " + SYSTEM_TENANT_NAME); |
| 122 | + } |
| 123 | + this.tenantName = tenantName; |
| 124 | + return self(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + protected void configure() { |
| 129 | + if (StringUtils.isNotBlank(mode)) { |
| 130 | + withEnv("MODE", mode); |
| 131 | + } |
| 132 | + if (enableFastboot) { |
| 133 | + withEnv("FASTBOOT", "true"); |
| 134 | + } |
| 135 | + if (!DEFAULT_TEST_TENANT_NAME.equals(tenantName)) { |
| 136 | + withEnv("OB_TENANT_NAME", tenantName); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments