1
1
package org .testcontainers .oceanbase ;
2
2
3
3
import org .testcontainers .containers .JdbcDatabaseContainer ;
4
+ import org .testcontainers .containers .wait .strategy .Wait ;
4
5
import org .testcontainers .utility .DockerImageName ;
5
6
6
7
/**
@@ -26,14 +27,20 @@ public class OceanBaseCEContainer extends JdbcDatabaseContainer<OceanBaseCEConta
26
27
27
28
private static final Integer RPC_PORT = 2882 ;
28
29
29
- private static final String DEFAULT_TEST_TENANT_NAME = "test" ;
30
+ private static final String DEFAULT_TENANT_NAME = "test" ;
30
31
31
- private static final String DEFAULT_USERNAME = "root" ;
32
+ private static final String DEFAULT_USER = "root" ;
32
33
33
34
private static final String DEFAULT_PASSWORD = "" ;
34
35
35
36
private static final String DEFAULT_DATABASE_NAME = "test" ;
36
37
38
+ private Mode mode = Mode .SLIM ;
39
+
40
+ private String tenantName = DEFAULT_TENANT_NAME ;
41
+
42
+ private String password = DEFAULT_PASSWORD ;
43
+
37
44
public OceanBaseCEContainer (String dockerImageName ) {
38
45
this (DockerImageName .parse (dockerImageName ));
39
46
}
@@ -43,6 +50,29 @@ public OceanBaseCEContainer(DockerImageName dockerImageName) {
43
50
dockerImageName .assertCompatibleWith (DEFAULT_IMAGE_NAME );
44
51
45
52
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 );
46
76
}
47
77
48
78
@ Override
@@ -64,19 +94,49 @@ public String getDatabaseName() {
64
94
65
95
@ Override
66
96
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 ;
71
98
}
72
99
73
100
@ Override
74
101
public String getPassword () {
75
- return DEFAULT_PASSWORD ;
102
+ return password ;
76
103
}
77
104
78
105
@ Override
79
106
protected String getTestQueryString () {
80
107
return "SELECT 1" ;
81
108
}
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
+ }
82
142
}
0 commit comments