11package org .testcontainers .oceanbase ;
22
33import org .testcontainers .containers .JdbcDatabaseContainer ;
4+ import org .testcontainers .containers .wait .strategy .Wait ;
45import org .testcontainers .utility .DockerImageName ;
56
67/**
@@ -26,14 +27,20 @@ public class OceanBaseCEContainer extends JdbcDatabaseContainer<OceanBaseCEConta
2627
2728 private static final Integer RPC_PORT = 2882 ;
2829
29- private static final String DEFAULT_TEST_TENANT_NAME = "test" ;
30+ private static final String DEFAULT_TENANT_NAME = "test" ;
3031
31- private static final String DEFAULT_USERNAME = "root" ;
32+ private static final String DEFAULT_USER = "root" ;
3233
3334 private static final String DEFAULT_PASSWORD = "" ;
3435
3536 private static final String DEFAULT_DATABASE_NAME = "test" ;
3637
38+ private Mode mode = Mode .SLIM ;
39+
40+ private String tenantName = DEFAULT_TENANT_NAME ;
41+
42+ private String password = DEFAULT_PASSWORD ;
43+
3744 public OceanBaseCEContainer (String dockerImageName ) {
3845 this (DockerImageName .parse (dockerImageName ));
3946 }
@@ -43,6 +50,29 @@ public OceanBaseCEContainer(DockerImageName dockerImageName) {
4350 dockerImageName .assertCompatibleWith (DEFAULT_IMAGE_NAME );
4451
4552 addExposedPorts (SQL_PORT , RPC_PORT );
53+ setWaitStrategy (Wait .forLogMessage (".*boot success!.*" , 1 ));
54+ }
55+
56+ @ Override
57+ protected void configure () {
58+ addEnv ("MODE" , mode .name ().toLowerCase ());
59+
60+ if (!DEFAULT_TENANT_NAME .equals (tenantName )) {
61+ if (mode == Mode .SLIM ) {
62+ logger ().warn ("The tenant name is not configurable on slim mode, so this option will be ignored." );
63+ // reset the tenant name to ensure the constructed username is correct
64+ tenantName = DEFAULT_TENANT_NAME ;
65+ } else {
66+ addEnv ("OB_TENANT_NAME" , tenantName );
67+ }
68+ }
69+
70+ addEnv ("OB_TENANT_PASSWORD" , password );
71+ }
72+
73+ @ Override
74+ protected void waitUntilContainerStarted () {
75+ getWaitStrategy ().waitUntilReady (this );
4676 }
4777
4878 @ Override
@@ -64,19 +94,49 @@ public String getDatabaseName() {
6494
6595 @ Override
6696 public String getUsername () {
67- // In OceanBase, the jdbc username is related to the name of user, tenant and cluster,
68- // if a tenant name other than the default value 'test' is used, you should manually
69- // construct the jdbc username by yourself.
70- return DEFAULT_USERNAME + "@" + DEFAULT_TEST_TENANT_NAME ;
97+ return DEFAULT_USER + "@" + tenantName ;
7198 }
7299
73100 @ Override
74101 public String getPassword () {
75- return DEFAULT_PASSWORD ;
102+ return password ;
76103 }
77104
78105 @ Override
79106 protected String getTestQueryString () {
80107 return "SELECT 1" ;
81108 }
109+
110+ public OceanBaseCEContainer withMode (Mode mode ) {
111+ this .mode = mode ;
112+ return this ;
113+ }
114+
115+ public OceanBaseCEContainer withTenantName (String tenantName ) {
116+ this .tenantName = tenantName ;
117+ return this ;
118+ }
119+
120+ public OceanBaseCEContainer withPassword (String password ) {
121+ this .password = password ;
122+ return this ;
123+ }
124+
125+ public enum Mode {
126+ /**
127+ * Use as much hardware resources as possible for deployment by default,
128+ * and all environment variables are available.
129+ */
130+ NORMAL ,
131+ /**
132+ * Use the minimum hardware resources for deployment by default,
133+ * and all environment variables are available.
134+ */
135+ MINI ,
136+ /**
137+ * Use minimal hardware resources and pre-built deployment files for quick startup,
138+ * and password of user tenant is the only available environment variable.
139+ */
140+ SLIM ,
141+ }
82142}
0 commit comments